Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Last active November 17, 2021 17:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulkaplan/ab11c0c267e771d0d670 to your computer and use it in GitHub Desktop.
Save paulkaplan/ab11c0c267e771d0d670 to your computer and use it in GitHub Desktop.
ascii_stl_writer.js
// Written by Paul Kaplan
var AsciiStlWriter = (function() {
// ASCI STL files
function stringifyVector(vec){
return ""+vec.x+" "+vec.y+" "+vec.z;
}
function stringifyVertex(vec){
return "vertex "+stringifyVector(vec)+" \n";
}
function geometryToStlString(geom){
var vertices = geom.vertices;
var tris = geom.faces;
var stl = "solid pixel";
for(var i = 0; i<tris.length; i++){
stl += ("facet normal "+stringifyVector( tris[i].normal )+" \n");
stl += ("outer loop \n");
stl += stringifyVertex(vertices[tris[i].a]);
stl += stringifyVertex(vertices[tris[i].b]);
stl += stringifyVertex(vertices[tris[i].c]);
stl += ("endloop \n");
stl += ("endfacet \n");
}
stl += ("endsolid");
return stl
}
var save = function(geometry, filename) {
var stlString = geometryToStlString(geometry);
var blob = new Blob([stlString], {type: 'text/plain'});
saveAs(blob, filename);
}
that.save = save;
return that;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment