Skip to content
Snippets Groups Projects
GCodeExporter.js 3.57 KiB
Newer Older
/**
 * Created by aghassaei on 3/10/15.
 */

function GCodeExporter() {
}

GCodeExporter.prototype.makeHeader = function(){
    var data = "";
amandaghassaei's avatar
amandaghassaei committed
    if (dmaGlobals.lattice.get("units") == "inches") data += this.addLine("G20", [], "units inches");
    else data += this.addLine("G21", [], "units mm");
    data += this.addLine("G90", [], "absolute positioning");
    data += this.addLine("G54", [], "work offset");
//    data += this.addLine("G49", [], "cancel tool length comp");
    data += this.addLine("G40", [], "cancel tool radius comp");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
//    data += this.addLine("M09", [], "coolant off");

    data += this.goHome();

    //set rapid and feed speeds
    var rapidSpeeds = dmaGlobals.assembler.get("rapidSpeeds");
    var feedRate = dmaGlobals.assembler.get("feedRate");

    return data;
};

GCodeExporter.prototype.addLine = function(command, params, comment){
    var data = "";
    data += command + " ";
    _.each(params, function(param){
        if (!param) return;
amandaghassaei's avatar
amandaghassaei committed
        data += param + " ";
    });
    if (comment) data += "(" + comment + ")";
    data += "\n";
    return data;
};

Amanda Ghassaei's avatar
Amanda Ghassaei committed
GCodeExporter.prototype.addComment = function(comment){
    return "(" + comment + ")" + "\n";
};

GCodeExporter.prototype.rapidXYZ = function(x, y, z){
    return this.moveXYZ(x,y,z);
GCodeExporter.prototype.rapidXY = function(x, y){
    return this.rapidXYZ(x, y, null);
};

amandaghassaei's avatar
amandaghassaei committed
GCodeExporter.prototype.rapidZ = function(z){
    return this.rapidXYZ(null, null, z);
};

GCodeExporter.prototype.moveXYZ = function(x, y, z){
amandaghassaei's avatar
amandaghassaei committed
    if (x !== null) x = "X"+parseFloat(x).toFixed(3);
    if (y !== null) y = "Y"+parseFloat(y).toFixed(3);
    if (z !== null) z = "Z"+parseFloat(z).toFixed(3);
    return this.addLine("G01", [x,y,z]);
};

GCodeExporter.prototype.moveXY = function(x, y){
    return this.moveXYZ(x, y, null);
};

GCodeExporter.prototype.moveZ = function(z){
    return this.moveXYZ(null, null, z);
GCodeExporter.prototype.goHome = function(){
    return this.moveXYZ(0,0,dmaGlobals.assembler.get("rapidHeight"));
};

GCodeExporter.prototype.makeFooter = function(){
    var data = "";
    data += this.addLine("M30", [], "program stop");

    return data;
};

GCodeExporter.prototype.save = function(data){
    var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    saveAs(blob, "GCodeExport" + ".nc");
amandaghassaei's avatar
amandaghassaei committed

GCodeExporter.prototype.simulate = function(line, machine, wcs,  callback){
    if (line == "(get stock)"){
        machine.pickUpStock();
        return callback();
    }
    if (line.substr(0,2) == "({"){
        machine.releaseStock(line.substr(1,line.length-2));
        return callback();
    }
    if (line == "" || line[0] == "(" || line.substr(0,3) != "G01"){
        return callback();
    }
    if (line.substr(0,3) == "G01"){
        //return this._simulateGetPosition(line, dmaGlobals.assembler.get("feedRate"), machine, wcs, callback);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        return this._simulateGetPosition(line, dmaGlobals.assembler.get("rapidSpeeds"), machine, wcs, callback);
amandaghassaei's avatar
amandaghassaei committed
    } else {
        console.warn("problem parsing gcode: " + line);
        return callback();
    }
};

GCodeExporter.prototype._simulateGetPosition = function(line, speed, machine, wcs, callback){
    var data = line.split(" ");
    var position = {x:"",y:"",z:""};
    if (data.length<2) console.warn("problem parsing gcode " + line);
    for (var i=1;i<data.length;i++){
        var item = data[i];
        if (item[0] == "X") position.x = item.substr(1);
        if (item[0] == "Y") position.y = item.substr(1);
        if (item[0] == "Z") position.z = item.substr(1);
    }
    machine.moveTo(position.x, position.y, position.z, speed, wcs, callback);
};