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