From b9d696e8862550e0ed0b98f0ab5be0e3eb28ace3 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 14 Aug 2015 13:58:27 -0400
Subject: [PATCH] scale gcode

---
 js/cam/Cam.js                       | 62 +++++++++++++----------
 js/cam/assemblers/Assembler.js      | 77 +++++++++++++----------------
 js/cam/processes/GCodeExporter.js   | 28 +++++------
 js/menus/CamMenuView.js             |  2 +-
 js/menus/templates/CamMenuView.html |  6 +--
 js/plists/CamPList.js               |  4 +-
 6 files changed, 92 insertions(+), 87 deletions(-)

diff --git a/js/cam/Cam.js b/js/cam/Cam.js
index b7ea7925..4c1aacc0 100644
--- a/js/cam/Cam.js
+++ b/js/cam/Cam.js
@@ -54,28 +54,28 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
             this.listenTo(appState, "change:currentNav", this._navChanged);
             this.listenTo(this, "change:originPosition", this._moveOrigin);
 //            this.listenTo(this, "change:stockPosition", this._moveStock);
-//            this.listenTo(this,
-//                    "change:originPosition " +
-//                    "change:stockPosition " +
-//                    "change:feedRate " +
-//                    "change:rapidSpeeds " +
-//                    "change:camProcess " +
-//                    "change:camStrategy " +
-//                    "change:placementOrder " +
-//                    "change:safeHeight " +
-//                    "change:stockArraySize " +
-//                    "change:stockSeparation " +
-//                    "change:multipleStockPositions " +
-//                    "change:rapidHeight " +
-//                    "change:machineName",
-//                this._setNeedsPostProcessing);
-//            this.listenTo(lattice,
-//                    "change:scale" +
-//                    "change:units" +
-//                    "change:numCells " +
-//                    "change:cellType " +
-//                    "change:connectionType",
-//                this._setNeedsPostProcessing);
+            this.listenTo(this,
+                    "change:originPosition " +
+                    "change:stockPosition " +
+                    "change:feedRate " +
+                    "change:rapidSpeeds " +
+                    "change:camProcess " +
+                    "change:camStrategy " +
+                    "change:placementOrder " +
+                    "change:safeHeight " +
+                    "change:stockArraySize " +
+                    "change:stockSeparation " +
+                    "change:multipleStockPositions " +
+                    "change:rapidHeight " +
+                    "change:machineName",
+                this._setNeedsPostProcessing);
+            this.listenTo(lattice,
+                    "change:scale" +
+                    "change:units" +
+                    "change:numCells " +
+                    "change:cellType " +
+                    "change:connectionType",
+                this._setNeedsPostProcessing);
             this.listenTo(appState, "change:stockSimulationPlaying", this._stockSimulation);
 
             this.listenTo(lattice, "change:partType", this._updatePartType);
@@ -299,20 +299,32 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
 
         postProcess: function(){
             this.set("needsPostProcessing", false);
+
+            var scale = lattice.get("scale");
+            var scaledSettings = {
+                scale: scale,
+                rapidHeight: this.get("rapidHeight"),
+                safeHeight: this.get("safeHeight"),
+                originPosition: this.get("originPosition").clone().multiplyScalar(scale),
+                stockPosition: this.get("stockPosition").clone().multiplyScalar(scale),
+                rapidSpeeds: new THREE.Vector3(this.get("rapidSpeeds").xy, this.get("rapidSpeeds").xy, this.get("rapidSpeeds").z),
+                feedRate: new THREE.Vector3(this.get("feedRate").xy, this.get("feedRate").xy, this.get("feedRate").z)
+            };
+
             var self = this;
             this._getExporter(function(exporter){
                 var data = "";
-                data += exporter.makeHeader();
+                data += exporter.makeHeader(scaledSettings);
                 data += "\n\n";
                 data += exporter.addComment("begin program");
                 data += "\n";
 
-                data = self.get("assembler").postProcess(data, exporter);
+                data += self.get("assembler").postProcess(scaledSettings, exporter);
 
                 data += "\n\n";
                 data += exporter.addComment("end program");
                 data += "\n";
-                data += exporter.makeFooter();
+                data += exporter.makeFooter(scaledSettings);
 
                 self.set("dataOut", data);
                 self.set("editsMadeToProgram", false);
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index c1b96b9c..3f5d827e 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -123,67 +123,60 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
     
     
     
-    Assembler.prototype.postProcess = function(data, exporter){//override in subclasses
-    
-        var rapidHeight = cam.get("rapidHeight");
-        var safeHeight = cam.get("safeHeight");
-        var wcs = cam.get("originPosition");
-    
-        var stockPosition = cam.get("stockPosition");
-        var stockNum = 0;//position of stock in stock array
-        var multStockPositions = cam.get("multipleStockPositions");
-        var stockSeparation = cam.get("stockSeparation");
-        var stockArraySize = cam.get("stockArraySize");
+    Assembler.prototype.postProcess = function(settings, exporter){//override in subclasses
+        var data = "";
         var self = this;
     
         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(exporter);
+                data += self._postGetStock(cellIndex, cellPosition, 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);
+//                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);
             }
-            var cellPosition = cell.getAbsolutePosition();
-            data += self._postMoveXY(exporter, cellPosition.x-wcs.x, cellPosition.y-wcs.y);
-            data += self._postReleaseStock(cellPosition, cell, exporter, rapidHeight, wcs, safeHeight);
+            data += self._postMoveXY(cellPosition.x-settings.originPosition.x, cellPosition.y-settings.originPosition.x, settings, exporter);
+            data += self._postReleaseStock(cellPosition, cell, settings, exporter);
             data += "\n";
         });
         return data;
     };
     
-    Assembler.prototype._postMoveXY = function(exporter, x, y){
-        return exporter.rapidXY(x, y);
-    };
-    
-    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._postMoveXY = function(x, y, settings, exporter){
+        return exporter.rapidXY(x, y, settings);
     };
     
-    Assembler.prototype._postGetStock = function(exporter){
+//    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, position, settings, exporter){
         return exporter.addComment("get stock");
     };
     
-    Assembler.prototype._postReleaseStock = function(cellPosition, cell, exporter, rapidHeight, wcs, safeHeight){
+    Assembler.prototype._postReleaseStock = function(cellPosition, cell, settings, exporter){
         var data = "";
-        data += exporter.rapidZ(cellPosition.z-wcs.z+safeHeight);
-        data += exporter.moveZ(cellPosition.z-wcs.z);
+        data += exporter.rapidZ(cellPosition.z-settings.originPosition.z+settings.safeHeight, settings);
+        data += exporter.moveZ(cellPosition.z-settings.originPosition.z, settings);
         data += exporter.addComment(JSON.stringify(cell.getAbsoluteIndex()));
-        data += exporter.moveZ(cellPosition.z-wcs.z+safeHeight);
-        data += exporter.rapidZ(rapidHeight);
+        data += exporter.moveZ(cellPosition.z-settings.originPosition.z+settings.safeHeight, settings);
+        data += exporter.rapidZ(settings.rapidHeight, settings);
         return data;
     };
     
diff --git a/js/cam/processes/GCodeExporter.js b/js/cam/processes/GCodeExporter.js
index feb519a0..b14e42d3 100644
--- a/js/cam/processes/GCodeExporter.js
+++ b/js/cam/processes/GCodeExporter.js
@@ -10,7 +10,7 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
         this.animationSpeed = null;
     }
 
-    GCodeExporter.prototype.makeHeader = function(){
+    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");
@@ -20,7 +20,7 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
     //    data += this.addLine("G40", [], "cancel tool radius comp");
     ////    data += this.addLine("M09", [], "coolant off");
 
-        data += this.goHome();
+        data += this.goHome(settings);
 
         return data;
     };
@@ -49,15 +49,15 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
     //    return this._goXYZ(x,y,z);
     //};
 
-    GCodeExporter.prototype.rapidXY = function(x, y){
+    GCodeExporter.prototype.rapidXY = function(x, y, settings){
         var data = "";
-        if (this.postSpeed != cam.get("rapidSpeeds").xy) data += this._setSpeed(cam.get("rapidSpeeds").xy);
+        if (this.postSpeed != settings.rapidSpeeds.x) data += this._setSpeed(settings.rapidSpeeds.x);
         return data + this._goXYZ(x, y, null);
     };
 
-    GCodeExporter.prototype.rapidZ = function(z){
+    GCodeExporter.prototype.rapidZ = function(z, settings){
         var data = "";
-        if (this.postSpeed != cam.get("rapidSpeeds").z) data += this._setSpeed(cam.get("rapidSpeeds").z);
+        if (this.postSpeed != settings.rapidSpeeds.z) data += this._setSpeed(settings.rapidSpeeds.z);
         return data + this._goXYZ(null, null, z);
     };
 
@@ -68,24 +68,24 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
         return this.addLine("G01", [x,y,z]);
     };
 
-    GCodeExporter.prototype.moveXY = function(x, y){
+    GCodeExporter.prototype.moveXY = function(x, y, settings){
         var data = "";
-        if (this.postSpeed != cam.get("feedRate").xy) data += this._setSpeed(cam.get("feedRate").xy);
+        if (this.postSpeed != settings.feedRate.x) data += this._setSpeed(settings.feedRate.x);
         return data + this._goXYZ(x, y, null);
     };
 
-    GCodeExporter.prototype.moveZ = function(z){
+    GCodeExporter.prototype.moveZ = function(z, settings){
         var data = "";
-        if (this.postSpeed != cam.get("feedRate").z) data += this._setSpeed(cam.get("feedRate").z);
+        if (this.postSpeed != settings.feedRate.z) data += this._setSpeed(settings.feedRate.z);
         return data + this._goXYZ(null, null, z);
     };
 
-    GCodeExporter.prototype.goHome = function(){
-        var data = this._setSpeed(cam.get("rapidSpeeds").z);
-        return data + this._goXYZ(0,0,cam.get("rapidHeight"));
+    GCodeExporter.prototype.goHome = function(settings){
+        var data = this._setSpeed(settings.rapidSpeeds.z);
+        return data + this._goXYZ(0,0,settings.rapidHeight);
     };
 
-    GCodeExporter.prototype.makeFooter = function(){
+    GCodeExporter.prototype.makeFooter = function(settings){
         var data = "";
     //    data += this.addLine("M30", [], "program stop");
         return data;
diff --git a/js/menus/CamMenuView.js b/js/menus/CamMenuView.js
index 6a9577d7..4618023d 100644
--- a/js/menus/CamMenuView.js
+++ b/js/menus/CamMenuView.js
@@ -36,7 +36,7 @@ define(['jquery', 'underscore', 'menuParent', 'camPlist', 'cam', 'lattice', 'tex
         },
 
         _makeTemplateJSON: function(){
-            var data = _.extend(this.model.toJSON(), cam.toJSON(), lattice.toJSON(), camPlist, cam.get("assembler").toJSON());
+            var data = _.extend(this.model.toJSON(), cam.toJSON(), camPlist, cam.get("assembler").toJSON(), lattice.toJSON());
             if (cam.get("stockPositionRelative")){
                 var relStockPos = {};
                 relStockPos.x = data.stockPosition.x - data.originPosition.x;
diff --git a/js/menus/templates/CamMenuView.html b/js/menus/templates/CamMenuView.html
index 8ab84d5b..abf7b09f 100644
--- a/js/menus/templates/CamMenuView.html
+++ b/js/menus/templates/CamMenuView.html
@@ -46,7 +46,7 @@ Origin (xyz):&nbsp;&nbsp;
                     Stock separation: &nbsp;&nbsp;<input data-property="stockSeparation" value="<%= stockSeparation %>" placeholder="X" class="form-control floatInput assembler" type="text"><br/><br/>
                 <% } %>
     <% } %>
-    Clearance Height: &nbsp;&nbsp;
+    Clearance Height (<%= units %>): &nbsp;&nbsp;
         <input data-property="rapidHeight" value="<%= rapidHeight %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/>
             <label class="checkbox" for="rapidPosRel">
             <input id="rapidPosRel" data-property="rapidHeightRelative" type="checkbox" <% if (rapidHeightRelative){ %> checked="checked"<% } %> value="" data-toggle="checkbox" class="assembler custom-checkbox">
@@ -55,8 +55,8 @@ Origin (xyz):&nbsp;&nbsp;
 <% } else { %><br/><br/>
     Stock Height: &nbsp;&nbsp;<input data-property="stockPosition" data-key="z" value="<%= stockPosition.z %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
 <% } %>
-Approach Height: &nbsp;&nbsp;<input data-property="safeHeight" value="<%= safeHeight %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
-Speeds (measured in <%= units %> per second):<br/><br/>
+Approach Height  (<%= units %>): &nbsp;&nbsp;<input data-property="safeHeight" value="<%= safeHeight %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
+Speeds (<%= units %> per second):<br/><br/>
     Rapids (xy, z): &nbsp;&nbsp;<input data-property="rapidSpeeds" data-key="xy" value="<%= rapidSpeeds.xy %>" placeholder="XY" class="form-control floatInput assembler" type="text">&nbsp;
         <input data-property="rapidSpeeds" data-key="z" value="<%= rapidSpeeds.z %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
     Feed Rate (xy, z): &nbsp;&nbsp;<input data-property="feedRate" data-key="xy" value="<%= feedRate.xy %>" placeholder="XY" class="form-control floatInput assembler" type="text">&nbsp;
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index 06a7e0fc..8c3c587c 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -3,7 +3,7 @@
  */
 
 
-define([], function(){
+define(['three'], function(THREE){
 
     return {
 
@@ -141,7 +141,7 @@ define([], function(){
                     rapidHeight:10,
                     rapidHeightRelative: true,
                     safeHeight: 0.5,
-                    originPosition: {x:0,y:0,z:0},
+                    originPosition: new THREE.Vector3(0,0,0),
                     rapidSpeeds:{xy: 3, z: 2},
                     feedRate:{xy: 0.1, z: 0.1}
                 }
-- 
GitLab