From 9dd6dc6808ad5cb36c51dc2ad9b25f0cb4a7f073 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 11 Jun 2015 18:25:34 -0700
Subject: [PATCH] load files working

---
 index.html                           |  4 +-
 js/cam/Machine.js                    |  2 +-
 js/cam/assemblers/Assembler.js       |  2 +-
 js/cells/CubeCell.js                 |  4 +-
 js/cells/DMACell.js                  | 11 +++---
 js/cells/GIKCell.js                  |  4 +-
 js/cells/KelvinCell.js               |  4 +-
 js/cells/OctaEdgeCell.js             |  4 +-
 js/cells/OctaFaceCell.js             |  4 +-
 js/cells/OctaRotEdgeCell.js          |  4 +-
 js/cells/OctaVertexCell.js           |  4 +-
 js/cells/TetraStackedCell.js         |  4 +-
 js/cells/TetraVertexCell.js          |  4 +-
 js/cells/TruncatedCubeCell.js        |  4 +-
 js/cells/supercells/DMASuperCell.js  |  4 +-
 js/cells/supercells/GIKSuperCell.js  |  4 +-
 js/lattice/CompositeEditorLattice.js | 14 +------
 js/lattice/CubeLattice.js            |  4 +-
 js/lattice/GIKLattice.js             |  4 +-
 js/lattice/KelvinLattice.js          |  4 +-
 js/lattice/Lattice.js                |  7 ++--
 js/lattice/LatticeBase.js            | 39 +++++++++++++++----
 js/lattice/OctaEdgeLattice.js        |  4 +-
 js/lattice/OctaFaceLattice.js        |  4 +-
 js/lattice/OctaRotEdgeLattice.js     |  4 +-
 js/lattice/OctaVertexLattice.js      |  4 +-
 js/lattice/TetraStackedLattice.js    |  4 +-
 js/lattice/TetraVertexLattice.js     |  4 +-
 js/lattice/TruncatedCubeLattice.js   |  4 +-
 js/models/FileSaver.js               | 57 +++++++++++++++-------------
 30 files changed, 121 insertions(+), 103 deletions(-)

diff --git a/index.html b/index.html
index 197c6d81..9187d7e3 100644
--- a/index.html
+++ b/index.html
@@ -87,7 +87,7 @@
             <p class="modal-title" >Enter File Name</p>
             </div>
             <div class="modal-body">
-                <input id="saveAsFileName" type="text" value="file" placeholder="Enter file name" class="form-control filename">
+                <input id="saveAsFileName" type="text" value="DM Assembly" placeholder="Enter file name" class="form-control filename">
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
@@ -105,7 +105,7 @@
             <p class="modal-title" >Save User Settings</p>
             </div>
             <div class="modal-body">
-                <input id="saveUserFileName" type="text" value="user" placeholder="Enter file name" class="form-control filename">
+                <input id="saveUserFileName" type="text" value="User" placeholder="Enter file name" class="form-control filename">
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
diff --git a/js/cam/Machine.js b/js/cam/Machine.js
index d5a098d6..09587679 100644
--- a/js/cam/Machine.js
+++ b/js/cam/Machine.js
@@ -70,7 +70,7 @@ Machine.prototype.setScale = function(scale){
 
 Machine.prototype._makeStockCell = function(){
     if (globals.lattice.makeSuperCell) return globals.lattice.makeSuperCell();
-    return globals.lattice.makeCellForLatticeType(null, globals.lattice.get("scale"));
+    return globals.lattice.makeCellForLatticeType({}, globals.lattice.get("scale"));
 };
 
 Machine.prototype.updateCellType = function(){
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index 232478be..46bfb07c 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -22,7 +22,7 @@ function Assembler(){
 
 Assembler.prototype._buildStock = function(){
     if (globals.lattice.makeSuperCell) return globals.lattice.makeSuperCell();
-    return globals.lattice.makeCellForLatticeType(null);
+    return globals.lattice.makeCellForLatticeType({});
 };
 
 Assembler.prototype._positionStockRelativeToEndEffector = function(){
diff --git a/js/cells/CubeCell.js b/js/cells/CubeCell.js
index ff808f38..81d38571 100644
--- a/js/cells/CubeCell.js
+++ b/js/cells/CubeCell.js
@@ -8,8 +8,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
     var unitCellGeo = new THREE.BoxGeometry(1,1,1);
 
-    function CubeCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function CubeCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     CubeCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 812b88cd..1cc4227f 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -8,12 +8,12 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
 
     var wireframeMaterial = new THREE.MeshBasicMaterial({color:0x000000, wireframe:true});
 
-    function DMACell(index, superCell, material, stopSetMode){
+    function DMACell(json, superCell){
 
-        if (index) this.index = new THREE.Vector3(index.x, index.y, index.z);
+        if (json.index) this.index = new THREE.Vector3(json.index.x, json.index.y, json.index.z);
         if (superCell) this.superCell = superCell;
 
-        this.material = material || lattice.get("materialType");
+        this.material = json.material || lattice.get("materialType");
 
         //object 3d is parent to all 3d elements owned by cell: cell mesh and wireframe, parts, beams, nodes, etc
         this.object3D = this._buildObject3D();
@@ -354,9 +354,10 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
 
     DMACell.prototype.toJSON = function(){
         var data = {
-            index:this.index//todo get rid of this and calculate from min and max
+            material: this.material
         };
-        if (this.parts) data.parts = this.parts;
+        if (globals.materials.compositeMaterials[this.material]) return data;//material definition in material composites
+        if (this.cells) data.cells = this.cells;
         return data;
     };
 
diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js
index 7a35dc59..f17442b0 100644
--- a/js/cells/GIKCell.js
+++ b/js/cells/GIKCell.js
@@ -6,8 +6,8 @@
 define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cubeCell'],
     function(_, THREE, three, lattice, appState, CubeCell){
 
-    function GIKCell(index, superCell){
-        CubeCell.call(this, index, superCell);
+    function GIKCell(json, superCell){
+        CubeCell.call(this, json, superCell);
     }
     GIKCell.prototype = Object.create(CubeCell.prototype);
 
diff --git a/js/cells/KelvinCell.js b/js/cells/KelvinCell.js
index 911043bf..e21d9ba8 100644
--- a/js/cells/KelvinCell.js
+++ b/js/cells/KelvinCell.js
@@ -97,8 +97,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'truncatedCu
     ];
     unitGeo.computeFaceNormals();
 
-    function KelvinCell(index, superCell){
-        TruncatedCubeCell.call(this, index, superCell);
+    function KelvinCell(json, superCell){
+        TruncatedCubeCell.call(this, json, superCell);
     }
     KelvinCell.prototype = Object.create(TruncatedCubeCell.prototype);
     
diff --git a/js/cells/OctaEdgeCell.js b/js/cells/OctaEdgeCell.js
index 689efe73..9cc1a98a 100644
--- a/js/cells/OctaEdgeCell.js
+++ b/js/cells/OctaEdgeCell.js
@@ -6,8 +6,8 @@
 define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'octaFaceCell'],
     function(_, THREE, three, lattice, appState, OctaFaceCell){
 
-    function OctaEdgeCell(index, superCell){
-        OctaFaceCell.call(this, index, superCell);
+    function OctaEdgeCell(json, superCell){
+        OctaFaceCell.call(this, json, superCell);
     }
     OctaEdgeCell.prototype = Object.create(OctaFaceCell.prototype);
 
diff --git a/js/cells/OctaFaceCell.js b/js/cells/OctaFaceCell.js
index b1b02166..2de0d9d7 100644
--- a/js/cells/OctaFaceCell.js
+++ b/js/cells/OctaFaceCell.js
@@ -10,8 +10,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     unitGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(-3*Math.PI/12));
     unitGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.asin(2/Math.sqrt(2)/Math.sqrt(3))));
 
-    function OctaFaceCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function OctaFaceCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     OctaFaceCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/OctaRotEdgeCell.js b/js/cells/OctaRotEdgeCell.js
index c41c0a56..378eecfa 100644
--- a/js/cells/OctaRotEdgeCell.js
+++ b/js/cells/OctaRotEdgeCell.js
@@ -9,8 +9,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     var unitGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2));
     unitGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI/4));
 
-    function OctaRotEdgeCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function OctaRotEdgeCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     OctaRotEdgeCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/OctaVertexCell.js b/js/cells/OctaVertexCell.js
index e60050e9..4390c904 100644
--- a/js/cells/OctaVertexCell.js
+++ b/js/cells/OctaVertexCell.js
@@ -8,8 +8,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
     var unitGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2));
 
-    function OctaVertexCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function OctaVertexCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     OctaVertexCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/TetraStackedCell.js b/js/cells/TetraStackedCell.js
index 29be42c5..2e477b29 100644
--- a/js/cells/TetraStackedCell.js
+++ b/js/cells/TetraStackedCell.js
@@ -12,8 +12,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     unitGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-2/Math.sqrt(6)));
     unitGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI));
 
-    function TetraStackedCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function TetraStackedCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     TetraStackedCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/TetraVertexCell.js b/js/cells/TetraVertexCell.js
index 8a9c392c..57bdda5e 100644
--- a/js/cells/TetraVertexCell.js
+++ b/js/cells/TetraVertexCell.js
@@ -12,8 +12,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     unitGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-2/Math.sqrt(6)));
     unitGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI));
 
-    function TetraStackedCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function TetraStackedCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     TetraStackedCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/TruncatedCubeCell.js b/js/cells/TruncatedCubeCell.js
index ee4addcf..ceac7658 100644
--- a/js/cells/TruncatedCubeCell.js
+++ b/js/cells/TruncatedCubeCell.js
@@ -50,8 +50,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     ];
     unitGeo.computeFaceNormals();
 
-    function TruncatedCubeCell(index, superCell){
-        DMACell.call(this, index, superCell);
+    function TruncatedCubeCell(json, superCell){
+        DMACell.call(this, json, superCell);
     }
     TruncatedCubeCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js
index 6c8b8cb4..fc4dba85 100644
--- a/js/cells/supercells/DMASuperCell.js
+++ b/js/cells/supercells/DMASuperCell.js
@@ -7,11 +7,11 @@
 define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     function(_, THREE, three, lattice, appState, DMACell){
 
-    function DMASuperCell(index, superCell){//supercells might have supercells
+    function DMASuperCell(json, superCell){//supercells might have supercells
 
         this.cells = true;//flag for now
 
-        DMACell.call(this, index, superCell);
+        DMACell.call(this, json, superCell);
 
         var material = this.getMaterial();
         var range = material.dimensions || appState.get("superCellRange");
diff --git a/js/cells/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js
index 1b7ca982..1b69218b 100644
--- a/js/cells/supercells/GIKSuperCell.js
+++ b/js/cells/supercells/GIKSuperCell.js
@@ -16,8 +16,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell',
         return geo;
     }
 
-    GIKSuperCell = function(index, superCell){
-        DMASuperCell.call(this, index, superCell);
+    GIKSuperCell = function(json, superCell){
+        DMASuperCell.call(this, json, superCell);
     };
     GIKSuperCell.prototype = Object.create(DMASuperCell.prototype);
 
diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js
index 247377af..344a416a 100644
--- a/js/lattice/CompositeEditorLattice.js
+++ b/js/lattice/CompositeEditorLattice.js
@@ -20,7 +20,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
         }),
 
         __initialize: function(options, callback){
-            if (!options.id || options.id == "") this.set("id", this.cid);
+            if (!options.id || options.id == "") this.set("id", "super" + this.cid);
             if (!options.color || options.color == "") this.set("color",  makeRandomColor(), {silent:true});
             if (callback) callback(this);
         },
@@ -29,18 +29,6 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             var self = this;
             require([subclass], function(subclassObject){
                 _.extend(self, subclassObject);
-
-//                //copy over cells to new lattice type
-//                var cells = self.cells;
-//                self._loopCells(cells, function(cell, x, y, z){
-//                    if (!cell) return;
-//                    var index = _.clone(cell.index);
-//                    if (cell.destroy) cell.destroy();
-//                    self.makeCellForLatticeType(index, function(newCell){
-//                        cells[x][y][z] = newCell;
-//                    });
-//                });
-//                three.render();
             });
         },
 
diff --git a/js/lattice/CubeLattice.js b/js/lattice/CubeLattice.js
index 492fddc8..cd9dc04f 100644
--- a/js/lattice/CubeLattice.js
+++ b/js/lattice/CubeLattice.js
@@ -38,9 +38,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 1+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['cubeCell'], function(CubeCell){
-                var cell = new CubeCell(indices);
+                var cell = new CubeCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js
index 25e65679..ef08736f 100644
--- a/js/lattice/GIKLattice.js
+++ b/js/lattice/GIKLattice.js
@@ -38,9 +38,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 1.28*(1+2*cellSeparation);
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['gikSuperCell'], function(GIKSuperCell){
-                var cell = new GIKSuperCell(indices);
+                var cell = new GIKSuperCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/KelvinLattice.js b/js/lattice/KelvinLattice.js
index 231de87a..f3b712df 100644
--- a/js/lattice/KelvinLattice.js
+++ b/js/lattice/KelvinLattice.js
@@ -39,9 +39,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 2*Math.sqrt(2)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['kelvinCell'], function(KelvinCell){
-                var cell = new KelvinCell(indices);
+                var cell = new KelvinCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js
index 0c4c5c24..fcd6e4cb 100644
--- a/js/lattice/Lattice.js
+++ b/js/lattice/Lattice.js
@@ -50,14 +50,12 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
 
         //lattice type
 
-        _updateLatticeType: function(loadingFromFile){//do not clear cells if loading from file (cells array contains important metadata)
+        _updateLatticeType: function(){//do not clear cells if loading from file (cells array contains important metadata)
 
             this._setToDefaultsSilently();
             this._setDefaultCellMode();
             this._loadMaterialClass();
 
-            if (loadingFromFile === undefined) loadingFromFile = false;
-            if (loadingFromFile) console.warn('loading from file');
             this.clearCells();
 
             if (this._undo) this._undo();
@@ -92,13 +90,14 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
                 _.extend(self, subclassObject);
                 self._initLatticeType();
 
+                //todo parse cells?
                 //copy over cells to new lattice type
                 var cells = self.cells;
                 self._loopCells(cells, function(cell, x, y, z){
                     if (!cell) return;
                     var index = _.clone(cell.index);
                     if (cell.destroy) cell.destroy();
-                    self.makeCellForLatticeType(index, function(newCell){
+                    self.makeCellForLatticeType({index:index}, function(newCell){
                         cells[x][y][z] = newCell;
                     });
                 });
diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js
index 8e7137d0..d2cf38c9 100644
--- a/js/lattice/LatticeBase.js
+++ b/js/lattice/LatticeBase.js
@@ -53,7 +53,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
                     for (var z=relativeMin.z;z<=relativeMax.z;z++){
                         if (!this.sparseCells[x][y][z]) {
                             var self = this;
-                             this.makeCellForLatticeType((new THREE.Vector3(x, y, z)).add(cellsMin), function(cell){
+                             this.makeCellForLatticeType({index: (new THREE.Vector3(x, y, z)).add(cellsMin)}, function(cell){
                                 self.sparseCells[x][y][z] = cell;
                                 self.set("numCells", self.get("numCells")+1);
                             });
@@ -64,16 +64,16 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             three.render();
         },
 
-        addCellAtIndex: function(indices, noRender, noCheck){//no render no check from fill
+        addCellAtIndex: function(index, noRender, noCheck, material){//no render no check from fill/load
 
-            if (!noCheck || noCheck === undefined) this.checkForMatrixExpansion(this.sparseCells, indices, indices);
+            if (!noCheck || noCheck === undefined) this.checkForMatrixExpansion(this.sparseCells, index, index);
 
-            var index = (new THREE.Vector3()).subVectors(indices, this.get("cellsMin") || indices);
-            if (!this.sparseCells[index.x][index.y][index.z]) {
+            var relIndex = (new THREE.Vector3()).subVectors(index, this.get("cellsMin") || index);
+            if (!this.sparseCells[relIndex.x][relIndex.y][relIndex.z]) {
                 var self = this;
                 if (!noRender || noRender === undefined) three.setRenderFlag();
-                this.makeCellForLatticeType(indices, function(cell){
-                    self.sparseCells[index.x][index.y][index.z] = cell;
+                this.makeCellForLatticeType({index:index, material:material}, function(cell){
+                    self.sparseCells[relIndex.x][relIndex.y][relIndex.z] = cell;
                     self.set("numCells", self.get("numCells")+1);
                 });
             } else console.warn("already a cell there");
@@ -336,6 +336,31 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
 
         //save/load
 
+        parseJSON: function(json){
+            var self = this;
+            _.each(_.keys(json), function(key){
+                if (key == "cellsMin" || key == "cellsMax" || key == "numCells") return;
+                self.set(key, json[key], {silent:true});
+            });
+            this.checkForMatrixExpansion(this.sparseCells, new THREE.Vector3(json.cellsMax.x, json.cellsMax.y, json.cellsMax.z),
+                new THREE.Vector3(json.cellsMin.x, json.cellsMin.y, json.cellsMin.z));
+            this.trigger("change");
+        },
+
+        parseCellsJSON: function(sparseCells){
+            var cellsMin = this.get("cellsMin");
+            this._loopCells(sparseCells, function(cell, x, y, z, self){
+                if (cell) self.addCellAtIndex((new THREE.Vector3(x, y, z)).add(cellsMin), true, true, cell.material);
+            });
+            three.render();//todo in callback
+        },
+
+        getSaveJSON: function(){
+            var data = this.toJSON();
+            data.sparseCells = this.sparseCells;
+            return data;
+        },
+
         toJSON: function(){//a minimal toJSON for ui stuff - no need to parse all cells
             return _.omit(this.attributes, ["nodes"]);//omit makes a copy
         }//todo something weird here
diff --git a/js/lattice/OctaEdgeLattice.js b/js/lattice/OctaEdgeLattice.js
index 6d95089e..8db77437 100644
--- a/js/lattice/OctaEdgeLattice.js
+++ b/js/lattice/OctaEdgeLattice.js
@@ -76,9 +76,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 2/Math.sqrt(6)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['octaEdgeCell'], function(OctaEdgeCell){
-                var cell = new OctaEdgeCell(indices);
+                var cell = new OctaEdgeCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/OctaFaceLattice.js b/js/lattice/OctaFaceLattice.js
index e4b4250f..a01225ee 100644
--- a/js/lattice/OctaFaceLattice.js
+++ b/js/lattice/OctaFaceLattice.js
@@ -45,9 +45,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 2/Math.sqrt(6)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['octaFaceCell'], function(OctaFaceCell){
-                var cell = new OctaFaceCell(indices);
+                var cell = new OctaFaceCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/OctaRotEdgeLattice.js b/js/lattice/OctaRotEdgeLattice.js
index b189274e..2d8b52bf 100644
--- a/js/lattice/OctaRotEdgeLattice.js
+++ b/js/lattice/OctaRotEdgeLattice.js
@@ -43,9 +43,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return Math.sqrt(2)/2 + 2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['octaRotEdgeCell'], function(OctaRotEdgeCell){
-                var cell = new OctaRotEdgeCell(indices);
+                var cell = new OctaRotEdgeCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/OctaVertexLattice.js b/js/lattice/OctaVertexLattice.js
index 0e2af753..ea9acd58 100644
--- a/js/lattice/OctaVertexLattice.js
+++ b/js/lattice/OctaVertexLattice.js
@@ -38,9 +38,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return Math.sqrt(2)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['octaVertexCell'], function(OctaVertexCell){
-                var cell = new OctaVertexCell(indices);
+                var cell = new OctaVertexCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/TetraStackedLattice.js b/js/lattice/TetraStackedLattice.js
index 5be5601b..ce40d530 100644
--- a/js/lattice/TetraStackedLattice.js
+++ b/js/lattice/TetraStackedLattice.js
@@ -45,9 +45,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 2/Math.sqrt(6)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['tetraStackedCell'], function(TetraStackedCell){
-                var cell = new TetraStackedCell(indices);
+                var cell = new TetraStackedCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/TetraVertexLattice.js b/js/lattice/TetraVertexLattice.js
index 47dd4fd0..337e434d 100644
--- a/js/lattice/TetraVertexLattice.js
+++ b/js/lattice/TetraVertexLattice.js
@@ -47,9 +47,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return 2/Math.sqrt(6)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['tetraVertexCell'], function(TetraVertexCell){
-                var cell = new TetraVertexCell(indices);
+                var cell = new TetraVertexCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/lattice/TruncatedCubeLattice.js b/js/lattice/TruncatedCubeLattice.js
index 8df8ebab..e5c34b67 100644
--- a/js/lattice/TruncatedCubeLattice.js
+++ b/js/lattice/TruncatedCubeLattice.js
@@ -39,9 +39,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return Math.sqrt(2)+2*cellSeparation;
         },
 
-        makeCellForLatticeType: function(indices, callback){
+        makeCellForLatticeType: function(json, callback){
             require(['truncatedCubeCell'], function(TruncatedCubeCell){
-                var cell = new TruncatedCubeCell(indices);
+                var cell = new TruncatedCubeCell(json);
                 if (callback) callback(cell);
             });
         },
diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js
index be441f13..dac9a312 100644
--- a/js/models/FileSaver.js
+++ b/js/models/FileSaver.js
@@ -3,26 +3,26 @@
  */
 
 
-define(['underscore', 'fileSaverLib'], function(_, saveAs){
+define(['underscore', 'fileSaverLib', 'lattice'], function(_, saveAs, lattice){
 
     function _saveFile(data, name, extension){
         var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
         saveAs(blob, name + extension);
     }
 
-    function save(name){
-        if (!name || name == "" || name == undefined) name = "file";
-        var data = JSON.stringify({
-            lattice:_getLatticeDataToSave(),
-            assembler:_getAssemblerDataToSave()
-        });
-        _saveFile(data, name, ".json");
-    }
+//    function save(name){
+//        if (!name || name == "" || name == undefined) name = "file";
+//        var data = JSON.stringify({
+//            lattice:_getLatticeDataToSave(),
+////            assembler:_getAssemblerDataToSave()
+//        });
+//        _saveFile(data, name, ".json");
+//    }
 
-    function saveLattice(name){
-        if (!name || name == "" || name == undefined) name = "lattice";
+    function save(name){
+        if (!name || name == "" || name == undefined) name = "DM Assembly";
         var data = JSON.stringify({
-            lattice:_getLatticeDataToSave()
+            assembly:_getLatticeDataToSave()
         });
         _saveFile(data, name, ".json");
     }
@@ -49,14 +49,18 @@ define(['underscore', 'fileSaverLib'], function(_, saveAs){
     }
 
     function _getLatticeDataToSave(){
-        return globals.lattice.attributes;
+        return lattice.getSaveJSON();
     }
 
-    function loadFile(data){//todo make this better
-        globals.lattice.getUItarget().clearCells();
-        _setData(data, false);
-        globals.lattice._updateLatticeType(true);
-        globals.lattice.trigger("change:scale");
+    function loadFile(data){//parsed json todo make this better - load composite
+        if (!data.assembly){
+            console.warn("no assembly in this file");
+            return;
+        }
+        lattice.clearCells();
+        var sparseCells = data.assembly.sparseCells;
+        lattice.parseJSON(_.omit(data.assembly, sparseCells), false);
+        if (sparseCells) lattice.parseCellsJSON(sparseCells);
     }
 
     function loadUser(data){
@@ -64,20 +68,21 @@ define(['underscore', 'fileSaverLib'], function(_, saveAs){
     }
 
     function _setData(data, silent){
-        _.each(_.keys(data.lattice), function(key){
-            globals.lattice.set(key, data.lattice[key], {silent:silent});
+        if (silent === undefined) silent = false;
+        _.each(_.keys(data.assembly), function(key){
+            lattice.set(key, data.assembly[key], {silent:silent});
         });
-        _.each(_.keys(data.assembler), function(key){
-            globals.cam.set(key, data.assembler[key]);
+        _.each(_.keys(data.cam), function(key){
+            cam.set(key, data.assembler[key]);
         });
     }
 
     return {//return public methods
+//        save: save,
         save: save,
-        saveLattice: saveLattice,
 //        saveAssembler: saveAssembler,
-        saveUser: saveUser,
-        loadFile: loadFile,
-        loadUser: loadUser
+//        saveUser: saveUser,
+        loadFile: loadFile
+//        loadUser: loadUser
     }
 });
\ No newline at end of file
-- 
GitLab