From 53d2a948538e69f4b11464976b637b8d05b5d2c0 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 27 Mar 2015 03:29:10 -0400
Subject: [PATCH] machine defaults

---
 js/cam/Assembler.js     | 21 +++++++--------------
 js/cam/Machine.js       | 17 +++++++++++++++++
 js/cam/MachineOneBit.js |  8 ++++++++
 3 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/js/cam/Assembler.js b/js/cam/Assembler.js
index 3f9e9f25..f7639768 100644
--- a/js/cam/Assembler.js
+++ b/js/cam/Assembler.js
@@ -39,8 +39,6 @@ Assembler = Backbone.Model.extend({
 
     initialize: function(options){
 
-        this.selectMachine();
-
         _.bindAll(this, "postProcess");
 
         //bind events
@@ -85,16 +83,10 @@ Assembler = Backbone.Model.extend({
         if (this.get("machine")) this.get("machine").destroy();
         if (machineName == "shopbot"){
             this.set("machine", new Shopbot());
-            this.set("camProcess", "shopbot");
         } else if (machineName == "handOfGod"){
             this.set("machine", new God());
-            this.set("camProcess", "gcode");
-            this.set("originPosition", {x:0,y:0,z:0});
-            this.set("stockPosition", {x:0,y:0,z:150});//todo calculate a good stock position
         } else if (machineName == "oneBitBot"){
             this.set("machine", new OneBitBot());
-            this.set("camProcess", "gcode");
-            this.set("stockFixed", true);
         } else console.warn("selected machine not recognized");
     },
 
@@ -113,17 +105,17 @@ Assembler = Backbone.Model.extend({
     },
 
     _updateCellType: function(){
-        this.get("machine").updateCellType();
+        if (this.get("machine")) this.get("machine").updateCellType();
         this.set("machineName", "handOfGod");//todo this should go away with dynamic allocation of this model
 
     },
 
     _updatePartType: function(){
-        this.get("machine").updatePartType();
+        if (this.get("machine")) this.get("machine").updatePartType();
     },
 
     _updateCellMode: function(){
-        this.get("machine").setVisibility(this.isVisible());
+        if (this.get("machine")) this.get("machine").setVisibility(this.isVisible());
         dmaGlobals.three.render();
     },
 
@@ -131,7 +123,7 @@ Assembler = Backbone.Model.extend({
         var scale = dmaGlobals.lattice.get("scale");
         this.get("origin").scale.set(scale/8, scale/8, scale/8);
         this.get("stock").scale.set(scale/8, scale/8, scale/8);
-        this.get("machine").setScale(scale);
+        if (this.get("machine")) this.get("machine").setScale(scale);
     },
 
     _tabChanged: function(){
@@ -143,7 +135,8 @@ Assembler = Backbone.Model.extend({
         var visible = this.isVisible();
         this.get("origin").visible = visible;
         this.get("stock").visible = visible;
-        this.get("machine").setVisibility(visible);
+        if (visible && !this.get("machine")) this.selectMachine();
+        if (this.get("machine")) this.get("machine").setVisibility(visible);
         dmaGlobals.three.render();
     },
 
@@ -168,7 +161,7 @@ Assembler = Backbone.Model.extend({
         this.get("origin").position.set(position.x, position.y, position.z);
         if (this.get("stockFixed")) this._updateStockPosToOrigin(position, this.previous("originPosition"));
         dmaGlobals.three.render();
-        if (this.get("machine").setMachinePosition) this.get("machine").setMachinePosition();
+        if (this.get("machine") && this.get("machine").setMachinePosition) this.get("machine").setMachinePosition();
     },
 
     _updateStockPosToOrigin: function(newOrigin, lastOrigin){
diff --git a/js/cam/Machine.js b/js/cam/Machine.js
index 1a6a9034..a652ce45 100644
--- a/js/cam/Machine.js
+++ b/js/cam/Machine.js
@@ -6,6 +6,7 @@
 function Machine() {
 
     this.hasStock = false;
+    this._setDefaults();
 
     this.meshes = {};
     this.material = new THREE.MeshLambertMaterial({color:0xaaaaaa, shading: THREE.FlatShading, transparent:true, opacity:1});
@@ -22,6 +23,12 @@ function Machine() {
     this.setVisibility(false);
 }
 
+Machine.prototype._setDefaults = function(){
+    dmaGlobals.assembler.set("camProcess", "gcode");
+    dmaGlobals.assembler.set("originPosition", {x:0,y:0,z:0});
+    dmaGlobals.assembler.set("stockPosition", {x:0,y:0,z:0});
+};
+
 Machine.prototype.setVisibility = function(visible){
     if (visible == null || visible === undefined) {
         if (dmaGlobals.assembler) visible = dmaGlobals.assembler.isVisible();
@@ -267,6 +274,11 @@ Shopbot.prototype._buildMeshes = function(callback){
     });
 };
 
+Shopbot.prototype._setDefaults = function(){
+    Machine.prototype._setDefaults.call(this);
+    dmaGlobals.assembler.set("camProcess", "shopbot");
+};
+
 Shopbot.prototype._moveAxis = function(startingPos, target, axis, speed, callback){
     if (target == null || target === undefined) {
         callback();
@@ -285,6 +297,11 @@ function God(){
 }
 God.prototype = Object.create(Machine.prototype);
 
+God.prototype._setDefaults = function(){
+    Machine.prototype._setDefaults.call(this);
+    dmaGlobals.assembler.set("stockPosition", {x:0,y:0,z:150});
+};
+
 God.prototype._buildMeshes = function(callback){
     callback({});
 };
diff --git a/js/cam/MachineOneBit.js b/js/cam/MachineOneBit.js
index 46ddb32f..98ff1f01 100644
--- a/js/cam/MachineOneBit.js
+++ b/js/cam/MachineOneBit.js
@@ -7,6 +7,14 @@ function OneBitBot(){
 }
 OneBitBot.prototype = Object.create(Machine.prototype);
 
+OneBitBot.prototype._setDefaults = function(){
+    Machine.prototype._setDefaults.call(this);
+    dmaGlobals.assembler.set("stockFixed", true);
+    var scale = dmaGlobals.lattice.get("scale");
+    dmaGlobals.assembler.set("stockPosition", {x:1.8*scale,y:0,z:1.1*scale});
+    dmaGlobals.assembler.set("rapidHeight", 2*scale);
+};
+
 OneBitBot.prototype.setMachinePosition = function(){
     if (!dmaGlobals.assembler) return;
     this.position = dmaGlobals.assembler.get("originPosition");
-- 
GitLab