Skip to main content

One language to define your mechanical and electrical CAD designs, federated to your modeling application.

Project description

CodeToCAD

CodeToCAD accelerates mechanical and electrical CAD design, simulations/FEA, controls software and MCU firmware by giving you one language to define your design — your script is federated to the modeling or design application automatically.

Install

pip install codetocad

Quick start (CLI)

codetocad init cup

This creates a cup/ folder with a cup.py file and opens an interactive menu to create parts and sketches, transform, boolean, shell, constrain and export them. Every action updates generated python part files (for example cup_cylinder.py), so the CLI extends to the full functionality of the CodeToCAD classes.

Run a script:

codetocad path/to/script.py

Quick start (Python)

import codetocad

body = codetocad.cylinder(radius="2cm", height="5cm")
body.shell(thickness="5mm")
body.set_material(codetocad.aluminum_material())
body.export("cup.stl")

Highlights

  • Units: floats are meters/radians; strings such as "2in", "10 deg" or expressions like "2in - 5mm" are parsed and converted.
  • Locations: 6-dof positions/orientations; CubeLocations shortcuts to the 23 topological locations of any shape's bounding cube; the @codetocad.location decorator marks named locations on your part classes.
  • Parts & assemblies: Part2D/Part3D with extrude, shell, fillet, chamfer, hole; Assembly2D/Assembly3D constraints (coincide, parallel, fixed, revolute, prismatic, ...) recorded in ledgers.
  • Primitives: cube, cylinder, sphere, rectangle, circle, text, import_file and material presets.
  • ECAD: led, resistor, capacitor components and an ECADMixin for electrical properties.
  • Mixins: sensors (CameraMixin, IMUMixin, MicrophoneMixin) and actuators (DCMotorMixin, BLDCMotorMixin) for custom parts.
  • Fasteners: CommonFasteners enum that can build() a part or apply features (clearance holes) to another part.

Build123D integration

Install the extra (uv sync --extra build123d) and your parts are federated to real OpenCascade solids — booleans, shells, fillets, chamfers, holes and transforms are replayed natively, and geometry queries, analysis and STL/STEP export use the native topology:

from codetocad_integrations.build123d import make_cube

if __name__ == "__main__":
    cube = make_cube("10cm", "10cm", "5cm")
    cube.hole(cube.top_center, radius="4cm", amount="5cm")
    cube.export("my_cube.stl")

Subclass codetocad_integrations.build123d.Part3D and override build_native() to model a custom base shape with the Build123D API; all CodeToCAD operations still apply on top. adapt(part) converts any core CodeToCAD part (including led(), resistor(), fasteners, ...) into a Build123D-federated one.

See codetocad_integrations/build123d/examples/ for the full gallery.

Blender integration

With Blender on your PATH (or CODETOCAD_BLENDER pointing at it), the same designs federate to Blender mesh objects — booleans, shells (solidify), fillets/chamfers (bevel), holes and transforms are replayed with modifiers, and you can export .stl, .obj, .glb, .fbx or a full .blend scene:

from codetocad_integrations.blender import ensure_blender, make_cube

if __name__ == "__main__":
    ensure_blender()  # relaunches this script under `blender --background`
    cube = make_cube("10cm", "10cm", "5cm")
    cube.hole(cube.top_center, radius="4cm", amount="5cm")
    cube.export("my_cube.blend")

Subclass codetocad_integrations.blender.Part3D and override build_native() to model with bpy/bmesh directly. See codetocad_integrations/blender/examples/.

Simulation (PyBullet & MuJoCo)

Model in Build123D or Blender, assemble with joint constraints, and import right into physics simulation — simulate(part) walks the assembly, exports the meshes and generates a URDF (PyBullet) or MJCF (MuJoCo):

from codetocad import Location
from codetocad_integrations.build123d import make_cube, make_cylinder
from codetocad_integrations.pybullet import simulate  # or ...mujoco

mount = make_cube("6cm", "6cm", "4cm", start_location=Location(z="52cm"))
rod = make_cylinder("1cm", "40cm", start_location=Location(z="30cm"))
pivot = Location.from_euler(0, 0, "50cm", x_deg=-90, name="pivot")
mount.revolute(pivot, rod, pivot)  # hinge about the Y axis

sim = simulate(mount, gui=True)
sim.set_joint_value("pivot", 1.0)
sim.run(10.0, realtime=True)

Joint axes come from the constraint Location's orientation, limits from min_limits/max_limits, masses/inertias from part materials and geometry, and codetocad.Lighting describes scene lights. See the examples in codetocad_integrations/pybullet/examples/ and codetocad_integrations/mujoco/examples/ (6-DOF keyboard-controlled arm, pendulum, double pendulum).

FEA (CalculiX)

Analyze the same parts with finite elements — analyze(part) meshes the exported geometry with gmsh, applies fixtures/loads described with Locations, solves with CalculiX via pygccx, and returns displacement and von Mises stress fields with visualization:

from codetocad import steel_material
from codetocad_integrations.build123d import make_cube
from codetocad_integrations.calculix import analyze

beam = make_cube("200mm", "20mm", "10mm")
beam.set_material(steel_material())

fea = analyze(beam)
fea.fix(beam.left_center)                          # clamp the left face
fea.add_force(beam.right_center, force=(0, 0, -100))
results = fea.solve()
print(results.max_displacement, results.max_von_mises)
results.visualize("beam_fea.png")

Materials carry elastic properties (steel_material(), aluminum_material() or set youngs_modulus/poissons_ratio on any MaterialBase). The ccx solver is auto-discovered from CODETOCAD_CCX, the PATH, or ~/.codetocad/ccx/bin/ccx. See codetocad_integrations/calculix/examples/.

Visualization (Open3D)

Display any Part3D — core, Build123D- or Blender-federated — in an Open3D window, or render a screenshot headlessly for docs/CI:

from codetocad_integrations.build123d import make_cube
from codetocad_integrations.open3d import show, render

cube = make_cube("10cm", "10cm", "5cm")
cube.hole(cube.top_center, radius="4cm", amount="5cm")

show(cube)                          # interactive window
render(cube, path="cube.png")       # offscreen screenshot

The part is exported (part.export()) to a temporary mesh and loaded into Open3D, so it works with any backend — Open3D itself isn't a CAD kernel. See codetocad_integrations/open3d/examples/.

User-defined parts

Define a part with the API of your choice (for example Build123D):

import build123d
import codetocad

class Box(codetocad.Part3D):
    def build(self):
        length, width, thickness = 80.0, 60.0, 10.0
        with build123d.BuildPart() as ex1:
            build123d.Box(length, width, thickness)

    @codetocad.location
    def example_location(self):
        return codetocad.CubeLocations.top_center.translate(x="2cm", y="2mm")

See CodeToCAD.md for the full design document.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codetocad-2026.7.5.1.tar.gz (61.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codetocad-2026.7.5.1-py3-none-any.whl (64.0 kB view details)

Uploaded Python 3

File details

Details for the file codetocad-2026.7.5.1.tar.gz.

File metadata

  • Download URL: codetocad-2026.7.5.1.tar.gz
  • Upload date:
  • Size: 61.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.5

File hashes

Hashes for codetocad-2026.7.5.1.tar.gz
Algorithm Hash digest
SHA256 5da4fc1abc5c304dbc3e05dcab73df4ba644d184d58a0ea8779fa22436b357be
MD5 a9e179fd87d486014159a83f46c94905
BLAKE2b-256 d74c8fd7a34fe0e9b320f39417ce98d1b448359792afd6094c4ec6f55e5310cf

See more details on using hashes here.

File details

Details for the file codetocad-2026.7.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for codetocad-2026.7.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f24d025454fd0822753ab1208a275040a8874090cc94ba5287ef53f9bcd14823
MD5 f5182e9b0c1df4c80dfdef657826ebc7
BLAKE2b-256 b9cd4c6b024e4b73d38ef4500b223a6849a37de759dc31097e2a0dc2b8305298

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page