From 00e0a48c4ab96bcbffbc3f2a74c99d7fa6ea5d86 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 19 Aug 2015 01:19:41 -0400
Subject: [PATCH] adding in custom gcode functions

---
 js/cam/assemblers/Assembler.js     | 149 +---------------------
 js/cam/assemblers/AssemblerPost.js | 194 +++++++++++++++++++++++++++++
 js/cam/processes/GCodeExporter.js  |  55 ++++----
 js/main.js                         |   1 +
 js/plists/CamPList.js              |   2 +-
 5 files changed, 228 insertions(+), 173 deletions(-)
 create mode 100644 js/cam/assemblers/AssemblerPost.js

diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index e864f936..cbf6980f 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -2,8 +2,8 @@
  * Created by aghassaei on 5/28/15.
  */
 
-define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', 'component', 'stockComponent'],
-    function(_, appState, lattice, THREE, three, cam, Component, StockComponent){
+define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', 'component', 'stockComponent', 'assemblerPost'],
+    function(_, appState, lattice, THREE, three, cam, Component, StockComponent, AssemblerPostMethods){
     
     var assemblerMaterial = new THREE.MeshLambertMaterial({color:0xaaaaaa, shading: THREE.FlatShading, transparent:true, opacity:0.3});
     var stlLoader = new THREE.STLLoader();
@@ -19,16 +19,6 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         this.camProcesses = json.camProcesses;
         this.numMaterials = json.numMaterials;
 
-        this.customHeader = json.customHeader || function(settings){
-            return "";
-        };
-        this.customFooter = json.customFooter || function(){};
-        this.customHome = json.customHome || function(){};
-        this.customPickUpStock = json.customPickUpStock || function(){};
-        this.customPlacePart = json.customPlacePart || function(){};
-        this.customFunctionsContext = {
-            clearHeight: 8
-        };
 
         this.object3D = new THREE.Object3D();
         three.sceneAdd(this.object3D);
@@ -43,7 +33,10 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
 
         this.setVisibility(cam.isVisible());
         three.render();
+
+        AssemblerPostMethods.call(this, id, json);//needed to separate this for now
     }
+    Assembler.prototype = Object.create(AssemblerPostMethods.prototype);
 
     Assembler.prototype.getID = function(){
         return this.id;
@@ -183,139 +176,7 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         return tree;
     };
     
-    
-    
-
-
-
-
-    //post process
-
-    Assembler.prototype.postProcess = function(settings, exporter){
-
-        _.each(this.components, function(component){
-            component.postReset();
-        });
-
-        var data = "";
-        data += exporter.makeHeader(settings);
-        data += this._makeHeader(settings, exporter);
-        data += "\n\n";
-        data += exporter.addComment("begin program");
-        data += "\n";
-
-        data += this._postProcessCells(settings, exporter);
-
-        data += "\n\n";
-        data += exporter.addComment("end program");
-        data += "\n";
-        data += exporter.makeFooter(settings);
-
-        return data;
-    };
-
-    Assembler.prototype._makeHeader = function(settings, exporter){
-        var data = this.customHeader(settings);
-        data += this.home(exporter, settings);
-        return data;
-    };
-
-    Assembler.prototype.home = function(exporter, settings){
-//        this.components.zAxis.postMoveTo(settings.rapidHeight*settings*scale);
-        return exporter.goHome(settings);
-    };
-    
-    Assembler.prototype._postProcessCells = function(settings, exporter){
-        var data = "";
-        var self = this;
-
-        var lastIndex = null;
-    
-        lattice.rasterCells(cam._getOrder(cam.get("camStrategy")), function(cell){
-            if (!cell) return;
-            var cellPosition = cell.getAbsolutePosition().multiplyScalar(settings.scale);
-            var cellIndex = cell.getAbsoluteIndex();
-
-            if (!self.shouldPickUpStock){
-                data += self._postGetStock(cellIndex, lastIndex, cellPosition, cell.materialName, settings, exporter);
-            } else {
-//                var thisStockPosition = _.clone(stockPosition);
-//                if (multStockPositions) {
-//                    thisStockPosition.x += stockNum % stockArraySize.y * stockSeparation;
-//                    thisStockPosition.y -= Math.floor(stockNum / stockArraySize.y) * stockSeparation;
-//                    stockNum += 1;
-//                    if (stockNum >= stockArraySize.x * stockArraySize.y) stockNum = 0;
-//                }
-//                data += self._postMoveXY(exporter, stockPosition.x-wcs.x, stockPosition.y-wcs.y);
-//                data += self._postMoveToStock(exporter, thisStockPosition, rapidHeight, wcs, safeHeight);
-            }
-            data += self._postReleaseStock(cellIndex, cellPosition, cell.materialName, settings, exporter);
-            data += "\n";
-            lastIndex = cellIndex.clone();
-        });
-
-        return data;
-    };
-    
-    Assembler.prototype._postMoveXY = function(position, settings, exporter){
-        position = this.components.zAxis.applyAbsoluteRotation(position);
-        return exporter.rapidXY(position, settings);
-    };
-    
-//    Assembler.prototype._postMoveToStock = function(exporter, stockPosition, rapidHeight, wcs, safeHeight){
-//        var data = "";
-//        data += exporter.rapidZ(stockPosition.z-wcs.z+safeHeight);
-//        data += exporter.moveZ(stockPosition.z-wcs.z);
-//        data += this._postGetStock(exporter);
-//        data += exporter.moveZ(stockPosition.z-wcs.z+safeHeight);
-//        data += exporter.rapidZ(rapidHeight);
-//        return data;
-//    };
-    
-    Assembler.prototype._postGetStock = function(index, lastIndex, position, material, settings, exporter){
-        var data = "";
-        if (lastIndex === null || (lastIndex.z-index.z)%2 != 0){
-            data += exporter.addLine("G0", ["A" + (index.z%2*0.3125).toFixed(4)], "new layer");
-            data += "\n";
-        }
-        data += exporter.addComment("get stock " + JSON.stringify(index));
-        return data;
-    };
-    
-    Assembler.prototype._postReleaseStock = function(index, position, material, settings, exporter){
-        var data = "";
-        var stock = _.find(this.stock, function(thisStock){
-            return thisStock.getMaterial() == material
-        });
-        if (stock === undefined) {
-            console.warn("no stock defined of type " + material + " for this assembler");
-            return data;
-        }
-        position.sub(stock.getPosition().multiplyScalar(settings.scale));
-
-
-        position.sub(settings.originPosition);
-        data += this._postMoveXY(position.clone(), settings, exporter);
-
-        data += exporter.rapidZ(position.z+settings.safeHeight, settings);
-        data += exporter.moveZ(position.z, settings);
-
-        //place part
-        data += exporter.addComment(JSON.stringify(index));
-        if (material == "brass") data += exporter.addLine("M3");
-        else if (material == "fiberGlass") data += exporter.addLine("M4");
-        data += exporter.addLine("G4", ["P" + 0.75]);
-        data += exporter.addLine("M5");
-        data += exporter.moveZ(position.z, settings);
 
-        data += exporter.moveZ(position.z+settings.safeHeight, settings);
-        data += exporter.rapidZ(settings.rapidHeight, settings);
-        return data;
-    };
-
-
-    
-    
     
     
     //animation methods
diff --git a/js/cam/assemblers/AssemblerPost.js b/js/cam/assemblers/AssemblerPost.js
new file mode 100644
index 00000000..d2b8febd
--- /dev/null
+++ b/js/cam/assemblers/AssemblerPost.js
@@ -0,0 +1,194 @@
+/**
+ * Created by aghassaei on 8/19/15.
+ */
+
+
+
+define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, lattice, cam){
+
+
+    function AssemblerPost(id, json){
+
+        //settings:
+//        feedRate: THREE.Vector3
+//        originPosition: THREE.Vector3
+//        rapidHeight: 30 - max z height of machine, used when we want to be sure we're clear
+//        rapidSpeeds: THREE.Vector3
+//        safeHeight: 4.5 - height above parts where we start to move slowly
+//        scale: 1.27
+//        stockPosition: THREE.Vector3
+
+        this.customFunctionsContext = {
+            zClearHeight: 8,//height above part to clear during assembly
+            zPreload: 0.2,
+            wait: 0.75,//seconds
+            blOvershoot: 1.0
+        };
+
+        this.customHeader = function(exporter, settings, context){
+            var data = "";
+            data += exporter.setUnits(lattice.get("units"));
+            data += this.customHome(exporter, settings, context);
+            return data;
+        };
+
+        this.customFooter = function(exporter, settings, context){
+            var data = "";
+            data += this.customHome(exporter, settings, context);
+            return data;
+        };
+
+        this.customHome = function(exporter, settings, context){
+            var data = "";
+            data += exporter.goHome(settings);
+            return data;
+        };
+
+        this.customPickUpStock = function(exporter, settings, context){//not relevant for your assembler
+            var data = "";
+            return data;
+        };
+
+        this.customChangeZLayer = function(currentIndex, lastIndex, exporter, settings, context){
+            var data = "";
+            if (lastIndex === null || (currentIndex.z-lastIndex.z)%2 != 0){
+                data += exporter.addLine("G0", ["A" + (currentIndex.z%2*0.3125).toFixed(4)], "new layer");
+                data += "\n";
+            }
+            return data;
+        };
+
+        this.customMoveXY = function(position, index, exporter, settings, context){
+            var data = "";
+            data += exporter.rapidXY(position, settings);
+            return data;
+        };
+
+        this.customPlacePart = function(position, index, material, exporter, settings, context){
+            var data = "";
+            data += exporter.rapidZ(position.z + settings.safeHeight, settings);
+            data += exporter.moveZ(position.z, settings);
+
+            if (material == "brass") data += exporter.addLine("M3");
+            else if (material == "fiberGlass") data += exporter.addLine("M4");
+
+            data += exporter.addComment(JSON.stringify(index));
+
+            data += exporter.addLine("G4", ["P" + context.wait]);
+            data += exporter.addLine("M5");
+            data += exporter.moveZ(position.z, settings);
+
+            data += exporter.moveZ(position.z + settings.safeHeight, settings);
+            data += exporter.rapidZ(position.z + context.zClearHeight, settings);
+            return data;
+        };
+
+
+
+
+    }
+
+
+
+    //post process
+
+    AssemblerPost.prototype.postProcess = function(settings, exporter){//called from outside);
+
+        var data = "";
+        data += this.customHeader(exporter, settings, this.customFunctionsContext);
+
+        data += exporter.newLine();
+        data += exporter.newLine();
+
+        data += exporter.addComment("begin program");
+
+        data += exporter.newLine();
+
+        data += this._postProcessCells(exporter, settings, this.customFunctionsContext);
+
+        data += exporter.newLine();
+        data += exporter.newLine();
+
+        data += exporter.addComment("end program");
+
+        data += exporter.newLine();
+
+        data += this.customFooter(exporter, settings, this.customFunctionsContext);
+
+        return data;
+    };
+
+    AssemblerPost.prototype._postProcessCells = function(exporter, settings, context){
+        var data = "";
+        var self = this;
+        var lastIndex = null;
+
+        lattice.rasterCells(cam._getOrder(cam.get("camStrategy")), function(cell){
+            if (!cell) return;
+
+            var cellPosition = cell.getAbsolutePosition().multiplyScalar(settings.scale);
+            var cellIndex = cell.getAbsoluteIndex();
+
+            if (!self.shouldPickUpStock){
+                data += self._postGetStock(cellIndex, lastIndex, cellPosition, cell.materialName, settings, exporter, context);
+            } else {
+//                var thisStockPosition = _.clone(stockPosition);
+//                if (multStockPositions) {
+//                    thisStockPosition.x += stockNum % stockArraySize.y * stockSeparation;
+//                    thisStockPosition.y -= Math.floor(stockNum / stockArraySize.y) * stockSeparation;
+//                    stockNum += 1;
+//                    if (stockNum >= stockArraySize.x * stockArraySize.y) stockNum = 0;
+//                }
+//                data += self._postMoveXY(exporter, stockPosition.x-wcs.x, stockPosition.y-wcs.y);
+//                data += self._postMoveToStock(exporter, thisStockPosition, rapidHeight, wcs, safeHeight);
+            }
+            data += self._postReleaseStock(cellIndex, cellPosition, cell.materialName, settings, exporter, context);
+            data += "\n";
+            lastIndex = cellIndex.clone();
+        });
+
+        return data;
+    };
+
+
+//    Assembler.prototype._postMoveToStock = function(exporter, stockPosition, rapidHeight, wcs, safeHeight){
+//        var data = "";
+//        data += exporter.rapidZ(stockPosition.z-wcs.z+safeHeight);
+//        data += exporter.moveZ(stockPosition.z-wcs.z);
+//        data += this._postGetStock(exporter);
+//        data += exporter.moveZ(stockPosition.z-wcs.z+safeHeight);
+//        data += exporter.rapidZ(rapidHeight);
+//        return data;
+//    };
+
+    AssemblerPost.prototype._postGetStock = function(index, lastIndex, position, material, settings, exporter, context){
+        var data = "";
+        if (lastIndex === null || lastIndex.z != index.z){
+            data += this.customChangeZLayer(index, lastIndex, exporter, settings, context)
+        }
+        data += exporter.addComment("get stock " + JSON.stringify(index));
+        return data;
+    };
+
+    AssemblerPost.prototype._postReleaseStock = function(index, position, material, settings, exporter, context){
+        var data = "";
+        var stock = _.find(this.stock, function(thisStock){
+            return thisStock.getMaterial() == material
+        });
+        if (stock === undefined) {
+            console.warn("no stock defined of type " + material + " for this assembler");
+            return data;
+        }
+        position.sub(stock.getPosition().multiplyScalar(settings.scale));
+        position.sub(settings.originPosition);
+
+        data += this.customMoveXY(position.clone(), index.clone(), exporter, settings, context);
+
+        data += this.customPlacePart(position, index, material, exporter, settings, context);
+        return data;
+    };
+
+
+    return AssemblerPost;
+
+});
\ No newline at end of file
diff --git a/js/cam/processes/GCodeExporter.js b/js/cam/processes/GCodeExporter.js
index 6ec8212e..a6c0e404 100644
--- a/js/cam/processes/GCodeExporter.js
+++ b/js/cam/processes/GCodeExporter.js
@@ -10,19 +10,6 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
         this.animationSpeed = null;
     }
 
-    GCodeExporter.prototype.makeHeader = function(settings){
-        var data = "";
-        if (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");
-    ////    data += this.addLine("M09", [], "coolant off");
-//        data += this.goHome(settings);
-        return data;
-    };
-
     GCodeExporter.prototype.addLine = function(command, params, comment){
         var data = "";
         data += command + " ";
@@ -40,9 +27,21 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
     };
 
     GCodeExporter.prototype._setSpeed = function(speed){
+        this.postSpeed = speed;
         return "F"+ speed + "\n";
     };
 
+    GCodeExporter.prototype.setUnits = function(units){
+        if (units == "inches") return this.addLine("G20", [], "units inches");
+        else if (units == "mm") return this.addLine("G21", [], "units mm");
+        console.warn("units " + units + " not recognized");
+        return "";
+    };
+
+    GCodeExporter.prototype.newLine = function(){
+        return "\n";
+    };
+
     //GCodeExporter.prototype._rapidXYZ = function(x, y, z){
     //    return this._goXYZ(x,y,z);
     //};
@@ -59,13 +58,6 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
         return data + this._goXYZ(null, null, z);
     };
 
-    GCodeExporter.prototype._goXYZ = function(x, y, z){
-        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, settings){
         var data = "";
         if (this.postSpeed != settings.feedRate.x) data += this._setSpeed(settings.feedRate.x);
@@ -78,18 +70,25 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
         return data + this._goXYZ(null, null, z);
     };
 
-    GCodeExporter.prototype.goHome = function(settings){
-        var data = this._setSpeed(settings.rapidSpeeds.z);
-        return data + this._goXYZ(0,0,settings.rapidHeight);
+    GCodeExporter.prototype._goXYZ = function(x, y, z){
+        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.makeFooter = function(settings){
-        var data = "";
-        data += this.goHome(settings);
-    //    data += this.addLine("M30", [], "program stop");
-        return data;
+    GCodeExporter.prototype.goHome = function(settings){
+        var data = this.rapidZ(settings.rapidHeight, settings);
+        return data + this.rapidXY({x:0, y:0}, settings);
     };
 
+
+
+
+
+
+
+
     GCodeExporter.prototype.save = function(data){
         var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
         saveAs(blob, "GCodeExport" + ".nc");
diff --git a/js/main.js b/js/main.js
index 0f4ef86e..2f3c128f 100644
--- a/js/main.js
+++ b/js/main.js
@@ -184,6 +184,7 @@ require.config({
 
         //assemblers
         assembler: 'cam/assemblers/Assembler',
+        assemblerPost: 'cam/assemblers/AssemblerPost',
         component: 'cam/assemblers/Component',
         stockComponent: 'cam/assemblers/StockComponent',
 
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index 83c5c99d..6368967b 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -126,7 +126,7 @@ define(['three'], function(THREE){
                     camStrategy: "raster",
                     placementOrder: "XYZ",//used for raster strategy entry
                     camProcess: "gcode",
-                    rapidHeight:30,
+                    rapidHeight: 30,
                     rapidHeightRelative: true,
                     safeHeight: 4.5,
                     originPosition: new THREE.Vector3(0,0,0),
-- 
GitLab