From 1b805278e150b3a5a43d5685f10f58cd10c90e7e Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 23 Oct 2015 18:47:13 -0400
Subject: [PATCH] export buffer geo

---
 dependencies/loaders/binary_stl_writer.js | 30 +++++++++++++++++++----
 js/cells/DMACell.js                       | 25 +++++++++----------
 js/models/FileSaver.js                    |  1 +
 3 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/dependencies/loaders/binary_stl_writer.js b/dependencies/loaders/binary_stl_writer.js
index 047cf7ae..2d8a55eb 100755
--- a/dependencies/loaders/binary_stl_writer.js
+++ b/dependencies/loaders/binary_stl_writer.js
@@ -5,7 +5,6 @@ var geometryToSTLBin = function(geometryArray) {
 
     var writeVector = function(dataview, bufferIndex, vector, offset, orientation, isLittleEndian) {
         vector = vector.clone();
-        console.log(orientation);
         if (orientation) vector.applyQuaternion(orientation);
         if (offset) vector.add(offset);
         bufferIndex = writeFloat(dataview, bufferIndex, vector.x, isLittleEndian);
@@ -27,14 +26,35 @@ var geometryToSTLBin = function(geometryArray) {
         var geometry = geometryArray[index].geo;
         var orientation = geometryArray[index].orientation;
         var offset = geometryArray[index].offset;
-        var tris = geometry.faces;
-        var verts = geometry.vertices;
 
-        for(var n = 0; n < tris.length; n++) {
-            floatData.push([tris[n].normal, verts[tris[n].a], verts[tris[n].b], verts[tris[n].c], offset, orientation]);
+        if (geometry instanceof THREE.BufferGeometry){
+
+            var normals = geometry.attributes.normal.array;
+            var vertices = geometry.attributes.position.array;
+            for (var n=0;n<vertices.length;n+=9){
+                var normal = new THREE.Vector3(normals[n], normals[n+1], normals[n+2]);
+                var verta = new THREE.Vector3(vertices[n], vertices[n+1], vertices[n+2]);
+                var vertb = new THREE.Vector3(vertices[n+3], vertices[n+4], vertices[n+5]);
+                var vertc = new THREE.Vector3(vertices[n+6], vertices[n+7], vertices[n+8]);
+                floatData.push([normal, verta, vertb, vertc, offset, orientation]);
+            }
+
+        } else {
+
+            var tris = geometry.faces;
+            var verts = geometry.vertices;
+
+            for(var n = 0; n < tris.length; n++) {
+                floatData.push([tris[n].normal, verts[tris[n].a], verts[tris[n].b], verts[tris[n].c], offset, orientation]);
+            }
         }
     }
 
+    if (floatData.length == 0){
+        console.warn("no data to write to stl");
+        return null;
+    }
+
     //write to DataView
     var bufferSize = 84 + (50 * floatData.length);
     var buffer = new ArrayBuffer(bufferSize);
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 3d1619ce..b73ae8e2 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -350,26 +350,23 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', '
     };
 
     DMACell.prototype.getVisibleGeometry = function(){//for save stl
-        var meshes = this.getVisibleMeshes();
         var geometry = [];
-        var self = this;
-        _.each(meshes, function(mesh){
-            geometry.push({geo: mesh.geometry, offset:self.getAbsolutePosition(), orientation:self.getAbsoluteOrientation()});
-        });
-        return geometry;
-    };
-
-    DMACell.prototype.getVisibleMeshes = function(){
-        if (!this.object3D.visible) return [];
+        if (!this.object3D.visible) return geometry;
         var meshes = _.filter(this.object3D.children, function(child){
             return child.visible && child instanceof THREE.Mesh
         });
-        if (meshes.length > 0) return meshes;
-        if (!this.cells) return [];
+        if (meshes.length > 0) {
+            var self = this;
+            _.each(meshes, function(mesh){
+                geometry.push({geo: mesh.geometry, offset:self.getAbsolutePosition(), orientation:mesh.quaternion.clone().multiply(self.getAbsoluteOrientation())});
+            });
+            return geometry;
+        }
+        if (!this.cells) return geometry;
         this._loopCells(function(cell){
-            if (cell) meshes = meshes.concat(cell.getVisibleMeshes());
+            if (cell) geometry = geometry.concat(cell.getVisibleGeometry());
         });
-        return meshes;
+        return geometry;
     };
 
 
diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js
index 7fcdb152..497eb414 100644
--- a/js/models/FileSaver.js
+++ b/js/models/FileSaver.js
@@ -137,6 +137,7 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra
                 if (cell) geoArray = geoArray.concat(cell.getVisibleGeometry());
             });
             var stlBin = geometryToSTLBin(geoArray);
+            if (!stlBin) return;
             var blob = new Blob([stlBin], {type: 'application/octet-binary'});
             saveAs(blob, "STL.stl");
         });
-- 
GitLab