@steeringwaves/openscad-js
v0.0.5
Published
openscad code generator for javascript
Keywords
Readme
openscad-js
Javascript/typescript based code generator for openscad.
usage
import fs from "fs";
import Scad from "@steeringwaves/openscad-js";
const scad = new Scad.Module({
fs: fs // Only required when calling toScadFile()
});
const origin = scad.modules.translate([0, 0, 0]);
scad.specials.$fn = 200;
scad.specials.$vpr = [0, 0, 0];
const default_fn = scad.addVariable("default_fn", 200);
const sphere_diameter = scad.addVariable("sphere_diameter", 5, { section: "Options", comment: "Diameter of the sphere" });
const cube1 = scad.addVariable<Scad.IVector3>("cube1", [1, 2, 3], {
section: "Options",
comment: "Dimensions of first cube"
});
const cube2 = scad.addVariable<Scad.IVector3>("cube2", [5, 5, 5], {
section: "Options",
comment: "Dimensions of second cube"
});
const sphere = origin(scad.modules.sphere({ r: sphere_diameter, $fn: 200 }));
scad.add(sphere);
scad.add(scad.modules.translate([10, 0, 0])(sphere));
scad.add(scad.modules.translate([-10, 0, 0])(scad.modules.sphere({ r: 5, $fn: 8 })));
scad.add(scad.modules.translate([20, 0, 0])(scad.modules.cube(cube1)));
scad.add(scad.modules.translate([20, 10, 0])(scad.modules.cube(cube2)));
scad.add(scad.modules.translate([20, 40, 0])(scad.modules.resize(cube2)(scad.modules.cube(cube1))));
scad.add(scad.modules.translate([20, -10, 0])(scad.modules.cube(5)));
scad.add(scad.modules.translate([40, 0, 0])(scad.modules.cylinder({ r1: 3, r2: 5, h: 25, center: true })));
scad.add(scad.modules.translate([40, 20, 0])(scad.modules.cylinder({ r: 5, h: 25 })));
console.log(scad.toString());
scad.toScadFile(__filename);const Scad = require("@steeringwaves/openscad-js");
const scad = new Scad.Module({});
const origin = scad.modules.translate([0, 0, 0]);
scad.specials.$fn = 200;
scad.specials.$vpr = [0, 0, 0];
const sphere_diameter = scad.addVariable("sphere_diameter", 5, { section: "Options", comment: "Diameter of the sphere" });
const sphere = origin(scad.modules.sphere({ r: sphere_diameter }));
scad.add(sphere);
console.log(scad.toString());js2scad cli
This package ships a small cli that runs a model and (optionally) re-runs it whenever a source file changes. It boots
node with ts-node for typescript models, so there is nothing to configure beyond having ts-node and typescript
installed alongside your models.
installing it
# as a project dependency, gives you node_modules/.bin/js2scad
npm install --save-dev @steeringwaves/openscad-js
npx js2scad model.ts # or `yarn js2scad model.ts`, or plain `js2scad` inside a package.json script
# or globally, gives you a js2scad on your PATH anywhere
npm install -g @steeringwaves/openscad-js
js2scad model.tsA bare js2scad in your shell only works with the global install, a project install puts the command in
node_modules/.bin which npm/yarn add to the path for npx, yarn and package scripts. Without any install you would
need npx -p @steeringwaves/openscad-js js2scad model.ts, since npx js2scad alone looks for a package named
js2scad.
using it
# run a model once, it writes its own .scad (via scad.toScadFile(__filename))
js2scad jscad/models/example1/example1.ts
# same thing, but re-run on every save
js2scad --watch jscad/models/example1/example1.ts
# type checking is the slow part, skip it while iterating
js2scad --watch --transpile-only jscad/models/example1/example1.ts
# anything after -- is handed to the model itself as process.argv
js2scad model.ts -- --width 40usage:
js2scad [options] <file> [-- <model args>]
options:
-w, --watch re-run whenever the model or one of its local imports changes
-T, --transpile-only skip typescript type checking (much faster rebuilds)
-C, --cwd <dir> working directory used to run the model (default: current directory)
-c, --clear clear the screen before each re-run (watch mode only)
-q, --quiet only report failures
-h, --help show this help
-v, --version show the js2scad versionNotes:
- The model decides where its output lands,
js2scadnever writes the.scaditself. It only reports the file when it notices one was written next to the model. - Watch mode tracks the model's real dependency list (every non
node_modulesfile it loaded), so editing a shared helper triggers a rebuild too. - Status output goes to stderr, so
js2scad model.ts > model.scadstill works for models that print withconsole.log(scad.toString()). - Both
.jsand.tsmodels are supported,ts-nodeis only required for the typescript ones.
unsupported methods
All the known methods of writing this document are supported with types in this library under the modules object. However in order to future proof a bit we also support a special object named any which allows you to call any new/unsupported methods anyways.
// modules.color is a supported method and has type checking
scad.modules.color([255, 0, 0])(scad.modules.sphere({ r: 5 }));
// any.color is identical to the above except that it has no type checking
scad.any.color([255, 0, 0])(scad.modules.sphere({ r: 5 }));
// any.dosomethingcool allows this module to support newer versions or custom modifications
scad.any.dosomethingcool(100)(scad.modules.sphere({ r: 5 }));available functions under the modules object
union
difference
intersection
translate
mirror
rotate
color
scale
resize
multimatrix
offset
fill
hull
minkowski
sphere
cube
cylinder
polyhedron
linear_extrude
rotate_extrude
square
circle
polygon
text
projectionSpecial variables available under the specials object
$fa
$fs
$fn
$t
$vpr
$vpt
$vpd
$vpf
$children
$previewcustom variables
Customizable variables are support by calling the addVariable method which will return a special type that can be reference in your code. NOTE: This is a bit of a hack because if the user changes this value from within openscad our javascript has already executed and will be unable to react to changes that aren't coded in already.
const sphere_diameter = scad.addVariable("sphere_diameter", 5, { section: "Options", comment: "Diameter of the sphere" });
const sphere = origin(scad.modules.sphere({ r: sphere_diameter }));development
yarn install
yarn run format && yarn run ci