cad
3D CAD and parametric geometry - 3D vectors, affine transforms, solid primitives, mesh construction, CSG boolean operations, NURBS/B-splines, extrude/revolve/sweep/loft, fillet/chamfer, assemblies, constraints, import/export in STL, OBJ, STEP, and IGES.
Load with: use cad
What this module does
cad provides the building blocks for 3D solid modelling and parametric
CAD. You construct shapes (box, sphere, cylinder, cone, torus, capsule), combine them
with CSG booleans (union, difference, intersection), build and manipulate triangle meshes,
define curves and surfaces (Bézier, B-spline, NURBS), and produce swept and lofted forms.
Assemblies let you position parts with transforms and express geometric constraints -
solved by the host CAD kernel.
Most heavy computation (CSG evaluation, NURBS evaluation, mesh subdivision, mass
properties) is delegated to host natives (__cad_*).
Quick example
use cad
# Primitives and CSG
box = cadbx(10, 5, 3)
hole = cdsph(2.5)
body = cddff(box, hole) # box minus sphere
# Extrude a 2D shape to 3D
solid = cdxtr(my_profile, 20)
# Build a mesh manually
m = cdmsh()
v0 = cdvtx(m, 0, 0, 0)
v1 = cdvtx(m, 1, 0, 0)
v2 = cdvtx(m, 0, 1, 0)
cdfce(m, [v0, v1, v2])
# Export
cd2s0(m, "output.stl")
cd2bj(m, "output.obj")
Functions
Vectors and math
cadv3(x, y, z)
Creates a 3D vector as a 3-element list [x, y, z].
caddt(a, b)
Dot product of two vectors.
cdcrs(a, b)
Cross product.
cadln(v)
Euclidean length (magnitude) of a vector.
cdnrm(v)
Normalised unit vector. Returns [0,0,0] for zero-length input.
caddd(a, b) / cadsb(a, b) / cadml(v, s)
Vector add, subtract, and scalar multiply.
cddst(a, b)
Euclidean distance between two points.
cdlrp(a, b, t)
Linear interpolation between a and b at parameter t.
Transforms (4×4 matrices)
cdtrn(x, y, z)
Translation matrix.
cadrt(axis, angle)
Rotation matrix around an arbitrary axis by angle radians. Uses the host native __cad_rotmtx.
cdscl(x, y, z)
Non-uniform scale matrix.
cdply(transform, point)
Applies a 4×4 matrix transform to a 3D point. Returns the transformed point.
cdmlt(t1, t2)
Multiplies two 4×4 transforms together. Returns a combined "cmp" transform.
Primitives
cadbx(w, h, d)
Axis-aligned box with dimensions width × height × depth.
cdsph(r)
Sphere of radius r centred at the origin.
cdcyl(r, h)
Cylinder of radius r and height h.
cdcps(r, h)
Capsule (cylinder with hemispherical end caps).
cdcne(r, h)
Cone with base radius r and height h.
cdtrs(R, r)
Torus with major radius R (centre to tube centre) and minor radius r (tube radius).
Mesh
cdmsh()
Creates an empty mesh with vertex (v), face (f), edge (e), normal (n), and UV (uv) lists.
cdvtx(m, x, y, z)
Adds a vertex to the mesh. Returns its index.
cdfce(m, vertex_indices)
Adds a face from a list of vertex indices. Automatically computes and stores the face normal from the first three vertices. Returns the face index.
CSG booleans
cadnn(a, b)
Union of two shapes - the combined volume of both.
cddff(a, b)
Difference - shape a minus shape b.
cdnts(a, b)
Intersection - the volume that both shapes share.
cadvl(csg)
Evaluates a CSG tree to a mesh via host native __cad_csg_eval.
Sweep operations
cdxtr(shape, height)
Extrudes a 2D profile to a 3D solid.
cadrv(shape, axis, angle)
Revolves a profile around an axis by angle radians.
cdswp(shape, path)
Sweeps a profile along a path curve.
cdlft(shapes)
Lofts through a list of profiles, blending between them.
Edge operations
cadfl(shape, radius, edges)
Rounds specified edges with a fillet of the given radius.
cdchm(shape, distance, edges)
Bevels specified edges with a chamfer of the given distance.
Curves
cadbz(points)
Bézier curve through control points.
cdbsp(points, degree, knots)
B-spline curve.
cdcrc(centre, r)
Full circle with given centre and radius.
cadrc(centre, r, start, end)
Arc from start to end angle (radians).
NURBS surfaces
cdbrp(control_pts, degrees)
B-spline surface from a 2D grid of control points and [du, dv] degrees.
cdnrb(ctrl, weights, knots, degrees)
NURBS surface with explicit weights and knot vectors.
cdnr0(nurbs, u, v)
Evaluates a NURBS surface at parameters (u, v) via __cad_nurbs_eval.
cdsb0(surface)
Catmull-Clark subdivision of a surface.
cdtrm(surface, curve)
Trims a surface with a curve.
Mesh operations
cddcm(mesh, ratio)
Decimates (reduces polygon count) by ratio (0–1).
cdsbd(mesh, n)
Subdivides the mesh n times.
cdsmt(mesh, iters)
Laplacian smoothing for iters passes.
cdrms(mesh, target_edge_len)
Remeshes to uniform triangle size.
cadv0(mesh) / cdsrf(mesh)
Computes volume or surface area.
cdcnt(mesh) / cdnrt(mesh) / cadbb(mesh)
Centroid, moment of inertia tensor, and axis-aligned bounding box.
Assembly and constraints
cdsmp(shape, transform, name)
Creates a named part with a position transform.
cdsmn(parts, constraints)
Creates an assembly from a list of parts and constraints.
cdsmc(type, p1, p2, params)
Defines a constraint between two parts. Types include "dist", "par", "perp", "ang", "coin", "fix".
cdslv(constraints, values)
Solves a constraint system via __cad_solve_cns.
Parametric modeling
cdpr0(values, fn)
Creates a parametric model - a set of named parameter values and a function that rebuilds the shape from them.
cdprm(model)
Evaluates the parametric model with its current parameter values.
Import / Export
cd2s0(mesh, path) / cdfr0(path)
Export to / import from STL.
cd2bj(mesh, path) / cdfrm(path)
Export to / import from OBJ.
cd2st(csg, path)
Export CSG tree to STEP format.
cd2gs(csg, path)
Export CSG tree to IGES format.
Notes
- CSG evaluation, NURBS evaluation, and all mesh operations delegate to
__cad_*host natives - a CAD kernel must be linked. - All angles are in radians.
- Requires
trl,txt,jsn, andsci.