Skip to content

Instantly share code, notes, and snippets.

@HustLion
Forked from paulkaplan/STLFileSaver.js
Created May 9, 2016 13:45
Show Gist options
  • Select an option

  • Save HustLion/f16ec119663d65a2a7755b133e3d8137 to your computer and use it in GitHub Desktop.

Select an option

Save HustLion/f16ec119663d65a2a7755b133e3d8137 to your computer and use it in GitHub Desktop.

Revisions

  1. @paulkaplan paulkaplan revised this gist Sep 14, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions STLFileSaver.js
    Original file line number Diff line number Diff line change
    @@ -20,13 +20,13 @@ function generateSTL(geometry){
    var vertices = geometry.vertices;
    var tris = geometry.faces;

    var stl = "solid pixel";
    var stl = "solid pixel\n";
    for(var i = 0; i<tris.length; i++){
    stl += ("facet normal "+stringifyVertex( tris[i].normal )+" \n");
    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 += stringifyVertex( vertices[ tris[i].a ];
    stl += stringifyVertex( vertices[ tris[i].b ];
    stl += stringifyVertex( vertices[ tris[i].c ];
    stl += ("endloop \n");
    stl += ("endfacet \n");
    }
  2. @paulkaplan paulkaplan revised this gist Sep 14, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions STLFileSaver.js
    Original file line number Diff line number Diff line change
    @@ -22,11 +22,11 @@ function generateSTL(geometry){

    var stl = "solid pixel";
    for(var i = 0; i<tris.length; i++){
    stl += ("facet normal "+stringifyVector( tris[i].normal )+" \n");
    stl += ("facet normal "+stringifyVertex( 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 += stringifyVertex( vertices[ tris[i].a ]);
    stl += stringifyVertex( vertices[ tris[i].b ]);
    stl += stringifyVertex( vertices[ tris[i].c ]);
    stl += ("endloop \n");
    stl += ("endfacet \n");
    }
  3. @paulkaplan paulkaplan created this gist Sep 10, 2013.
    46 changes: 46 additions & 0 deletions STLFileSaver.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    /*
    Paul Kaplan, @ifitdidntwork
    Create an ASCII STL file from a THREE.js mesh
    that can be saved save from browser and 3D printed
    --------------------------------------------------
    See further explanation here:
    http://buildaweso.me/project/2013/2/25/converting-threejs-objects-to-stl-files
    --------------------------------------------------
    Saving the file out of the browser is done using FileSaver.js
    find that here: https://github.com/eligrey/FileSaver.js
    */

    function stringifyVertex(vec){
    return "vertex "+vec.x+" "+vec.y+" "+vec.z+" \n";
    }

    // Given a THREE.Geometry, create an STL string
    function generateSTL(geometry){
    var vertices = geometry.vertices;
    var tris = geometry.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
    }

    // Use FileSaver.js 'saveAs' function to save the string
    function saveSTL( geometry, name ){
    var stlString = generateSTL( geometry );

    var blob = new Blob([stlString], {type: 'text/plain'});

    saveAs(blob, name + '.stl');

    }