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