CM3D-008: Model code conventions: one package, frozen parameters, algebra style, interfaces
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.toolsholds the CLI and runners,cm3d.commonholds purchased parts, fits and print rules, andcm3d.pod.<module>andcm3d.station.<module>hold the models. The bill of materials is data too: each assembly has abom.pythat builds on the vitamins and the catalog incm3d.common, andcm3d bomwrites the docs pages from it. The package lives undersrc/, 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.pyand its docs page. -
Algebra style only: parts are built with
Box(…) - Cylinder(…),Pos(…) * shapeandextrude(sketch, h), never insidewith BuildPart(),BuildSketch()orBuildLine(). 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_initchecks the ranges and raises aValueErrorthat 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 incm3d.common.fits, and process constants and printability helpers are incm3d.common.printing. -
Part functions:
<part>(p) → Partreturns 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>.interfaceexposes 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
$fnequivalent in the model code.
Alternatives considered
-
Builder style (
with BuildPart()): Matches most online examples, but a helper that creates aBoxinside 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/andpod/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 Buildundersrc/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).