cadrum
Rust CAD library powered by statically linked, headless OpenCASCADE (OCCT 8.0.0-beta1).
Summary
Other Rust CAD bindings either require the user to install OCCT ahead of time
(with all the version skew this entails on Linux distros and Windows) or
expose OCCT's class hierarchy 1:1, where building a cube ends up touching
gp_Pnt, gp_Ax2, BRepPrimAPI_MakeBox, and TopoDS_Shape before any
geometry actually appears.
cadrum takes a different bet:
- Static linking with prebuilt binaries.
cargo buildon a supported target downloads a self-contained OCCT 8.0.0 tarball and links it statically. No system OCCT, no dynamic libraries to ship, noLD_LIBRARY_PATHin production. - A minimal type surface. Three concrete shape types —
Solid,Edge,Face— plus a triangleMeshfor visual output andglamvectors for input. Operations are inherent methods on the shape types, soSolid::cube(...).rotate_z(0.5).translate(DVec3::X * 10.0)chains like any value-returning Rust API. - Collections are first-class.
Vec<Solid>and[Solid; N]carry the same transform, query, and boolean methods as a singleSolidvia theCompoundtrait; the wire / edge-list pair has the parallelWiretrait.
Introduction
OpenCASCADE represents shapes as a boundary representation (BRep): a solid is a topological assembly of faces, faces are trimmed surfaces bounded by edges, edges are 3D curves with a parameter range. Booleans, fillets, sweeps, and the like rebuild this assembly under the hood; CAD I/O formats like STEP / IGES preserve it exactly across applications.
Working at that level pays off when the application needs to reason about
geometry — closest-point queries, swept profiles along arbitrary spines,
history-tracked face derivation through booleans — and not merely render
triangles. Triangle meshes are a separate, lossy projection that cadrum
exposes through Solid::mesh when an STL export or SVG render is required.
Capabilities
| Area | Methods |
|---|---|
| Primitives | Solid::cube, Solid::sphere, Solid::cylinder, Solid::cone, Solid::torus, Solid::half_space |
| Curves | Edge::line, Edge::arc_3pts, Edge::circle, Edge::polygon, Edge::helix, Edge::bspline |
| Surfacing | Solid::extrude, Solid::sweep, Solid::loft, Solid::bspline |
| Editing | Solid::shell, Solid::fillet_edges, Solid::chamfer_edges, Solid::clean |
| Booleans | Solid::union, Solid::subtract, Solid::intersect |
Transforms (shared by Solid / Edge / Compound / Wire) |
translate, rotate, rotate_x / _y / _z, scale, mirror, align_x / _y / _z |
| Queries | Solid::volume, Solid::area, Solid::center, Solid::inertia, Solid::bounding_box, Solid::contains |
| Topology | Solid::iter_face, Solid::iter_edge, Face::iter_edge, Face::project, Edge::project |
| Identity / history | Solid::id, Face::id, Edge::id, Solid::iter_history |
| I/O | Solid::read_step / Solid::write_step, Solid::read_brep_binary / Solid::write_brep_binary, Solid::read_brep_text / Solid::write_brep_text |
| Mesh | Solid::mesh → Mesh, Mesh::write_stl, Mesh::write_svg |
Color (feature color) |
per-face color preserved across STEP / BRep / STL / SVG round-trips |
Usage
| primitives | write read | transform | boolean |
|---|---|---|---|
| extrude | loft | sweep | shell |
| bspline | fillet | chamfer | |
More examples with source code are available at lzpel.github.io/cadrum.
Add this to your Cargo.toml:
[]
= "^0.7"
Build
cargo build automatically downloads a prebuilt OCCT 8.0.0-rc5 binary for the targets below.
| Target | Prebuilt | |
|---|---|---|
x86_64-unknown-linux-gnu |
✅ | |
aarch64-unknown-linux-gnu |
✅ | |
x86_64-pc-windows-msvc |
✅ | |
x86_64-pc-windows-gnu |
✅ |
For other targets, build OCCT from source:
OCCT_ROOT=/path/to/occt
If OCCT_ROOT is not set, built binaries are cached under target/.
Requirements when building OpenCASCADE from source
- C++17 compiler (GCC, Clang, or MSVC)
- CMake
Examples
Primitives
Primitive solids: box, cylinder, sphere, cone, torus — colored and exported as STEP + SVG.
//! Primitive solids: box, cylinder, sphere, cone, torus — colored and exported as STEP + SVG.
use ;
Write read
Read and write: chain STEP, BRep text, and BRep binary round-trips with progressive rotation.
//! Read and write: chain STEP, BRep text, and BRep binary round-trips with progressive rotation.
use ;
use FRAC_PI_8;
Transform
Transform operations: translate, rotate, scale, and mirror applied to a cone.
//! Transform operations: translate, rotate, scale, and mirror applied to a cone.
use ;
use PI;
Boolean
Boolean operations: union, subtract, and intersect between a box and a cylinder.
//! Boolean operations: union, subtract, and intersect between a box and a cylinder.
use ;
Extrude
Demo of Solid::extrude: push a closed 2D profile along a direction vector.
//! Demo of `Solid::extrude`: push a closed 2D profile along a direction vector.
//!
//! - **Box**: square polygon extruded along Z
//! - **Oblique cylinder**: circle extruded at a steep angle
//! - **L-beam**: L-shaped polygon extruded along Z
//! - **Heart**: BSpline heart-shaped profile extruded along Z
use ;
/// Square polygon → box (simplest extrude).
/// Circle extruded at a steep angle → oblique cylinder.
/// L-shaped polygon → L-beam.
/// Heart-shaped BSpline profile extruded along Z.
Loft
Demo of Solid::loft: skin a smooth solid through cross-section wires.
//! Demo of `Solid::loft`: skin a smooth solid through cross-section wires.
//!
//! - **Frustum**: two circles of different radii → truncated cone (minimal loft)
//! - **Morph**: square polygon → circle (cross-section shape transition)
//! - **Tilted**: three non-parallel circular sections → twisted loft
use ;
/// Two circles → frustum (minimal loft example).
/// Square polygon → circle (2-section morph loft).
/// Three non-parallel circular sections → twisted loft.
Sweep
Sweep showcase: M2 screw (helix spine) + U-shaped pipe (line+arc+line spine)
//! Sweep showcase: M2 screw (helix spine) + U-shaped pipe (line+arc+line spine)
//! + twisted ribbon (`Auxiliary` aux-spine mode).
//!
//! `ProfileOrient` controls how the profile is oriented as it travels along the spine:
//!
//! - `Fixed`: profile is parallel-transported without rotating. Cross-sections
//! stay parallel to the starting orientation. Suited for straight extrusions;
//! on a curved spine the profile drifts off the tangent and the result breaks.
//! - `Torsion`: profile follows the spine's principal normal (raw Frenet–Serret
//! frame). Suited for constant-curvature/torsion curves like helices and for
//! 3D free curves where the natural twist should carry into the profile.
//! Fails near inflection points where the principal normal flips.
//! - `Up(axis)`: profile keeps `axis` as its binormal — at every point the
//! profile is rotated around the tangent so one in-plane axis stays in the
//! tangent–`axis` plane. Suited for roads/rails/pipes that must preserve a
//! gravity direction. On a helix, `Up(helix_axis)` is equivalent to `Torsion`.
//! Fails when the tangent becomes parallel to `axis`.
//! - `Auxiliary(aux_spine)`: profile's tracked axis points from the main spine
//! toward a parallel auxiliary spine. Arbitrary twist control — e.g. a
//! helical `aux_spine` on a straight `spine` produces a twisted ribbon.
use ;
// ==================== Component 1: M2 ISO screw ====================
// ==================== Component 2: U-shaped pipe ====================
// ==================== Component 3: Auxiliary-spine twisted ribbon ====================
// Sweeping a straight spine with `Auxiliary(&[helix])` rotates the tracked
// axis of the profile at each point to face the corresponding helix point.
// A pitch=h helix makes exactly one 360° turn over [0, h], so a flat
// rectangular profile becomes a ribbon twisted once. With `Fixed` or
// `Torsion` the profile wouldn't rotate along a straight spine — visible
// twist is therefore proof that Auxiliary is in effect.
// ==================== main: side-by-side layout ====================
//
// Each builder places its component at its final world position (screw at
// origin, U-pipe at x=6, ribbon at x=12) and applies its color, so main
// just concatenates them.
Shell
Demo of Solid::shell:
//! Demo of `Solid::shell`:
//! - Cube: remove top face, offset inward → open-top container
//! - Sealed cube: empty open_faces → solid with an internal void (outer skin
//! + reversed inner shell)
//! - Torus: bisect with a half-space to introduce planar cut faces, then
//! shell using those cut faces as the openings → thin-walled half-ring
//! with both cross-sections exposed
use ;
Bspline
use ;
use TAU;
// 2 field-period stellarator-like torus.
// `Solid::bspline` is fed a 2D control-point grid to build a periodic B-spline solid.
// Every variation below is invariant under phi → phi+π (or shifts by a multiple
// of 2π), so the resulting shape has 180° rotational symmetry around the Z axis:
// a(phi) = 1.8 + 0.6 * sin(2φ) radial semi-axis
// b(phi) = 1.0 + 0.4 * cos(2φ) Z semi-axis
// psi(phi) = 2 * phi cross-section twist (2 turns per loop)
// z_shift(phi) = 1.0 * sin(2φ) vertical undulation
const M: usize = 48; // toroidal (U) — must be even for 180° symmetry
const N: usize = 24; // poloidal (V) — arbitrary
const RING_R: f64 = 6.0;
Fillet
Demo of Solid::fillet_edges:
//! Demo of `Solid::fillet_edges`:
//! - All 12 cube edges filleted uniformly (rounded cube)
//! - Only top 4 edges filleted (soft top, sharp base)
//! - Cylinder top circular edge filleted (coin shape)
use ;
Chamfer
Demo of Solid::chamfer_edges — mirror of 10_fillet.rs using bevels:
//! Demo of `Solid::chamfer_edges` — mirror of `10_fillet.rs` using bevels:
//! - All 12 cube edges chamfered uniformly (beveled cube)
//! - Only top 4 edges chamfered (soft top, sharp base)
//! - Cylinder top circular edge chamfered (coin with beveled rim)
use ;
The Type Map
Three concrete shape types and two trait umbrellas form the whole public surface:
Edge ── single 3D curve ┐
Face ── trimmed 3D surface │ concrete BRep handles
Solid ── connected closed body ┘
Wire ── trait carrying methods on Edge / Vec<Edge> / [Edge; N]
Compound ── trait carrying methods on Solid / Vec<Solid> / [Solid; N]
On a single Solid or single Edge, every method is reachable inherently —
no trait import needed:
# use ;
let s = cube.rotate_z.translate;
let v = s.volume;
On a Vec<Solid> or [Solid; N], the same operations live behind the
Compound trait. A single use cadrum::Compound; brings them into scope
on the collection — including the spatial transforms, which on collections
distribute element-wise:
use ;
let parts: = vec!;
let shifted = parts.translate;
let total = shifted.volume; // Σ per-element volumes
let bbox = shifted.bounding_box; // union AABB
Vec<Edge> plays the equivalent role for wires (open or closed polylines
made of edges) under Wire. There is no separate Wire type — an ordered
Vec<Edge> is a wire, and sweep / loft / extrude all take any
IntoIterator<Item = &Edge>.
Spatial transforms across the whole hierarchy
The transform family — translate, rotate, rotate_x / _y / _z,
scale, mirror, align_x / _y / _z — is implemented identically on
every shape and every collection. The same method name and signature works
on:
- a single
Solid—cube.rotate_z(angle) - a single
Edge—circle.translate(offset) Vec<Solid>/[Solid; N]viaCompound— element-wiseVec<Edge>/[Edge; N]viaWire— element-wise
use ;
use FRAC_PI_4;
let s: Solid = sphere.translate;
let e: Edge = circle?.rotate_x;
let v_s: = vec!.translate;
let v_e: = polygon?.rotate_z;
# Ok::
On Solid / Edge themselves the methods are inherent (no import
required); on collections use cadrum::Compound; / use cadrum::Wire;
brings them into scope.
Working with Wires
Wire constructors return either a single Edge or Vec<Edge> depending on
what is natural for the curve:
use ;
// Single-edge primitives → Edge
let line = line?;
let arc = arc_3pts?;
let circle = circle?;
let helix = helix?;
// Multi-edge primitive → Vec<Edge>
let square = polygon?;
// Free-form curve → Edge (single B-spline)
let curve = bspline?;
# Ok::
Either shape feeds Solid::extrude, Solid::sweep, or Solid::loft
uniformly because they take IntoIterator<Item = &Edge>:
# use ;
# let circle = circle?;
# let square: = vec!;
let s1 = extrude?;
let s2 = extrude?;
# Ok::
Pass a single edge as &[edge] rather than relying on a sugar that lets
&edge adapt — the slice form keeps the "this function consumes a
collection" intent visible at the call site.
Booleans and Topology History
Boolean operations return Vec<Solid> because a subtraction or
intersection can split into several disjoint pieces. Each result solid
carries a Solid::iter_history log of [post_id, src_id] pairs — every
face in the result remembers which face of which input it came from. That
makes face selectors stable across boolean stages:
use ;
let block = cube;
let hole = cylinder
.translate;
let drilled = block.subtract?;
let from_block: = drilled
.iter_history
.filter // faces inherited from `block`
.map
.collect;
# Ok::
See examples/08_shell.rs for a worked end-to-end use of this mechanism
(shelling a torus through cut faces produced by a half-space subtraction).
Mesh and Visual Output
Solid::mesh flattens any number of solids into a single triangle Mesh
using OCCT's BRep mesher (BRepMesh_IncrementalMesh). From a Mesh,
Mesh::write_stl emits a standard binary STL and Mesh::write_svg
renders a hidden-line-removed 2D projection — handy for documentation and
quick visual diffs:
use ;
let parts = ;
let mesh = mesh?;
mesh.write_stl?;
mesh.write_svg?;
# Ok::
Errors
Every fallible operation returns Result<T, Error> with Error
enumerating the failure modes (Error::SweepFailed,
Error::FilletFailed, Error::InvalidEdge, etc.). Variants that need
detail carry a String payload identifying which constructor or parameter
combination tripped OCCT, so panics are reserved for true logic bugs.
Features
color(default): EnablesSolid::colorand per-face colormap propagation through STEP / BRep / STL / SVG I/O via OCCT's XDE document model. Disable for a smaller binary if shape color is irrelevant.source-build: When the prebuilt-binary cache is empty, fall back to building OCCT from upstream sources via CMake instead of failing. Required on targets without a published prebuilt (anything outside the four-way Linux / Windows × x86_64 / aarch64 table). Pullscmakein as a build-dep.
Showcase
A browser-based configurator that lets you tweak dimensions of a STEP model and get an instant 3D preview and quote. cadrum powers the parametric reshaping and meshing on the backend.
License
This project is licensed under the MIT License.
Compiled binaries include OpenCASCADE Technology (OCCT), which is licensed under the LGPL 2.1. Users who distribute applications built with cadrum must comply with the LGPL 2.1 terms. Since cadrum builds OCCT from source, end users can rebuild and relink OCCT to satisfy this requirement.