Skip to content

Instantly share code, notes, and snippets.

@smj-edison
Created December 11, 2018 21:06
Show Gist options
  • Save smj-edison/fd03ffb0a791aac5852ccc2bf6026313 to your computer and use it in GitHub Desktop.
Save smj-edison/fd03ffb0a791aac5852ccc2bf6026313 to your computer and use it in GitHub Desktop.
A simple stl loader to convert to a ascii format
//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