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