4. Fasten the lid
Add four screws that pass through the lid into heat-set inserts in the box.
The hole pattern, hardware and assembly relationship will come from one
InsertMount definition.
This step changes the box body, adds a shared mount recipe and connects the parts. The excerpts below show each change. A complete file follows them so you can check your edits or restart here.
Make room for the inserts
Add these two constants after LID_THICKNESS:
The 2.4 mm walls are too thin for the example's 4 mm insert pockets. Inside
box_body(), replace return outside.cut(cavity) with:
shell = outside.cut(cavity)
# Four pads join the walls; each will receive a blind insert pocket.
pads = (cq.Workplane("XY").pushPoints(MOUNT_POINTS)
.circle(4.5).extrude(-8))
return shell.union(pads)
This adds four 9 mm diameter pads at (±34, ±19), extending 8 mm down from
the rim. Each pad overlaps the adjacent walls, making one connected solid.
The lid body stays unchanged.
Define one shared mount
After the body builders, add the imports and a small insert builder:
from cadkit import FastenerSpec
import cadkit as ck
def insert_envelope(spec):
"""A dimensioned placeholder, not a supplier-specific insert model."""
model = (cq.Workplane("XY").circle(2.35).circle(1.5)
.extrude(spec.length_mm).val())
model.thread_diameter = 3
model.thread_pitch = 0.5
return model
Then define the shared mounting recipe:
MOUNT = ck.InsertMount(
pattern=ck.PointPattern(MOUNT_POINTS),
screw=FastenerSpec("socket_head_cap_screw", "M3-0.5", length_mm=SCREW_LENGTH),
insert=FastenerSpec(
"heat_set_insert", "M3-0.5", length_mm=5.7, standard="tutorial-envelope",
factory=insert_envelope, representation="envelope",
description="Tutorial insert envelope; select and calibrate a real insert before printing.",
),
clearance_diameter=3.4,
pocket=ck.InsertPocket(diameter=4, depth=6.7, insert_outer_diameter=4.7),
minimum_engagement=3,
)
MOUNT contains the four sites, an M3 × 8 mm screw, the insert definition,
a 3.4 mm clearance diameter and a 6.7 mm deep blind pocket.
The insert builder makes a simple annulus and labels it representation="envelope".
Its outer diameter is 4.7 mm and its length is 5.7 mm. This lets us explore the
assembly without claiming that we have selected a manufacturer's insert.
The 4 mm pocket is an explicit modelling input; a real pocket must be chosen
for the purchased insert, material and printing process.
Give each part its role
Replace the previous BOX and LID definitions with:
BOX = ck.Part(
"box", box_body, manufacture=ck.FDM("PETG"),
features={"lid-mount": MOUNT.insert_side()},
)
LID = ck.Part(
"lid", lid_body, manufacture=ck.FDM("PETG"),
features={"lid-mount": MOUNT.clearance_side(thickness=LID_THICKNESS)},
)
The box owns the receiving role, MOUNT.insert_side(). The lid owns the
through-hole role, MOUNT.clearance_side(thickness=LID_THICKNESS). Building a
part now runs its CadQuery body builder and then applies its named features.
Both use the same MOUNT, so there is one source for the mating hole pattern.
Connect the lid to the box
Replace the assembly block with:
DESIGN = ck.Assembly("enclosure")
box = DESIGN.add("box", BOX, color=(0.32, 0.58, 0.72))
lid = DESIGN.add("lid", LID, color=(0.86, 0.70, 0.40), explode=(0, 0, 20))
DESIGN.fix(box)
closure = DESIGN.connect(
"lid-mount", MOUNT,
through=lid.feature("lid-mount"), into=box.feature("lid-mount"),
)
PROJECT = DESIGN.as_project()
Both role frames are at the rim, with +Z pointing out of the box into the lid.
This places pockets below the rim and clearance holes above it. The code now
fixes only the box. DESIGN.connect(...) aligns the two feature frames,
places the lid and creates the associated hardware. It replaces fix(lid);
a connected instance should not also have a fixed placement.
Before running the model, copy the complete snapshot below into enclosure.py
or compare it with your edits. It includes the insert builder and all imports.
Complete enclosure with fasteners: enclosure.py
"""Chapter 4: one mount defines the lid holes, insert pockets and hardware."""
import cadquery as cq
WIDTH = 80
DEPTH = 50
HEIGHT = 20
WALL = 2.4
LID_THICKNESS = 3
SCREW_LENGTH = 8
MOUNT_POINTS = ((-34, -19), (-34, 19), (34, -19), (34, 19))
def box_body():
# The rim is Z=0; the box extends down from it.
outside = cq.Workplane("XY").rect(WIDTH, DEPTH).extrude(-HEIGHT)
cavity = (cq.Workplane("XY").rect(WIDTH - 2 * WALL, DEPTH - 2 * WALL)
.extrude(-(HEIGHT - WALL)))
shell = outside.cut(cavity)
# Four pads join the walls; each will receive a blind insert pocket.
pads = (cq.Workplane("XY").pushPoints(MOUNT_POINTS)
.circle(4.5).extrude(-8))
return shell.union(pads)
def lid_body():
return cq.Workplane("XY").rect(WIDTH, DEPTH).extrude(LID_THICKNESS)
from cadkit import FastenerSpec
import cadkit as ck
def insert_envelope(spec):
"""A dimensioned placeholder, not a supplier-specific insert model."""
model = (cq.Workplane("XY").circle(2.35).circle(1.5)
.extrude(spec.length_mm).val())
model.thread_diameter = 3
model.thread_pitch = 0.5
return model
MOUNT = ck.InsertMount(
pattern=ck.PointPattern(MOUNT_POINTS),
screw=FastenerSpec("socket_head_cap_screw", "M3-0.5", length_mm=SCREW_LENGTH),
insert=FastenerSpec(
"heat_set_insert", "M3-0.5", length_mm=5.7, standard="tutorial-envelope",
factory=insert_envelope, representation="envelope",
description="Tutorial insert envelope; select and calibrate a real insert before printing.",
),
clearance_diameter=3.4,
pocket=ck.InsertPocket(diameter=4, depth=6.7, insert_outer_diameter=4.7),
minimum_engagement=3,
)
BOX = ck.Part(
"box", box_body, manufacture=ck.FDM("PETG"),
features={"lid-mount": MOUNT.insert_side()},
)
LID = ck.Part(
"lid", lid_body, manufacture=ck.FDM("PETG"),
features={"lid-mount": MOUNT.clearance_side(thickness=LID_THICKNESS)},
)
DESIGN = ck.Assembly("enclosure")
box = DESIGN.add("box", BOX, color=(0.32, 0.58, 0.72))
lid = DESIGN.add("lid", LID, color=(0.86, 0.70, 0.40), explode=(0, 0, 20))
DESIGN.fix(box)
closure = DESIGN.connect(
"lid-mount", MOUNT,
through=lid.feature("lid-mount"), into=box.feature("lid-mount"),
)
PROJECT = DESIGN.as_project()
After saving, show the lid and hardware in the app. You should see four screw heads. Hide the lid to inspect the four receiving pads and inserts. The box remains hollow between the pads.
Count and inspect the hardware
uv run cadkit --project enclosure:PROJECT bom
uv run cadkit --project enclosure:PROJECT mechanics
uv run cadkit --project enclosure:PROJECT describe
The BOM has two rows: four M3 × 8 screws and four M3 inserts. The
mechanics output includes the lid-mount fastening and its four locations.
In the project description, the box's design.operations records insert
installation as a secondary manufacturing operation. Generating the pocket
geometry does not perform that operation.
The lid is 3 mm thick, so an 8 mm screw extends 5 mm into the receiver. That exceeds the declared 3 mm minimum engagement and stays within the 6.7 mm pocket. The next chapter will ask the mechanical validator to check this relationship, then intentionally break it.
Complete example · ← Inspect and export · Next: check a change →
