From 4b52360c5fd7f66e8a7250eea9a7c36db764aedb Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 13 Aug 2015 01:16:23 -0400
Subject: [PATCH] eod

---
 js/cam/Cam.js                         | 131 +++++++++++++++-----------
 js/cam/assemblers/Assembler.js        | 103 ++++++++++++++------
 js/cam/assemblers/Component.js        |   7 +-
 js/cam/assemblers/StaplerAssembler.js |   7 +-
 js/plists/CamPList.js                 |  28 +++---
 5 files changed, 174 insertions(+), 102 deletions(-)

diff --git a/js/cam/Cam.js b/js/cam/Cam.js
index f0a556a2..20f2d8de 100644
--- a/js/cam/Cam.js
+++ b/js/cam/Cam.js
@@ -52,35 +52,34 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
             //bind events
             this.listenTo(appState, "change:currentTab", this._tabChanged);
             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(appState, "change:stockSimulationPlaying", this._stockSimulation);
-
-            this.listenTo(lattice, "change:partType", this._updatePartType);
-            this.listenTo(lattice, "change:cellType change:connectionType", this._updateCellType);
-            this.listenTo(appState, "change:cellMode", this._updateCellMode);
+//            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(appState, "change:stockSimulationPlaying", this._stockSimulation);
+
+//            this.listenTo(lattice, "change:partType", this._updatePartType);
+//            this.listenTo(appState, "change:cellMode", this._updateCellMode);
             this.listenTo(this, "change:machineName", this.selectMachine);
 
             this._navChanged();
@@ -98,23 +97,29 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
 
 
         selectMachine: function(){
+
             var machineName = this.get("machineName");
-            if (this.get("assembler")) this.get("assembler").destroy();
-            this.set("assembler", null);
-            this._setMachineDefaults(machineName);
+            if (this.get("assembler")){
+                if (this.get("assembler").getID() == machineName) return;
+                else {
+                    this.get("assembler").destroy();
+                    this.set("assembler", null);
+                }
+            }
+            var machineJSON = camPlist.allMachines[machineName];
+            if (machineJSON.defaults) this._setMachineDefaults(machineJSON.defaults);
+
             var self = this;
-            require([machineName], function(MachineClass){
-                self.set('assembler', new MachineClass());
+            require(['assembler'], function(Assembler){
+                self.set('assembler', new Assembler(machineName, machineJSON));
             });
         },
 
-        _setMachineDefaults: function(machineName){
+        _setMachineDefaults: function(defaults){
             var self = this;
-            if (camPlist.allMachines[machineName].defaults){
-                _.each(camPlist.allMachines[machineName].defaults, function(value, key){
-                    self.set(key, value, {silent:true});
-                });
-            }
+            _.each(defaults, function(value, key){
+                self.set(key, value, {silent:true});
+            });
         },
 
         makeProgramEdits: function(data){
@@ -127,16 +132,21 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
 
 
 
+
+
+
+
+
         //events
 
         _navChanged: function(){
             if (appState.get("currentNav") == "navAssemble") {
-                this._setToDefaults();
+                this._setDefaultMachineForLatticeType();
                 this._calculateNumMaterials();
             }
         },
 
-        _setToDefaults: function(){
+        _setDefaultMachineForLatticeType: function(){
             //call this each time we switch to assemble tab
             var availableMachines = camPlist.machineTypesForLattice[lattice.get("cellType")][lattice.get("connectionType")];
             if (availableMachines.indexOf(this.get("machineName")) < 0){
@@ -161,12 +171,6 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
             three.render();
         },
 
-        _updateCellType: function(){
-            if (this.get("assembler")) this.get("assembler").updateCellType();
-            this.set("machineName", "handOfGod");//todo this should go away with dynamic allocation of this model
-
-        },
-
         _updatePartType: function(){
             if (this.get("assembler")) this.get("assembler").updatePartType();
         },
@@ -230,9 +234,18 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
             three.render();
         },
 
-    ///////////////////////////////////////////////////////////////////////////////////////////////
-    ///////////////////////////////SIMULATION//////////////////////////////////////////////////////
-    ///////////////////////////////////////////////////////////////////////////////////////////////
+
+
+
+
+
+
+
+
+
+
+
+        //simulation
 
         resetSimulation: function(){
             this.set("simLineNumber", 0, {silent:true});
@@ -266,9 +279,19 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
 
         },
 
-    ///////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////POST PROCESSING////////////////////////////////////////////////
-    ///////////////////////////////////////////////////////////////////////////////////////////////
+
+
+
+
+
+
+
+
+
+
+
+
+        //post processing
 
         _setNeedsPostProcessing: function(){
             this.set("needsPostProcessing", true);
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index ea57acac..ab9b0eed 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -2,54 +2,103 @@
  * Created by aghassaei on 5/28/15.
  */
 
-define(['underscore', 'appState', 'lattice', 'three', 'threeModel', 'cam', 'component'], function(_, appState, lattice, THREE, three, cam, Component){
+define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', 'component'],
+    function(_, appState, lattice, THREE, three, cam, Component){
     
     var assemblerMaterial = new THREE.MeshLambertMaterial({color:0xaaaaaa, shading: THREE.FlatShading, transparent:true, opacity:0.5});
-    
-    function Assembler(){
+    var stlLoader = new THREE.STLLoader();
+
+    function Assembler(id, json){
 
+        this.id = id;
         this.components = {};
-        this.stock = this._buildStock();
-        this._positionStockRelativeToEndEffector(this.stock);
 
         this.object3D = new THREE.Object3D();
         three.sceneAdd(this.object3D);
-        this._buildAssemblerComponents();
-        this._configureAssemblerMovementDependencies();
-        three.render();
+
+//        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(componentsJSON, this.components);
+            this._configureAssemblerMovementDependencies(componentsJSON, this.components, this.object3D);
+        }
 
         this.setVisibility(cam.isVisible());
+        three.render();
     }
-    
-    Assembler.prototype._buildStock = function(callback){
-        lattice.makeCellForLatticeType({}, callback);
+
+    Assembler.prototype.getID = function(){
+        return this.id;
     };
-    
-    Assembler.prototype._positionStockRelativeToEndEffector = function(stock){
+
+    Assembler.prototype._getStlNames = function(json){
+        var stls = {};
+        _.each(json, function(component, id){
+            if (component === undefined || component.stl === undefined || component.stl.filename === undefined){
+                console.warn("no stl found for component " + component.name);
+            }
+            stls[id] = "bin!" + component.stl.filename;
+        });
+        return stls;
     };
-    
-    Assembler.prototype._buildAssemblerComponents = function(){
-        var allSTLs = this._getSTLs();
-        var self = this;
-        _.each(allSTLs, function(geometry, name){
-            var component = new Component(geometry, assemblerMaterial, name);
-            self.components[component.getID()] = component;
+
+    Assembler.prototype._loadSTLs = function(json, components){
+        var stlFilenames = this._getStlNames(json);
+
+        function geometryPreProcess(geometry){//todo do this better
+            if(geometry === undefined || (geometry.vertices && geometry.vertices.length == 0)) return null;
+
+            geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-4.0757, -4.3432, -6.2154));
+            geometry.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
+
+            var unitScale = 20;
+            geometry.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
+            geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-21, -0.63, 0));
+            return geometry;
+        }
+
+        _.each(stlFilenames, function(filename, id){
+            require([filename], function(geo){
+                geo = geometryPreProcess(stlLoader.parse(geo));
+                if (geo === null) {
+                    console.warn("no geometry loaded for " + filename);
+                    return;
+                }
+                components[id].makeGeometry(geo, assemblerMaterial);
+            });
         });
     };
 
-    Assembler.prototype.getComponentByName = function(name){
-        return _.find(_.values(this.components), function(component){
-            return component.name == name;
+    Assembler.prototype._buildAssemblerComponents = function(componentsJSON){
+        var components = [];
+        _.each(componentsJSON, function(componentJSON, id){
+            components[id] = new Component(id);
         });
+        return components;
     };
 
-    Assembler.prototype.getComponentById = function(id){
-        return this.components[id];
+    Assembler.prototype._configureAssemblerMovementDependencies = function(json, components, object3D){
+        _.each(json, function(componentJSON, id){
+            if (componentJSON.parent) components[componentJSON.parent].addChild(components[id]);
+            else object3D.add(components[id].getObject3D());
+        });
     };
     
-    Assembler.prototype._configureAssemblerMovementDependencies = function(){
-        //override in subclasses
+    Assembler.prototype._buildStock = function(callback){
+        lattice.makeCellForLatticeType({}, callback);
+    };
+    
+    Assembler.prototype._positionStockRelativeToEndEffector = function(stock){
     };
+
+    
+
     
     Assembler.prototype.setVisibility = function(visible){
         this.object3D.visible = visible;
diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js
index a16e4181..e9d37f81 100644
--- a/js/cam/assemblers/Component.js
+++ b/js/cam/assemblers/Component.js
@@ -8,7 +8,7 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
     var id = 0;
 
     function Component(geometry, material, name){
-        this.object3D = new THREE.Mesh(geometry, material);
+        this.object3D = new THREE.Object3D();
         this.id = "id" + id++;
         this.name = name || "";
         this.parent = null;
@@ -17,6 +17,11 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
 
     //assembler setup
 
+
+    Component.prototype.makeGeometry = function(geo, material){
+        this.object3D.add(new THREE.Mesh(geo, material));
+    };
+
     Component.prototype.addChild = function(child){
         if (this.checkAncestry(child)){
             console.warn("can't add parent as child");
diff --git a/js/cam/assemblers/StaplerAssembler.js b/js/cam/assemblers/StaplerAssembler.js
index 5b5f36c7..305382b0 100644
--- a/js/cam/assemblers/StaplerAssembler.js
+++ b/js/cam/assemblers/StaplerAssembler.js
@@ -44,12 +44,7 @@ define(['underscore', 'assembler', 'stlLoader', 'gikSuperCell',
     };
 
     StaplerAssembler.prototype._configureAssemblerMovementDependencies = function(){
-        this.getComponentByName("zAxis").addChild(this.stock);
-        this.getComponentByName("xAxis").addChild(this.getComponentByName("zAxis"));
-        this.getComponentByName("frame").addChild(this.getComponentByName("xAxis"));
-        this.getComponentByName("frame").addChild(this.getComponentByName("yAxis"));
-        this.getComponentByName("substrate").addChild(this.getComponentByName("frame"));
-        this.object3D.add(this.getComponentByName("substrate").getObject3D());
+
     };
 
     StaplerAssembler.prototype._getSTLs = function(){
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index da298b4c..4516a495 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -41,10 +41,10 @@ define([], function(){
                         axisOfMotion: null,
                         //minBound
                         //maxBound
-                        parent: null,
-                        children: [],
+                        parent: "frame",
+                        children: ["zAxis"],
                         stl: {
-                            filename: "jflsdkjfsd.stl",
+                            filename: "assets/stls/stapler/xAxis.stl",
                             offset: {x:0,y:0,z:0},
                             scale: 1.0,
                             rotation: {x:0,y:0,z:0}
@@ -56,10 +56,10 @@ define([], function(){
                         axisOfMotion: null,
                         //minBound
                         //maxBound
-                        parent: null,
-                        children: ["zAxis"],
+                        parent: "frame",
+                        children: [],
                         stl: {
-                            filename: "jflsdkjfsd.stl",
+                            filename: "assets/stls/stapler/yAxis.stl",
                             offset: {x:0,y:0,z:0},
                             scale: 1.0,
                             rotation: {x:0,y:0,z:0}
@@ -71,10 +71,10 @@ define([], function(){
                         axisOfMotion: null,
                         //minBound
                         //maxBound
-                        parent: null,
-                        children: [],
+                        parent: "xAxis",
+                        children: [],//stock
                         stl: {
-                            filename: "jflsdkjfsd.stl",
+                            filename: "assets/stls/stapler/zAxis.stl",
                             offset: {x:0,y:0,z:0},
                             scale: 1.0,
                             rotation: {x:0,y:0,z:0}
@@ -86,10 +86,10 @@ define([], function(){
                         axisOfMotion: null,
                         //minBound
                         //maxBound
-                        parent: null,
-                        children: [],
+                        parent: "substrate",
+                        children: ["xAxis", "yAxis"],
                         stl: {
-                            filename: "jflsdkjfsd.stl",
+                            filename: "assets/stls/stapler/frame.stl",
                             offset: {x:0,y:0,z:0},
                             scale: 1.0,
                             rotation: {x:0,y:0,z:0}
@@ -102,9 +102,9 @@ define([], function(){
                         //minBound
                         //maxBound
                         parent: null,
-                        children: [],
+                        children: ["frame"],
                         stl: {
-                            filename: "jflsdkjfsd.stl",
+                            filename: "assets/stls/stapler/substrate.stl",
                             offset: {x:0,y:0,z:0},
                             scale: 1.0,
                             rotation: {x:0,y:0,z:0}
-- 
GitLab