Skip to main content

Open CAD and code-driven design

📍 Where we are: Part III · The Software Stack — Chapter 7 of 16. Slicers turn a model into G-code; this chapter is about making the model itself with open tools, especially by writing it as code.

The simple version

A drawing is a picture of one object. A recipe is instructions that can make many versions of it: more flour, fewer eggs, same method. Code-driven CAD is the recipe approach. You write down how to build a bracket (a plate, four holes, rounded corners), and then change "60 mm long" to "80 mm long" and rebuild. Because the recipe is a plain text file, you can also keep every version of it, compare versions and share it, exactly as programmers do with software.

What this chapter covers​

We compare the main open-source CAD tools, split into those you drive with a mouse and those you drive with code. Then we build the same mounting plate three ways, with OpenSCAD, CadQuery and build123d, run each, and check the result numerically. We close with reproducible design under version control and a short, honest look at "generative design", meaning algorithms that propose shapes rather than only draw them.

The open tool map​

ToolStyleWhat the project says about itself
FreeCADParametric GUI modeller with a Python APIAn open-source parametric 3D modeller; geometry kernel is OpenCASCADE; LGPL-2.1 licence [1]
OpenSCADScript-based solid modelling"Not an interactive modeller", more of a 3D compiler that reads a script; CSG and extrusion of 2D outlines; GPL v2 [2]
CadQueryPython library, boundary-representation (B-rep) CADBuilt on the OpenCASCADE kernel; Apache-2.0; exports STEP, STL, AMF, 3MF and others [3]
build123dPython B-rep frameworkAlso on OpenCASCADE; Apache-2.0; borrows parts of CadQuery's code but is a separate, reworked framework [4]
libfiveImplicit (function-based) modellingShapes are mathematical functions; its meshing yields watertight, manifold triangle meshes; library under MPL 2, Studio GUI under GPL v2 or later [5]; the underlying idea, function representation of solids, was set out in a 1995 paper [6]
BlenderGeneral 3D suite, mesh-centredFree and open source, GPL v3 overall [7]

Two terms need unpacking. A B-rep (boundary representation) describes a solid by its exact bounding surfaces (planes, cylinders, splines), so a hole is a true cylinder. A mesh describes it with many small triangles, so a hole is a polygon with many sides. B-rep tools (FreeCAD, CadQuery, build123d) can export exact STEP files; mesh-centred tools such as Blender work directly on triangles rather than on dimensioned, parametric features. OpenSCAD sits in between: it works with CSG, and the export formats listed in its manual (STL, 3MF, AMF, OFF and others) are meshes or 2D drawings, with no STEP among them [8]. Export support differs from tool to tool, so check before you commit to one.

Six open CAD tools (FreeCAD, CadQuery, build123d, OpenSCAD, libfive, Blender) feeding a text source with parameters, which exports STEP, STL and 3MF files. Different open tools, one workflow: a parametric text source is rebuilt on demand and exported to files such as STEP, STL and 3MF. Original diagram by the authors, created with AI assistance.

I did not run FreeCAD or Blender for this chapter, so the table above is from the projects' own descriptions, not from a test.

One part, three ways​

The test part is a rectangular mounting plate, 60 by 30 by 4 mm, with four 5 mm holes inset 8 mm from the edges. Every dimension is a parameter.

OpenSCAD​

// Parametric mounting plate. Override on the command line with -D.
length = 60; // mm
width = 30; // mm
thickness = 4; // mm
hole_d = 5; // mm
hole_inset = 8; // mm
$fn = 48;

difference() {
cube([length, width, thickness]);
for (x = [hole_inset, length - hole_inset], y = [hole_inset, width - hole_inset])
translate([x, y, -1]) cylinder(d = hole_d, h = thickness + 2);
}

The command line renders without opening the GUI. -o sets the output file, and its extension selects the format; -D overrides a variable [8]. A parameter sweep is then a shell loop:

for L in 40 60 80; do openscad -o plate_$L.stl -D length=$L bracket.scad; done

Run status: executed with an OpenSCAD development snapshot (version 2026.10.11) installed on macOS. It reported a manifold top-level object and wrote ASCII STL (the CLI default, as the manual says). I computed the volume from each STL: about 4,487, 6,887 and 9,287 mm³ for lengths 40, 60 and 80, and the maximum x coordinate matched each length. In my test, the Homebrew openscad cask was disabled and I used openscad@snapshot; your install route may differ. Note that circles are polygons: $fn = 48 makes each hole a 48-sided prism, so the volume is a hair above the ideal-circle value.

OpenSCAD's slow step has been boolean operations. A 2023 pull request added the Manifold library as an experimental backend, with the author reporting speedups of roughly 5 to 30 times over the previous fast-CSG path; the same pull request notes that Manifold uses single-precision floats, unlike OpenSCAD's exact arithmetic [9]. Manifold's own README states its goal as guaranteed manifold output and lists OpenSCAD among its users [10].

CadQuery​

import cadquery as cq

# Parameters: change these and re-run; nothing else to edit.
length, width, thickness = 60.0, 30.0, 4.0 # mm
hole_d, hole_inset = 5.0, 8.0 # mm
fillet_r = 3.0 # mm

plate = (
cq.Workplane("XY")
.box(length, width, thickness)
.edges("|Z").fillet(fillet_r) # round the four vertical corners
.faces(">Z").workplane()
.rect(length - 2 * hole_inset, width - 2 * hole_inset, forConstruction=True)
.vertices().hole(hole_d) # one hole at each corner of the rectangle
)

cq.exporters.export(plate, "bracket.step")
cq.exporters.export(plate, "bracket.stl", tolerance=0.01, angularTolerance=0.1)
cq.exporters.export(plate, "bracket.3mf")

solid = plate.val()
expected = (length * width - (4 - 3.141592653589793) * fillet_r**2
- 4 * 3.141592653589793 * (hole_d / 2) ** 2) * thickness
print(f"volume {solid.Volume():.1f} mm^3, expected {expected:.1f}, valid={solid.isValid()}")
bb = solid.BoundingBox()
print(f"bbox {bb.xlen:.1f} x {bb.ylen:.1f} x {bb.zlen:.1f}")

CadQuery's documentation describes a fluent API in which each call selects or modifies geometry, much like a chain of queries [11]. Its README says pip install cadquery works for Python 3.9 to 3.12 when I read it, and recommends a virtual environment [3].

Run status: executed in a fresh virtual environment with Python 3.12 and CadQuery 2.8.0. Output: volume 6854.9 mm^3, expected 6854.9, valid=True and bbox 60.0 x 30.0 x 4.0. The exact solid matches the closed-form volume, which is the point of a B-rep: the fillets and holes are exact, not polygons.

build123d​

from build123d import *

length, width, thickness = 60.0, 30.0, 4.0
hole_d, hole_inset = 5.0, 8.0
fillet_r = 3.0

with BuildPart() as plate:
Box(length, width, thickness)
fillet(plate.edges().filter_by(Axis.Z), fillet_r)
with Locations((length / 2 - hole_inset, width / 2 - hole_inset),
(-(length / 2 - hole_inset), width / 2 - hole_inset),
(length / 2 - hole_inset, -(width / 2 - hole_inset)),
(-(length / 2 - hole_inset), -(width / 2 - hole_inset))):
Hole(hole_d / 2)

export_step(plate.part, "b123d.step")
export_stl(plate.part, "b123d.stl")
print(f"volume {plate.part.volume:.1f} mm^3, valid={plate.part.is_valid}")

This is build123d's builder mode, where context blocks such as BuildPart hold state like a design history; it also offers a stateless algebra mode using operators [4]. Run status: executed with build123d 0.12.0 in the same environment; it printed volume 6854.9 mm^3, valid=True, matching CadQuery.

Reproducible design under version control​

Code-driven CAD earns its keep in three ways. The source is text, so a git diff shows exactly which parameter or step changed, which a binary file cannot. The build is repeatable: the same script, the same tool version and the same parameters should give the same geometry, so pin the tool version and keep the generated STL out of the repository, or tag it with the version that made it. And a design can be tested: the CadQuery script above checks its own volume against a formula and its validity flag, a tiny version of a CI check that fails when a change breaks the part.

The trade-offs are real. Scripts have a learning curve; a person who thinks visually may prefer FreeCAD's sketches and constraints. CAD kernels differ between tools, and a script is only reproducible within the tool and version that ran it. For exchange with others, export STEP for downstream CAD and a mesh format for printing; the next chapters cover those formats.

Generative design​

"Generative design" covers algorithms that propose geometry from goals and constraints. The best-documented open example is topology optimisation: given loads, supports and a material budget, find where material should be. The method goes back to a 1988 homogenization-based formulation [12], and a later review compares the main families of approaches [13]. A well-known open teaching implementation is the "88 lines of code" MATLAB program, an extended and faster descendant of Sigmund's earlier 99-line code [14], published with a benchmark in which the authors report a 100-fold speed-up on a 7,500-element problem [15]. It is short enough to read in one sitting, and it shows the key lesson: the optimiser returns a density field, not a CAD model. Someone still has to turn it into smooth, printable geometry, check it, and often design for supports and post-processing. Research has since added manufacturing constraints to the optimisation itself, for example overhang constraints that aim to eliminate sacrificial support material through design [16] and a filter intended to give print-ready designs [17]; I did not test either.

Parametric sweeps like the shell loop above are the simpler, everyday relative: generate many variants from one recipe, evaluate each, keep the best. I did not test any open topology-optimisation tool for this chapter, and I do not recommend a specific one.

Why it matters​

When a design is code, it can be reviewed, tested, shared, parameterised for a customer, and rebuilt years later. That is the same property that made open-source software work, applied to physical parts.

In the real world​

Many open-hardware printers publish their parts as STEP or STL, and some projects publish OpenSCAD or Python sources so that others can adapt them. If you only need to tweak a dimension on a part someone else made, a parametric source is far easier to edit than a mesh.

Key terms​

  • Parametric design — a model defined by named parameters, so changing a value rebuilds the geometry.
  • CSG (constructive solid geometry) — building solids by combining primitives with union, difference and intersection.
  • B-rep (boundary representation) — describing a solid by its exact bounding surfaces rather than triangles.
  • Mesh — a surface approximated by many small triangles, as in STL.
  • Implicit (F-rep) model — a shape defined by a function whose value tells whether a point is inside it.
  • Geometry kernel — the library that does the underlying solid-modelling maths, such as OpenCASCADE.
  • Topology optimisation — an algorithm that decides where to place material for given loads and constraints.
  • Parameter sweep — generating many variants by running one script with different parameter values.

Where this leads​

A model leaves CAD as a file, and the printer receives it through software that manages jobs and machines. Next: Host software and print farms.