From 8e251e318b102957bf6ea2da5607345d9f20abb2 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 25 Mar 2015 14:07:56 -0400
Subject: [PATCH] relative stock position

---
 js/cam/Assembler.js          | 11 +++++++++++
 js/menus/CamMenuView.js      | 35 ++++++++++++-----------------------
 js/threeViews/Highlighter.js |  9 ++++++++-
 3 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/js/cam/Assembler.js b/js/cam/Assembler.js
index 58e8899e..5b5b3431 100644
--- a/js/cam/Assembler.js
+++ b/js/cam/Assembler.js
@@ -163,10 +163,21 @@ Assembler = Backbone.Model.extend({
     _moveOrigin: function(){
         var position = this.get("originPosition");
         this.get("origin").position.set(position.x, position.y, position.z);
+        if (this.get("stockPositionRelative")) this._updateStockPosToOrigin(position);
         dmaGlobals.three.render();
         if (this.get("machine").setMachinePosition) this.get("machine").setMachinePosition();
     },
 
+    _updateStockPosToOrigin: function(newOrigin){
+        var lastOrigin = this.previous("originPosition");
+        var newStockPosition = _.clone(this.get("stockPosition"));
+        _.each(_.keys(newStockPosition), function(key){
+            newStockPosition[key] += newOrigin[key] - lastOrigin[key];
+            newStockPosition[key] = parseFloat(newStockPosition[key].toFixed(4));
+        });
+        this.set("stockPosition", newStockPosition);
+    },
+
     _moveStock: function(){
         var position = this.get("stockPosition");
         this.get("stock").position.set(position.x, position.y, position.z);
diff --git a/js/menus/CamMenuView.js b/js/menus/CamMenuView.js
index d5cf92c8..d52545b0 100644
--- a/js/menus/CamMenuView.js
+++ b/js/menus/CamMenuView.js
@@ -65,29 +65,18 @@ CamMenuView = Backbone.View.extend({
         else if ($(".stockSeparation").is(":focus")) this._updateNumber(e, "stockSeparation");
     },
 
-    _updateNumber: function(e, property){
-        e.preventDefault();
+    _getNumber: function(e, dontRound){
         var newVal = parseFloat($(e.target).val());
-        if (isNaN(newVal)) return;
+        if (isNaN(newVal)) return null;
+        if (dontRound) return newVal;
         newVal = parseFloat(newVal.toFixed(4));
-        var object = this.assembler.get(property);
-        if ($(e.target).data("type")) {
-            object[$(e.target).data("type")] = newVal;
-            this.assembler.trigger("change:" + property);
-            this.assembler.trigger("change");
-        }
-        else this.assembler.set(property, newVal);
+        return newVal;
     },
 
-    _updatePosNumber: function(e, property){
+    _updateNumber: function(e, property){
         e.preventDefault();
-        var newVal = parseFloat($(e.target).val());
-        if (isNaN(newVal)) return;
-        newVal = parseFloat(newVal.toFixed(4));
-        if (newVal <= 0) {
-            console.warn("value must be positive");
-            return;
-        }
+        var newVal = this._getNumber(e);
+        if (!newVal) return;
         var object = this.assembler.get(property);
         if ($(e.target).data("type")) {
             object[$(e.target).data("type")] = newVal;
@@ -99,8 +88,8 @@ CamMenuView = Backbone.View.extend({
 
     _updateRelativeStockPosition: function(e){
         e.preventDefault();
-        var newVal = parseFloat($(e.target).val());
-        if (isNaN(newVal)) return;
+        var newVal = this._getNumber(e, true);
+        if (!newVal) return;
         var dim = $(e.target).data("type");
         newVal = (newVal + this.assembler.get("originPosition")[dim]).toFixed(4);
         this.assembler.get("stockPosition")[dim] = parseFloat(newVal);
@@ -110,8 +99,8 @@ CamMenuView = Backbone.View.extend({
 
     _updateAbsoluteRapidHeight: function(e){
         e.preventDefault();
-        var newVal = parseFloat($(e.target).val());
-        if (isNaN(newVal)) return;
+        var newVal = this._getNumber(e, true);
+        if (!newVal) return;
         newVal -= this.assembler.get("originPosition").z.toFixed(4);//always store relative to origin
         this.assembler.set("rapidHeight", parseFloat(newVal));
     },
@@ -196,7 +185,7 @@ CamMenuView = Backbone.View.extend({
                 &nbsp;<input data-type="y" value="<%= stockArraySize.y %>" placeholder="Y" class="form-control numberInput stockArraySize" type="text"><br/><br/>\
                 Stock separation: &nbsp;&nbsp;<input value="<%= stockSeparation %>" placeholder="X" class="form-control numberInput stockSeparation" type="text"><br/><br/>\
             <% } %>\
-            Clearance Height: &nbsp;&nbsp;<input value="<%= rapidHeight %>" placeholder="Z" class="form-control numberInput rapidHeight" type="text"><br/>\
+            Clearance Height: &nbsp;&nbsp;<input value="<%= rapidHeight.toFixed(4) %>" placeholder="Z" class="form-control numberInput rapidHeight" 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="custom-checkbox">\
             <span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\
diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js
index 94f13021..736534a6 100644
--- a/js/threeViews/Highlighter.js
+++ b/js/threeViews/Highlighter.js
@@ -85,7 +85,14 @@ Highlighter = Backbone.View.extend({
     ///////////////////////////////////////////////////////////////////////////////////
 
     getHighlightedObjectPosition: function(){
-        if (this.highlightedObject instanceof DMACell) return this.highlightedObject.getPosition();
+        if (this.highlightedObject instanceof DMACell) {
+            var position = this.highlightedObject.getPosition();
+            return {
+                x:parseFloat(position.x.toFixed(4)),
+                y:parseFloat(position.y.toFixed(4)),
+                z:parseFloat(position.z.toFixed(4))
+            };
+        }
         return null;
     },
 
-- 
GitLab