From 06502e5ca74d57118b821623248f3051c0473144 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 14 Aug 2015 11:23:57 -0400
Subject: [PATCH] stock setup working

---
 js/cam/Cam.js                                 |  2 +-
 js/cam/assemblers/Assembler.js                | 55 +++++++-------
 js/cam/assemblers/StockComponent.js           | 71 +++++++++++++++++++
 js/cells/DMACell.js                           |  2 +-
 js/main.js                                    |  1 +
 .../templates/AssemblerSetupMenuView.html     |  5 ++
 js/plists/CamPList.js                         | 20 ++++++
 7 files changed, 124 insertions(+), 32 deletions(-)
 create mode 100644 js/cam/assemblers/StockComponent.js

diff --git a/js/cam/Cam.js b/js/cam/Cam.js
index 4832f14d..23d7c507 100644
--- a/js/cam/Cam.js
+++ b/js/cam/Cam.js
@@ -79,7 +79,7 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
 //            this.listenTo(appState, "change:stockSimulationPlaying", this._stockSimulation);
 
 //            this.listenTo(lattice, "change:partType", this._updatePartType);
-//            this.listenTo(appState, "change:cellMode", this._updateCellMode);
+            this.listenTo(appState, "change:cellMode", this._updateCellMode);
             this.listenTo(this, "change:machineName", this.selectMachine);
 
             this._navChanged();
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index f8a1fb7c..47e7453e 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'],
-    function(_, appState, lattice, THREE, three, cam, Component){
+define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', 'component', 'stockComponent'],
+    function(_, appState, lattice, THREE, three, cam, Component, StockComponent){
     
     var assemblerMaterial = new THREE.MeshLambertMaterial({color:0xaaaaaa, shading: THREE.FlatShading, transparent:true, opacity:0.5});
     var stlLoader = new THREE.STLLoader();
@@ -11,7 +11,6 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
     function Assembler(id, json){
 
         this.id = id;
-        this.components = {};
         this.rotation = json.rotation;
         this.translation = json.translation;
         this.scale = json.scale;
@@ -24,18 +23,14 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         this.object3D = new THREE.Object3D();
         three.sceneAdd(this.object3D);
 
-//        var self = this;
-//        this._buildStock(function(stock){
-//            self.stock = stock;
-//            self._positionStockRelativeToEndEffector(stock);
-//        });
 
-        if (json.components && _.keys(json.components).length > 0) {
-            var componentsJSON = json.components;
-            this.components = this._buildAssemblerComponents(componentsJSON);
-            this._loadSTLs(json, this.components);
-            this._configureAssemblerMovementDependencies(componentsJSON, this.components, this.object3D);
-        }
+        var componentsJSON = json.components;
+        this.components = this._buildAssemblerComponents(componentsJSON);
+        this._loadSTLs(json, this.components);
+        this._configureAssemblerMovementDependencies(componentsJSON, this.components, this.components, this.object3D);
+
+        this.stock = this._buildAssemblerComponents(json.stock, StockComponent);
+        this._configureAssemblerMovementDependencies(json.stock, this.components, this.stock, this.object3D);
 
         this.setVisibility(cam.isVisible());
         three.render();
@@ -86,29 +81,25 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         });
     };
 
-    Assembler.prototype._buildAssemblerComponents = function(componentsJSON){
+    Assembler.prototype._buildAssemblerComponents = function(componentsJSON, Class){
         var components = {};
+        if (componentsJSON === undefined ) return components;
         _.each(componentsJSON, function(componentJSON, id){
-            components[id] = new Component(id, componentJSON);
+            if (Class) components[id] = new Class(id, componentJSON);
+            else components[id] = new Component(id, componentJSON);
         });
         return components;
     };
 
-    Assembler.prototype._configureAssemblerMovementDependencies = function(json, components, object3D){
+    Assembler.prototype._configureAssemblerMovementDependencies = function(json, components, newComponents, object3D){
+        if (json === undefined) return;
         _.each(json, function(componentJSON, id){
-            if (componentJSON.parent) components[componentJSON.parent].addChild(components[id]);
-            else object3D.add(components[id].getObject3D());
+            if (componentJSON.parent) components[componentJSON.parent].addChild(newComponents[id]);
+            else object3D.add(newComponents[id].getObject3D());
         });
     };
-    
-    Assembler.prototype._buildStock = function(callback){
-        lattice.makeCellForLatticeType({}, callback);
-    };
-    
-    Assembler.prototype._positionStockRelativeToEndEffector = function(stock){
-        var object3D = stock.getObject3D();
-        object3D.position.set((2.4803+0.2)*20, (-1.9471+0.36)*20, 1.7*20);
-    };
+
+
 
     
 
@@ -202,11 +193,15 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
     //animation methods
     
     Assembler.prototype.updateCellMode = function(){//message from cam
-        this.stock.setMode();
+        _.each(this.stock, function(stock){
+            stock.setMode();
+        });
     };
     
     Assembler.prototype.pickUpStock = function(){
-        this.stock.show();
+        _.each(this.stock, function(stock){
+            stock.show();
+        });
     };
     
     Assembler.prototype.releaseStock = function(index){
diff --git a/js/cam/assemblers/StockComponent.js b/js/cam/assemblers/StockComponent.js
new file mode 100644
index 00000000..8fb6fa23
--- /dev/null
+++ b/js/cam/assemblers/StockComponent.js
@@ -0,0 +1,71 @@
+/**
+ * Created by aghassaei on 8/13/15.
+ */
+
+
+define(['underscore', 'cam', 'three', 'component', 'lattice'], function(_, cam, THREE, Component, lattice){
+
+    function StockComponent(id, json){
+
+        Component.call(this, id, json);
+        //material
+
+        var self = this;
+        this._makeCell(json.description, function(cell){
+            self.cell = cell;
+            self._setPosition(cell, json.position, json.rotation);
+            self.object3D.add(cell.getObject3D());
+            cell.show();
+        });
+    }
+    StockComponent.prototype = Object.create(Component.prototype);
+    //assembler setup
+
+
+
+
+    StockComponent.prototype._makeCell = function(json, callback){
+        lattice.makeCellForLatticeType(json, callback);
+    };
+
+    StockComponent.prototype._setPosition = function(cell, position, rotation){
+        var object3D = cell.getObject3D();//todo need this?
+        object3D.position.set(position.x, position.y, position.z);
+        //todo rotation
+    };
+
+
+
+
+
+    //simulation animation
+
+    StockComponent.prototype.show = function(){
+        this.cell.show();
+    };
+
+    StockComponent.prototype.hide = function(){
+        this.cell.hide();
+    };
+
+    StockComponent.prototype.setMode = function(){
+        this.cell.setMode();
+    };
+
+
+
+    //helper
+
+    StockComponent.prototype.destroy = function(){
+        Component.prototype.destroy.call(this);
+
+    };
+
+
+    StockComponent.prototype.toJSON = function(){
+        var json  = Component.prototype.toJSON.call(this);
+        return json;
+    };
+
+    return StockComponent;
+});
\ No newline at end of file
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 1c5e78a6..c3f58443 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -21,7 +21,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', '
 
         if (this.superCell) this.superCell.addChildren(this.object3D);//add as child of supercell
 
-        if (this.index){
+        if (this.index){//todo gik supcells in stock have index but no highlight
             if (!this.cells) lattice.getUItarget().addHighlightableCell(this.object3D.children[0]);//add mesh as highlightable object, only for lowest level of hierarchy
             if (!superCell || superCell === undefined) three.sceneAdd(this.object3D);//add object3d as child of scene if top level of hierarchy
         } else this.hide();//stock cell
diff --git a/js/main.js b/js/main.js
index 7ce0c919..d7ddb87d 100644
--- a/js/main.js
+++ b/js/main.js
@@ -175,6 +175,7 @@ require.config({
         //assemblers
         assembler: 'cam/assemblers/Assembler',
         component: 'cam/assemblers/Component',
+        stockComponent: 'cam/assemblers/StockComponent',
 
         //processes
         gcode: 'cam/processes/GCodeExporter',
diff --git a/js/menus/templates/AssemblerSetupMenuView.html b/js/menus/templates/AssemblerSetupMenuView.html
index d4d1668e..356e7d4a 100644
--- a/js/menus/templates/AssemblerSetupMenuView.html
+++ b/js/menus/templates/AssemblerSetupMenuView.html
@@ -3,6 +3,11 @@ Parent/Child Tree:<br/>
     <%= component.name %> <a data-id="<%= component.id %>" class="editMachineComponent" href="#">Edit</a><br/>
 <% }); %><br/>
 <a href="#" id="newMachineComponent" class=" btn btn-block btn-lg btn-default">+ New Machine Component</a><br/>
+Stock:<br/>
+<% _.each(stock, function(stockEl, id){ %>
+    <%= stockEl.name %> <a data-id="<%= id %>" class="edit Stock" href="#">Edit</a><br/>
+<% }); %><br/>
+<a href="#" id="addStock" class=" btn btn-block btn-lg btn-default">+ New Stock</a><br/>
 Offset (xyz):&nbsp;&nbsp;
     <input data-property="translation" data-key="x" value="<%= translation.x %>" placeholder="X" class="form-control floatInput assembler" type="text">&nbsp;
     <input data-property="translation" data-key="y" value="<%= translation.y %>" placeholder="Y" class="form-control floatInput assembler" type="text">&nbsp;
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index bced04ce..95c92ccf 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -114,6 +114,26 @@ define([], function(){
                         }
                     }
                 },
+                stock:{
+                    stock1: {
+                        description:{
+                            materialName: "brass",
+                            length: 4
+                        },
+                        name: "Stock 1",
+                        parent: "zAxis",
+                        position: {x:0,y:0,z:0}
+                    },
+                    stock2: {
+                        description:{
+                            materialName: "fiberGlass",
+                            length: 4
+                        },
+                        name: "Stock 2",
+                        parent: "zAxis",
+                        position: {x:26.1,y:0,z:0}
+                    }
+                },
                 defaults: {
                     camStrategy: "raster",
                     placementOrder: "XYZ",//used for raster strategy entry
-- 
GitLab