Skip to content
You Start with one site
Amounts in Start with one site
Engineering · Mechanical Engineering & Design

Code-Based Mechanical Design

Write mechanical parts as short Python programs in CadQuery. Change one number, and the part, its checks and its files follow.

Found this useful?
The two-stage gear train of lesson 2: a small blue pinion drives an orange gear, and a green pinion on the same shaft drives a large pink output gear, each on its steel shaft.

A bolt pocket 1 mm too small. A gear tooth curve copied from the web and never checked. A box that no longer fits after the board changes. Each time, someone draws the part again by hand.

In this course the part is a program. Its sizes come from standards, equations and data files, the program checks them, and one changed number gives you a new part, its STEP file and the proof that it works.

How each lesson goes
  1. 1Start from a needThe board changed, and the box must follow.
  2. 2Work out the sizesFin count and height from the heat to remove.
  3. 3Write the part as codeEvery size is a variable in one script.
  4. 4Check it and exportTests on size, mass and fit, then STEP and STL.
You will be able to
  1. Write a part as a program, with each size from a standard, an equation or a data file
  2. Check a design in code for stress, heat, fit and mass before anything is made
  3. Export STEP and STL files a workshop or a printer can use

New to this? New to code CAD? Start with lesson 0: install CadQuery and run eight short examples.

You need Python 3.10 or newer on Windows, Mac or Linux, and a code editor. FreeCAD for lesson 7.

The engineering behind it

Parts described in code, so a family of mounts or cases is one file and not fifty drawings.

More about this course

Every size comes from somewhere

In a script a size is not typed in because it looks right. It is read from a table, solved from an equation or taken from a file.

  • Bolt head widthread from the ISO table for that sizeStandards
  • Tooth curvesolved from the involute of the base circleGeometry
  • Box openingtaken from the connector place in the KiCad fileData
  • Fin countthe one that keeps the part coolest in still airHeat transfer
  • Wire diameterkeeps the stress under the fatigue limitStrength

A script is not a drawing

Drawing CAD
FreeCAD, SolidWorks
  • Sketch with the mouse, then constrain
  • The history lives inside the model file
  • Strong for drawings and assemblies

Best when you shape a part by looking at it.

Code CAD
CadQuery
  • Sizes, loops and equations in Python
  • The model is a text file
  • Strong for families of parts and checks

Best when the shape comes from a table, an equation or a data file.

Why CadQuery

Sizes are variablesChange one number and the part is built again, correctly.
Plain PythonNumPy and Matplotlib in the same script as the part.
Text you can compareA model is a .py file. Version control shows what changed.
Yours to keepOpen source. No account, and nothing expires.
Windows, Mac and LinuxOne pip install, the same on every system.
Files any workshop takesSTEP for machining, STL for printing.

Honest limits. CadQuery makes no dimensioned drawings of its own. Many teams use it beside FreeCAD or SolidWorks: code for the parts that come from tables and equations, a drawing program for the rest.

Before you start

  • No CAD experience needed
  • Python basics: functions, loops and dictionaries
  • Python 3.10 or newer, and pip install cadquery
  • FreeCAD for lesson 7, for its stress solver
The full course notes

Design production-quality mechanical parts entirely from Python code using CadQuery. This self-contained course covers fastener libraries, involute gears, thermal optimization, lattice structures, spring verification, and FEA-driven design, with every lesson producing manufacturable STEP and STL output. #CadQuery #CodeBasedCAD #MechanicalDesign

Why CadQuery?

Python-Native CAD

CadQuery is a Python library, not a GUI application with a scripting layer bolted on. Your CAD models are Python programs: testable, diffable, and automatable.

Parametric by Default

Every dimension is a variable. Change one parameter and the entire model regenerates correctly. No manual constraint repair, no broken sketches.

Engineering Ecosystem

Direct access to NumPy, SciPy, matplotlib, pandas. Run thermal calculations, parse data files, optimize parameters, and generate geometry, all in one script.

Production Output

Export STEP files for CNC machining, STL for 3D printing, DXF for laser cutting. Industry-standard formats, no vendor lock-in.

Course Structure

Each lesson follows a consistent approach:

  1. Engineering Problem Start with a real design challenge (a hardware library, a gear system, a thermal problem), not abstract exercises.

  2. Theory & Standards Cover the engineering fundamentals: ISO standards, involute mathematics, thermal resistance models, stress equations.

  3. CadQuery Implementation Write Python code that translates engineering knowledge into 3D geometry. Every function is parametric and reusable.

  4. Verification & Output Validate designs against engineering criteria and export production-ready files.

Lessons

Lesson 0: Code-Based CAD in Action

Start here. Set up your Python environment and run 8 complete CadQuery scripts that produce colorful 3D models, from parametric Lego bricks to planetary gear sets. Each example highlights what becomes possible when your CAD model is a Python script, and by the end you will have a working CadQuery setup ready for the rest of the course.

Lesson 1: Parametric Hardware Library

Parametric Hardware Library from Engineering Standards. Build a reusable library of ISO-standard fasteners (hex bolts, socket cap screws, nuts, and washers) generated from standards tables. Set up CadQuery, Jupyter, and OCP CAD Viewer. Output: 50+ standard parts as STEP/STL files.

Lesson 2: Involute Gear Systems

Involute Gear Systems. Generate mathematically-perfect involute gears from parametric equations. Build single gears, meshing pairs, and a complete gear train with housing. Cover Lewis bending stress, contact ratio, and backlash. Output: 3D-printable gear train assembly.

Lesson 3: Custom Enclosure from PCB

Custom Enclosure from PCB Data. Parse KiCad PCB files to auto-generate enclosures with standoffs, port cutouts, ventilation, snap-fits, and lids. Output: print-ready enclosure for a real board (e.g., Raspberry Pi).

Lesson 4: Heat Sink Optimization

Heat Sink Design & Thermal Optimization. Model thermal resistance from junction to ambient. Compare straight, pin-fin, and radial fin geometries. Sweep parameters and visualize thermal performance maps with matplotlib. Output: optimized heat sink with thermal justification.

Lesson 5: Lattice Structures & TPMS

Lattice Structures & TPMS for Additive Manufacturing. Generate strut-based lattices (cubic, BCC, octet-truss) and TPMS surfaces (gyroid, Schwarz-P, diamond). Implement graded density and conformal lattices. Address printability constraints. Output: lightweight structural parts for SLA/SLS/FDM.

Lesson 6: Spring Design

Spring Design with Engineering Stress Verification. Design compression, extension, and torsion springs from load requirements. Generate helical geometry with CadQuery wire sweeps. Apply Wahl correction factor and Goodman fatigue analysis. Output: springs designed to specific load and life requirements.

Lesson 7: FEA-Driven Optimization

FEA-Driven Structural Optimization. Close the loop between CadQuery and FreeCAD FEM. Generate parametric brackets, mesh and solve with CalculiX, record results, sweep parameters, and plot Pareto fronts (mass vs. stress). Output: optimized bracket with full FEA justification.

Prerequisites

Python basics required

You should be comfortable with:

  • Python fundamentals (functions, loops, dictionaries)
  • Basic command-line usage (pip install, running scripts)
  • Engineering drawings and 3D concepts

Helpful but not required:

Getting Started

  1. Set up a virtual environment (requires Python 3.10+)

    Terminal window
    python3 -m venv cadquery-env
    source cadquery-env/bin/activate # Windows: cadquery-env\Scripts\activate
    pip install cadquery trimesh numpy scipy

    This gives you everything needed to run every example in the course, starting from Lesson 0.

  2. Start with Lesson 0: Code-Based CAD in Action. It walks you through 8 short scripts that produce colorful 3D models you can view in SiliconWit’s GLB Viewer. By the end you will have a working environment and a feel for what code-based CAD can do.

  3. Move to Lesson 1, which adds Jupyter and OCP CAD Viewer for interactive development, then builds a reusable parts library from ISO standards.

  4. Work through lessons sequentially, as each builds on patterns from earlier lessons.

  5. Modify parameters and experiment: the whole point of code-based design is easy iteration.

  6. Export and manufacture: every lesson produces STEP/STL files ready for production.

© 2021-2026 SiliconWit®. All rights reserved.

9 lessons, in order.

SiliconWit makes these courses. We also run siliconwit.io, a monitoring service.