Search in sources :

Example 1 with GcodeParser

use of com.willwinder.universalgcodesender.gcode.GcodeParser in project Universal-G-Code-Sender by winder.

the class GcodeViewParse method toObjFromReader.

/**
 * Almost the same as toObjRedux, convert gcode to a LineSegment collection.
 * I've tried refactoring this, but the function is so small that merging
 * toObjFromReader and toObjRedux adds more complexity than having these two
 * methods.
 *
 * @param gcode commands to visualize.
 * @param arcSegmentLength length of line segments when expanding an arc.
 */
public List<LineSegment> toObjFromReader(GcodeStreamReader reader, double arcSegmentLength) throws IOException, GcodeParserException {
    lines.clear();
    GcodeParser gp = getParser(arcSegmentLength);
    // Save the state
    Position start = new Position();
    while (reader.getNumRowsRemaining() > 0) {
        GcodeCommand commandObject = reader.getNextCommand();
        List<String> commands = gp.preprocessCommand(commandObject.getCommandString(), gp.getCurrentState());
        for (String command : commands) {
            List<GcodeMeta> points = gp.addCommand(command, commandObject.getCommandNumber());
            for (GcodeMeta meta : points) {
                if (meta.point != null) {
                    addLinesFromPointSegment(start, meta.point, arcSegmentLength, lines);
                    start.set(meta.point.point());
                }
            }
        }
    }
    return lines;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 2 with GcodeParser

use of com.willwinder.universalgcodesender.gcode.GcodeParser in project Universal-G-Code-Sender by winder.

the class GUIBackendPreprocessorTest method testRegularPreprocessAndExportToFile.

/**
 * Test of preprocessAndExportToFile method, of class GUIBackend.
 */
@Test
public void testRegularPreprocessAndExportToFile() throws Exception {
    System.out.println("regularPreprocessAndExportToFile");
    GUIBackend backend = new GUIBackend();
    GcodeParser gcp = new GcodeParser();
    // Double all the commands that go in.
    gcp.addCommandProcessor(commandDoubler);
    gcp.addCommandProcessor(new CommentProcessor());
    // Create input file, comment-only line shouldn't be processed twice.
    List<String> lines = Arrays.asList("line one", "; comment", "line two");
    Files.write(inputFile, lines, Charset.defaultCharset(), StandardOpenOption.WRITE);
    backend.preprocessAndExportToFile(gcp, inputFile.toFile(), outputFile.toFile());
    List<String> expectedResults = Arrays.asList("line one", "line one", "", "line two", "line two");
    try (GcodeStreamReader reader = new GcodeStreamReader(outputFile.toFile())) {
        Assert.assertEquals(expectedResults.size(), reader.getNumRows());
        for (String expected : expectedResults) {
            Assert.assertEquals(expected, reader.getNextCommand().getCommandString());
        }
    }
}
Also used : GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) CommentProcessor(com.willwinder.universalgcodesender.gcode.processors.CommentProcessor) Test(org.junit.Test)

Example 3 with GcodeParser

use of com.willwinder.universalgcodesender.gcode.GcodeParser in project Universal-G-Code-Sender by winder.

the class GcodeViewParse method toObjRedux.

/**
 * The original (working) gcode to LineSegment collection code.
 * @param gcode commands to visualize.
 * @param arcSegmentLength length of line segments when expanding an arc.
 */
public List<LineSegment> toObjRedux(List<String> gcode, double arcSegmentLength) throws GcodeParserException {
    GcodeParser gp = getParser(arcSegmentLength);
    lines.clear();
    // Save the state
    Position start = new Position();
    for (String s : gcode) {
        List<String> commands = gp.preprocessCommand(s, gp.getCurrentState());
        for (String command : commands) {
            List<GcodeMeta> points = gp.addCommand(command);
            for (GcodeMeta meta : points) {
                if (meta.point != null) {
                    addLinesFromPointSegment(start, meta.point, arcSegmentLength, lines);
                    start.set(meta.point.point());
                }
            }
        }
    }
    return lines;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser)

Example 4 with GcodeParser

use of com.willwinder.universalgcodesender.gcode.GcodeParser in project Universal-G-Code-Sender by winder.

the class GcodeViewParse method getParser.

/**
 * Create a gcode parser with required configuration.
 */
private static GcodeParser getParser(double arcSegmentLength) {
    GcodeParser gp = new GcodeParser();
    gp.addCommandProcessor(new CommentProcessor());
    gp.addCommandProcessor(new WhitespaceProcessor());
    // gp.addCommandProcessor(new ArcExpander(true, arcSegmentLength, 4));
    return gp;
}
Also used : WhitespaceProcessor(com.willwinder.universalgcodesender.gcode.processors.WhitespaceProcessor) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) CommentProcessor(com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)

Example 5 with GcodeParser

use of com.willwinder.universalgcodesender.gcode.GcodeParser in project Universal-G-Code-Sender by winder.

the class CommandProcessorLoaderTest method testInvalidParametersToValidProcessor.

@Test
public void testInvalidParametersToValidProcessor() throws Exception {
    System.out.println("InvalidProcessor");
    GcodeParser gcp = new GcodeParser();
    JsonObject args = new JsonObject();
    args.addProperty("segmentLengthMM", "NotANumber");
    JsonObject object = new JsonObject();
    object.addProperty("name", "ArcExpander");
    object.add("args", args);
    JsonArray array = new JsonArray();
    array.add(object);
    boolean threwException = false;
    try {
        List<CommandProcessor> result = CommandProcessorLoader.initializeWithProcessors(array.toString());
    } catch (IllegalArgumentException e) {
        threwException = true;
    }
    assertTrue(threwException);
}
Also used : JsonArray(com.google.gson.JsonArray) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Aggregations

GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)9 Test (org.junit.Test)4 CommentProcessor (com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)3 Position (com.willwinder.universalgcodesender.model.Position)3 GcodeStreamReader (com.willwinder.universalgcodesender.utils.GcodeStreamReader)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)2 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)2 ArcExpander (com.willwinder.universalgcodesender.gcode.processors.ArcExpander)1 LineSplitter (com.willwinder.universalgcodesender.gcode.processors.LineSplitter)1 M30Processor (com.willwinder.universalgcodesender.gcode.processors.M30Processor)1 MeshLeveler (com.willwinder.universalgcodesender.gcode.processors.MeshLeveler)1 Translator (com.willwinder.universalgcodesender.gcode.processors.Translator)1 WhitespaceProcessor (com.willwinder.universalgcodesender.gcode.processors.WhitespaceProcessor)1 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)1 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)1 Settings (com.willwinder.universalgcodesender.utils.Settings)1 AutoLevelSettings (com.willwinder.universalgcodesender.utils.Settings.AutoLevelSettings)1 BufferedReader (java.io.BufferedReader)1