Skip to content

CAD & AI with CadQuery and Codex

CAD and AI workflow

This page starts a workflow for using CadQuery as code-first CAD and Codex as the assistant that helps write, review, and iterate the model code.

The goal is simple:

  • describe the part in engineering language
  • let Codex draft CadQuery Python code
  • run the code locally
  • inspect the shape in CQ-editor
  • export STEP/STL when the model is good

Reference to watch later:


Install with uv

CadQuery supports pip installation, and uv can manage the virtual environment and package installation. The official CadQuery docs still describe conda as the more mature route, but pip is supported on Linux, macOS, and Windows.

Use a stable Python version. Avoid the newest Python if CadQuery dependencies have not caught up yet.

1
2
3
4
5
6
mkdir cadquery_ai
cd cadquery_ai

uv init
uv python pin 3.11
uv add cadquery

Test the install:

uv run python -c "import cadquery as cq; print(cq.Workplane('XY').box(1, 2, 3).toSvg()[:80])"

If you see SVG text printed, CadQuery is working.


Add CQ-editor

CQ-editor is a GUI editor/viewer for CadQuery. It lets you run scripts, inspect the model, and export CAD files.

Install it in the same uv project:

uv add "git+https://github.com/CadQuery/CQ-editor.git"

Run it:

uv run cq-editor

Hello World CadQuery part

Create code/bracket.py:

docs/Other/cad/code/bracket.py
import cadquery as cq


length = 80
width = 30
thickness = 8
hole_diameter = 8

part = (
    cq.Workplane("XY")
    .box(length, width, thickness)
    .edges("|Z")
    .fillet(3)
    .faces(">Z")
    .workplane()
    .pushPoints([(-25, 0), (25, 0)])
    .hole(hole_diameter)
)

show_object(part)

Open CQ-editor:

uv run cq-editor code/bracket.py

If CQ-editor does not receive the file path on your platform, open CQ-editor first and then load code/bracket.py from the GUI.


Run without CQ-editor

For command-line testing, show_object is only available inside CQ-editor. Use this version when running with Python:

docs/Other/cad/code/bracket_export.py
import cadquery as cq


length = 80
width = 30
thickness = 8
hole_diameter = 8

part = (
    cq.Workplane("XY")
    .box(length, width, thickness)
    .edges("|Z")
    .fillet(3)
    .faces(">Z")
    .workplane()
    .pushPoints([(-25, 0), (25, 0)])
    .hole(hole_diameter)
)

cq.exporters.export(part, "bracket.step")
cq.exporters.export(part, "bracket.stl")

print("exported bracket.step and bracket.stl")

Run it:

uv run python code/bracket_export.py

Using Codex for CAD

CadQuery is a good fit for Codex because the CAD model is normal Python code. Instead of clicking features in a GUI, you can ask Codex to change dimensions, add holes, add fillets, or split the model into named parameters.

Good first prompt:

1
2
3
4
Create a CadQuery model for a simple mounting bracket.
Use named parameters for length, width, thickness, hole diameter, and fillet radius.
Make the script work in CQ-editor with show_object(part).
Keep the model simple and explain each modeling step.

Then iterate:

1
2
3
4
5
Modify this CadQuery part:
- add two counterbored holes on the top face
- keep all dimensions as named parameters
- export STEP and STL from a command-line version
- explain which dimensions control the hole spacing

Ask Codex to review the CAD code too:

1
2
3
Review this CadQuery script for fragile selectors, hard-coded dimensions,
and places where changing one parameter will break the model.
Suggest a cleaner parametric structure.

Practical workflow

  1. Start with a rough part description.
  2. Ask Codex for a CadQuery draft.
  3. Run it in CQ-editor.
  4. Fix one modeling issue at a time.
  5. Move magic numbers into named parameters.
  6. Export STEP for CAD/CAM and STL for printing.

Keep the first models small. A simple bracket with holes and fillets is a better first test than a full assembly.


Sources


Demo: Create my sport gym storage

Plan
# Olympic Plate and Bar Storage Rack Design Brief

## Summary

Design a freestanding welded-steel storage rack for Olympic weight plates and one Olympic bar. The plate storage should follow the floor-rack layout from the first reference image, with plates standing vertically in repeated slots. The bar storage should follow the second reference image, with the Olympic bar held horizontally on rear cradles integrated into the rack.

This document records the design intent and default parameters for the CadQuery storage rack model.

## Intended Layout

- Store 10 Olympic plates vertically in a row.
- Use repeated divider hoops between plate slots to keep plates upright and separated.
- Use lower rails and cross tubes to support the plates and stabilize the frame.
- Add rear upright posts with two horizontal cradle hooks for one Olympic bar.
- Keep optional simplified placeholder plates and a placeholder bar in the CAD model for visual scale.

## Default Parameters

All major dimensions should remain named parameters in the CadQuery script.

```python
plate_count = 10
plate_outer_diameter = 450.0
plate_center_hole_diameter = 50.0
max_plate_thickness = 45.0
plate_slot_clearance = 15.0
bar_length = 2200.0
bar_sleeve_diameter = 50.0
Additional named parameters should control the rack frame, including tube diameter, foot width, divider height, cradle height, cradle spacing, cradle depth, fillet radius, and export filenames. ## Spacing Rule The plate slot pitch is controlled by:
plate_slot_pitch = max_plate_thickness + plate_slot_clearance
Increasing `max_plate_thickness` makes each slot fit thicker plates. Increasing `plate_slot_clearance` adds extra handling room between plates. The rack length should be derived from `plate_count`, `plate_slot_pitch`, and named end clearances. ## Export Workflow The command-line CadQuery script should export both STEP and STL:
uv run python main.py
Recommended output names for the storage rack model:
step_filename = "plate_bar_storage.step"
stl_filename = "plate_bar_storage.stl"
## Assumptions - This is a parametric concept CAD model, not a certified structural design. - The fabrication style is welded steel using simplified round-tube geometry. - Tube wall thickness, weld beads, exact fasteners, and load certification are out of scope for the first model. - Default Olympic plate diameter is 450 mm with a 50 mm center hole. - Default thickest plate is 45 mm until actual measured plate thickness is available. - The Olympic bar is stored horizontally behind or above the plate rack on two integrated rear cradles. ## Verification After implementation, verify that: - The script runs with `uv run python main.py`. - STEP and STL files are regenerated successfully. - The model shows 10 plate slots. - The slot spacing follows `max_plate_thickness + plate_slot_clearance`. - The rear bar cradles are visible and aligned. - Placeholder plates and bar do not intersect the rack frame. ```