CAD as Code
Recently, my dad had a nice little task for me. He upgraded his turntable with an external power supply and needed a cover for the cutout where the old power supply's on/off switch was located. I was excited to finally have an easy project to start designing my own parts, again. The last time I used CAD software was already a few years ago during university. The exercises weren't difficult for me so this project should be easy as well, right? Turns out that the commercial software I learned back then used different concepts than the FOSS alternative FreeCAD I planned to use now. The workflows I was used to simply didn't work anymore. I was reset to a complete beginner level and in need to rewire my brain to work with FreeCAD. This frustrated me because I already had the complete model in my mind. I just needed to transfer it into an .stl
file.
Clearly, I could've done it the boring way by sitting down and just learning to use FreeCAD. But I decided to explore an idea that came into my mind. In my master's thesis, I've created basically all of the figures using TikZ code. What if there was an equivalent for drawing 3D models? Actually, there are programs that do exactly what I was looking for! The seemingly most popular one, OpenSCAD, has packages available for most Linux distros, so I decided to give it a try.
The first steps looked promising but then I got stuck. How can I round the upper edges to make it look a bit softer? To achieve this, you need an operation called Minkowski sum which takes every combination of elements from two sets of position vectors, calculates their respective sum, and puts the results in a new set of position vectors. Here you can also find a more detailed explaination from one of my favorite YouTube channels, Reducible, if you want to know more about that topic.
However, remember that I originally looked for a simpler solution to my problem. That's why I also explored CadQuery, a Python library which provides native support for a larger set of operations and claims to have better performance while being easier to use1. I saw the documentation mentioning Jupyter so I quickly set up a Jupyter notebook inside a virtual environment and installed cadquery[ipython]
. I created the same cuboids, selected the edges I wanted to round using edges
and faces
, and applied fillet
. To remove some material, I hollowed the inner part using shell
and finally just glued both parts together with +
. Very intuitive!
import cadquery as cq
inner = (
cq.Workplane("XY")
.box(30, 22, 3)
.faces("|Z")
.shell(-1)
.translate((0, 0, -1.5))
)
outer = (
cq.Workplane("XY")
.box(32, 24, 2)
.edges("|Z")
.fillet(1)
.faces(">Z")
.fillet(0.5)
.translate((0, 0, 1))
)
result = inner + outer
result.export("Cover.stl")
During the construction, I could easily check the results after each step using the interactive 3D previews in Jupyter which worked out-of-the-box2. The people behind CadQuery also provide CQ-editor, an IDE with additional debugging features. This might come in handy when dealing with more complex projects.
I summary, CadQuery has a great user experience, especially when you're used to writing Python scripts. In addition to making hardware design more accessible for software devs, the CAD as code approach itself has several advantages compared to "classical" CAD software:
- the code can be managed in a VCS like Git with all its features
- tasks can be automated and make use of Python's large ecosystem of packages
- having the model's complete receipe instead of a ready meal allows you to vary every step
- comments allow you to take notes in the margin of your cookbook
For me, these are the killer arguments why I prefer keeping my data as text files wherever possible. There exist so many tools out there helping you deal with text. In contrast, there is significantly less choice when dealing with special, possibly proprietary, file formats like CAD models. That's why I'll definitely keep CadQuery in my repertoire for use in future projects.
Make sure to specify the ipython
extra when installing cadquery
!