Post

Trying OpenSCAD, a CAD software with a very different kind of interface

If you love designing in CAD and love to code, this might be right up your alley!

Computer Aided Design, or CAD, is a type of software that allows users to model out 3D parts and assemblies. While most CAD programs are GUI-based, where you use your mouse to click & drag shapes and extrude (add depth to 2D drawings) from there, OpenSCAD requires the user to write code to generate models.

png How the application looks like

If you are familiar with tools like Onshape, this is a very different way of thinking about CAD (seriously, this is coming from a person who uses Onshape). From my experience using it so far, it feels like OpenSCAD is less about the “sketch (2D drawing) to 3D model” and rather allows the user to directly create parts using 3D specification.

Since you are essentially writing code to make these CAD models, all the coding tools/methodologies apply. You can use your preferred editor (meaning I get to keep my Vim keybindings), use Git for version control, and even potentially do code reviews too (if you are on a team)!

Note: to use your own code editor, just open the .scad file being read by OpenSCAD in the editor. Whenever you save the file, OpenSCAD re-renders the model.

png The official logo (source)

For my first CAD model, I wanted to make the OpenSCAD logo (see above). Here is the code I used below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$fn = 100;

sphere_size = 6;
cylinder_radius = sphere_size / 2;
cylinder_height = 15;

difference() {
  sphere(sphere_size);
  
  cylinder(cylinder_height, cylinder_radius, cylinder_radius, center = true);
  rotate([90, 0, 0]) {
    #cylinder(cylinder_height, cylinder_radius, cylinder_radius, center = true);
  }
  rotate([0, 90, 0]) {
    cylinder(cylinder_height, cylinder_radius, cylinder_radius, center = true);
  }
}

What does the code imply? Imagine you want to describe what the 3D model looks like. You’d probably say that the object is a sphere with 6 holes. If you pair opposing holes, you can describe the object as a sphere with 3 cylinders-worth of volume removed (one in each direction). That is what the code is describing - a sphere minus (denoted by difference()) three cylinders (one in each direction through rotate()). Note the use of variables to define certain lengths (which makes it easier to keep track of dimensions) - another plus of this software / paradigm.

And this is how it renders.

png The generated model from my code. The window on the left is the code opened in Vim. On the right is the render.

Looks pretty close!

Finally, I wanted to also point out that FreeCad just released its 1.0 version. This is a free, open-source, GUI-based CAD software that has an ability to integrate with OpenSCAD. Am planning to check that out too!

Excited to see what projects I can use OpenSCAD for!

This post is licensed under CC BY 4.0 by the author.