Last active
November 17, 2021 17:32
-
-
Save paulkaplan/ab11c0c267e771d0d670 to your computer and use it in GitHub Desktop.
ascii_stl_writer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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