From 1079a8f0129937e11d9ea8dcb26975ccc11b2912 Mon Sep 17 00:00:00 2001
From: amandaghassaei <amandaghassaei@gmail.com>
Date: Tue, 17 Mar 2015 14:56:55 -0400
Subject: [PATCH] parts destroyed when changing type

---
 js/fea/DmaCell.js    | 12 ++++++++----
 js/models/Lattice.js | 12 ++++++++++--
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 78881c70..a4862a97 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -55,6 +55,13 @@ DMACell.prototype.hide = function(){
     });
 };
 
+DMACell.prototype.destroyParts = function(){
+    _.each(this.parts, function(part){
+        if (part) part.destroy();
+    });
+    this.parts = null;
+};
+
 DMACell.prototype._buildCellMesh = function(material){//called from every subclass
     var unitCellGeo = this._getGeometry();
     if (!material) material = cellMaterials;
@@ -215,11 +222,8 @@ DMACell.prototype.destroy = function(){
 //            material.dispose();
         this.cellMesh = null;
     }
-    _.each(this.parts, function(part){
-        if (part) part.destroy();
-    });
+    this.destroyParts();
     this.indices = null;
-    this.parts = null;
     this.nodes = null;
     this.beams = null;
 };
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index 0684541a..f6a35389 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -38,7 +38,7 @@ Lattice = Backbone.Model.extend({
         //bind events
         this.listenTo(this, "change:scale", this._scaleDidChange);
         this.listenTo(options.appState, "change:cellMode", this._updateForMode);
-        this.listenTo(this, "change:partType", this._updateForMode);
+        this.listenTo(this, "change:partType", this._updatePartType);
         this.listenTo(this, "change:cellType change:connectionType", this._updateLatticeType);
         this.listenTo(this, "change:cellSeparation", this._updateCellSeparation);
     },
@@ -304,6 +304,13 @@ Lattice = Backbone.Model.extend({
     ////////////////////////////////////EVENTS//////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////////
 
+    _updatePartType: function(){
+        this._iterCells(this.get("cells"), function(cell){
+            if (cell) cell.destroyParts();
+        });
+        this._updateForMode();
+    },
+
     _updateForMode: function(){
         var cellMode = dmaGlobals.appState.get("cellMode");
         var partType =  this.get("partType");
@@ -390,7 +397,8 @@ Lattice = Backbone.Model.extend({
                 if (!cell) return;
 
                 var index = _.clone(cell.indices);
-                var parts = _.clone(cell.parts);
+                var  parts = null;
+                if (loadingFromFile) parts = _.clone(cell.parts);
                 if (cell.parentOrientation) var parentOrientation = new THREE.Quaternion(cell.parentOrientation._x, cell.parentOrientation._y, cell.parentOrientation._z, cell.parentOrientation._w);
                 if (cell.parentPosition) var parentPos = cell.parentPosition;
                 if (cell.direction) var direction = new THREE.Vector3(cell.direction.x, cell.direction.y, cell.direction.z);
-- 
GitLab