Search in sources :

Example 1 with GcodeParserException

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

the class GcodeParser method handleGCode.

/**
 * Branch parser to handle specific gcode command.
 *
 * A copy of the state object should go in the resulting GcodeMeta object.
 */
private static GcodeMeta handleGCode(final Code code, List<String> args, int line, GcodeState state, boolean hasAxisWords) throws GcodeParserException {
    GcodeMeta meta = new GcodeMeta();
    meta.code = code;
    Position nextPoint = null;
    // If it is a movement code make sure it has some coordinates.
    if (code.consumesMotion()) {
        nextPoint = GcodePreprocessorUtils.updatePointWithCommand(args, state.currentPoint, state.inAbsoluteMode);
        if (nextPoint == null) {
            if (!code.motionOptional()) {
                throw new GcodeParserException(Localization.getString("parser.gcode.missing-axis-commands") + ": " + code);
            }
        }
    }
    if (nextPoint == null && meta.point != null) {
        nextPoint = meta.point.point();
    }
    switch(code) {
        case G0:
            meta.point = addLinearPointSegment(nextPoint, true, line, state);
            break;
        case G1:
            meta.point = addLinearPointSegment(nextPoint, false, line, state);
            break;
        // Arc command.
        case G2:
            meta.point = addArcPointSegment(nextPoint, true, args, line, state);
            break;
        case G3:
            meta.point = addArcPointSegment(nextPoint, false, args, line, state);
            break;
        case G17:
        case G18:
        case G19:
        case G17_1:
        case G18_1:
        case G19_1:
            state.plane = Plane.lookup(code);
            break;
        // inch
        case G20:
            state.isMetric = false;
            state.units = G20;
            state.currentPoint = state.currentPoint.getPositionIn(UnitUtils.Units.INCH);
            break;
        // mm
        case G21:
            state.isMetric = true;
            state.units = G21;
            state.currentPoint = state.currentPoint.getPositionIn(UnitUtils.Units.MM);
            break;
        // probe toward workpiece, stop on contact, signal error if failure
        case G38_2:
        // probe toward workpiece, stop on contact
        case G38_3:
        // probe away from workpiece, stop on loss of contact, signal error if failure
        case G38_4:
        case // probe away from workpiece, stop on loss of contact
        G38_5:
            meta.point = addProbePointSegment(nextPoint, true, line, state);
            break;
        // These are not used in the visualizer.
        case G54:
        case G55:
        case G56:
        case G57:
        case G58:
        case G59:
        case G59_1:
        case G59_2:
        case G59_3:
            state.offset = code;
            break;
        case G90:
            state.inAbsoluteMode = true;
            state.distanceMode = G90;
            break;
        case G91:
            state.inAbsoluteMode = false;
            state.distanceMode = G91;
            break;
        case G90_1:
            state.inAbsoluteIJKMode = true;
            state.arcDistanceMode = G90_1;
            break;
        case G91_1:
            state.inAbsoluteIJKMode = false;
            state.arcDistanceMode = G91_1;
            break;
        case G93:
        case G94:
        case G95:
            state.feedMode = code;
            break;
        default:
            break;
    }
    if (code.getType() == Motion) {
        state.currentMotionMode = code;
    }
    meta.state = state.copy();
    return meta;
}
Also used : Position(com.willwinder.universalgcodesender.model.Position) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException)

Example 2 with GcodeParserException

use of com.willwinder.universalgcodesender.gcode.util.GcodeParserException 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 3 with GcodeParserException

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

the class CommandLengthProcessor method processCommand.

@Override
public List<String> processCommand(String command, GcodeState state) throws GcodeParserException {
    if (command.length() > length)
        throw new GcodeParserException("Command '" + command + "' is longer than " + length + " characters.");
    List<String> ret = new ArrayList<>();
    ret.add(command);
    return ret;
}
Also used : ArrayList(java.util.ArrayList) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException)

Example 4 with GcodeParserException

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

the class GcodeModel method generateObject.

/**
 * Parse the gcodeFile and store the resulting geometry and data about it.
 */
private boolean generateObject() {
    isDrawable = false;
    if (this.gcodeFile == null) {
        return false;
    }
    try {
        GcodeViewParse gcvp = new GcodeViewParse();
        logger.log(Level.INFO, "About to process {}", gcodeFile);
        try {
            GcodeStreamReader gsr = new GcodeStreamReader(new File(gcodeFile));
            gcodeLineList = gcvp.toObjFromReader(gsr, 0.3);
        } catch (GcodeStreamReader.NotGcodeStreamFile e) {
            List<String> linesInFile;
            linesInFile = VisualizerUtils.readFiletoArrayList(this.gcodeFile);
            gcodeLineList = gcvp.toObjRedux(linesInFile, 0.3);
        }
        this.objectMin = gcvp.getMinimumExtremes();
        this.objectMax = gcvp.getMaximumExtremes();
        if (gcodeLineList.isEmpty()) {
            return false;
        }
        // Grab the line number off the last line.
        this.lastCommandNumber = gcodeLineList.get(gcodeLineList.size() - 1).getLineNumber();
        System.out.println("Object bounds: X (" + objectMin.x + ", " + objectMax.x + ")");
        System.out.println("               Y (" + objectMin.y + ", " + objectMax.y + ")");
        System.out.println("               Z (" + objectMin.z + ", " + objectMax.z + ")");
        Point3d center = VisualizerUtils.findCenter(objectMin, objectMax);
        System.out.println("Center = " + center.toString());
        System.out.println("Num Line Segments :" + gcodeLineList.size());
        objectSize.x = this.objectMax.x - this.objectMin.x;
        objectSize.y = this.objectMax.y - this.objectMin.y;
        objectSize.z = this.objectMax.z - this.objectMin.z;
        /*
            this.scaleFactorBase = VisualizerUtils.findScaleFactor(this.xSize, this.ySize, this.objectMin, this.objectMax);
            this.scaleFactor = this.scaleFactorBase * this.zoomMultiplier;

            this.dimensionsLabel = Localization.getString("VisualizerCanvas.dimensions") + ": " 
                    + Localization.getString("VisualizerCanvas.width") + "=" + format.format(objectWidth) + " " 
                    + Localization.getString("VisualizerCanvas.height") + "=" + format.format(objectHeight);
            */
        // Now that the object is known, fill the buffers.
        this.isDrawable = true;
        this.numberOfVertices = gcodeLineList.size() * 2;
        this.lineVertexData = new float[numberOfVertices * 3];
        this.lineColorData = new byte[numberOfVertices * 3];
        this.updateVertexBuffers();
    } catch (GcodeParserException | IOException e) {
        String error = Localization.getString("mainWindow.error.openingFile") + " : " + e.getLocalizedMessage();
        System.out.println(error);
        GUIHelpers.displayErrorDialog(error);
        return false;
    }
    return true;
}
Also used : GcodeViewParse(com.willwinder.universalgcodesender.visualizer.GcodeViewParse) Point3d(javax.vecmath.Point3d) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) List(java.util.List) IOException(java.io.IOException) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) File(java.io.File)

Example 5 with GcodeParserException

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

the class VisualizerCanvas method generateObject.

/**
 * Parse the gcodeFile and store the resulting geometry and data about it.
 */
private void generateObject() {
    if (this.gcodeFile == null) {
        return;
    }
    try {
        GcodeViewParse gcvp = new GcodeViewParse();
        // Load from stream
        if (this.processedGcodeFile) {
            GcodeStreamReader gsr = new GcodeStreamReader(new File(this.gcodeFile));
            gcodeLineList = gcvp.toObjFromReader(gsr, 0.3);
        } else // Load raw file
        {
            List<String> linesInFile;
            linesInFile = VisualizerUtils.readFiletoArrayList(this.gcodeFile);
            gcodeLineList = gcvp.toObjRedux(linesInFile, 0.3);
        }
        this.objectMin = gcvp.getMinimumExtremes();
        this.objectMax = gcvp.getMaximumExtremes();
        if (gcodeLineList.size() == 0) {
            return;
        }
        // Grab the line number off the last line.
        this.lastCommandNumber = gcodeLineList.get(gcodeLineList.size() - 1).getLineNumber();
        System.out.println("Object bounds: X (" + objectMin.x + ", " + objectMax.x + ")");
        System.out.println("               Y (" + objectMin.y + ", " + objectMax.y + ")");
        System.out.println("               Z (" + objectMin.z + ", " + objectMax.z + ")");
        this.center = VisualizerUtils.findCenter(objectMin, objectMax);
        System.out.println("Center = " + center.toString());
        System.out.println("Num Line Segments :" + gcodeLineList.size());
        this.maxSide = VisualizerUtils.findMaxSide(objectMin, objectMax);
        this.scaleFactorBase = 1.0 / this.maxSide;
        this.scaleFactorBase = VisualizerUtils.findScaleFactor(this.xSize, this.ySize, this.objectMin, this.objectMax, 0.9);
        this.scaleFactor = this.scaleFactorBase * this.zoomMultiplier;
        this.isDrawable = true;
        double objectWidth = this.objectMax.x - this.objectMin.x;
        double objectHeight = this.objectMax.y - this.objectMin.y;
        this.dimensionsLabel = Localization.getString("VisualizerCanvas.dimensions") + ": " + Localization.getString("VisualizerCanvas.width") + "=" + format.format(objectWidth) + " " + Localization.getString("VisualizerCanvas.height") + "=" + format.format(objectHeight);
        // Now that the object is known, fill the buffers.
        this.createVertexBuffers();
        this.colorArrayDirty = true;
        this.vertexArrayDirty = true;
    } catch (GcodeParserException | IOException | GcodeStreamReader.NotGcodeStreamFile e) {
        String error = Localization.getString("mainWindow.error.openingFile") + " : " + e.getLocalizedMessage();
        System.out.println(error);
        GUIHelpers.displayErrorDialog(error);
    }
}
Also used : GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) IOException(java.io.IOException) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) File(java.io.File)

Aggregations

GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)9 Position (com.willwinder.universalgcodesender.model.Position)6 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)3 Code (com.willwinder.universalgcodesender.gcode.util.Code)3 GcodeStreamReader (com.willwinder.universalgcodesender.utils.GcodeStreamReader)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 SplitCommand (com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand)2 PlaneFormatter (com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)2 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)2 PointSegment (com.willwinder.universalgcodesender.types.PointSegment)2 Iterables (com.google.common.collect.Iterables)1 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)1 CommandProcessor (com.willwinder.universalgcodesender.gcode.processors.CommandProcessor)1 M30Processor (com.willwinder.universalgcodesender.gcode.processors.M30Processor)1 Stats (com.willwinder.universalgcodesender.gcode.processors.Stats)1 Translator (com.willwinder.universalgcodesender.gcode.processors.Translator)1 Motion (com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion)1 UNKNOWN (com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN)1