@barocss/cad
The CAD kernel. It is a boundary-representation (B-rep) modeller with NURBS curves and surfaces, written in TypeScript, with no WebAssembly and no external geometry library. The studio, the document model and the agent's tools all run on it.
Run @barocss/cad/api live in your browser
import { part } from '@barocss/cad/api';
const flange = part('NEMA17')
.box({ w: 48, d: 48, h: 6 })
.hole(b => b.faces().facing('+z'), { d: 22, x: 24, y: 24 })
.hole(b => b.faces().facing('+z'), { d: 3.5, x: 8.6, y: 8.6 })
.hole(b => b.faces().facing('+z'), { d: 3.5, x: 39.4, y: 8.6 })
.hole(b => b.faces().facing('+z'), { d: 3.5, x: 8.6, y: 39.4 })
.hole(b => b.faces().facing('+z'), { d: 3.5, x: 39.4, y: 39.4 })
.fillet(b => b.edges().along('z'), { radius: 4 });
const stepText = flange.step(); // ISO 10303-21 STEP AP242Click Run Pure-TS Kernel ▶ to evaluate this exact B-Rep solid directly in your browser (no WASM, no server) and export STEP AP242.
- Exact surfaces. Planes, cylinders, cones, tori and B-spline surfaces stay exact through Booleans, fillets and offsets.
- STEP is written from them.
- Mass properties are integrated over them. The few curved bodies the quadrature can't integrate fall back to the tessellation, and their result says so (
method: 'tessellated'), with an error bound.
- Stable names. A part is a list of features. Faces are named by the feature and role that made them, such as
p.toporh.side0. Those names are kept through every later feature, and through parameter edits that keep the topology. - Correct or refused. An operation gives a valid body, or a
CadPartErrorsaying why it can't. It doesn't return an approximate body. See B-rep, robustness and refusals. - Meshes too. Imported STL, OBJ and tessellated STEP bodies are faceted B-reps, with their own Booleans, holes and splits.
Status and install
Use @barocss/cad from the repository workspace.
- From the repository. Use it from a checkout, as a workspace package.
- TypeScript sources. Its entry point is the TypeScript source itself (
./src/index.ts). Use it with a bundler or a TypeScript-aware runtime; there is no build step to run first. - Two entry points.
@barocss/cadis the core: the part document, its evaluation, and everything else the kernel does.@barocss/cad/apiis the object facade over it, for writing parts in code.
import { part } from '@barocss/cad/api'; // the object facade
import { evaluateCadPart, CadPartError, checkOutput, reevaluate, massPropertiesOf } from '@barocss/cad'; // the core: the part document and its evaluationA part in code
A part is a JSON document: its units, its parameters, and its features in order. The facade's part() writes one, verb by verb, and .doc is the document. evaluateCadPart evaluates a document and returns a snapshot. Its outputs are the bodies no later feature consumes.
// A 60 × 40 × 8 plate with a Ø8 hole through it.
const plate = part('plate')
.box({ w: 60, d: 40, h: 8 })
.hole(body => body.faces().facing('+z'), { d: 8, x: 30, y: 20 });
const doc = plate.doc; // the part document: its features in order
assert.deepEqual(doc.features.map(f => [f.id, f.kind]), [['sketch1', 'sketch'], ['extrude1', 'extrude'], ['hole1', 'hole']]);
const snapshot = evaluateCadPart(doc); // what the facade evaluates
const [body] = snapshot.outputs; // the parts no later feature consumesThe facade gives the results as plain data, in millimetres and grams. Each output of the snapshot also checks itself:
const mass = plate.mass({ density: 2.7 }); // aluminium: mm³, g, mm, g·mm²
assert.ok(Math.abs(mass.volume - (60 * 40 * 8 - Math.PI * 16 * 8)) < 1e-6);
assert.ok(Math.abs(mass.mass - mass.volume * 2.7e-3) < 1e-9);
assert.equal(checkOutput(body).valid, true); // strict B-rep validity, on the core's output
assert.match(plate.step(), /^ISO-10303-21;/);
const stl = plate.stl(); // binary STL of the display tessellation
assert.equal(stl.length, 84 + 50 * new DataView(stl.buffer, stl.byteOffset).getUint32(80, true));These samples run in apps/library-docs (pnpm test:samples), against the package's sources.
Where next
- Modeling API: the part document, evaluating, outputs and tessellation.
- B-rep, robustness and refusals: what the kernel guarantees, and what it refuses.
- Exchange formats: STEP, IGES, STL and DXF.
- @barocss/cad-model: the studio's document model on top of the kernel.