Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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