Skip to content

Commit

Permalink
Merge pull request #9160 from aWeirdo/patch-1
Browse files Browse the repository at this point in the history
Update stlSerializer.ts
  • Loading branch information
deltakosh committed Oct 15, 2020
2 parents 0536ed5 + 5c623a0 commit bdac65f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
1 change: 1 addition & 0 deletions dist/preview release/what's new.md
Expand Up @@ -160,6 +160,7 @@
- Added support for KHR_materials_unlit to glTF serializer ([Popov72](https://github.com/Popov72))
- Added support for glTF Skins to glTF serializer ([Drigax](https://github.com/Drigax))
- Added support for glTF Morph Target serialization ([Drigax](https://github.com/Drigax))
- Fixed several bugs in stlSerializer ([aWeirdo](https://github.com/aWeirdo))

### Navigation

Expand Down
26 changes: 11 additions & 15 deletions serializers/src/stl/stlSerializer.ts
Expand Up @@ -12,23 +12,23 @@ export class STLExport {
* @param download triggers the automatic download of the file.
* @param fileName changes the downloads fileName.
* @param binary changes the STL to a binary type.
* @param isLittleEndian toggle for binary type exporter.
* @param isLittleEndian toggle for binary type exporter.
* @returns the STL as UTF8 string
*/
public static CreateSTL(meshes: Mesh[], download: boolean= true, fileName: string= 'STL_Mesh', binary: boolean= false, isLittleEndian: boolean = true): any {
public static CreateSTL(meshes: Mesh[], download: boolean = true, fileName: string = 'stlmesh', binary: boolean = false, isLittleEndian: boolean = true): any {

//Binary support adapted from https://gist.github.com/paulkaplan/6d5f0ab2c7e8fdc68a61

let getFaceData = function(indices: any, vertices: any, i: number) {
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])
new Vector3(vertices[id[0]], vertices[id[0] + 2], vertices[id[0] + 1]),
new Vector3(vertices[id[1]], vertices[id[1] + 2], vertices[id[1] + 1]),
new Vector3(vertices[id[2]], vertices[id[2] + 2], vertices[id[2] + 1])
];
let p1p2 = v[0].subtract(v[1]);
let p3p2 = v[2].subtract(v[1]);
let n = (Vector3.Cross(p1p2, p3p2)).normalize();
let n = (Vector3.Cross(p3p2, p1p2)).normalize();

return {v, n};
};
Expand All @@ -53,7 +53,7 @@ export class STLExport {
for (let i = 0; i < meshes.length; i++) {
let mesh = meshes[i];
let indices = mesh.getIndices();
faceCount += indices ? indices.length : 0;
faceCount += indices ? indices.length / 3 : 0;
}

let bufferSize = 84 + (50 * faceCount);
Expand All @@ -64,8 +64,8 @@ export class STLExport {
data.setUint32(offset, faceCount, isLittleEndian);
offset += 4;

}else {
data = 'solid exportedMesh\r\n';
} else {
data = 'solid stlmesh\r\n';
}

for (let i = 0; i < meshes.length; i++) {
Expand All @@ -83,7 +83,7 @@ export class STLExport {
offset = writeVector(data, offset, fd.v[1], isLittleEndian);
offset = writeVector(data, offset, fd.v[2], isLittleEndian);
offset += 2;
}else {
} else {
data += 'facet normal ' + fd.n.x + ' ' + fd.n.y + ' ' + fd.n.z + '\r\n';
data += '\touter loop\r\n';
data += '\t\tvertex ' + fd.v[0].x + ' ' + fd.v[0].y + ' ' + fd.v[0].z + '\r\n';
Expand All @@ -97,17 +97,13 @@ export class STLExport {
}

if (!binary) {
data += 'endsolid exportedMesh';
data += 'endsolid stlmesh';
}

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();
}
Expand Down

0 comments on commit bdac65f

Please sign in to comment.