From c793d8a2c6356e34a0ba4e833845b08a0474df2b Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 20 Aug 2015 20:05:14 -0400
Subject: [PATCH] load assembler from file

---
 js/cam/Cam.js                             | 27 ++++++++++++++---------
 js/cam/assemblers/Assembler.js            | 25 ++++++++++++++-------
 js/cam/assemblers/Component.js            |  3 +--
 js/cam/assemblers/StockComponent.js       | 14 +++++-------
 js/menus/AssemblerMenuView.js             |  5 ++++-
 js/menus/templates/AssemblerMenuView.html | 15 +++++++------
 js/models/FileSaver.js                    | 11 +++++++++
 7 files changed, 64 insertions(+), 36 deletions(-)

diff --git a/js/cam/Cam.js b/js/cam/Cam.js
index a86eab5c..76650eac 100644
--- a/js/cam/Cam.js
+++ b/js/cam/Cam.js
@@ -97,17 +97,23 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
         },
 
 
-        selectMachine: function(){
-
-            var machineName = this.get("machineName");
-            if (this.get("assembler")){
-                if (this.get("assembler").getID() == machineName) return;
-                else {
-                    this.get("assembler").destroy();
-                    this.set("assembler", null);
+        selectMachine: function(json){
+
+            var machineJSON = json;
+            if (machineJSON === undefined){
+                var machineName = this.get("machineName");
+                if (this.get("assembler")){
+                    if (this.get("assembler").getID() == machineName) return;
+                    else {
+                        this.get("assembler").destroy();
+                        this.set("assembler", null);
+                    }
                 }
+                machineJSON = camPlist.allMachines[machineName];
+            } else {
+                machineName = "customMachine";
+                this.set("machineName", machineName, {silent:true});
             }
-            var machineJSON = camPlist.allMachines[machineName];
             if (machineJSON.defaults) this._setMachineDefaults(machineJSON.defaults);
             if (machineJSON.lattice) this._setLatticeDefaults(machineJSON.lattice)
 
@@ -126,7 +132,8 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
         _setMachineDefaults: function(defaults){
             var self = this;
             _.each(defaults, function(value, key){
-                self.set(key, value, {silent:true});
+                if (value.x !== undefined && value.x !== null) self.set(key, new THREE.Vector3(value.x, value.y, value.z), {silent:true});
+                else self.set(key, value, {silent:true});
             });
         },
 
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index 948c0a59..c1f0c04e 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -238,9 +238,6 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         }
 
         var startingPos = this.components.xAxis.getPosition().add(this.components.frame.getPosition().add(this.components.zAxis.getPosition()));//this.components.zAxis.getAbsolutePosition();//get position of end effector
-        console.log("here");
-        console.log(startingPos);//this.components.zAxis.applyAbsoluteRotation(
-        console.log(position);
         speed = this._normalizeSpeed(startingPos, position, new THREE.Vector3(speed, speed, speed));//todo fix this
 
         this.components.xAxis.moveTo(position, speed.x, sketchyCallback);
@@ -279,9 +276,14 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         var self = this;
         _.each(this.components, function(component, index){
             component.destroy();
-            self[index] = null;
+            self.components[index] = null;
+        });
+        _.each(this.stock, function(thisStock, index){
+            thisStock.destroy();
+            self.stock[index] = null;
         });
         this.components = null;
+        this.stock = null;
         three.sceneRemove(this.object3D);
         this.stock = null;
         this.components.zAxis = null;
@@ -309,7 +311,7 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             rapidSpeeds: cam.get("rapidSpeeds"),
             feedRate: cam.get("feedRate")
         };
-        return json;
+        return {assembler: json};
     };
 
     Assembler.prototype.toJSON = function(){
@@ -321,10 +323,18 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         _.each(this.stock, function(thisStock, id){
             stockJSON[id] = thisStock.toJSON();
         });
-        return {
-            name: this.name,
+        var json = this.basicJSON();
+        _.extend(json, {
             components: componentsJSON,
             stock: stockJSON,
+            tree: this.tree || this.buildComponentTree()
+        });
+        return json;
+    };
+
+    Assembler.prototype.basicJSON = function(){
+        return {
+            name: this.name,
             translation: this.translation,
             scale: this.scale,
             rotation: this.rotation,
@@ -332,7 +342,6 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             relative: this.relative,
             camProcesses: this.camProcesses,
             numMaterials: this.numMaterials,
-            tree: this.tree || this.buildComponentTree()
         }
     };
 
diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js
index 1afebd5b..3069c7b4 100644
--- a/js/cam/assemblers/Component.js
+++ b/js/cam/assemblers/Component.js
@@ -22,7 +22,6 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
         if (json.motionVector) this.motionVector.set(json.motionVector.x, json.motionVector.y, json.motionVector.z);
         this.stlJSON = json.stl;
 
-
         this.postReset();
     }
 
@@ -297,7 +296,7 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
             parent: this.parent || "",
             translation: this.object3D.position,
             scale: this.object3D.scale.x,
-            rotation: this.object3D.rotation,
+            rotation: this.object3D.rotation.toVector3(),
             isStatic: this.isStatic,
             rotary: this.rotary,
             motionVector: this.motionVector,
diff --git a/js/cam/assemblers/StockComponent.js b/js/cam/assemblers/StockComponent.js
index 31c911ef..0bb9539e 100644
--- a/js/cam/assemblers/StockComponent.js
+++ b/js/cam/assemblers/StockComponent.js
@@ -11,10 +11,12 @@ define(['underscore', 'cam', 'three', 'component', 'lattice', 'threeModel'],
         Component.call(this, id, json);
         //material
 
+        this.object3D.position.set(json.translation.x, json.translation.y, json.translation.z);
+        if (json.rotation) this.object3D.rotation.set(json.rotation.x, json.rotation.y, json.rotation.z);
+
         var self = this;
         this._makeCell(json.description, function(cell){
             self.cell = cell;
-            self._setPosition(cell, json.translation, json.rotation);
             self.object3D.add(cell.getObject3D());
         });
     }
@@ -28,12 +30,6 @@ define(['underscore', 'cam', 'three', 'component', 'lattice', 'threeModel'],
         lattice.makeCellForLatticeType(json, callback);
     };
 
-    StockComponent.prototype._setPosition = function(cell, position, rotation){
-        var object3D = cell.getObject3D();//todo need this?
-        if (position) object3D.position.set(position.x, position.y, position.z);
-        if (rotation) object3D.rotation.set(rotation.x, rotation.y, rotation.z);
-    };
-
     StockComponent.prototype.getPosition = function(){
         return this.cell.getPosition();
     };
@@ -55,6 +51,7 @@ define(['underscore', 'cam', 'three', 'component', 'lattice', 'threeModel'],
 
     StockComponent.prototype.show = function(){
         this.cell.show();
+        console.log(this.cell);
     };
 
     StockComponent.prototype.hide = function(){
@@ -75,7 +72,8 @@ define(['underscore', 'cam', 'three', 'component', 'lattice', 'threeModel'],
 
     StockComponent.prototype.destroy = function(){
         Component.prototype.destroy.call(this);
-
+        this.cell.destroy();
+        this.cell = null;
     };
 
 
diff --git a/js/menus/AssemblerMenuView.js b/js/menus/AssemblerMenuView.js
index 85340094..aacf4129 100644
--- a/js/menus/AssemblerMenuView.js
+++ b/js/menus/AssemblerMenuView.js
@@ -36,7 +36,10 @@ define(['jquery', 'underscore', 'menuParent', 'camPlist', 'cam', 'lattice', 'mat
         },
 
         _makeTemplateJSON: function(){
-            return _.extend(this.model.toJSON(), cam.toJSON(), lattice.toJSON(), camPlist, {materials:materials.list});
+            var assembler = cam.get("assembler");
+            if (assembler)  assembler = assembler.basicJSON();
+            else assembler = camPlist.allMachines[cam.get("machineName")];
+            return _.extend(this.model.toJSON(), {thisAssembler: assembler}, cam.toJSON(), lattice.toJSON(), camPlist, {materials:materials.list});
         },
     
         template: _.template(template)
diff --git a/js/menus/templates/AssemblerMenuView.html b/js/menus/templates/AssemblerMenuView.html
index 9d027b96..06063640 100644
--- a/js/menus/templates/AssemblerMenuView.html
+++ b/js/menus/templates/AssemblerMenuView.html
@@ -1,9 +1,10 @@
 <% if (numCells == 0){ %>
     <div class="inlineWarning">No cells in assembly!</div>
 <% } %>
+<a href="#" class="importJSON btn btn-block btn-lg btn-default">Load Assembler From File</a><br/>
 Machine: &nbsp;&nbsp;
     <div class="btn-group">
-        <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= allMachines[machineName].name %><span class="caret"></span></button>
+        <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= thisAssembler.name %><span class="caret"></span></button>
         <ul role="menu" class="dropdown-menu">
             <% _.each(machineTypesForLattice[cellType][connectionType], function(machine){ %>
                 <li><a class="assembler dropdownSelector" data-property="machineName" data-value="<%= machine %>" href="#"><%= allMachines[machine].name %></a></li>
@@ -11,15 +12,15 @@ Machine: &nbsp;&nbsp;
         </ul>
     </div><br/>
     <label class="checkbox" for="relativeAssembler">
-        <input id="relativeAssembler" data-property="relative" type="checkbox" <% if (allMachines[machineName].relative){ %> checked="checked"<% } %> value="" data-toggle="checkbox" class="machine custom-checkbox">
+        <input id="relativeAssembler" data-property="relative" type="checkbox" <% if (thisAssembler.relative){ %> checked="checked"<% } %> value="" data-toggle="checkbox" class="machine custom-checkbox">
         <span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>
         Relative Assembler</label>
     <label class="checkbox" for="stockAttached">
-        <input id="stockAttached" data-property="shouldPickUpStock" type="checkbox" <% if (allMachines[machineName].shouldPickUpStock){ %> checked="checked"<% } %> value="" data-toggle="checkbox" class="machine custom-checkbox">
+        <input id="stockAttached" data-property="shouldPickUpStock" type="checkbox" <% if (thisAssembler.shouldPickUpStock){ %> checked="checked"<% } %> value="" data-toggle="checkbox" class="machine custom-checkbox">
         <span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>
         Separate Stock Position</label>
-        <% if (allMachines[machineName].numMaterials > -1){ %>
-            Num Materials: &nbsp;&nbsp;<%= allMachines[machineName].numMaterials %><br/>
+        <% if (thisAssembler.numMaterials > -1){ %>
+            Num Materials: &nbsp;&nbsp;<%= thisAssembler.numMaterials %><br/>
         <% } %><br/>
 Strategy: &nbsp;&nbsp;
     <div class="btn-group">
@@ -33,8 +34,8 @@ Strategy: &nbsp;&nbsp;
 <% if (camStrategy == "raster"){ %>
     Raster Order: &nbsp;&nbsp;<input value="<%= placementOrder %>" placeholder="Placement Order" class="form-control placementOrder halfWidth" type="text"><br/><br/>
 <% } %>
-<% if (allMachines[machineName].numMaterials > -1 && allMachines[machineName].numMaterials < allCAMMaterialTypes.length){ %>
-    <div class="inlineWarning">Number of materials in assembly exceeds available materials ( <%= allMachines[machineName].numMaterials %> ) for assembler type.</div>
+<% if (thisAssembler.numMaterials > -1 && thisAssembler.numMaterials < allCAMMaterialTypes.length){ %>
+    <div class="inlineWarning">Number of materials in assembly exceeds available materials ( <%= thisAssembler.numMaterials %> ) for assembler type.</div>
 <% } %>
 Num Materials in Assembly: &nbsp;&nbsp;<%= allCAMMaterialTypes.length %><br/>
 <% _.each(allCAMMaterialTypes, function(material){ %>
diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js
index 899c113a..70587b3b 100644
--- a/js/models/FileSaver.js
+++ b/js/models/FileSaver.js
@@ -76,6 +76,10 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra
     }
 
     function loadFile(data){//parsed json todo make this better - load composite
+        if (data.assembler) {
+            _loadAssembler(data.assembler);
+            return;
+        }
         if (!data.materials){
             console.warn("no material definitions in this file");
             return;
@@ -95,6 +99,13 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra
         menuWrapper.render();
     }
 
+    function _loadAssembler(data){
+        require(['cam'], function(cam){
+            cam.selectMachine(data);
+            console.log("loaded");
+        });
+    }
+
     function loadUser(data){
         _setData(data);
     }
-- 
GitLab