Skip to content

1. Make a box and a lid

You will make two useful shapes before introducing CadKit's project model: an open box and a flat lid that rests on its rim.

Create a working directory

Install uv if you do not already have it. Then run:

uv init --python 3.12 cadkit-box
cd cadkit-box
uv add 'cadkit-py[desktop]'

Commit pyproject.toml and uv.lock. Run the remaining commands from this directory.

Write the shapes

Create enclosure.py with this complete example:

"""Chapter 1: a CadQuery box and loose lid. Run to write native STEP files."""
from pathlib import Path

import cadquery as cq

WIDTH = 80
DEPTH = 50
HEIGHT = 20
WALL = 2.4
LID_THICKNESS = 3


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)))
    return outside.cut(cavity)


def lid_body():
    return cq.Workplane("XY").rect(WIDTH, DEPTH).extrude(LID_THICKNESS)


if __name__ == "__main__":
    output = Path("build/first-box")
    output.mkdir(parents=True, exist_ok=True)
    cq.exporters.export(box_body(), str(output / "box.step"))
    cq.exporters.export(lid_body(), str(output / "lid.step"))
    print(f"Wrote {output / 'box.step'} and {output / 'lid.step'}")

Workplane("XY") starts a sketch in the XY plane. rect makes a centred rectangle, and extrude gives it height. Subtracting the smaller, shallower solid with cut leaves four walls and a 2.4 mm floor.

We put the rim at Z = 0. The box extends down to Z = −20; the lid extends up to Z = 3. Both shapes therefore use the same mating plane. Keeping that plane fixed will make the later assembly easier to describe.

Each builder is a function that returns a CadQuery Workplane. Nothing is built merely by importing the file. The if __name__ == "__main__" block runs only when you execute it directly.

Export the first result

uv run python enclosure.py

You should see:

Wrote build/first-box/box.step and build/first-box/lid.step

These are native solid models. If you already use a STEP viewer, open them. The box should measure 80 × 50 × 20 mm; the lid should measure 80 × 50 × 3 mm. In the next chapter you will open both together in CadKit Desktop.

Try changing HEIGHT to 24, run the command again, then change it back to 20. Only the box gets taller: its rim stays at Z = 0, and the lid is unchanged. You now have a small parametric model, with dimensions collected at the top of the file rather than spread across geometry operations.

Complete example · Tutorial overview · Next: make a project →