Skip to content

5. Check a change before exporting

The mount describes where parts and hardware belong. Now tell CadKit two more things about the assembly: the lid must meet the rim, and a straight driver must be able to reach each screw.

Declare contact and access

In enclosure.py, add this interface immediately before the final PROJECT = DESIGN.as_project() line:

# Require the installed lid to touch the rim, without a material overlap.
DESIGN.interface(
    "lid-on-rim", left=box, right=lid,
    kind="contact", max_gap_mm=0.01,
)

The contact interface permits a maximum gap of 0.01 mm and no material overlap. It checks the two named parts in their installed positions.

Then add the driver-access declaration, still before as_project():

# Check a straight, 3 mm diameter driver against these two parts.
DESIGN.driver_access(
    "lid-driver", connection=closure,
    diameter=3, length=30, obstacles=(box, lid),
)

This supplies a 3 mm diameter, 30 mm long straight probe at each screw seat, checked against the box and lid. It checks that particular tool envelope against the listed obstacles. It does not describe every possible driver handle or an entire assembly procedure.

These are the only additions to chapter 4. If you are starting here, copy this complete snapshot into enclosure.py.

Complete enclosure with checks: enclosure.py
"""Chapter 5: explicit rim contact and driver-access checks. Try SCREW_LENGTH = 4."""
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"),
)

# Require the installed lid to touch the rim, without a material overlap.
DESIGN.interface(
    "lid-on-rim", left=box, right=lid,
    kind="contact", max_gap_mm=0.01,
)
# Check a straight, 3 mm diameter driver against these two parts.
DESIGN.driver_access(
    "lid-driver", connection=closure,
    diameter=3, length=30, obstacles=(box, lid),
)

PROJECT = DESIGN.as_project()

Read the report

uv run cadkit --project enclosure:PROJECT validate-assembly --output build/assembly-checks.json

The command prints the report and writes the same JSON to build/assembly-checks.json. With this example, the summary should have fail: 0; the overall status is incomplete. Find the passing lid-on-rim contact, engagement, bottoming and driver-access findings.

incomplete is expected. The inserts are qualified envelopes; the model does not establish their physical fit, the printed material's strength, screw preload or a complete assembly sequence. Keep those unverified findings when you share the result. They are different from a confirmed geometric failure.

You can run and inspect these checks in the app's Connections tab too.

Assembly checks for the tutorial enclosure

Make one bad change

Change the constant near the top of enclosure.py:

SCREW_LENGTH = 4

Run the validation command again. It should return a nonzero exit status and an engagement failure: only 1 mm of the screw reaches beyond the 3 mm lid, below the declared 3 mm minimum.

Try exporting this bad version:

uv run cadkit --project enclosure:PROJECT build all --output-dir build/short-screw

The export should stop with the confirmed assembly failure. The validation report remains in that output directory so you can inspect why it stopped.

Restore SCREW_LENGTH = 8, validate again, then export:

uv run cadkit --project enclosure:PROJECT validate-assembly --output build/assembly-checks.json
uv run cadkit --project enclosure:PROJECT build all --output-dir build/enclosure

There should be no confirmed failures, and both parts should export. The build manifest retains the mechanical review, including its unverified findings. You now have a useful edit loop: change one parameter, inspect the geometry, validate the assembly, then export.

Complete example · ← Fasten the lid · Next: work with an agent →