Search in sources :

Example 1 with GcodeMeta

use of com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta in project Universal-G-Code-Sender by winder.

the class MeshLeveler method processCommand.

@Override
public List<String> processCommand(final String commandString, GcodeState state) throws GcodeParserException {
    List<GcodeMeta> commands = GcodeParser.processCommand(commandString, 0, state);
    // If there are no lines, return unmodified input.
    if (!hasJustLines(commands)) {
        return Collections.singletonList(commandString);
    }
    if (commands.size() > 1) {
        throw new GcodeParserException(Localization.getString("parser.processor.general.multiple-commands"));
    }
    GcodeMeta command = commands.get(0);
    if (command == null || command.point == null) {
        throw new GcodeParserException(ERROR_MISSING_POINT_DATA);
    }
    Position start = state.currentPoint;
    Position end = command.point.point();
    if (start.z != end.z) {
        this.lastZHeight = end.z;
    }
    // Get offset relative to the expected surface height.
    // Visualizer normalizes everything to MM but probe mesh might be INCH
    double probeScaleFactor = UnitUtils.scaleUnits(UnitUtils.Units.MM, this.unit);
    double zScaleFactor = UnitUtils.scaleUnits(UnitUtils.Units.MM, state.isMetric ? Units.MM : Units.INCH);
    double zPointOffset = surfaceHeightAt(end.x / zScaleFactor, end.y / zScaleFactor) - (this.materialSurfaceHeight / probeScaleFactor);
    zPointOffset *= zScaleFactor;
    // Update z coordinate.
    end.z = this.lastZHeight + zPointOffset;
    // end.z /= resultScaleFactor;
    String adjustedCommand = GcodePreprocessorUtils.generateLineFromPoints(command.code, start, end, command.state.inAbsoluteMode, null);
    return Collections.singletonList(adjustedCommand);
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException)

Example 2 with GcodeMeta

use of com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta 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;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 3 with GcodeMeta

use of com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta in project Universal-G-Code-Sender by winder.

the class GcodeParserTest method motionNoAxes.

@Test
public void motionNoAxes() throws Exception {
    List<GcodeMeta> metaList = GcodeParser.processCommand("G3", 0, new GcodeState());
    GcodeMeta meta = Iterables.getOnlyElement(metaList);
    assertThat(meta.code).isEqualTo(G3);
    assertThat(meta.state.currentPoint).isEqualTo(new Position(0, 0, 0, MM));
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) Test(org.junit.Test)

Example 4 with GcodeMeta

use of com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta in project Universal-G-Code-Sender by winder.

the class GcodeParserTest method testCommand.

private void testCommand(List<GcodeMeta> segments, int numResults, double speed, double x, double y, double z, boolean fastTraversal, boolean zMovement, boolean arc, boolean clockwise, boolean isMetric, int num) {
    int points = 0;
    for (GcodeMeta meta : segments) {
        if (meta.point != null) {
            points++;
            PointSegment ps = meta.point;
            assertEquals(ps.getSpeed(), speed, 0);
            assertEquals(x, ps.point().x, 0);
            assertEquals(y, ps.point().y, 0);
            assertEquals(z, ps.point().z, 0);
            assertEquals(fastTraversal, ps.isFastTraverse());
            assertEquals(zMovement, ps.isZMovement());
            assertEquals(arc, ps.isArc());
            if (arc) {
                assertEquals(clockwise, ps.isClockwise());
            }
            assertEquals(num, ps.getLineNumber());
            assertEquals(isMetric, ps.isMetric());
        }
    }
    assertEquals(numResults, points);
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) PointSegment(com.willwinder.universalgcodesender.types.PointSegment)

Example 5 with GcodeMeta

use of com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta 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;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser)

Aggregations

GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)11 Position (com.willwinder.universalgcodesender.model.Position)7 Test (org.junit.Test)5 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)3 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)2 SplitCommand (com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand)2 Code (com.willwinder.universalgcodesender.gcode.util.Code)2 PointSegment (com.willwinder.universalgcodesender.types.PointSegment)2 ArrayList (java.util.ArrayList)2 PlaneFormatter (com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)1 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)1