I love using OpenSCAD but I really miss the power of a full programming language. (I particularly miss data structures.) So, I’ve written this little TypeScript library that generates OpenSCAD files.
|
Note
|
This project is a work in progress. |
-
create a TypeScript file that creates a shape and renders it
// TODO: add imports from jsr
const randomThing = difference(
union(
cuboid(100, 50, 25).round({ edges: 'all', radius: 4 }).move(0, -100, 0),
cylinder(10, 50).chamfer({ radius: 4, edges: 'bottom' }).move(4, 4, 0),
),
cuboid(32, 16, 42).round({ radius: 4, edges: 'sides' }).move(20, 100, -1),
)
const variables = {
'$fn': 60,
chamfer_radius: 0.6,
}
randomThing.writeCADAndRender({ filename: 'my-awesome-model.scad', variables })-
run
denoin watch mode
$ deno run --allow-write --allow-run --watch my-awesome-model.ts-
--allow-writeis needed if rendering to .scad withwriteCAD -
--allow-runis needed in addition to--allow-writeif automatically rendering to stl withwriteCADAndRender-
make changes;
-
-
2d shapes: circle, square, polygon, import, projection
-
3d shapes: sphere, finish cube and cylinder, polyhedron, import, linear_extrude, rotate_extrude, surface
-
make examples use published version
-
TODOs all over the code
-
TESTS TESTS TESTS
-
maybe be able to run a temporary script that uses
fontmetricsortextmetricsto help calculate font size? -
fantasy: output directly to some other format like stl, 3mf, step, obj, or amf maybe
TODO: much more to explain here
-
cube
-
normally use
cuboid(for cuboid with sides of varying links) -
use
cubefor actual cube
-
-
cylinder
-
cylinderis reserved for a cylinder with ends of the same diameter -
use
frustumfor a frustum with different size circles on each end -
use
conefor a cone
-
-
null or undefined values in most things that take a list of shapes like union or difference are ignored TODO: describe why this is useful
-
some idiosyncrasies of OpenSCAD have been removed; some have not
-
some of my own idiosyncrasies have certainly found their way in 😂
Pros
-
full power of TypeScript
-
full power of TypeScript (main reason for doing this; counts as at least two pros 😁)
-
some idiosyncrasies of OpenSCAD have been fixed
-
can have STL files generated automatically on save
Cons
-
need to check for errors in two places (mitigated somewhat by extra error checking in TypeScript)
-
potentially slower but not noticeable to me
-
in OpenSCAD, you can more easily include logic inside modules; in TypeScript you have to do the logic up front or use an Immediately Invoked Function Expression TODO: add example of this
-
some idiosyncrasies of OpenSCAD have been fixed (might be confusing especially since there isn’t great documentation for this library yet)
-
has its own set of idiosyncrasies
-
when dealing with text, there is no way currently to call
fontmetricsortextmetrics
-
Plain ol' trusty OpenSCAD