gleamscad

A Gleam library for programmatically generating OpenSCAD files.

Features

Package Version Hex Docs

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

2D

3D

Transformations

Special variables

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
Search Document