jscad / io

DEPRECATED : Input Output handling for JSCAD (see the link below)
https://github.com/jscad/OpenJSCAD.org/tree/V2/packages
34 stars 13 forks source link

Writing STL files using Node #87

Closed jasonwebb closed 5 years ago

jasonwebb commented 5 years ago

I'm looking into whether or not its possible to create and write STL files to the filesystem using Node only, without a browser. When looking at the usage example provided for in the @jscad/stl-seializer package, I see it uses Blob, which is not available in Node.

Can anyone provide some tips on how I can take my geometry data and turn it into an STL file on my filesystem?

I've tried using blob, node-blob (as in my example below), file-saver, and saveAs, but haven't gotten any of them to work. I have a feeling I may be approaching this wrong, so I'm partly asking here as a sanity check :) Any ideas?

const scadApi = require('@jscad/scad-api')
const stlSerializer = require('@jscad/stl-serializer')
const fs = require('fs')
const Blob = require('node-blob')

const {cube} = scadApi.primitives3d

const base = cube({size: 1, center: true})

const rawData = stlSerializer.serialize(base)

// Now that the geometry has been serialized as STL data, how can it be written to the filesystem?

// node-blob just writes the text "[object Blob]" to the file
const stlBlob = new Blob(rawData)
fs.writeFile('test.stl', stlBlob)

// This just writes the text "[object ArrayBuffer],[object ArrayBuffer],[object ArrayBuffer]" to the file
fs.writeFile('test.stl', rawData);
z3dev commented 5 years ago

@jasonwebb Thanks for the interest.

Please see the CLI (command line application) package as part of OpenJSCAD.org repository. I believe the README file is up to date. (packages/cli)

jasonwebb commented 5 years ago

Thanks! I looked at that earlier and didn't know if I could make it work for my application, but now I'm thinking I can use Node's child_process.exec() method to run the commands needed. I'm wanting to iterate over a set of SVG files spread across multiple directories, spitting out STLs files for each one by extruding their paths. Seems like I should be able to create a Node script that does the traversing and SVG parsing, then child_prrocess.exec() to do the STL creation 🤔 Thanks!

RedHatter commented 4 years ago

@jasonwebb It's unlikely you still need this but I though I would comment just in case.

The easiest way to write buffers in node is to use a stream.

const file = fs.createWriteStream('test.stl')
rawData.forEach(buf => file.write(Buffer.from(buf)))