Model a 3D-printable part as one parametric FreeCAD script (build.py) run headless by freecadcmd: named parameters, solids from primitives, booleans and lofts, checks written to out/report.json, STL and STEP out, a non-zero exit on any failure. Use when asked to model, design or CAD a part, write or fix a build.py, export STL or STEP, add a geometry check, or when a FreeCAD build silently passes.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Is that skill safe to install?
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
Are you the author of Freecad Build?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/alexnodeland-freecad-build)
---
name: freecad-build
description: "Model a 3D-printable part as one parametric FreeCAD script (build.py) run headless by freecadcmd: named parameters, solids from primitives, booleans and lofts, checks written to out/report.json, STL and STEP out, a non-zero exit on any failure. Use when asked to model, design or CAD a part, write or fix a build.py, export STL or STEP, add a geometry check, or when a FreeCAD build silently passes."
---
# FreeCAD build: one script, headless, checked
Every part is rebuilt from code by one `build.py`, run by `freecadcmd`. Nothing is drawn by hand, so changing a fit is
one number and a rerun. The build checks itself and fails loudly.
Start from `templates/build.py`: a holder with a pocket, a finger notch, a chamfered foot, a fit coupon and 12 checks.
It runs in about 2 s on FreeCAD 1.1.4. Copy it into the project folder (laid out by the `maker-project` skill) and
replace the example.
## Run it
```sh
PYTHONUNBUFFERED=1 /Applications/FreeCAD.app/Contents/Resources/bin/freecadcmd build.py; echo "exit $?"
```
- Exit 0: every check passed. 1: a check failed (see the FAIL lines). 2: an exception (the traceback is above).
- `PYTHONUNBUFFERED=1` keeps the PASS/FAIL lines in order with the STEP writer's noise, and gets output out before a
segfault.
- Pass options as environment variables (`ONLY=coupons freecadcmd build.py`), not arguments.
- The run writes `__pycache__/` beside the script (freecadcmd imports it) and `out/`. Gitignore both.
## What goes in build.py, top to bottom
1. A docstring: what it builds and the run command.
2. **Parameters.** Every dimension is a named UPPERCASE constant, grouped: the contents (each with where its number came
from: datasheet, calipers, photo-scaled, estimate), fit, print, bed. Derived values follow, computed, never typed.
3. Helpers: `box`, `fuse_all`, rounded outlines (`rrect`), extrudes and chamfer bands (ruled lofts), `print_pose`,
`mesh_of`.
4. One function per part, built in its **assembled** position. Dummies of the contents (the item, cards, pins) are
solids in the same frame, for the checks and the renders.
5. Coupons: small cuts of the real parts (`part.common(box(...))`) or rebuilt slices with the same parameters.
6. `check(name, ok, detail)`: appends to `REPORT` and prints PASS or FAIL.
7. `main()`: build, check, pose for printing, export, write `out/report.json` (checks, dims, every UPPERCASE
number), return 1 on any failure.
8. The ending: `try: rc = main()` / `except: traceback.print_exc(); rc = 2`, flush both streams, `os._exit(rc)`.
## Modeling
- **Solids from primitives:** `Part.makeBox`, `makeCylinder`, `makeCone`, `Part.Face(wire).extrude(...)`, and ruled
lofts (`Part.makeLoft([w0, w1], True, True)`) for chamfers, tapers and draft. Combine with `fuse`, `cut`, `common`.
- **`removeSplitter()` after every fuse** and cut. Without it, seam faces pile up (two boxes side by side fuse to 10
faces, 6 after it) and later booleans slow down.
- **Cutting tools overshoot** the faces they cut by about 1 mm. An exactly coplanar cut is fine, but derived numbers
can miss by 1e-9 and leave a paper-thin skin.
- **Symmetric features:** build one, then `shape.mirror(center, normal)` or rotate a copy. `shape.rotate` and
`translate` change the shape in place: `copy()` first, or use `translated()`.
- **Rounded outlines:** an inset outline's corner radius is `R - inset`. Assert it stays above 0.05.
- Keep `makeFillet` to simple solids, inside a `try`, and never in the path of a later boolean.
## Checks every build runs
Each is a few lines; `references/checks.md` has the code.
| Check | How |
|-------|-----|
| Valid, one solid | `s.isValid() and len(s.Solids) == 1` |
| Interference | `a.common(b).Volume < 1e-3` mm³ (guard `isNull()`) |
| Clearance | `a.distToShape(b)[0] >= gap` (0 means touching or overlapping) |
| Fit gap per side | sections with `shape.slice(V(0,0,1), z)`, compare wire bounding boxes |
| Lifts out, swings clear | translate or rotate in steps, interference at each step |
| Minimum wall | distance from the cavity tool to the outer skin faces |
| Fits the bed | posed bounding box ≤ 256 × 256 mm (Bambu Lab X2D) |
| Flat on the bed | exactly one face at z = 0, and its area |
| STL watertight, one body | `m.isSolid() and not m.hasNonManifolds() and m.countComponents() == n` |
When you fix a bug, add the check that would have caught it.
## Export
- **STEP:** `posed.exportStep(path)`, in print pose.
- **STL: `mesh_of(shape, path)`**, which is `MeshPart.meshFromShape(Shape=s, LinearDeflection=0.02,
AngularDeflection=0.15, Relative=False)`, then `m.write(path)`. It returns the mesh it wrote, so the watertight check
tests the file itself, and the chord error is one you chose. `shape.exportStl` returns nothing to check.
- **`print_pose(shape, flip, on_side)`:** rotate a copy to its print orientation (a cover roof-down, a clasp on its
side), then move its bounding-box minimum to the origin. Export only posed shapes; keep the assembled ones for checks.
- Write the render meshes (assembled parts, dummies, section cuts) into `out/ref/`.
- **Several pieces on one plate:** place them in bed coordinates, check they stay inside the bed and at least 5 mm apart,
and write one STL per piece (pcie-box's `out/test-plate/`), so the slicer keeps the layout.
## Traps
Each was hit in a real project. Evidence and more in `references/traps.md`.
- **freecadcmd exits 0 on a Python exception.** It prints one line with no traceback, runs the file a second time, and
exits 0. End with the try/except and `os._exit(rc)` after flushing. Check the exit code every run.
- **`if __name__ == "__main__":` never runs** under `freecadcmd build.py`: the module is named after the file.
- **The working directory is the caller's.** Build paths from `os.path.dirname(os.path.abspath(__file__))`.
- **A segfault prints nothing** when stdout is piped. Use `PYTHONUNBUFFERED=1`; add `faulthandler.enable()` when
hunting one.
- **Booleans can hang** on tangent solids (lynx: fusing tangent pipes). Overlap or separate them; for render-only
parts, use `Part.makeCompound` instead of a fuse.
- **`makeOffset2D` segfaults** on complex outlines such as glyphs (xiangqi). Check those by raster instead.
- **A STEP of hundreds of inlay solids** was about 300 MB and took over 30 minutes (xiangqi). Export per part.
## The `freecad` MCP server
`freecad` (freecad-mcp 0.1.27) drives a running FreeCAD GUI: `execute_code`, `create_object`, `get_objects`,
`get_view`. It needs FreeCAD open with its add-on's RPC server running, and it often fails to connect (the
`fabrication-doctor` skill checks it). Use it for quick
looks and interactive probing: load `out/*.step` with `Part.show(Part.read(path))`, then `get_view` for a screenshot.
Anything that must be reproducible goes in `build.py`. Never `exec` a build.py inside the GUI: its `os._exit` closes
FreeCAD.
## Where it fits
1. Numbers in: `measure-from-photos`, or `remix-a-model` from someone's 3MF or STL.
2. Shape it: `design-for-fdm`, `fits-and-tolerances`, `mechanisms`, `boxes-and-enclosures`, `gridfinity`.
3. Look at it: `render-and-review`.
4. Coupons first, then the parts: slice with the `bambu-cli-slicing` skill (and the `keep-project-settings` skill for
an exported 3MF), print with the `print-a-plate` skill.
5. Tune the named parameters from what the coupons say, and rerun.