Desktop app for designing parametric 3D models in CadQuery together with the coding agent of your choice
TypeScript 3d-modeling3d-printing3d-printing-toolsagentagentic-aiai-cadcadcadqueryclaude-codecodexelectronfemllmmacdmakeropencodeparametric-designpythonsteptext-to-cad
title: Clean modeling (CadQuery) name: Clean CadQuery modeling summary: Parameters at the top, one function per part, a fixed order of operations, robust selectors, avoiding typical errors, checking every part. order: 2 category: Way of working default: always when: you write or restructure CadQuery code topics: Parameters and structure; Order of operations; Selectors; Typical errors; Check every part
Skill: Clean CadQuery modeling
Parameters and structure
- Base dimensions are named constants at the top of
model.py, everything else is derived (INNER_W = WIDTH - 2 * WALL). Never type the same number twice. - The base dimensions the user would want to change themselves become
param()with a range in which the model works (WIDTH = param(40.0, "Width", min=20, max=80, unit="mm")), see AGENTS.md. Derived values never do. - One function per part (
def make_lid(): ... return lid), theadd_partcalls at the end ofmodel.py. Move parts intoparts/whenmodel.pygrows beyond about 200 lines. The constants then move toparts/params.py, imported bymodel.pyand the part modules. - Build each part around a sensible origin (mounting face on XY, symmetric parts centered), then place it with
.translate()and.rotate().
Order of operations
- Base solid:
box,extrude,revolve. - Additions and cuts: bosses, pockets, openings.
- Holes:
hole,cboreHole,cskHole, many at once withpushPoints. - Fillets and chamfers last, only on edges you selected precisely.
Selectors
- Select while the part is still simple and check the count:
assert body.edges("|Z").size() == 4. - On complex parts use
.tag("name")andworkplaneFromTagged("name")instead of long selector chains. - One specific edge or face:
cq.selectors.NearestToPointSelector((x, y, z)).
Typical errors
- Fillet fails (
StdFail_NotDone,command not done): radius too large for the adjacent faces. Use a smaller radius or fewer edges at once. - Cutting tools that end exactly on a face leave skins or fail: let them run through by at least 0.1 mm. Bodies that are joined overlap a little instead of touching.
- Many single cuts in a loop are slow: one tool with
pushPoints, or union the tools first. - No helical threads: model the hole (clearance hole or tap drill) and name the thread in a comment.
Check every part
s = part.val()
assert s.isValid() and len(part.solids().vals()) == 1
bb = s.BoundingBox() # compare bb.xlen, bb.ylen, bb.zlen with the intended size
Several solids where you expect one: the pieces do not overlap where they should be joined.