From 2d4ae617cda529614c5041753cce2dfa671adfca Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Sat, 7 Mar 2015 01:12:59 -0500 Subject: [PATCH] save and load json for regular lattices --- css/main.css | 6 ++++++ js/fea/DmaCell.js | 13 ++++++++++--- js/menus/Navbar.js | 29 ++++++++++++++++++++++++++++- js/menus/exportMenu.js | 21 --------------------- js/models/Lattice.js | 40 ++++++++++++++++++++++++++++++---------- main.html | 4 ++-- 6 files changed, 76 insertions(+), 37 deletions(-) delete mode 100644 js/menus/exportMenu.js diff --git a/css/main.css b/css/main.css index f96e2100..9d5e095b 100644 --- a/css/main.css +++ b/css/main.css @@ -128,6 +128,12 @@ nav .btn { background-color: transparent; } +#jsonInput { + width: 0; + height: 0; + overflow: hidden; +} + #menuContent{ padding: 30px; height:100%; diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js index d0061863..e69ff8be 100644 --- a/js/fea/DmaCell.js +++ b/js/fea/DmaCell.js @@ -121,9 +121,7 @@ DMACell.prototype.toJSON = function(){ if (!this.parts) this.parts = this._initParts(); return { indices:this.indices, - parts: this.parts, -// position: this.getPosition(), -// orientation: this.getOrientation() + parts: this.parts }; }; @@ -233,6 +231,15 @@ DMACell.prototype.toJSON = function(){ return position; }; + DMAFreeFormOctaCell.prototype.toJSON = function(){ + var json = DMACell.prototype.toJSON.call(this); + _.extend(json, { + position: this.getPosition(), + orientation: this.getOrientation() + }); + return json; + } + self.DMAFreeFormOctaCell = DMAFreeFormOctaCell; })(); diff --git a/js/menus/Navbar.js b/js/menus/Navbar.js index e9b8103d..aabdd116 100644 --- a/js/menus/Navbar.js +++ b/js/menus/Navbar.js @@ -13,7 +13,9 @@ NavBar = Backbone.View.extend({ events: { "click #showHideMenu": "_setMenuVis", "click .menuHoverControls": "_setNavSelection", - "click #saveAs": "_saveAs" + "click #saveAs": "_saveAs", + "click #importJSON": "_importJSON", + "change #jsonInput": "_selectJSONFiles", }, initialize: function(){ @@ -63,6 +65,31 @@ NavBar = Backbone.View.extend({ }); }, + _importJSON: function(e){ + e.preventDefault(); + $("#jsonInput").click(); + }, + + _selectJSONFiles: function(e){ + e.preventDefault(); + var input = $(e.target), + numFiles = input.get(0).files ? input.get(0).files.length : 1, + label = input.val().replace(/\\/g, '/').replace(/.*\//, ''); + this._readDataURL(numFiles, label, input.get(0).files); + input.val(""); + }, + + _readDataURL: function(numFiles, filename, files){ + if (numFiles>1) console.warn("too many files selected"); + var reader = new FileReader(); + reader.readAsText(files[0]); + reader.onload = (function() { + return function(e) { + dmaGlobals.lattice.loadFromJSON(e.target.result); + } + })(); + }, + _saveAs: function(e){ e.preventDefault(); dmaGlobals.lattice.saveAsJSON(); diff --git a/js/menus/exportMenu.js b/js/menus/exportMenu.js deleted file mode 100644 index b538dbf3..00000000 --- a/js/menus/exportMenu.js +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Created by aghassaei on 1/9/15. - */ - - -$(function(){ - - $("#exportSTL").click(function(e){ - e.preventDefault(); - - _.each(three.scene.children, function(object){ - if (object instanceof THREE.Mesh){ - console.log(object.geometry); - stlFromGeometry(object.geometry, {download:true}); - } - }); - - - }) - -}); \ No newline at end of file diff --git a/js/models/Lattice.js b/js/models/Lattice.js index fc13658a..5e660e52 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -161,7 +161,7 @@ Lattice = Backbone.Model.extend({ _clearInverseCells: function(){ this._iterCells(this.get("inverseCells"), function(cell){ - if (cell) cell.destroy(); + if (cell && cell.destroy) cell.destroy(); }); this.set("inverseCells", [[[null]]]); this.set("inverseCellsMin", {x:0, y:0, z:0}); @@ -368,10 +368,12 @@ Lattice = Backbone.Model.extend({ ///////////////////////////////CONNECTION TYPE////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// - _updateLatticeType: function(){ + _updateLatticeType: function(arg1, arg2, arg3, loadingFromFile){//do not clear cells if loading from file (cells array contains important metadata) + if (typeof loadingFromFile == "undefined") loadingFromFile = false; +// console.log(this.previous("connectionType")); var cellType = this.get("cellType"); var connectionType = this.get("connectionType"); - if (!this.get("shouldPreserveCells") || this.previous("connectionType") == "freeformFace") this.clearCells(); + if (!loadingFromFile && (!this.get("shouldPreserveCells") || this.previous("connectionType") == "freeformFace")) this.clearCells(); if (this._undo) this._undo(); if (this.get("basePlane")) this.get("basePlane").destroy(); if (this.get("highlighter")) this.get("highlighter").destroy(); @@ -379,7 +381,7 @@ Lattice = Backbone.Model.extend({ if (connectionType == "face"){ _.extend(this, this.OctaFaceLattice); } else if (connectionType == "freeformFace"){ - this.clearCells(); + if (!loadingFromFile) this.clearCells(); _.extend(this, this.OctaFreeFormFaceLattice); } else if (connectionType == "edge"){ _.extend(this, this.OctaFaceLattice); @@ -394,16 +396,27 @@ Lattice = Backbone.Model.extend({ } this._initLatticeType(); - if (this.get("shouldPreserveCells")){ + if (loadingFromFile || this.get("shouldPreserveCells")){ var self = this; var scale = this.get("scale"); var cells = this.get("cells"); this._clearInverseCells(); this._loopCells(cells, function(cell, x, y, z){ if (!cell) return; - var index = cell.indices; - cell.destroy(); - cells[x][y][z] = self._makeCellForLatticeType(index, scale) + var index = _.clone(cell.indices); + var parts = _.clone(cell.parts); +// if (cell.orientation) orientation + if (cell.destroy) cell.destroy(); + var newCell = self._makeCellForLatticeType(index, scale); + if (parts) { + //todo make this better + newCell.parts = newCell._initParts(); + for (var i=0;i<newCell.parts.length;i++){ + if (!parts[i]) newCell.parts[i].destroy(); + } + } + cells[x][y][z] = newCell; + }); dmaGlobals.three.render(); } @@ -440,8 +453,15 @@ Lattice = Backbone.Model.extend({ saveAs(blob, "lattice.json"); }, - loadFromJSON: function(){ - + loadFromJSON: function(data){ + this.clearCells(); + var data = JSON.parse(data); + var self = this; + _.each(_.keys(data), function(key){ + self.set(key, data[key], {silent:true}); + }); + this.set("shouldPreserveCells", true, {silent:true}); + this._updateLatticeType(null, null, null, true); }, //////////////////////////////////////////////////////////////////////////////////////// diff --git a/main.html b/main.html index e5f001ae..90543f9e 100644 --- a/main.html +++ b/main.html @@ -67,7 +67,6 @@ <script src="js/menus/PartMenuView.js"></script> <script src="js/menus/SketchMenuView.js"></script> <script src="js/menus/ScriptMenuView.js"></script> - <script src="js/menus/exportMenu.js"></script> <script src="js/menus/PhysicsMenuView.js"></script> <script src="js/menus/MaterialMenuView.js"></script> <script src="js/menus/OptimizationMenuView.js"></script> @@ -96,7 +95,7 @@ <a href="#" class="dropdown-toggle" data-toggle="dropdown">File <b class="caret"></b></a> <span class="dropdown-arrow"></span> <ul class="dropdown-menu"> - <li><a id="exportSTL" href="#">Import Lattice</a></li> + <li><a id="importJSON" href="#">Import Lattice JSON</a></li> <li><a id="saveAs" href="#">Save Lattice JSON</a></li> <li><a href="#">Something else here</a></li> <li class="divider"></li> @@ -110,6 +109,7 @@ </ul> </div><!-- /.navbar-collapse --> + <input id="jsonInput" type="file"> </nav> -- GitLab