Search in sources :

Example 11 with GcodeStreamReader

use of com.willwinder.universalgcodesender.utils.GcodeStreamReader in project Universal-G-Code-Sender by winder.

the class BufferedCommunicatorTest method responseMessageOnErrorOnCommandStreamShouldPauseTheCommunicator.

@Test
public void responseMessageOnErrorOnCommandStreamShouldPauseTheCommunicator() throws IOException, GcodeStreamReader.NotGcodeStreamFile {
    // Given
    Connection connection = mock(Connection.class);
    instance.setConnection(connection);
    // Create a gcode file stream
    File gcodeFile = new File(tempDir, "gcodeFile");
    GcodeStreamWriter gcodeStreamWriter = new GcodeStreamWriter(gcodeFile);
    gcodeStreamWriter.addLine("G0", "G0", null, 0);
    gcodeStreamWriter.addLine("G0", "G0", null, 0);
    gcodeStreamWriter.close();
    // Stream
    instance.queueStreamForComm(new GcodeStreamReader(gcodeFile));
    instance.streamCommands();
    // When
    instance.responseMessage("error");
    // Then
    assertTrue(instance.isPaused());
}
Also used : GcodeStreamWriter(com.willwinder.universalgcodesender.utils.GcodeStreamWriter) Connection(com.willwinder.universalgcodesender.connection.Connection) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) File(java.io.File) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 12 with GcodeStreamReader

use of com.willwinder.universalgcodesender.utils.GcodeStreamReader in project Universal-G-Code-Sender by winder.

the class GUIBackendPreprocessorTest method testGcodeStreamPreprocessAndExportToFile.

@Test
public void testGcodeStreamPreprocessAndExportToFile() throws Exception {
    System.out.println("gcodeStreamPreprocessAndExportToFile");
    GUIBackend backend = new GUIBackend();
    GcodeParser gcp = new GcodeParser();
    // Double all the commands that go in.
    gcp.addCommandProcessor(commandDoubler);
    // Create GcodeStream input file by putting it through the preprocessor.
    List<String> lines = Arrays.asList("line one", "line two");
    Files.write(outputFile, lines, Charset.defaultCharset(), StandardOpenOption.WRITE);
    backend.preprocessAndExportToFile(gcp, outputFile.toFile(), inputFile.toFile());
    // Pass a gcodestream into
    backend.preprocessAndExportToFile(gcp, inputFile.toFile(), outputFile.toFile());
    List<String> expectedResults = Arrays.asList("line one", "line one", "line one", "line one", "line two", "line two", "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) Test(org.junit.Test)

Example 13 with GcodeStreamReader

use of com.willwinder.universalgcodesender.utils.GcodeStreamReader in project Universal-G-Code-Sender by winder.

the class GcodeParserTest method autoLevelerProcessorSet.

@Test
public void autoLevelerProcessorSet() throws Exception {
    System.out.println("autoLevelerProcessorSet");
    GcodeParser gcp = new GcodeParser();
    gcp.addCommandProcessor(new CommentProcessor());
    gcp.addCommandProcessor(new ArcExpander(true, 0.1));
    gcp.addCommandProcessor(new LineSplitter(1));
    Position[][] grid = { { new Position(-5, -5, 0, MM), new Position(-5, 35, 0, MM) }, { new Position(35, -5, 0, MM), new Position(35, 35, 0, MM) } };
    gcp.addCommandProcessor(new MeshLeveler(0, grid, Units.MM));
    Path output = Files.createTempFile("autoleveler_processor_set_test.nc", "");
    // Copy resource to temp file since my parser methods need it that way.
    URL file = this.getClass().getClassLoader().getResource("./gcode/circle_test.nc");
    File tempFile = File.createTempFile("temp", "file");
    IOUtils.copy(file.openStream(), FileUtils.openOutputStream(tempFile));
    GcodeParserUtils.processAndExport(gcp, tempFile, output.toFile());
    GcodeStreamReader reader = new GcodeStreamReader(output.toFile());
    file = this.getClass().getClassLoader().getResource("./gcode/circle_test.nc.processed");
    Files.lines(Paths.get(file.toURI())).forEach((t) -> {
        try {
            GcodeCommand c = reader.getNextCommand();
            if (c == null) {
                Assert.fail("Reached end of gcode reader before end of expected commands.");
            }
            Assert.assertEquals(c.getCommandString(), t);
        } catch (IOException ex) {
            Assert.fail("Unexpected exception.");
        }
    });
    assertEquals(1027, reader.getNumRows());
    output.toFile().delete();
}
Also used : Path(java.nio.file.Path) Position(com.willwinder.universalgcodesender.model.Position) LineSplitter(com.willwinder.universalgcodesender.gcode.processors.LineSplitter) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) URL(java.net.URL) CommentProcessor(com.willwinder.universalgcodesender.gcode.processors.CommentProcessor) MeshLeveler(com.willwinder.universalgcodesender.gcode.processors.MeshLeveler) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) ArcExpander(com.willwinder.universalgcodesender.gcode.processors.ArcExpander) File(java.io.File) Test(org.junit.Test)

Example 14 with GcodeStreamReader

use of com.willwinder.universalgcodesender.utils.GcodeStreamReader in project Universal-G-Code-Sender by winder.

the class GcodeTilerTopComponent method generateOneTile.

private void generateOneTile(double offsetX, double offsetY, PrintWriter output) {
    String gcodeFile = backend.getGcodeFile().getAbsolutePath();
    UnitUtils.Units u = selectedUnit(this.units.getSelectedIndex());
    GcodeParser parser = new GcodeParser();
    parser.addCommandProcessor(new Translator(new Position(offsetX, offsetY, 0.0, u)));
    parser.addCommandProcessor(new M30Processor());
    output.println(GcodeUtils.unitCommand(u) + "G90");
    output.println("G0X" + offsetX + "Y" + offsetY);
    try {
        File file = new File(gcodeFile);
        try {
            try (GcodeStreamReader gsr = new GcodeStreamReader(file)) {
                while (gsr.getNumRowsRemaining() > 0) {
                    GcodeCommand next = gsr.getNextCommand();
                    applyTranslation(next.getCommandString(), parser, output);
                }
            }
        } catch (GcodeStreamReader.NotGcodeStreamFile e) {
            try (FileInputStream fstream = new FileInputStream(file);
                DataInputStream dis = new DataInputStream(fstream);
                BufferedReader fileStream = new BufferedReader(new InputStreamReader(dis))) {
                String line;
                while ((line = fileStream.readLine()) != null) {
                    applyTranslation(line, parser, output);
                }
            }
        }
    } catch (GcodeParserException | IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) UnitUtils(com.willwinder.universalgcodesender.model.UnitUtils) Translator(com.willwinder.universalgcodesender.gcode.processors.Translator) BufferedReader(java.io.BufferedReader) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) M30Processor(com.willwinder.universalgcodesender.gcode.processors.M30Processor) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) File(java.io.File)

Example 15 with GcodeStreamReader

use of com.willwinder.universalgcodesender.utils.GcodeStreamReader in project Universal-G-Code-Sender by winder.

the class GcodeParserUtils method processAndExportGcodeStream.

/**
 * Attempts to read the input file in GcodeStream format.
 * @return whether or not we succeed processing the file.
 */
private static boolean processAndExportGcodeStream(GcodeParser gcp, BufferedReader input, File output) throws IOException, GcodeParserException {
    // Preprocess a GcodeStream file.
    try {
        GcodeStreamReader gsr = new GcodeStreamReader(input);
        try (GcodeStreamWriter gsw = new GcodeStreamWriter(output)) {
            int i = 0;
            while (gsr.getNumRowsRemaining() > 0) {
                i++;
                GcodeCommand gc = gsr.getNextCommand();
                write(gcp, gsw, gc.getOriginalCommandString(), gc.getCommandString(), gc.getComment(), i);
            }
            // Done processing GcodeStream file.
            return true;
        }
    } catch (GcodeStreamReader.NotGcodeStreamFile ex) {
    // File exists, but isn't a stream reader. So go ahead and try parsing it as a raw gcode file.
    }
    return false;
}
Also used : GcodeStreamWriter(com.willwinder.universalgcodesender.utils.GcodeStreamWriter) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Aggregations

GcodeStreamReader (com.willwinder.universalgcodesender.utils.GcodeStreamReader)15 File (java.io.File)11 Test (org.junit.Test)9 GcodeStreamWriter (com.willwinder.universalgcodesender.utils.GcodeStreamWriter)7 GcodeStreamTest (com.willwinder.universalgcodesender.utils.GcodeStreamTest)6 IOException (java.io.IOException)6 Connection (com.willwinder.universalgcodesender.connection.Connection)3 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)3 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)3 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)3 CommentProcessor (com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)2 Position (com.willwinder.universalgcodesender.model.Position)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 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)1 GcodeViewParse (com.willwinder.universalgcodesender.visualizer.GcodeViewParse)1 BufferedReader (java.io.BufferedReader)1