Created
December 11, 2018 21:06
-
-
Save smj-edison/fd03ffb0a791aac5852ccc2bf6026313 to your computer and use it in GitHub Desktop.
A simple stl loader to convert to a ascii format
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
//load files and parse libraries | |
var fs = require('fs'); | |
var stl = require('stl'); | |
//load stl file | |
var facets = stl.toObject(fs.readFileSync('leafer-thingie.stl')); | |
//round points to minimize usage | |
function myRound(num) { | |
return Math.round(num * 1000) / 1000; | |
} | |
//join text together | |
//level 1, iterate over facets | |
facets = facets.facets.map(function(facet) { | |
//level 2, iterate over facet verticies | |
return facet.verts.map(function(vert) { | |
//3d point returned here (with rounding) | |
return `${myRound(vert[0])},${myRound(vert[1])},${myRound(vert[2])}`; | |
}).join("|"); //join with | symbol, seperating 3d verticies | |
}).join("@"); //seperate facet | |
var leaferJson = facets; | |
fs.writeFileSync('leafer.bin', leaferJson, 'ascii'); //write ascii | |
console.log(`wrote ${leaferJson.length} bytes`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment