Search in sources :

Example 1 with SplitCommand

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

the class LineSplitter method processCommand.

@Override
public List<String> processCommand(String commandString, GcodeState state) throws GcodeParserException {
    List<GcodeParser.GcodeMeta> commands = GcodeParser.processCommand(commandString, 0, state);
    List<String> results = new ArrayList<>();
    Code code = hasLine(commands);
    if (code == null) {
        return Collections.singletonList(commandString);
    }
    SplitCommand sc = GcodePreprocessorUtils.extractMotion(code, commandString);
    if (sc.remainder.length() > 0) {
        results.add(sc.remainder);
    }
    GcodeMeta command = Iterables.getLast(commands);
    if (command == null || command.point == null) {
        throw new GcodeParserException("Internal parser error: missing data.");
    }
    // line length
    Position start = state.currentPoint;
    Position end = command.point.point();
    Position current = start;
    double length = start.distance(end);
    // Check if line needs splitting.
    if (length > this.maxSegmentLength) {
        int numSegments = (int) Math.ceil(length / this.maxSegmentLength);
        double segmentLength = length / Math.ceil(length / this.maxSegmentLength);
        // Create line segments, stop before the last one which uses the end point.
        for (int i = 1; i < numSegments; i++) {
            double k = 1 / (length / (i * segmentLength));
            double newX = start.x + k * (end.x - start.x);
            double newY = start.y + k * (end.y - start.y);
            double newZ = start.z + k * (end.z - start.z);
            Position next = new Position(newX, newY, newZ, start.getUnits());
            results.add(GcodePreprocessorUtils.generateLineFromPoints(command.code, current, next, command.state.inAbsoluteMode, null));
            current = next;
        }
        // Add the last line point.
        results.add(GcodePreprocessorUtils.generateLineFromPoints(command.code, current, end, command.state.inAbsoluteMode, null));
    } else {
        return Collections.singletonList(commandString);
    }
    return results;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) ArrayList(java.util.ArrayList) SplitCommand(com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) Code(com.willwinder.universalgcodesender.gcode.util.Code)

Example 2 with SplitCommand

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

the class ArcExpander method processCommand.

@Override
public List<String> processCommand(String command, GcodeState state) throws GcodeParserException {
    if (state.currentPoint == null)
        throw new GcodeParserException(Localization.getString("parser.processor.arc.start-error"));
    List<String> results = new ArrayList<>();
    List<GcodeMeta> commands = GcodeParser.processCommand(command, 0, state);
    // If this is not an arc, there is nothing to do.
    Code c = hasArcCommand(commands);
    if (c == null) {
        return Collections.singletonList(command);
    }
    SplitCommand sc = GcodePreprocessorUtils.extractMotion(c, command);
    if (sc.remainder.length() > 0) {
        results.add(sc.remainder);
    }
    GcodeMeta arcMeta = Iterables.getLast(commands);
    PointSegment ps = arcMeta.point;
    Position start = state.currentPoint;
    Position end = arcMeta.point.point();
    List<Position> points = GcodePreprocessorUtils.generatePointsAlongArcBDring(start, end, ps.center(), ps.isClockwise(), ps.getRadius(), 0, this.length, new PlaneFormatter(ps.getPlaneState()));
    // That function returns the first and last points. Exclude the first
    // point because the previous gcode command ends there already.
    points.remove(0);
    if (convertToLines) {
        // Tack the speed onto the first line segment in case the arc also
        // changed the feed value.
        String feed = "F" + arcMeta.point.getSpeed();
        for (Position point : points) {
            results.add(GcodePreprocessorUtils.generateLineFromPoints(G1, start, point, state.inAbsoluteMode, df) + feed);
            start = point;
            feed = "";
        }
    } else {
        // TODO: Generate arc segments.
        throw new UnsupportedOperationException("I have not implemented this.");
    }
    return results;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) ArrayList(java.util.ArrayList) SplitCommand(com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand) Code(com.willwinder.universalgcodesender.gcode.util.Code) PointSegment(com.willwinder.universalgcodesender.types.PointSegment) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) PlaneFormatter(com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)

Aggregations

GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)2 SplitCommand (com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand)2 Code (com.willwinder.universalgcodesender.gcode.util.Code)2 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)2 Position (com.willwinder.universalgcodesender.model.Position)2 ArrayList (java.util.ArrayList)2 PlaneFormatter (com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)1 PointSegment (com.willwinder.universalgcodesender.types.PointSegment)1