Skip to main content

README.md

# 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.


[![Package Version](https://img.shields.io/hexpm/v/gleamscad)](https://hex.pm/packages/gleamscad)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gleamscad/)

### Supported feature set

Not every feature of OpenSCAD is supported yet. For a comparison of all features of OpenSCAD see: [https://openscad.org/cheatsheet/](https://openscad.org/cheatsheet/)

For a list of currently available commands via this library see here:

#### Boolean operations
- [x] union()
- [x] difference()
- [x] intersection()

#### 2D
- [x] circle(radius | d=diameter)
- [ ] square(size,center)
- [x] square([width,height],center)
- [x] polygon([points])
- [ ] polygon([points],[paths])
- [ ] text(text,size,font,direction,language,script, halign,valign,spacing)
- [ ] import("….ext", convexity)
- [ ] projection(cut)

#### 3D
- [x] sphere(radius | d=diameter)
- [x] cube(size, center)
- [x] cube([width,depth,height], center)
- [x] cylinder(h,r|d,center)
- [x] cylinder(h,r1|d1,r2|d2,center)
- [ ] polyhedron(points, faces, convexity)
- [ ] import("….ext", convexity)
- [x] linear_extrude(height,center,convexity,twist,slices)
- [ ] rotate_extrude(angle,convexity)
- [ ] surface(file = "….ext",center,convexity)

#### Transformations
- [x] translate([x,y,z])
- [x] rotate([x,y,z])
- [ ] rotate(a, [x,y,z])
- [x] 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)
- [x] hull()
- [x] minkowski()

#### Special variables
- [x] $fa (minimum angle)
- [x] $fs (minimum size)
- [x] $fn (number of segments)
- [x] $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

```sh
gleam add gleamscad
```

```gleam
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:
```gleam
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):
```openscad
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:
```sh
gleam add simplifile
```

And extend the code a bit:
```gleam
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:
```sh
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

```sh
gleam run   # Run the project
gleam test  # Run the tests
```