For a while now I’ve been tinkering with an Elixir Library that is effectively a wrapper for OpenSCAD. I found the |> operator to be an intuitive way of working with models, but at the end of the day it was mostly syntactic sugar over the OpenSCAD language.

The intent was that I could leverage features of Elixir outside of the scope of OpenSCAD, and I recently added the first feature like this: Slicing.

Given a 3D object, I wanted to be able to take horizontal slices that could be made into physical objects by a number of 2D methods, which could then be stacked in order, recreating the 3D object in that topographical map kind of way.

So enough with the talky talky (writey writey?), let’s get to some code and pictures!

I began with a relatively basic model. Create a cube, and then subtract a cylinder from it, which given the r1: 0 parameter, makes this a cone.

[
  cube(size: [100, 100, 100], center: true),
  cylinder(r1: 0, r2: 45, h: 50, _fn: 100)
]
|> difference()

That shape will render in OpenSCAD like this:

OpenSCAD Render

OpenSCAD Render

Now that we’ve got the shape, we’ll want to slice it up. We can do so by adding this function call to the pipeline.

|> slice(layer: 5, height: 50, name: "output_dir")

This function will start at z: 0 and take SVG slices at the layer interval, up until height, which for this model will be 50. Anything below z: 0 will be ignored.

After running this output_dir will have a numbered SVG for each layer, and a cooresponding scad file if you want to check it out in the OpenSCAD app.

Here’s the middle layer’s SVG file:

05.svg: circle gets the square!

05.svg: circle gets the square!

That’s all well and good, but we’re not in this to make SVGs, we’re here to make real things!

Cardboard Model

Cardboard Model

Cardboard Model

Cardboard Model

Cardboard Model

Cardboard Model