gleamscad
A Gleam library for programmatically generating OpenSCAD files.
Features
- Type-safe OpenSCAD generation: Write OpenSCAD code in Gleam with compile-time checks.
- Easy integration: Use in any Gleam project targeting Erlang and JavaScript.
Supported feature set
Not every feature of OpenSCAD is supported yet. For a comparison of all features of OpenSCAD see: https://openscad.org/cheatsheet/
For a list of currently available commands via this library see here:
Boolean operations
- union()
- difference()
- intersection()
2D
- circle(radius | d=diameter)
- square(size,center)
- square([width,height],center)
- polygon([points])
- polygon([points],[paths])
- text(text,size,font,direction,language,script, halign,valign,spacing)
- import(“….ext”, convexity)
- projection(cut)
3D
- sphere(radius | d=diameter)
- cube(size, center)
- cube([width,depth,height], center)
- cylinder(h,r|d,center)
- cylinder(h,r1|d1,r2|d2,center)
- polyhedron(points, faces, convexity)
- import(“….ext”, convexity)
- linear_extrude(height,center,convexity,twist,slices)
- rotate_extrude(angle,convexity)
- surface(file = “….ext”,center,convexity)
Transformations
- translate([x,y,z])
- rotate([x,y,z])
- rotate(a, [x,y,z])
- scale([x,y,z])
- resize([x,y,z],auto,convexity)
- mirror([x,y,z])
- multmatrix(m)
- color(“colorname”,alpha)
- color(“#hexvalue”)
- color([r,g,b,a])
- offset(r|delta,chamfer)
- hull()
- minkowski()
Special variables
- $fa (minimum angle)
- $fs (minimum size)
- $fn (number of segments)
- $t (animation step)
- $vpr (viewport rotation angles in degrees)
- $vpt (viewport translation)
- $vpd (viewport camera distance)
- $vpf (viewport camera field of view)
- $children (number of module children)
- $preview (true in F5 preview, false for F6)
Example
gleam add gleamscad
import gleamscad as cad
pub fn main() {
cad.translate(
[cad.rotate([cad.cube(12.34, cad.NotCentered)], 90.0, 0.0, 0.0)],
1.0,
2.0,
3.0,
)
|> cad.to_openscad
|> echo
}
Alternatively you can write it like this:
import gleamscad as cad
pub fn main() {
cad.cube(12.34, cad.NotCentered)
|> cad.rotate_single(90.0, 0.0, 0.0)
|> cad.translate_single(1.0, 2.0, 3.0)
|> cad.to_openscad
|> echo
}
This will result in (indentations added for clarity):
translate([1.0, 2.0, 3.0])
{
rotate([90.0, 0.000000000, 0.000000000])
{
cube(size=[12.34, 12.34, 12.34], center=false);
}
}
Example 2
Outputting OpenSCAD to a console is not particularly usefull. But hang on a minute! We can easily save the output to a file!
Lets add another library to our previous example:
gleam add simplifile
And extend the code a bit:
import gleamscad as cad
import simplifile
pub fn main() {
let filepath = "./test/hello_scad.scad"
let assert Ok(_) =
cad.translate(
[cad.rotate([cad.cube(12.34, cad.NotCentered)], 90.0, 0.0, 0.0)],
1.0,
2.0,
3.0,
)
|> cad.to_openscad
|> simplifile.write(to: filepath)
}
Now execute the following commands:
gleam build
gleam run
After that there should be a file named hello_scad.scad in the sudirectory ./test, that can be opened and viewed with OpenSCAD. Every time you change the code in gleam, build and run it, OpenSCAD will display the change.
Further documentation can be found at https://hexdocs.pm/gleamscad.
Development
gleam run # Run the project
gleam test # Run the tests