Primitives¶
Primitives are the basic building blocks in CodeCAD. They represent simple 3D shapes that you can combine, transform, or modify to create more complex parts.
All primitives are parametric: you specify their size in code, and can later adapt them easily by changing parameters.
Model resolution
All the models are generated below with deflection 0.2 to lower the file size.
box(width, depth, height)¶
Creates a rectangular box (a cuboid).
local p = box(20, 10, 5)
emit(p)
- width → size along the X axis
- depth → size along the Y axis
- height → size along the Z axis
cylinder(diameter, height)¶
Creates a vertical cylinder centered in x/y
local c = cylinder(5, 20)
emit(c)
- diameter → cylinder diameter
- height → cylinder height
sphere(diameter)¶
Creates a sphere centered in origin.
local s = sphere(10)
emit(s)
- diameter → sphere diameter
cone(d1, d2, height)¶
Creates a conical or frustum-shaped solid.
local co = cone(10, 2, 10)
emit(co)
- d1 → bottom diameter
- d2 → top diameter (set to 0 for a true cone)
- height → cone height
wedge(dx, dy, dz, ltx)¶
Creates a wedge — essentially a box with slanted top faces.
local w = wedge(20, 10, 10, 5)
emit(w)
- dx, dy, dz → base box dimensions
- ltx → shift of left corner
hex_prism(diameter, height)¶
Creates a regular hexagonal prism (like a bolt head).
local h = hex_prism(5, 2)
emit(h)
- diameter → distance from flat to flat side (across)
- height → prism height
poisson_plate(spec, thickness)¶
Generates an organic “bubble” plate using Poisson disk sampling.
The result is a perforated metal plate with circular holes that look like rising bubbles.
local spec = PoissonDiskSpec.new()
spec.width = 1000
spec.height = 500
spec.margin = 20
spec.target_points = 100
spec.min_gap = 20
spec.r_min = 10
spec.r_max = 50
spec.seed = 42
spec.density_falloff = 1.0
spec.radius_falloff = 1.0
local plate = poisson_plate(spec, 5)
emit(plate)
Key fields on PoissonDiskSpec:
- width → plate width in mm (X direction)
- height → plate height in mm (Y direction)
- thickness → plate thickness in mm
- margin → border margin where no bubbles may appear
- target_points → approximate number of bubbles to try to place
- r_min / r_max → minimum/maximum bubble radius
- min_gap → minimum distance between adjacent bubbles
- radius_falloff → how fast bubble size shrinks towards the top
- density_falloff → how fast bubble count thins out towards the top
Tips for Working with Primitives¶
- Start simple: Every complex part begins as a box, cylinder, or combination.
- Think parametrically: Store dimensions in variables, so you can easily adapt them later.
- Combine with Booleans: Use difference, union, and intersection to cut holes, join parts, or trim shapes.