Selaa lähdekoodia

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 6 vuotta sitten
vanhempi
commit
945fc7f42d
4 muutettua tiedostoa jossa 61 lisäystä ja 2 poistoa
  1. 1 1
      .gitignore
  2. 2 1
      serializers/src/index.ts
  3. 1 0
      serializers/src/stl/index.ts
  4. 57 0
      serializers/src/stl/stlSerializer.ts

+ 1 - 1
.gitignore

@@ -196,4 +196,4 @@ gui/dist/
 /Viewer/tests/tsc
 /Viewer/tests/tsc.cmd
 /Viewer/tests/tsserver
-/Viewer/tests/tsserver.cmd
+/Viewer/tests/tsserver.cmd

+ 2 - 1
serializers/src/index.ts

@@ -1,2 +1,3 @@
 export * from "./OBJ";
-export * from "./glTF";
+export * from "./glTF";
+export * from "./stl";

+ 1 - 0
serializers/src/stl/index.ts

@@ -0,0 +1 @@
+export * from "./stlSerializer";

+ 57 - 0
serializers/src/stl/stlSerializer.ts

@@ -0,0 +1,57 @@
+import { Mesh } from "babylonjs/Meshes/mesh";
+import { VertexBuffer } from "babylonjs/Meshes/buffer";
+import { Vector3 } from "babylonjs/Maths/math";
+
+/**
+ * Class for generating STL data from a Babylon scene.
+ */
+export class STLExport {
+     /**
+     * Exports the geometry of a Mesh array in .STL file format (ASCII)
+     * @param mesh defines the mesh to serialize
+     * @param fileName Name of the file when downloaded.
+     * @param download triggers the automatic download of the file.
+     * @returns the ASCII STL format
+     */
+    public static ASCII(mesh: Mesh, download = false, fileName?: string): string {
+
+            let data = 'solid exportedMesh\r\n';
+            let vertices = mesh.getVerticesData(VertexBuffer.PositionKind) || [];
+            let indices = mesh.getIndices() || [];
+
+            for (let i = 0; i < indices.length; i += 3) {
+                let id = [indices[i] * 3, indices[i + 1] * 3, indices[i + 2] * 3];
+                let v = [
+                new Vector3(vertices[id[0]], vertices[id[0] + 1], vertices[id[0] + 2]),
+                new Vector3(vertices[id[1]], vertices[id[1] + 1], vertices[id[1] + 2]),
+                new Vector3(vertices[id[2]], vertices[id[2] + 1], vertices[id[2] + 2])
+                ];                
+                let p1p2 = v[0].subtract(v[1]);
+                let p3p2 = v[2].subtract(v[1]);
+                let n = (Vector3.Cross(p1p2, p3p2)).normalize();
+
+                data += 'facet normal ' + n.x + ' ' + n.y + ' ' + n.z + '\r\n';
+                data += '\touter loop\r\n';
+                data += '\t\tvertex ' + v[0].x + ' ' + v[0].y + ' ' + v[0].z + '\r\n';
+                data += '\t\tvertex ' + v[1].x + ' ' + v[1].y + ' ' + v[1].z + '\r\n';
+                data += '\t\tvertex ' + v[2].x + ' ' + v[2].y + ' ' + v[2].z + '\r\n';
+                data += '\tendloop\r\n';
+                data += 'endfacet\r\n';
+            }            
+            data += 'endsolid exportedMesh';
+
+            if (download) {
+                let a = document.createElement('a');
+                let blob = new Blob([data], {'type': 'application/octet-stream'});
+                a.href = window.URL.createObjectURL(blob);
+
+                if (!fileName) {
+                    fileName = "STL_Mesh";
+                }
+                a.download = fileName + ".stl";
+                a.click();
+            }
+
+            return data;
+    }
+}