Search in sources :

Example 6 with GcodeStreamReader

use of com.willwinder.universalgcodesender.utils.GcodeStreamReader 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 7 with GcodeStreamReader

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

the class GcodeModel method generateObject.

/**
 * Parse the gcodeFile and store the resulting geometry and data about it.
 */
private boolean generateObject() {
    isDrawable = false;
    if (this.gcodeFile == null) {
        return false;
    }
    try {
        GcodeViewParse gcvp = new GcodeViewParse();
        logger.log(Level.INFO, "About to process {}", gcodeFile);
        try {
            GcodeStreamReader gsr = new GcodeStreamReader(new File(gcodeFile));
            gcodeLineList = gcvp.toObjFromReader(gsr, 0.3);
        } catch (GcodeStreamReader.NotGcodeStreamFile e) {
            List<String> linesInFile;
            linesInFile = VisualizerUtils.readFiletoArrayList(this.gcodeFile);
            gcodeLineList = gcvp.toObjRedux(linesInFile, 0.3);
        }
        this.objectMin = gcvp.getMinimumExtremes();
        this.objectMax = gcvp.getMaximumExtremes();
        if (gcodeLineList.isEmpty()) {
            return false;
        }
        // Grab the line number off the last line.
        this.lastCommandNumber = gcodeLineList.get(gcodeLineList.size() - 1).getLineNumber();
        System.out.println("Object bounds: X (" + objectMin.x + ", " + objectMax.x + ")");
        System.out.println("               Y (" + objectMin.y + ", " + objectMax.y + ")");
        System.out.println("               Z (" + objectMin.z + ", " + objectMax.z + ")");
        Point3d center = VisualizerUtils.findCenter(objectMin, objectMax);
        System.out.println("Center = " + center.toString());
        System.out.println("Num Line Segments :" + gcodeLineList.size());
        objectSize.x = this.objectMax.x - this.objectMin.x;
        objectSize.y = this.objectMax.y - this.objectMin.y;
        objectSize.z = this.objectMax.z - this.objectMin.z;
        /*
            this.scaleFactorBase = VisualizerUtils.findScaleFactor(this.xSize, this.ySize, this.objectMin, this.objectMax);
            this.scaleFactor = this.scaleFactorBase * this.zoomMultiplier;

            this.dimensionsLabel = Localization.getString("VisualizerCanvas.dimensions") + ": " 
                    + Localization.getString("VisualizerCanvas.width") + "=" + format.format(objectWidth) + " " 
                    + Localization.getString("VisualizerCanvas.height") + "=" + format.format(objectHeight);
            */
        // Now that the object is known, fill the buffers.
        this.isDrawable = true;
        this.numberOfVertices = gcodeLineList.size() * 2;
        this.lineVertexData = new float[numberOfVertices * 3];
        this.lineColorData = new byte[numberOfVertices * 3];
        this.updateVertexBuffers();
    } catch (GcodeParserException | IOException e) {
        String error = Localization.getString("mainWindow.error.openingFile") + " : " + e.getLocalizedMessage();
        System.out.println(error);
        GUIHelpers.displayErrorDialog(error);
        return false;
    }
    return true;
}
Also used : GcodeViewParse(com.willwinder.universalgcodesender.visualizer.GcodeViewParse) Point3d(javax.vecmath.Point3d) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) List(java.util.List) IOException(java.io.IOException) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) File(java.io.File)

Example 8 with GcodeStreamReader

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

the class VisualizerCanvas method generateObject.

/**
 * Parse the gcodeFile and store the resulting geometry and data about it.
 */
private void generateObject() {
    if (this.gcodeFile == null) {
        return;
    }
    try {
        GcodeViewParse gcvp = new GcodeViewParse();
        // Load from stream
        if (this.processedGcodeFile) {
            GcodeStreamReader gsr = new GcodeStreamReader(new File(this.gcodeFile));
            gcodeLineList = gcvp.toObjFromReader(gsr, 0.3);
        } else // Load raw file
        {
            List<String> linesInFile;
            linesInFile = VisualizerUtils.readFiletoArrayList(this.gcodeFile);
            gcodeLineList = gcvp.toObjRedux(linesInFile, 0.3);
        }
        this.objectMin = gcvp.getMinimumExtremes();
        this.objectMax = gcvp.getMaximumExtremes();
        if (gcodeLineList.size() == 0) {
            return;
        }
        // Grab the line number off the last line.
        this.lastCommandNumber = gcodeLineList.get(gcodeLineList.size() - 1).getLineNumber();
        System.out.println("Object bounds: X (" + objectMin.x + ", " + objectMax.x + ")");
        System.out.println("               Y (" + objectMin.y + ", " + objectMax.y + ")");
        System.out.println("               Z (" + objectMin.z + ", " + objectMax.z + ")");
        this.center = VisualizerUtils.findCenter(objectMin, objectMax);
        System.out.println("Center = " + center.toString());
        System.out.println("Num Line Segments :" + gcodeLineList.size());
        this.maxSide = VisualizerUtils.findMaxSide(objectMin, objectMax);
        this.scaleFactorBase = 1.0 / this.maxSide;
        this.scaleFactorBase = VisualizerUtils.findScaleFactor(this.xSize, this.ySize, this.objectMin, this.objectMax, 0.9);
        this.scaleFactor = this.scaleFactorBase * this.zoomMultiplier;
        this.isDrawable = true;
        double objectWidth = this.objectMax.x - this.objectMin.x;
        double objectHeight = this.objectMax.y - this.objectMin.y;
        this.dimensionsLabel = Localization.getString("VisualizerCanvas.dimensions") + ": " + Localization.getString("VisualizerCanvas.width") + "=" + format.format(objectWidth) + " " + Localization.getString("VisualizerCanvas.height") + "=" + format.format(objectHeight);
        // Now that the object is known, fill the buffers.
        this.createVertexBuffers();
        this.colorArrayDirty = true;
        this.vertexArrayDirty = true;
    } catch (GcodeParserException | IOException | GcodeStreamReader.NotGcodeStreamFile e) {
        String error = Localization.getString("mainWindow.error.openingFile") + " : " + e.getLocalizedMessage();
        System.out.println(error);
        GUIHelpers.displayErrorDialog(error);
    }
}
Also used : GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) IOException(java.io.IOException) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) File(java.io.File)

Example 9 with GcodeStreamReader

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

the class BufferedCommunicatorTest method testStreamCommandsOrderStringCommandsFirst.

@Test
public void testStreamCommandsOrderStringCommandsFirst() throws Exception {
    // Given
    Connection connection = mock(Connection.class);
    instance.setConnection(connection);
    ArgumentCaptor<String> commandCaptor = ArgumentCaptor.forClass(String.class);
    doNothing().when(connection).sendStringToComm(commandCaptor.capture());
    // Create a gcode file stream
    File gcodeFile = new File(tempDir, "gcodeFile");
    GcodeStreamWriter gcodeStreamWriter = new GcodeStreamWriter(gcodeFile);
    gcodeStreamWriter.addLine("G0", "G0", null, 0);
    gcodeStreamWriter.close();
    instance.queueStreamForComm(new GcodeStreamReader(gcodeFile));
    instance.queueStringForComm("G1");
    // When
    instance.streamCommands();
    // Then
    assertEquals(2, commandCaptor.getAllValues().size());
    assertEquals("The first command processed should be the string command", "G1\n", commandCaptor.getAllValues().get(0));
    assertEquals("The second command should be from the stream", "G0\n", commandCaptor.getAllValues().get(1));
}
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 10 with GcodeStreamReader

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

the class BufferedCommunicatorTest method testSimpleStreamStream.

@Test
public void testSimpleStreamStream() throws Exception {
    String[] inputs = { "input1", "input2" };
    for (String i : inputs) {
        mockScl.messageForConsole(EasyMock.anyString());
        EasyMock.expect(EasyMock.expectLastCall());
        mockConnection.sendStringToComm(i + "\n");
        EasyMock.expect(EasyMock.expectLastCall());
        mockScl.commandSent(EasyMock.<GcodeCommand>anyObject());
        EasyMock.expect(EasyMock.expectLastCall());
    }
    EasyMock.replay(mockConnection, mockScl);
    File f = new File(tempDir, "gcodeFile");
    try (GcodeStreamWriter gsw = new GcodeStreamWriter(f)) {
        for (String i : inputs) {
            gsw.addLine("blah", i, null, -1);
        }
    }
    GcodeStreamReader gsr = new GcodeStreamReader(f);
    instance.queueStreamForComm(gsr);
    instance.streamCommands();
    assertEquals("input1, input2, 0 streaming commands.", instance.activeCommandSummary());
    EasyMock.verify(mockConnection, mockScl);
}
Also used : GcodeStreamWriter(com.willwinder.universalgcodesender.utils.GcodeStreamWriter) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) File(java.io.File) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

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