Using CLI to generate STLs based on SVGs?
See original GitHub issueI’m looking for a way to provide an SVG file as an input to a JSCAD script via the CLI so that I can generate an STL file based on it’s contents by simply extruding it’s paths. I’ve put together a small Node script that essentially looks for SVG files within a set of folders, then runs the openjscad
command via Node’s child_process.exec()
method.
What I’m hoping for is a way to pass SVG files into the openjscad script (like maybe with an -i
option?), but I haven’t found a way to do that in the User Guide yet. I see a package called svg-deserializer over in the io
repo, but I’m not sure how (or if) it can be used here.
Does anyone have any thoughts about how / if this can be done? Is there maybe another way I can try to approach this?
Node script (stl-creator.js
)
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const components = getDirectories(path.resolve('./components'));
// Traverse all folders inside ./components
components.forEach((component) => {
let fullPath = path.resolve('./components/' + component);
// For each component folder ...
fs.readdir(fullPath, (err, files) => {
// Compile list of all the .svg files here
let svgFiles = files.filter((file) => {
return path.extname(file).toLowerCase() === '.svg';
});
// For each .svg file ...
svgFiles.forEach((file) => {
let filename = file.split('.').slice(0, -1).join('.')
// Generate .stl file using OpenJSCAD
exec('openjscad stl-creator.jscad -o ' + fullPath + '/' + filename + '.stl', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
// console.log(`stdout: ${stdout}`);
// console.error(`stderr: ${stderr}`);
});
});
});
});
function getDirectories(dir) {
return fs.readdirSync(dir).filter((file) => {
return fs.statSync(dir + '/' + file).isDirectory();
});
}
stl-creator.jscad
function main() {
// Just generate a generic sphere for now
return sphere()
}
I then run the Node script using node stl-creator.js
, which produces STL files containing a sphere for each of the SVG files found. Now for the hard part: passing an SVG file (or filename) into the openjscad script and having it read it’s contents, extrude paths, and spit out an STL file.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
DXF and STL will land soon.
Thanks for the info! I can wait 😃 I see work is already underway with PR #463, so I’ll watch that for progress. This is a really cool project, and you guys are doing an outstanding job with it - keep it up!
Have any of the other deserializers been ported to V2 yet (like DXF or JSON)? Maybe I can use a different format in the meantime.