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());
}
}
}
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;
}
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);
}
}
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));
}
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);
}
Aggregations