CM3D-008: Model code conventions: one package, frozen parameters, algebra style, interfaces

Accepted · 2026-11-12 · @mathisloge

Context

With build123d (CM3D-007: build123d replaces OpenSCAD as design basis) the models are ordinary Python. Without rules, constants would be copied between files, helper functions would add geometry to an open builder by accident (the spike hit this), and modules would reach into each other’s internals. Several modules (pump, valve, pod enclosure, station) have to fit together in one machine.

Decision

  • One package cm3d: cm3d.tools holds the CLI and runners, cm3d.common holds purchased parts, fits and print rules, and cm3d.pod.<module> and cm3d.station.<module> hold the models. The bill of materials is data too: each assembly has a bom.py that builds on the vitamins and the catalog in cm3d.common, and cm3d bom writes the docs pages from it. The package lives under src/, so it does not clash with the folders at the repository root.

  • Units and frames: mm and degrees. Each module has one frame tied to a physical datum and states it in its interface.py and its docs page.

  • Algebra style only: parts are built with Box(…​) - Cylinder(…​), Pos(…​) * shape and extrude(sketch, h), never inside with BuildPart(), BuildSketch() or BuildLine(). Helpers take and return shapes, so they cannot leak into an open builder.

  • Parameters: one frozen dataclass per module with a module-level default instance. Variants come from dataclasses.replace. post_init checks the ranges and raises a ValueError that names the field. Derived values are properties or pure functions, never stored twice.

  • One source per number: purchased parts are frozen dataclasses with a standard or a "re-measure" note in cm3d.common.vitamins, calibrated printed fits are constants in cm3d.common.fits, and process constants and printability helpers are in cm3d.common.printing.

  • Part functions: <part>(p) → Part returns the part in module coordinates with a label, and <part>_print(p) returns it in print orientation with the bed at z = 0.

  • Module interface: cm3d.<asm>.<module>.interface exposes what other modules may rely on (mounting pattern, envelope, keep-outs, ports, a detailed model) as functions of the params. Other modules import only from there.

  • Geometry is exact: the tessellation is chosen at export, and there is no $fn equivalent in the model code.

Alternatives considered

  • Builder style (with BuildPart()): Matches most online examples, but a helper that creates a Box inside an open builder is added to the part silently.

  • Module-level constants only: Simple, but they cannot be overridden after import, which the variants of the old tests (-D PUMP_GAP=1.4) need.

  • Tools and models as separate packages, or mixed flat in one package: Tooling and models change at different rates. One package with two subpackages keeps the imports short without mixing them.

  • Top-level common/ and pod/ packages: Generic names that can clash with other libraries.

Consequences

  • Good: A number has one place, a part has one calling convention, and a module can be used by others without knowing its inside.

  • Good: The builder-context leak cannot happen, and a search for with Build under src/cm3d/ finds nothing.

  • Bad: Algebra style is less common in examples, so they have to be translated.

  • Bad: Explicit imports and params objects make the code longer than the SCAD (about 1.3 times for the lid geometry in the spike).