Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Last active November 17, 2021 17:32
Show Gist options
  • Select an option

  • Save paulkaplan/ab11c0c267e771d0d670 to your computer and use it in GitHub Desktop.

Select an option

Save paulkaplan/ab11c0c267e771d0d670 to your computer and use it in GitHub Desktop.

Revisions

  1. paulkaplan renamed this gist Oct 26, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. paulkaplan created this gist Oct 26, 2014.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    // 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;
    }());