# brepjs > Web CAD library with a layered architecture and pluggable kernel abstraction layer (supports OpenCascade and brepkit WASM backends). Create 2D sketches, extrude/revolve/loft/sweep into 3D solids, apply booleans/fillets/chamfers/shells, query topology, measure geometry, heal shapes, manage assemblies, and import/export STEP, STL, IGES, glTF, DXF, 3MF, OBJ, and SVG. ## API Reference & Discoverability - **Hosted API docs**: https://andymai.github.io/brepjs/ — searchable TypeDoc reference for all exports - **Function lookup table**: `docs/function-lookup.md` — alphabetical index mapping every symbol to its sub-path - **Which API guide**: `docs/which-api.md` — choosing between Sketcher, functional API, Drawing ## Authoring with an AI Agent (brepjs-cad) If you are an LLM generating brepjs CAD, use `brepjs-cad` — a CLI that runs your model on a real kernel and returns a deterministic report. **Judge a part by the report, never by how the code reads.** Published on npm as `brepjs-cad`; the companion Claude Code skill installs via `/plugin marketplace add andymai/brepjs` then `/plugin install brepjs@brepjs`. Install the runtime where `brepjs` resolves (it also bundles its own `brepjs` + `occt-wasm`, so it runs standalone): ```bash npm i -D brepjs-cad ``` A model is a `.brep.ts` module: a default-exported function returning a shape (or `Result`), optionally with an `expected` block that the CLI asserts: ```ts import { box } from 'brepjs'; export const expected = { volume: 8000, tolerancePct: 1 }; // optional: volume|area|bounds + tolerancePct export default () => box(40, 20, 10, { centered: true }); ``` To let a caller change a dimension, declare it with `defineParams` and take the values as the argument. Keep the default argument, so the part also builds when it is called with no argument: ```ts import { box, defineParams } from 'brepjs'; export const params = defineParams({ width: { type: 'number', default: 40, min: 20, max: 120, unit: 'mm' }, // also: boolean | string | choice (options: [...]) }); export const expected = (p = params.values) => ({ volume: p.width * 20 * 10, tolerancePct: 1 }); // a function of the values export default (p = params.values) => box(p.width, 20, 10, { centered: true }); ``` `brep verify part.brep.ts --param width=80` builds it at that size (repeat `--param` for more values; the report lists them as `params`; a bad value gives `PARAM_OUT_OF_RANGE`, `PARAM_INVALID_VALUE` or `PARAM_UNKNOWN`). `brep publish part.brep.ts --out site` writes a static page with one control for each parameter and STEP/3MF/STL downloads; a published part can import only `brepjs` and its own files, and it releases its intermediate shapes with `using`, because the page builds the part again for each change. Guide: https://brepjs.dev/agent/publish The loop, in order: brief → author `.brep.ts` → declare `expected` → `brep verify part.brep.ts --check --json report.json` → review `--snapshot shots/` → repair the smallest responsible section → `--step part.step` and hand off. Use `npx -y -p brepjs-cad brep …` if not installed. The report (stdout JSON) is the source of truth: - `ok` — `true` only if valid AND every assertion passes. `false` → not done. - `checks` — kernel validity (manifold solid, positive volume). A failed check = broken geometry. - `measurements` — measured `volume`/`area`/`bounds`. - `assertions` — declared intent vs reality. A failed assertion = valid-but-wrong (off dimensions). - `hints` — actionable fix + next step keyed on an error `code`; read before guessing. - `errorInfos` — raw `{code,message}` failures (authoring/kernel/export). Cite the `code` when repairing. CLI subcommands: `verify ` (default; flags `--check`, `--json`, `--step`, `--glb`, `--snapshot `, `--serve`, `--param name=value`), `init ` (scaffold), `watch `, `export ` (`--step`/`--glb`/`--stl`/`--all` behind a validity gate), `publish --out ` (static page; `--title`, `--force`), `measure [b]`, `diff `. Exits non-zero when not `ok`. Reliable first-try: primitives, booleans, 2D sketch → extrude, fillet/chamfer, shell/offset, transforms. Advanced (verify carefully, expect iteration): sweeps, lofts, revolves, multi-section, assemblies, text. Author parts in ESM (a CJS project needs `"type":"module"` or a `.mts` file); Node 24+ strips the part's types natively. ## Setup & Initialization Install: ```bash npm install brepjs occt-wasm ``` ### Quick start (zero ceremony, ESM only) ```typescript import { box } from 'brepjs/quick'; const myBox = box(10, 10, 10); // just works ``` `brepjs/quick` auto-initializes the WASM kernel via top-level await. No setup code needed. Requires ESM (top-level await is incompatible with CJS). ### Standard (explicit registration) ```typescript import { OcctKernel } from 'occt-wasm'; import { registerKernel, OcctWasmAdapter } from 'brepjs'; const kernel = await OcctKernel.init(); registerKernel('occt-wasm', OcctWasmAdapter.fromKernel(kernel)); ``` The kernel must be registered once before using any brepjs API. `await init()` (from `brepjs`) does the same in one call and resolves to the kernel id. From CommonJS use `await init()`: it loads occt-wasm with a dynamic `import()`, while `require('occt-wasm')` fails. ### Custom WASM location `OcctKernel.init()` finds `occt-wasm.wasm` next to its JS module via `import.meta.url`. To serve the binary yourself (CDN, custom path, pre-fetched bytes), pass `wasm` (`string | URL | ArrayBuffer | Uint8Array`): ```typescript import { OcctKernel } from 'occt-wasm'; import { registerKernel, OcctWasmAdapter, box } from 'brepjs'; const kernel = await OcctKernel.init({ wasm: '/wasm/occt-wasm.wasm' }); registerKernel('occt-wasm', OcctWasmAdapter.fromKernel(kernel)); const b = box(10, 10, 10); ``` `brepkit-wasm` is an experimental alternative kernel; `init()` falls back to it when occt-wasm is not installed. See `docs/kernel-swap.md` for registering and switching kernels. ### Quick reference See `docs/cheat-sheet.md` for a single-page reference covering all common operations (primitives, booleans, transforms, fillets, measurement, export, memory management, error handling). Works in both Node.js and browsers. For browser setup with Vite, see `docs/getting-started.md` (Browser Setup section). Try the [interactive playground](https://brepjs.dev/playground) for live experimentation. ## API Style brepjs uses a functional API with branded types (`Solid`, `Face`, `Edge`, etc.) and `Result` for error handling. Functions like `fuse()`, `translate()`, `fillet()` are pure — they never dispose inputs. Use `unwrap()` to extract values from `Result`, or `isOk()`/`isErr()` to check success. ### Immutability All brepjs operations are immutable — they return new shapes and never modify the original: ```typescript import { box, translate, rotate, fuse, cylinder, measureVolume, unwrap } from 'brepjs'; const original = box(30, 20, 10); const moved = translate(original, [100, 0, 0]); const rotated = rotate(moved, 45, { axis: [0, 0, 1] }); const combined = unwrap(fuse(original, cylinder(5, 15))); // original is unchanged after all operations console.log(unwrap(measureVolume(original))); // still the original box volume console.log(unwrap(measureVolume(moved))); // same volume, different position console.log(unwrap(measureVolume(rotated))); // same volume, different orientation ``` This means you can safely chain multiple transformations while preserving access to intermediate and original shapes. Each call produces a new independent shape. ### Branded Shape Types Shape types are lightweight branded handles (not classes) with a phantom `D extends Dimension` parameter for compile-time 2D/3D safety (default `'3D'`): - `Vertex`, `Edge`, `Wire`, `Face`, `Compound` — dimension-parameterized (default `'3D'`) - `Shell`, `Solid`, `CompSolid` — always 3D (no dimension parameter) - `AnyShape` — union of all shape types - `Shape3D` — union of Shell | Solid | CompSolid | Compound<'3D'> - `Shape1D` — Edge | Wire #### Validity Brands Layered on top of base types to encode topological invariants at compile time: - `ClosedWire`: wire proven to form a closed loop. - `PlanarWire`, `PlanarFace`: wire/face proven planar. `face()` requires `ClosedWire & PlanarWire` and returns `OrientedFace & PlanarFace`. - `OrientedFace`: face with consistent normal. `extrude()` and `revolve()` require `OrientedFace & PlanarFace`. Both also take an open `Wire` and return a shell. - `ManifoldShell` — shell proven to be watertight. - `ValidSolid`: solid passing BRepCheck validation. Returned by `box()`, `cylinder()`, etc. Required by `fillet()`, `chamfer()`, `shell()`, `offset()`; `Sketch.extrude()` returns plain `Shape3D`, which they reject at type check. Smart constructors prove validity at runtime: `closedWire(w)`, `planarWire(w)`, `orientedFace(f)`, `planarFace(f)`, `manifoldShell(s)`, `validSolid(s)` → `Result` (check with `isOk`, extract with `unwrap` — there is no `.valid`/`.value` field). Type guards narrow in-place: `isClosedWire(w)`, `isPlanarWire(w)`, `isOrientedFace(f)`, `isPlanarFace(f)`, `isManifoldShell(s)`, `isValidSolid(s)`. Convenience: `wireLoop(edges)` assembles edges and verifies closure → `Result`. All shapes have `.wrapped` (kernel handle) and `.delete()`. Use `using` syntax for auto-cleanup. Also exported: `Sketch`, `CompoundSketch`, `Sketches` (multi-profile container), `Drawing`, `DrawingPen`, `Blueprint`, `CompoundBlueprint`, `Blueprints` ### Type Conversion Helpers ```typescript import { toVec3, toVec2, resolveDirection } from 'brepjs'; toVec3([x, y]): Vec3 // 2D → 3D (z=0) toVec3([x, y, z]): Vec3 // identity toVec2([x, y, z]): Vec2 // 3D → 2D (drop z) resolveDirection('X'): Vec3 // → [1, 0, 0] resolveDirection('Y'): Vec3 // → [0, 1, 0] resolveDirection('Z'): Vec3 // → [0, 0, 1] resolveDirection([a, b, c]): Vec3 // identity ``` ### Shape Serialization ```typescript import { toBREP, fromBREP, unwrap } from 'brepjs'; const brep = unwrap(toBREP(shape)); // Result: shape → BREP string const restored = unwrap(fromBREP(brep)); // Result: BREP string → shape ``` ## Drawing & Sketching (Primary Entry Point) Most workflows start by drawing a 2D profile, then extruding or revolving it into 3D. ### DrawingPen (2D builder) ```typescript import { draw, drawRectangle, drawCircle, drawPolysides } from 'brepjs'; // Freeform drawing with a pen const shape2d = draw() .movePointerTo([0, 0]) .lineTo([10, 0]) .lineTo([10, 5]) .hLine(-5) .vLine(5) .close(); // Canned shapes const rect = drawRectangle(100, 50); const circle = drawCircle(25); const hex = drawPolysides(10, 6); ``` DrawingPen methods: - `movePointerTo(point)` — Move without drawing - `lineTo(point)`, `line(dx, dy)`, `hLine(d)`, `vLine(d)` — Straight lines - `hLineTo(xPos)`, `vLineTo(yPos)` — Absolute line targets - `polarLine(distance, angle)`, `polarLineTo(point)` — Polar coordinates - `tangentLine(distance)` — Tangent continuation - `threePointsArcTo(end, innerPoint)`, `threePointsArc(dx, dy, viaX, viaY)` — 3-point arcs - `tangentArcTo(end)`, `tangentArc(dx, dy)` — Tangent arcs - `sagittaArcTo(end, sagitta)`, `sagittaArc(dx, dy, sagitta)` — Sagitta arcs - `vSagittaArc(d, sagitta)`, `hSagittaArc(d, sagitta)` — Vertical/horizontal sagitta arcs - `bulgeArcTo(end, bulge)`, `bulgeArc(dx, dy, bulge)` — Bulge arcs - `vBulgeArc(d, bulge)`, `hBulgeArc(d, bulge)` — Vertical/horizontal bulge arcs - `ellipseTo(end, hRadius, vRadius, rotation?, longAxis?, sweep?)` — Elliptical arcs - `halfEllipseTo(end, vRadius, sweep?)` — Half-ellipse arcs - `bezierCurveTo(end, controlPoints)` — General Bezier - `quadraticBezierCurveTo(end, control)` — Quadratic Bezier - `cubicBezierCurveTo(end, c1, c2)` — Cubic Bezier - `smoothSplineTo(end, config?)`, `smoothSpline(dx, dy, config?)` — Smooth splines - `customCorner(radius, mode?)` — Round/bevel the corner at the current vertex (call before the next segment). `mode`: `'fillet'` (default) or `'chamfer'`. (The pen has no bare `.fillet`/`.chamfer`; those are Drawing methods, after `.close()`/`.done()`.) - `close()` — Close the path and return a Drawing - `closeWithMirror()` — Close by mirroring the path - `closeWithCustomCorner(radius, mode?)` — Close with a fillet or chamfer at the join. `mode`: `'fillet'` (default) or `'chamfer'` - `done()` — Leave path open and return a Drawing Drawing factory functions: - `draw(origin?)` — Start a new pen at optional `[x, y]` point - `drawRectangle(w, h, r?)` — Rectangle centered at origin, optional corner radius `r: number` or `{rx?, ry?}` - `drawRoundedRectangle(w, h, r?)` — Alias for `drawRectangle` - `drawCircle(radius)` — Circle centered at origin (built from two sagitta arcs) - `drawSingleCircle(radius)` — Circle as a single curve primitive (more efficient, less compatible with corner ops) - `drawEllipse(major, minor)` — Ellipse centered at origin (built from two half-ellipse arcs) - `drawSingleEllipse(major, minor)` — Ellipse as a single curve primitive - `drawPolysides(radius, sides, sagitta?)` — Regular polygon. `sagitta` bows the sides (positive = inward, negative = outward; `sketchPolysides` uses the opposite sign) - `drawText(text, {startX?, startY?, fontSize?, fontFamily?})` — Text outline (requires `loadFont()` first) - `drawPointsInterpolation(points, approxConfig?, {closeShape?})` — BSpline through Point2D[]. `approxConfig`: `{tolerance?, degMax?, degMin?, smoothing?}` - `drawParametricFunction(fn, {pointsCount?, start?, stop?, closeShape?}, approxConfig?)` — Parametric curve `fn: (t: number) => Point2D` - `drawProjection(shape, camera?)` — Project 3D shape to 2D. `camera`: `ProjectionPlane | Camera` (default `'front'`). Returns `{visible: Drawing, hidden: Drawing}` - `drawFaceOutline(face)` — Extract face outer wire as a 2D Drawing - `deserializeDrawing(data)` — Reconstruct from string produced by `drawing.serialize()` Drawing instance methods: - `clone()`, `serialize()` — Copy and serialization - `boundingBox` — Get `BoundingBox2d` - `repr` — String representation of the drawing - `blueprint` — Access the underlying `Blueprint` (throws if compound) - `translate(dx, dy)` or `translate([dx, dy])` — Move the drawing - `rotate(angle, center?)` — Rotate in degrees around optional center point - `scale(factor, center?)` — Uniform scale around optional center - `mirror(dirOrCenter, origin?, mode?)` — Mirror. `mode`: `'center'` (default) or `'plane'` - `stretch(ratio, direction, origin)` — Non-uniform scaling along a direction - `cut(other)`, `fuse(other)`, `intersect(other)` — 2D boolean operations (return new Drawing) - `fillet(radius, filter?)`, `chamfer(radius, filter?)` — Corner treatments. `filter`: `(c: CornerFinderFn) => CornerFinderFn` - `offset(distance, config?)` — Offset all curves by distance - `approximate('svg', options?)` — Approximate curves for SVG compatibility - `sketchOnPlane(plane?, origin?)` — Convert to 3D Sketch/Sketches. Returns `SketchInterface | Sketches` - `sketchOnFace(face, scaleMode)` — Project onto a face. `scaleMode`: controls UV mapping behavior - `punchHole(shape, faceFinder, {height?, origin?, draftAngle?}?)` — Punch this 2D profile through a 3D shape - `toSVG(margin?)` — Full SVG string with `` tag - `toSVGViewBox(margin?)` — SVG string with viewBox attribute - `toSVGPaths()` — Array of SVG path `d` strings ### Sketcher (3D sketching on a plane) ```typescript import { Sketcher, sketchCircle, sketchRectangle } from 'brepjs'; // Sketch on XY plane, then extrude const box = new Sketcher('XY') .movePointerTo([-5, -5]) .lineTo([5, -5]) .lineTo([5, 5]) .lineTo([-5, 5]) .close() .extrude(10); // Canned sketch shortcuts const cylinder = sketchCircle(10).extrude(20); const prism = sketchRectangle(30, 20).extrude(15); // Sketching on offset planes const top = sketchCircle(5, { plane: 'XY', origin: 20 }); // XY plane at Z=20 const angled = sketchCircle(5, { plane: myCustomPlane }); // custom Plane object ``` Sketcher has the same drawing methods as DrawingPen (both extend `BaseSketcher2d`), but its sagitta and bulge arcs bow the opposite way. Close with `.close()`, `.closeWithMirror()`, `.closeWithCustomCorner()`, or `.done()` (open path) to produce a `Sketch`. Canned sketch functions — all accept an optional last argument `PlaneConfig = { plane?: PlaneName | Plane, origin?: PointInput | number }`. When `origin` is a number, it offsets the named plane along its normal by that distance: - `sketchCircle(radius, planeConfig?)` — Circle sketch - `sketchRectangle(w, h, planeConfig?)` — Rectangle sketch - `sketchRoundedRectangle(w, h, r?, planeConfig?)` — Rounded rectangle. `r`: `number` or `{rx?, ry?}` - `sketchPolysides(radius, sides, sagitta?, planeConfig?)` — Regular polygon - `sketchEllipse(xRadius?, yRadius?, planeConfig?)` — Ellipse (defaults: xRadius=1, yRadius=2) - `sketchHelix(pitch, height, radius, center?, dir?, lefthand?)` — Helix curve (no PlaneConfig; uses center/dir directly) - `sketchFaceOffset(face, offset)` — Offset a face boundary (negative = inward, positive = outward) - `sketchParametricFunction(fn, planeConfig?, {pointsCount?, start?, stop?}?, approxConfig?)` — Parametric curve on plane - `polysideInnerRadius(outerRadius, sidesCount, sagitta?)` — Helper: compute inner radius of a polyside Sketch instance methods: - `extrude(distance, options?)` — Options: `{extrusionDirection?, extrusionProfile?, twistAngle?, origin?}` - `revolve(axis?, {origin?})`: full 360° revolve around `axis` (default `[0, 0, 1]`) through `origin` (default: the sketch plane's origin, not the world origin) - `loftWith(otherSketches, config?, returnShell?)` — Loft between profiles - `sweepSketch(sketchOnPlane, config?)` — Sweep along a spine - `face()` — Convert to face - `wires()` — Get wire(s) - `clone()`, `delete()` — Copy and cleanup ### Drawing to 3D Extrude a 2D rectangular sketch into a 3D solid: ```typescript import { drawRectangle, drawCircle, drawingCut, drawingToSketchOnPlane, shape } from 'brepjs'; // Simple rectangle → extrude to box const rect = drawRectangle(50, 30); const sketch = rect.sketchOnPlane('XY'); const solid = sketch.extrude(20); // 20mm height → Shape3D // Complex profile: rectangle with circular cutout → extrude const profile = drawingCut(drawRectangle(50, 30), drawCircle(8).translate([10, 0])); const profileSketch = drawingToSketchOnPlane(profile, 'XY'); const complexSolid = shape(profileSketch.face()).extrude(20).val; // Or use the sketchRectangle shortcut import { sketchRectangle } from 'brepjs'; const quickBox = sketchRectangle(50, 30).extrude(20); ``` Plane names: `'XY'`, `'XZ'`, `'YZ'`, `'ZX'`, `'YX'`, `'ZY'`, `'front'`, `'back'`, `'top'`, `'bottom'`, `'left'`, `'right'` ## 3D Operations ### Extrude & Revolve ```typescript // Extrude a sketch into a solid const box = sketchRectangle(10, 10).extrude(20); // Twist extrude const twisted = sketchRectangle(10, 10).extrude(20, { twistAngle: 45 }); // Revolve 360° around axis (default [0, 0, 1]) through origin (default: the sketch plane's origin). // The profile must not cross the axis: a circle centered on it fails with REVOLVE_FAILED. const torus = sketchCircle(2, { plane: 'XZ', origin: [10, 0, 0] }).revolve([0, 0, 1], { origin: [0, 0, 0] }); ``` Functional API: - `extrude(face: OrientedFace & PlanarFace, height: number | Vec3): Result`: extrude a face (what `face()` returns). Pass a number for Z-axis extrusion or a Vec3 for arbitrary direction (vector length = distance) - `revolve(face: OrientedFace & PlanarFace, { axis?, at?, angle? }?): Result`: revolve a face. `at` default `[0,0,0]`, `axis` default `[0,0,1]` (Z). **`angle` is in RADIANS** and defaults to a full turn (`2 * Math.PI`); `rotate()` and pattern `fullAngle` take degrees. - `sweep(wire, spine, config?, shellMode?): Result` — Sweep profile along spine - `complexExtrude(wire, center, normal, profileShape?, shellMode?): Result`: extrude with scaling profile (the tuple is returned when `shellMode` is true) - `twistExtrude(wire, angleDeg, center, normal, profileShape?, shellMode?): Result`: twist extrude, `angleDeg` in degrees - brepkit has no scaled sweep: `complexExtrude` and `twistExtrude` with a profile, and `sweep` with a `law`, return `UNSUPPORTED_CAPABILITY` there - `supportExtrude(wire, center, normal, support): Result` — Extrude constrained to a support surface `ExtrusionProfile`: `{ profile?: 's-curve' | 'linear', endFactor?: number }` — Controls scaling along extrusion path. `endFactor` 1 = same size, 0.5 = half size at end. `SweepOptions`: `{ frenet?, auxiliarySpine?, law?, transitionMode?: 'right' | 'transformed' | 'round', withContact?, support?, forceProfileSpineOthogonality? }` ### Loft & Sweep ```typescript import { sketchCircle, sketchHelix, Sketcher } from 'brepjs'; // Loft between profiles const bottom = sketchCircle(10); const top = sketchCircle(5, { plane: 'XY', origin: 20 }); // XY plane offset to Z=20 const lofted = bottom.loftWith([top]); // Sweep a profile along a spine (sweepSketch takes a function that builds the profile) const spine = sketchHelix(10, 50, 20); const coil = spine.sweepSketch((plane, origin) => new Sketcher(plane).movePointerTo([-2, -2]).lineTo([2, -2]).lineTo([2, 2]).lineTo([-2, 2]).close() ); ``` Functional API: - `loft(wires, config?): Result` — Loft config: `{ruled?: boolean (default true), startPoint?: PointInput, endPoint?: PointInput, rails?: (Edge | Wire)[], start?: LoftEndCondition, end?: LoftEndCondition}`. Rails each pass through one vertex of every profile. An end condition is `'free'`, `'normal'`, `{kind: 'normal', weight?}` or `{kind: 'tangent', face, weight?}`. Rails and end conditions need the brepkit kernel - `sketchLoft(sketch, otherSketches, config?, returnShell?): Shape3D` - `sketchSweep(sketch, sketchOnPlane, sweepConfig?): Shape3D` - `sketchExtrude(sketch, height, config?): Shape3D` — Functional version of `sketch.extrude()` - `sketchRevolve(sketch, axis?, {origin?}?): Shape3D` — Functional version of `sketch.revolve()` - `sketchFace(sketch): Face` — Get the face from a closed sketch - `sketchWires(sketch): Wire` — Get the wire from a sketch CompoundSketch functions (for multi-contour profiles like text): - `compoundSketchExtrude(sketch, height, config?): Shape3D` - `compoundSketchRevolve(sketch, axis?, {origin?}?): Shape3D` - `compoundSketchFace(sketch): Face` - `compoundSketchLoft(sketch, other, loftConfig): Shape3D` Drawing functional API: - `drawingToSketchOnPlane(drawing, plane?, origin?): SketchInterface | Sketches` - `drawingFuse(a, b): Drawing` - `drawingCut(a, b): Drawing` - `drawingIntersect(a, b): Drawing` - `drawingFillet(drawing, radius, filter?): Drawing` - `drawingChamfer(drawing, radius, filter?): Drawing` - `translateDrawing(drawing, dx, dy): Drawing` or `translateDrawing(drawing, [dx, dy])` - `rotateDrawing(drawing, angle, center?): Drawing` - `scaleDrawing(drawing, factor, center?): Drawing` - `mirrorDrawing(drawing, dir, origin?, mode?): Drawing` ### Boolean Operations ```typescript import { box, cylinder, fuse, cut, intersect, unwrap } from 'brepjs'; const myBox = box(20, 20, 20); // ValidSolid, so the fillet/chamfer samples below accept it const hole = cylinder(5, 30, { at: [10, 10, -5] }); // through the middle const withHole = unwrap(cut(myBox, hole)); // Subtraction → Result const merged = unwrap(fuse(myBox, hole)); // Union → Result const common = unwrap(intersect(myBox, hole)); // Intersection → Result ``` Full boolean API: ```typescript import { fuse, cut, intersect, section, split, slice } from 'brepjs'; fuse(a, b, options?): Result cut(base, tool, options?): Result intersect(a: Shape3D, b: Shape3D, options?): Result section(shape, plane, {approximation?, planeSize?}?): Result split(shape, tools): Result slice(shape, planes, options?): Result ``` All boolean operations validate inputs before calling OCCT: null shapes return `VALIDATION` errors with code `NULL_SHAPE_INPUT` and a message identifying which operand was invalid. Boolean options: `{ optimisation?: 'none' | 'commonFace' | 'sameFace', simplify?: boolean, strategy?: 'native' | 'pairwise', signal?: AbortSignal }` Batch: `fuseAll(shapes, options?): Result`, `cutAll(base, tools, options?): Result` ### Fillet & Chamfer ```typescript import { fillet, chamfer, getEdges, edgeFinder, unwrap } from 'brepjs'; // Fillet all edges — returns Result const rounded = unwrap(fillet(myBox, getEdges(myBox), 2)); // brepkit kernel: set a corner back along its rounds, or close it with a G2 patch // `edges` are the three edges that meet at `vertex`, and each one needs a set-back // fillet(myBox, edges, 2, { corners: [{ vertex, setbacks: edges.map((edge) => ({ edge, distance: 3 })), corner: 'g2' }] }) // Fillet specific edges using edgeFinder const selective = unwrap(fillet(myBox, edgeFinder().ofLength(20).findAll(myBox), 2)); // Chamfer const chamfered = unwrap(chamfer(myBox, getEdges(myBox), 1)); ``` Additional functional API: ```typescript import { fillet, chamfer, chamferDistAngleShape } from 'brepjs'; fillet(shape, radius): Result // All edges fillet(shape, edges, radius): Result // Selected edges // edges: Edge[] | FinderFn | ShapeFinder // radius: number | [r1, r2] | (edge => number | [r1, r2] | null) // [r1, r2] may be 0 at one end, so the fillet runs out there; occt-wasm builds that only through variableFillet. // brepkit takes one chamfer distance per call: different distances from a callback return UNSUPPORTED_CAPABILITY there. chamfer(shape, distance): Result // All edges chamfer(shape, edges, distance): Result // Selected edges // distance: number | [d1, d2] | { distance, angle } | (edge => number | [d1, d2] | { distance, angle, face? } | null) // A callback's kept edges all get distances or all get { distance, angle } (mixing is INVALID_CHAMFER_DISTANCE). // Different { distance, angle } per edge need occt-wasm 5.9.0+; other kernels return UNSUPPORTED_CAPABILITY. // face (one of the two faces the edge joins, e.g. facesOfEdge(shape, edge)[0]) is where distance is set back and angle measured from; // it needs the same per-edge support, and a face the edge does not join is CHAMFER_ANGLE_FACE_NOT_ADJACENT. // Edges of one tangent-continuous chain must state the same chamfer; occt-wasm 5.11.0+ also accepts it stated from the other face. // Without face, which of an edge's faces takes d1 (or the { distance, angle } distance) is up to the kernel; occt-wasm and brepkit pick opposite faces on a box edge. chamferDistAngleShape(shape, edges, distance, angleDeg): Result ``` ### Shell (Hollow Out) ```typescript import { box, shell, faceFinder, unwrap } from 'brepjs'; // Remove the top face and shell to 1mm walls (returns Result here) const b = box(30, 20, 10); // parallelTo('Z') alone also matches the bottom face (normal sign is ignored); atDistance keeps only z = 10 const topFaces = faceFinder().parallelTo('Z').atDistance(10, [0, 0, 0]).findAll(b); const hollowed = unwrap(shell(b, topFaces, 1)); ``` `shell(shape, faces, thickness, {tolerance?}?): Result` All modifier operations (`fillet`, `chamfer`, `shell`, `offset`, `thicken`) validate that the input shape is not null before calling OCCT, returning `NULL_SHAPE_INPUT` validation errors. Error messages from OCCT failures include operation name and parameter metadata (edge count, radius, distance). ### Offset & Thicken ```typescript import { offset, thicken } from 'brepjs'; offset(shape: ValidSolid, distance, {tolerance?}?): Result thicken(shape: Face | Shell, thickness): Result ``` ### Transformations All transforms return new shapes — the original is never modified. ```typescript import { translate, rotate, mirror, scale } from 'brepjs'; translate(shape, [10, 0, 0]): T rotate(shape, angle, { at?, axis? }?): T mirror(shape, { normal?, at? }?): T scale(shape, factor, { center? }?): T ``` Sequential transforms (functional API): ```typescript import { box, translate, rotate, scale } from 'brepjs'; const b = box(30, 20, 10); const moved = translate(b, [50, 0, 0]); // translate first const rotated = rotate(moved, 45, { axis: [0, 0, 1] }); // then rotate const scaled = scale(rotated, 2); // then scale // b is unchanged; each step produces a new shape ``` Sequential transforms (wrapper API): ```typescript import { box, shape } from 'brepjs'; const result = shape(box(30, 20, 10)) .translate([50, 0, 0]) .rotate(45, { axis: [0, 0, 1] }) .scale(2) .val; ``` ### Patterns ```typescript import { linearPattern, circularPattern } from 'brepjs'; // 5 total copies along X with 10mm spacing (original + 4 copies, fused together) const row = linearPattern(shape, [1, 0, 0], 5, 10); // 8 copies in a full circle around Z axis (fused together) const ring = circularPattern(shape, [0, 0, 1], 8); // 6 copies over a 180-degree arc around Z (0, 36, ..., 180), centered at [10, 0, 0] const arc = circularPattern(shape, [0, 0, 1], 6, 180, [10, 0, 0]); ``` `linearPattern(shape, direction, count, spacing, options?)`: `count` includes the original. Returns `Result` (all copies fused). `circularPattern(shape, axis, count, fullAngle?, center?, options?)`: `fullAngle` default 360 degrees. A full 360 spaces copies `360 / count` apart; any other angle is an open arc whose first copy stays put and last lands at `fullAngle` (spacing `fullAngle / (count - 1)`). `center` default `[0,0,0]`. Returns `Result`. `rectangularPattern(shape, { xDir, xCount, xSpacing, yDir, yCount, ySpacing })`: 2D grid pattern. Returns `Result`. ### Compound Operations High-level operations that combine primitives with booleans: ```typescript import { drill, pocket, boss, mirrorJoin, drawRectangle, drawCircle, unwrap } from 'brepjs'; // Drill a hole into a shape const drilled = unwrap(drill(myBox, { at: [10, 10], radius: 3, depth: 15 })); // Cut a pocket (shaped recess) const pocketed = unwrap(pocket(myBox, { profile: drawRectangle(20, 10), depth: 5 })); // Add a boss (raised feature) const bossed = unwrap(boss(myBox, { profile: drawCircle(8), height: 10 })); // Mirror and fuse with the original const symmetric = unwrap(mirrorJoin(halfShape, { normal: [1, 0, 0] })); ``` ```typescript drill(shape, { at, radius, depth?, axis? }): Result pocket(shape, { profile, face?, depth }): Result boss(shape, { profile, face?, height }): Result mirrorJoin(shape, { normal?, at? }?): Result ``` ### Matrix Transforms ```typescript import { applyMatrix, composeTransforms, transformCopy } from 'brepjs'; applyMatrix(shape, matrix): Result // 4x4 row-major matrix or { linear, translation } composeTransforms(ops): ComposedTransform // Pre-compose multiple transforms transformCopy(shape, composed): T // Apply composed transform (fast for repeated use) ``` `TransformOp`: `{ type: 'translate', v: Vec3 } | { type: 'rotate', angle: number, axis?: Vec3, center?: Vec3 }` ## Shape Queries ### Shape Introspection ```typescript import { clone, describe, getBounds, isEmpty, isSameShape, isEqualShape, simplify, toBREP } from 'brepjs'; clone(shape): Result // Deep clone describe(shape): ShapeDescription // {kind, faceCount, edgeCount, wireCount, vertexCount, valid, bounds} getBounds(shape): Bounds3D // {xMin, xMax, yMin, yMax, zMin, zMax} isEmpty(shape): boolean // Check if shape is null/empty isSameShape(a, b): boolean // Same TShape + location, orientation may differ isEqualShape(a, b): boolean // Same TShape + location + orientation (not geometric equality) simplify(shape): Result toBREP(shape): Result // Serialize to BREP format getHashCode(shape): number ``` ### Topology Traversal ```typescript import { getEdges, getFaces, getWires, getVertices, vertexPosition } from 'brepjs'; getEdges(shape): Edge[] getFaces(shape): Face[] getWires(shape): Wire[] getVertices(shape): Vertex[] vertexPosition(vertex): Vec3 // Iterator versions (lazy) iterEdges(shape): Generator iterFaces(shape): Generator iterWires(shape): Generator iterVertices(shape): Generator ``` ### Adjacency Queries ```typescript import { facesOfEdge, edgesOfFace, wiresOfFace, verticesOfEdge, adjacentFaces, sharedEdges } from 'brepjs'; facesOfEdge(parent, edge): Face[] edgesOfFace(face): Edge[] wiresOfFace(face): Wire[] verticesOfEdge(edge): Vertex[] adjacentFaces(parent, face): Face[] sharedEdges(face1, face2): Edge[] ``` ### Immutable Finders (Functional API) ```typescript import { edgeFinder, faceFinder, wireFinder, vertexFinder } from 'brepjs'; // Composable, immutable chain — findAll returns T[] const verticalEdges = edgeFinder() .inDirection([0, 0, 1]) // Edge tangent parallel to Z (vertical edges, not top edges) .ofLength(10, tolerance?) .ofCurveType('LINE') .findAll(shape); const zFaces = faceFinder() .inDirection('Z') // Normal parallel to Z, either sign (top AND bottom faces); 'Z' = [0,0,1] .ofSurfaceType('PLANE') .ofArea(100, tolerance?) .findAll(shape); // faceFinder also has: .parallelTo(dir) (alias for inDirection(dir, 0)), .atDistance(dist, point?) const closedWires = wireFinder() .isClosed() .ofEdgeCount(4) .findAll(shape); const cornerVerts = vertexFinder() .atPosition([0, 0, 0], tolerance?) .nearestTo([10, 0, 0]) .withinBox([0, 0, 0], [10, 10, 10]) .findAll(shape); // findUnique — returns Result, errors if 0 or >1 matches const uniqueEdge = edgeFinder().ofLength(10).findUnique(shape); // Combinators (available on all finders) edgeFinder().not((f) => f.ofCurveType('LINE')).findAll(shape); edgeFinder().either([(f) => f.ofLength(10), (f) => f.ofLength(20)]).findAll(shape); edgeFinder().ofLength(10).or(edgeFinder().ofLength(20)).findAll(shape); // also .and(other), .negate() edgeFinder().when(edge => customPredicate(edge)).findAll(shape); edgeFinder().inList(knownEdges).findAll(shape); ``` ### cornerFinder (2D) ```typescript import { cornerFinder } from 'brepjs'; // Immutable builder pattern — each method returns a new finder cornerFinder().inList(points).find(blueprint); cornerFinder().atDistance(dist, point?).find(blueprint); cornerFinder().atPoint(point).find(blueprint); cornerFinder().inBox(corner1, corner2).find(blueprint); cornerFinder().ofAngle(angle).find(blueprint); cornerFinder().not(f => f.atPoint([0, 0])).find(blueprint); cornerFinder().either([f => f.atPoint([0, 0]), f => f.atPoint([1, 1])]).find(blueprint); cornerFinder().when(corner => customPredicate(corner)).find(blueprint); ``` ## Curve Operations ```typescript import { getCurveType, curveStartPoint, curveEndPoint, curvePointAt, curveTangentAt, curveLength, curveIsClosed, curveIsPeriodic, curvePeriod, getOrientation, flipOrientation, offsetWire2D, interpolateCurve, approximateCurve } from 'brepjs'; getCurveType(edge): CurveType // 'LINE' | 'CIRCLE' | 'ELLIPSE' | 'BEZIER_CURVE' | 'BSPLINE_CURVE' | ... curveStartPoint(shape): Vec3 curveEndPoint(shape): Vec3 curvePointAt(shape, t?): Vec3 // Point at parameter t (0-1) curveTangentAt(shape, t?): Vec3 // Tangent at parameter t curveLength(shape): number curveIsClosed(shape): boolean curveIsPeriodic(shape): boolean curvePeriod(shape): number getOrientation(shape): 'forward' | 'backward' flipOrientation(shape): Edge | Wire // Create curves from points interpolateCurve(points, {periodic?, tolerance?}?): Result approximateCurve(points, {tolerance?, degMin?, degMax?, smoothing?}?): Result // 2D wire offset offsetWire2D(wire, offset, kind?): Result // kind: 'arc' | 'intersection' | 'tangent' ``` ## Face Operations ```typescript import { getSurfaceType, faceGeomType, faceOrientation, flipFaceOrientation, uvBounds, pointOnSurface, uvCoordinates, normalAt, faceCenter, classifyPointOnFace, outerWire, innerWires, projectPointOnFace } from 'brepjs'; getSurfaceType(face): Result // 'PLANE' | 'CYLINDRE' | 'CONE' | 'SPHERE' | 'TORUS' | 'BSPLINE_SURFACE' | ... faceGeomType(face): SurfaceType faceOrientation(face): 'forward' | 'backward' flipFaceOrientation(face): Face uvBounds(face): { uMin, uMax, vMin, vMax } pointOnSurface(face, u, v): Vec3 // u, v normalized 0-1 across uvBounds uvCoordinates(face, point): [number, number] // raw surface parameters (not normalized) normalAt(face, point?): Vec3 faceCenter(face): Vec3 classifyPointOnFace(face, point, tolerance?): 'in' | 'on' | 'out' outerWire(face): Wire innerWires(face): Wire[] projectPointOnFace(face, point): Result<{ uv, point, distance }> ``` ## Measurements ```typescript import { measureVolume, measureArea, measureLength, measureDistance } from 'brepjs'; measureVolume(solid); // Result, unwrap() measureArea(face); // Result, unwrap() measureLength(edge); // Result, unwrap() measureDistance(shape1, shape2); // Result, unwrap() ``` Functional measurement API: ```typescript import { measureVolume, measureArea, measureLength, measureDistance, measureVolumeProps, measureSurfaceProps, measureLinearProps, createDistanceQuery, measureCurvatureAt, measureCurvatureAtMid } from 'brepjs'; measureVolumeProps(shape): Result<{ volume, mass, centerOfMass }> measureSurfaceProps(shape): Result<{ area, mass, centerOfMass }> measureLinearProps(shape): Result<{ length, mass, centerOfMass }> measureDistance(shape1, shape2): Result createDistanceQuery(ref): Result<{ distanceTo(other): Result, dispose(): void }> // brepkit measures only from a vertex, or between solids: any other pair (face to face, edge to // face, face to solid) returns UNSUPPORTED_CAPABILITY there, as checkInterference does. // Surface curvature (u, v are raw surface parameters, see uvBounds; not 0-1) measureCurvatureAt(face: OrientedFace, u, v): Result // plain Face: unwrap(orientedFace(f)) measureCurvatureAtMid(face): Result // CurvatureResult: { mean, gaussian, maxCurvature, minCurvature, maxDirection, minDirection } ``` All measurement functions return `Err` (code `NULL_SHAPE_INPUT`) on null shape input instead of throwing, e.g. `"measureVolumeProps: shape is a null shape"`. `measureVolume` takes a `Shape3D` (shell, solid, compsolid, or 3D compound); an open shell is `Ok(0)`. ### Interference Detection ```typescript import { checkInterference, checkAllInterferences } from 'brepjs'; checkInterference(shape1, shape2, tolerance?): Result // InterferenceResult: { hasInterference, minDistance, pointOnShape1, pointOnShape2 } checkAllInterferences(shapes, tolerance?): InterferencePair[] // InterferencePair: { i, j, result: InterferenceResult } ``` `checkInterference` returns a `Result` error with `kind: 'VALIDATION'` and `code: 'NULL_SHAPE_INPUT'` if either shape is null. `checkAllInterferences` propagates via `unwrap` (throws on null). ## Shape Healing & Validation ```typescript import { isValid, healSolid, healFace, healWire, heal, autoHeal } from 'brepjs'; isValid(shape): boolean healSolid(solid): Result healFace(face): Result healWire(wire, face?): Result heal(shape): Result // Auto-healing pipeline with diagnostics autoHeal(shape, options?): Result<{ shape, report: HealingReport }> // Options: { fixWires?: boolean (default true), fixFaces?: boolean (default true), // fixSolids?: boolean (default true), sewTolerance?: number, // fixSelfIntersection?: boolean (default FALSE — unlike the others) } // HealingReport: { isValid, alreadyValid, wiresHealed, facesHealed, solidHealed, steps, diagnostics } // alreadyValid: true when shape was valid before healing — distinguishes "nothing to fix" from "fix succeeded" ``` ## Import / Export ### STEP Files End-to-end import → modify → export: ```typescript import { importSTEP, exportSTEP, isSolid, shape, unwrap } from 'brepjs'; import { readFileSync, writeFileSync } from 'fs'; // Import a STEP file const stepBytes = readFileSync('input.step'); const stepBlob = new Blob([stepBytes]); const imported = unwrap(await importSTEP(stepBlob)); // Result // Modify the imported shape if (!isSolid(imported)) throw new Error('expected a solid'); // importSTEP returns a 2D|3D shape union const modified = shape(imported).fillet(2).translate([0, 0, 10]).val; // Export back to STEP const outputBlob = unwrap(exportSTEP(modified)); // Result writeFileSync('output.step', Buffer.from(await outputBlob.arrayBuffer())); ``` Export assembly with colors and names: ```typescript import { exportAssemblySTEP, unwrap } from 'brepjs'; const stepBlob = unwrap(exportAssemblySTEP([ { shape: body, color: '#3366cc', name: 'Body' }, { shape: lid, color: '#cc6633', name: 'Lid' }, ], { unit: 'MM' })); ``` Functional API: ```typescript import { importSTEP, exportSTEP, exportAssemblySTEP } from 'brepjs'; importSTEP(blob): Promise> // one-solid file: that Solid; assembly or several parts: a Compound of every part at its placement (names/colors not read) exportSTEP(shape): Result exportAssemblySTEP(shapes, { unit?, modelUnit? }?): Result // ShapeOptions: { shape, color?: hex string (default red), alpha?: 0-1, name? } // SupportedUnit: 'MM' | 'CM' | 'M' | 'INCH' | 'FT' (or lowercase 'mm' | 'cm' | 'm' | 'inch' | 'ft') ``` ### STL Files ```typescript import { importSTL, exportSTL } from 'brepjs'; importSTL(blob): Promise> exportSTL(shape, { tolerance?, angularTolerance?, binary? }?): Result ``` ### IGES Files ```typescript import { importIGES, exportIGES } from 'brepjs'; importIGES(blob): Promise> // several parts: a Compound of every part, like importSTEP exportIGES(shape): Result ``` ### glTF / GLB (with PBR materials) ```typescript import { exportGltf, exportGlb } from 'brepjs'; const json = exportGltf(mesh, options?); // glTF JSON string const binary = exportGlb(mesh, options?); // GLB ArrayBuffer // GltfExportOptions: { materials?: Map } // GltfMaterial: { name?, baseColor?: [r,g,b,a], metallic?: number, roughness?: number } ``` ### DXF ```typescript import { importDXF, exportDXF, blueprintToDXF } from 'brepjs'; importDXF(blob, options?): Promise> // DXFImportOptions: { layer?: string } exportDXF(entities, options?): string blueprintToDXF(drawing, options?): string // DXFExportOptions: { layer?, curveSegments? } // DXFEntity: { type: 'LINE', start, end, layer? } | { type: 'POLYLINE', points, closed?, layer? } ``` ### 3MF ```typescript import { importThreeMF, exportThreeMF } from 'brepjs'; importThreeMF(blob): Promise> exportThreeMF(mesh, options?): ArrayBuffer // ThreeMFExportOptions: { name?, unit?: 'micron' | 'millimeter' | 'centimeter' | 'meter' | 'inch' | 'foot' } ``` ### OBJ ```typescript import { importOBJ, exportOBJ } from 'brepjs'; importOBJ(blob): Promise> exportOBJ(mesh): string ``` ### SVG Import ```typescript import { importSVGPathD, importSVG } from 'brepjs'; importSVGPathD(pathD): Result // Single SVG path d attribute importSVG(svgString): Result // Extract all elements from SVG string // Y is always flipped (SVG Y-down to brepjs Y-up). Neither function takes options; the exported SVGImportOptions type is unused ``` ## Topology Helpers Create shapes directly without the sketching API: ```typescript import { line, circle, ellipse, helix, threePointArc, ellipseArc, tangentArc, bsplineApprox, bezier, wire, face, filledFace, subFace, addHoles, polygon, cylinder, sphere, cone, torus, ellipsoid, box, vertex, offsetFace, makeBaseBox, compound, sewShells, solid } from 'brepjs'; // 1D shapes (edges & wires) line(from, to): Edge circle(radius, { at?, axis? }?): Edge ellipse(major, minor, { at?, axis?, xDir? }?): Result helix(pitch, height, radius, { at?, axis?, lefthand? }?): Wire threePointArc(v1, v2, v3): Edge ellipseArc(major, minor, startAngleDeg, endAngleDeg, { at?, axis?, xDir? }?): Result tangentArc(startPoint, startTangent, endPoint): Edge bsplineApprox(points, config?): Result bezier(points): Result wire(edgesOrWires): Result wireLoop(edgesOrWires): Result // Assemble + verify closure // 2D faces (require ClosedWire, return OrientedFace) face(wire: ClosedWire & PlanarWire, holes?: (ClosedWire & PlanarWire)[]): Result // narrow a wireLoop() result with isPlanarWire(w) filledFace(wire: ClosedWire): Result subFace(originFace, wire: ClosedWire): OrientedFace addHoles(face, holes: ClosedWire[]): OrientedFace polygon(points): Result // 3D solids (return ValidSolid) box(width, depth, height, { at?, centered? }?): ValidSolid sphere(radius, { at? }?): ValidSolid cylinder(radius, height, { at?, axis?, centered? }?): ValidSolid cone(bottomRadius, topRadius, height, { at?, axis?, centered? }?): ValidSolid torus(majorRadius, minorRadius, { at?, axis? }?): ValidSolid ellipsoid(rx, ry, rz, { at? }?): ValidSolid makeBaseBox(x, y, z): Shape3D solid(facesOrShells): Result // Vertex vertex(point): Vertex // Compound & shell compound(shapes): Compound sewShells(facesOrShells, ignoreType?): Result offsetFace(face, offset, tolerance?): Result ``` ## Constructive Geometry ```typescript import { hull, minkowski, polyhedron, surfaceFromGrid, surfaceFromImage, roof } from 'brepjs'; hull(shapes, options?): Result // Convex hull of shapes minkowski(shape, tool, options?): Result // Minkowski sum polyhedron(points, faces, options?): Result // From vertices + face indices surfaceFromGrid(heights, options?): Result // Height-map surface surfaceFromImage(blob, options?): Promise> // Image-based surface roof(wire: ClosedWire & PlanarWire, { angle? }?): Result // angle in degrees, default 45 ``` `SurfaceFromGridOptions`: `{ width?, depth?, scaleZ? }` `SurfaceFromImageOptions`: extends grid options + `{ channel?: 'r' | 'g' | 'b' | 'luminance', downsample? }` ### Advanced Sweeps ```typescript import { multiSectionSweep, guidedSweep } from 'brepjs'; multiSectionSweep(sections, spine, options?): Result // sections: { wire: Wire, location?: number }[] // MultiSweepOptions: { solid?, ruled?, tolerance? } guidedSweep(profile, spine, guides, options?): Result // GuidedSweepOptions: { transition?: 'transformed' | 'round' | 'right', solid?, tolerance? } ``` ## Shape Coloring & Tagging ```typescript import { colorFaces, colorShape, getFaceColor, getShapeColor } from 'brepjs'; colorFaces(shape, faces, color): T // Color specific faces colorShape(shape, color): T // Color entire shape getFaceColor(shape, face): Color | undefined // Get face color getShapeColor(shape): Color | undefined // Get shape color // ColorInput: string ('#ff0000') | [r, g, b] | [r, g, b, a] ``` ```typescript import { tagFaces, findFacesByTag, getFaceTags, setTagMetadata, getTagMetadata } from 'brepjs'; tagFaces(shape, selector, tag): AnyShape // Tag faces by array or predicate findFacesByTag(shape, tag): Face[] // Find faces by tag name getFaceTags(shape): Map // Get all tags setTagMetadata(shape, tag, metadata): AnyShape // Attach metadata to a tag getTagMetadata(shape, tag): Record | undefined ``` ## 2D Blueprints & Curves The `Drawing` class supports 2D boolean operations and transformations: ```typescript const plate = drawRectangle(100, 50); const hole = drawCircle(10).translate(20, 0); // Drawing.translate is still available const withHole = plate.cut(hole); // Drawing.cut is still available const filleted = withHole.fillet(3); // Drawing.fillet is still available const svg = filleted.toSVG(); ``` Functional Blueprint API: ```typescript import { createBlueprint, getBounds2D, getOrientation2D, translate2D, rotate2D, scale2D, mirror2D, stretch2D, toSVGPathD, isInside2D, sketchOnPlane2D, sketchOnFace2D } from 'brepjs'; createBlueprint(curves): Result getBounds2D(bp): BoundingBox2d getOrientation2D(bp): 'clockwise' | 'counterClockwise' translate2D(bp, dx, dy): Blueprint rotate2D(bp, angle, center?): Blueprint scale2D(bp, factor, center?): Blueprint mirror2D(bp, dir, origin?, mode?): Blueprint stretch2D(bp, ratio, direction, origin?): Blueprint toSVGPathD(bp): string isInside2D(bp, point): boolean sketchOnPlane2D(bp, plane?, origin?): SketchData // plain { wire, defaultOrigin, defaultDirection } (typed any), not a Sketch sketchOnFace2D(bp, face, scaleMode?): SketchData // plain { wire, baseFace } ``` Blueprint construction helpers: ```typescript import { polysidesBlueprint, roundedRectangleBlueprint, organiseBlueprints } from 'brepjs'; polysidesBlueprint(radius, sidesCount, sagitta?): Blueprint // Regular polygon as a Blueprint (lower-level than drawPolysides) roundedRectangleBlueprint(width, height, r?): Blueprint // Rounded rect as Blueprint. r: number | {rx?, ry?} organiseBlueprints(blueprints: Blueprint[]): Blueprints // Group flat blueprints into compound blueprints with hole detection ``` Low-level Blueprint boolean operations (single blueprint → single blueprint): ```typescript import { fuseBlueprints, cutBlueprints, intersectBlueprints } from 'brepjs'; fuseBlueprints(first: Blueprint, second: Blueprint): null | Blueprint | Blueprints cutBlueprints(first: Blueprint, second: Blueprint): null | Blueprint | Blueprints intersectBlueprints(first: Blueprint, second: Blueprint): null | Blueprint | Blueprints ``` 2D Boolean (functional): ```typescript import { fuse2D, cut2D, intersect2D } from 'brepjs'; fuse2D(first, second): Shape2D cut2D(first, second): Shape2D intersect2D(first, second): Shape2D // Shape2D = Blueprint | Blueprints | CompoundBlueprint | null ``` 2D Curve functions: ```typescript import { reverseCurve, curve2dBoundingBox, curve2dFirstPoint, curve2dLastPoint, curve2dSplitAt, curve2dParameter, curve2dTangentAt, curve2dIsOnCurve, curve2dDistanceFrom } from 'brepjs'; ``` ## Projection & Camera Project 3D shapes to 2D for technical drawings: ```typescript import { drawProjection, createCamera, cameraLookAt, unwrap } from 'brepjs'; // Quick projection from a named plane const { visible, hidden } = drawProjection(shape, 'front'); const svg = visible.toSVG(); // Custom camera const camera = unwrap(createCamera([100, 100, 100], [0, 0, -1])); const lookingAt = unwrap(cameraLookAt(camera, [0, 0, 0])); const projected = drawProjection(shape, lookingAt); ``` Camera API: ```typescript import { createCamera, cameraLookAt, cameraFromPlane, projectEdges } from 'brepjs'; createCamera(position?, direction?, xAxis?): Result cameraLookAt(camera, target): Result cameraFromPlane(planeName): Result projectEdges(shape, camera, withHiddenLines?): { visible: Edge[], hidden: Edge[] } ``` Additional projection helpers: ```typescript import { isProjectionPlane, makeProjectedEdges } from 'brepjs'; isProjectionPlane(plane): plane is ProjectionPlane // Type guard for ProjectionPlane strings makeProjectedEdges(shape, camera, withHiddenLines?): { visible: Edge[], hidden: Edge[] } // HLR projection ``` ## Text Render text as 2D outlines (requires font loading): ```typescript import { loadFont, getFont, drawText, sketchText, textBlueprints } from 'brepjs'; await loadFont('/fonts/Roboto-Regular.ttf', 'Roboto'); // 2D text drawing const text2d = drawText('Hello', { fontSize: 20, fontFamily: 'Roboto' }); // 3D text (sketch on plane, ready to extrude) const text3d = sketchText('Hello', { fontSize: 20, fontFamily: 'Roboto' }, { plane: 'XY' }); // Get raw blueprints const bps = textBlueprints('Hello', { fontSize: 20, fontFamily: 'Roboto' }); ``` ## Assembly Tree Build hierarchical assemblies with transforms: ```typescript import { createAssemblyNode, addChild, removeChild, updateNode, findNode, walkAssembly, countNodes, collectShapes } from 'brepjs'; const root = createAssemblyNode('Root'); const part = createAssemblyNode('Part', { shape: myShape, translate: [10, 0, 0], rotate: { angle: 45, axis: [0, 0, 1] }, metadata: { material: 'steel' } }); const assembly = addChild(root, part); const updated = updateNode(assembly, { translate: [20, 0, 0] }); const found = findNode(assembly, 'Part'); walkAssembly(assembly, (node, depth) => console.log(node.name, depth)); const count = countNodes(assembly); const shapes = collectShapes(assembly); const pruned = removeChild(assembly, 'Part'); ``` ### Assembly Mates (Constraints) ```typescript import { addMate, solveAssembly, unwrap } from 'brepjs'; // Add a constraint between parts const constrained = addMate(assembly, { type: 'coincident', entityA: { node: 'Lid', face: topFace }, entityB: { node: 'Body', face: bottomFace }, }); // Solve constraints to compute transforms const solved = unwrap(solveAssembly(constrained)); // solved: { transforms: Map, dof, converged } ``` Mate types: `'coincident'`, `'concentric'`, `'distance'`, `'angle'`, `'fixed'` ## Parametric History Track modeling operations for undo/replay: ```typescript import { createHistory, addStep, undoLast, findStep, getHistoryShape, stepCount, stepsFrom, registerShape, createRegistry, registerOperation, replayHistory, replayFrom, modifyStep } from 'brepjs'; // Create history and register operations let history = createHistory(); let registry = createRegistry(); registry = registerOperation(registry, 'extrude', (_inputs, params) => { const { w, h, depth } = params as { w: number; h: number; depth: number }; // params: Record return sketchRectangle(w, h).extrude(depth); }); // Add steps history = registerShape(history, 'base', baseShape); history = addStep(history, { id: 'step1', type: 'extrude', parameters: { w: 10, h: 10, depth: 20 }, inputIds: ['base'], outputId: 'extruded' }, resultShape); // Undo, replay, modify history = undoLast(history); const replayed = replayHistory(history, registry); const modified = modifyStep(history, 'step1', { depth: 30 }, registry); ``` ## Meshing Convert shapes to triangle meshes for rendering: ```typescript import { mesh, meshEdges } from 'brepjs'; const m = mesh(shape, { tolerance: 0.5, angularTolerance: 0.3, includeUVs: true }); // m.vertices: Float32Array (flat xyz) // m.triangles: Uint32Array (triangle indices) // m.normals: Float32Array (flat normals) // m.uvs: Float32Array (UV coordinates when includeUVs: true) // m.faceGroups: {start, count, faceId}[] const edgeMesh = meshEdges(shape, { tolerance: 0.5 }); // edgeMesh.lines: Float32Array (flat xyz, 2 points per segment) // edgeMesh.edgeGroups: {start, count, edgeId}[] // Full mesh options (with defaults): // mesh(shape, { // tolerance: 1e-3, // linear deflection; grows with bbox diagonal past 10 units // angularTolerance: 0.1, // angular deflection (radians) // skipNormals: false, // omit normals from output // includeUVs: false, // include UV coordinates per-vertex // cache: true, // cache results (WeakMap by shape) // signal?: AbortSignal, // abort between face iterations // }) ``` Mesh caching: ```typescript import { clearMeshCache, createMeshCache } from 'brepjs'; clearMeshCache(); // Clear global cache const cache = createMeshCache(); // Create isolated cache ``` ### Three.js Integration ```typescript import { toBufferGeometryData, toLineGeometryData, toGroupedBufferGeometryData } from 'brepjs'; const bufferData = toBufferGeometryData(mesh); // { position: Float32Array, normal: Float32Array, index: Uint32Array } const lineData = toLineGeometryData(edgeMesh); // { position: Float32Array } const grouped = toGroupedBufferGeometryData(mesh); // extends bufferData with: groups: [{start, count, materialIndex, faceId}] ``` ## Memory Management OCCT objects are allocated in WASM memory and are **not** garbage-collected by the JavaScript engine. You must explicitly clean them up. Every operation returns a new shape and leaves its inputs alive, so dispose each input and intermediate you own (on occt-wasm a missed `using` on an inner shape, e.g. the `box` in `translate(box(...), v)`, leaks an arena slot). Sub-shapes from `getFaces`/`getEdges`/finders are borrowed from their parent: don't use them after the parent is disposed. brepjs provides these cleanup patterns: ### Pattern 1: `using` keyword (recommended, TS 5.9+) The `using` declaration automatically disposes shapes when they go out of scope via TC39 `Symbol.dispose`: ```typescript import { box, cylinder, cut, unwrap } from 'brepjs'; { using b = box(10, 10, 10); using hole = cylinder(3, 15); const result = unwrap(cut(b, hole)); // b and hole are automatically freed at block end // result survives because it was not declared with `using` } // Works in loops too for (let i = 0; i < 100; i++) { using temp = box(1, 1, 1); processBox(temp); // temp freed each iteration — no memory leak } ``` Requires TypeScript 5.2+ (5.9+ recommended) with `"lib": ["ES2022", "ESNext.Disposable"]` and Node.js 20+ or modern browsers. ### Pattern 2: `DisposalScope` (deterministic, multiple temporaries) ```typescript import { DisposalScope, box, cylinder, cut, unwrap } from 'brepjs'; function buildPart() { using scope = new DisposalScope(); const b = scope.register(box(10, 10, 10)); // register for cleanup const hole = scope.register(cylinder(3, 15)); // register for cleanup return unwrap(cut(b, hole)); // result escapes; b and hole freed when scope exits } ``` ### Pattern 3: `withScope()` (scoped, returns result) ```typescript import { withScope, box, cylinder, cut, unwrap } from 'brepjs'; const result = withScope((scope) => { const b = scope.register(box(10, 10, 10)); const hole = scope.register(cylinder(3, 15)); return unwrap(cut(b, hole)); // returned value survives the scope }); ``` `withScopeResult(fn)` and `withScopeResultAsync(fn)` are variants that accept functions returning `Result` — useful inside operations that already use Result-based error handling: ```typescript import { withScopeResult } from 'brepjs'; const result: Result = withScopeResult((scope) => { const temp = scope.register(cylinder(5, 20)); return cut(myBox, temp); // returns Result directly }); ``` ### Low-level handles ```typescript import { createHandle, createKernelHandle } from 'brepjs'; const handle = createHandle(ocShape); // ShapeHandle: { wrapped, disposed, delete(), onDispose(cb), [Symbol.dispose] } const kernelHandle = createKernelHandle(ocObj); // KernelHandle: { value, disposed, [Symbol.dispose] } ``` `FinalizationRegistry` provides a safety net for missed cleanup, but relying on it is not recommended because GC timing is unpredictable. Always use one of the explicit patterns above. ## Error Handling Many operations return `Result`: ```typescript import { ok, err, OK, isOk, isErr, unwrap, unwrapOr, unwrapOrElse, match, map, andThen, collect, tryCatch, pipeline } from 'brepjs'; // Construction ok(value): Ok err(error): Err OK // Pre-built Ok for void success // Type guards isOk(result): result is Ok isErr(result): result is Err // Extraction unwrap(result): T // throws on Err unwrapOr(result, defaultValue): T unwrapOrElse(result, fn): T unwrapErr(result): E // throws on Ok // Combinators map(result, fn): Result mapErr(result, fn): Result andThen(result, fn): Result // flatMap alias collect(results): Result // All-or-nothing // Pattern matching match(result, { ok: fn, err: fn }): U // Try-catch boundary tryCatch(fn, mapError): Result tryCatchAsync(fn, mapError): Promise> // Pipeline pipeline(input).then(fn).then(fn).result // Chain Result operations ``` BrepError structure: ```typescript interface BrepError { kind: BrepErrorKind; // 'KERNEL_OPERATION' | 'VALIDATION' | 'TYPE_CAST' | 'SKETCHER_STATE' | 'MODULE_INIT' | 'COMPUTATION' | 'IO' | 'QUERY' | 'UNSUPPORTED' code: string; // e.g. 'FUSE_FAILED', 'STEP_IMPORT_FAILED' message: string; suggestion?: string; // how to fix it, when known cause?: unknown; metadata?: Record; } ``` Error constructors: `kernelError(code, msg, cause?, meta?, suggestion?)`, `validationError(...)`, `typeCastError(...)`, `ioError(...)`, `computationError(...)`, `queryError(...)`, `sketcherStateError(...)`, `moduleInitError(...)`, `unsupportedError(...)` ## Worker Protocol Off-main-thread CAD operations: ```typescript import { init, createWorkerClient, createOperationRegistry, registerHandler, createWorkerHandler, createTaskQueue, enqueueTask, dequeueTask, pendingCount, isQueueEmpty, rejectAll, isInitRequest, isOperationRequest, isDisposeRequest, isSuccessResponse, isErrorResponse } from 'brepjs'; // Client side const client = createWorkerClient({ worker: myWorker }); // optional wasmUrl is passed to the worker's init hook await client.init(); const result = await client.execute('fuse', [shape1Brep, shape2Brep], {}); client.dispose(); // Worker side let registry = createOperationRegistry(); registry = registerHandler(registry, 'fuse', (shapesBrep, params) => { // shapesBrep: ReadonlyArray — BREP-serialized input shapes // params: Readonly> — operation parameters // Must return { resultBrep?: string, resultData?: unknown } return { resultBrep: outputBrep }; }); createWorkerHandler(registry, async () => { await init(); }); // init() loads occt-wasm ``` ## Vec3 Math Utilities ```typescript import { vecAdd, vecSub, vecScale, vecNegate, vecDot, vecCross, vecLength, vecLengthSq, vecDistance, vecNormalize, vecEquals, vecIsZero, vecAngle, vecProjectToPlane, vecRotate, vecRepr } from 'brepjs'; vecAdd([1,0,0], [0,1,0]): Vec3 // [1, 1, 0] vecSub(a, b): Vec3 vecScale(v, scalar): Vec3 vecNegate(v): Vec3 vecDot(a, b): number vecCross(a, b): Vec3 vecLength(v): number vecLengthSq(v): number vecDistance(a, b): number vecNormalize(v): Vec3 vecEquals(a, b, tolerance?): boolean vecIsZero(v, tolerance?): boolean vecAngle(a, b): number // radians vecProjectToPlane(v, origin, normal): Vec3 vecRotate(v, axis, angleRad): Vec3 vecRepr(v): string // e.g. "x: 1, y: 2, z: 3" (rounded to 3 decimals) ``` ## Plane Operations ```typescript import { createPlane, createNamedPlane, resolvePlane, translatePlane, pivotPlane, makePlane } from 'brepjs'; createPlane(origin, xDirection?, normal?): Plane createNamedPlane(name, origin?): Result resolvePlane(input, origin?): Result // PlaneName | Plane → Plane translatePlane(plane, offset): Plane pivotPlane(plane, angleDeg, axis?): Plane makePlane(plane?, origin?): Plane // From PlaneName + origin ``` ## Branded Shape Types (Functional API) ```typescript import { castShape, getShapeKind, createVertex, createEdge, createWire, createFace, createShell, createSolid, createCompound, isVertex, isEdge, isWire, isFace, isShell, isSolid, isCompound, isShape3D, isShape1D, is3D, is2D, // Validity constructors and guards closedWire, orientedFace, manifoldShell, validSolid, isClosedWire, isOrientedFace, isManifoldShell, isValidSolid, } from 'brepjs'; castShape(ocShape): AnyShape // Auto-detect and wrap getShapeKind(shape): ShapeKind // 'vertex' | 'edge' | 'wire' | 'face' | 'shell' | 'solid' | 'compsolid' | 'compound' isVertex(s): s is Vertex // Type guards for shape kinds is3D(s): s is AnyShape<'3D'> // Dimension guards is2D(s): s is AnyShape<'2D'> closedWire(w): Result // Validity smart constructors orientedFace(f): Result manifoldShell(s): Result validSolid(s): Result isClosedWire(w): w is ClosedWire // Validity type guards isOrientedFace(f): f is OrientedFace isManifoldShell(s): s is ManifoldShell isValidSolid(s): s is ValidSolid ``` ## Kernel Boundary Conversions Low-level helpers for interop with raw kernel objects. The objects are kernel-specific (`KernelType`): on occt-wasm a vector is a plain `{ x, y, z }` record. ```typescript import { toKernelVec, fromKernelVec, fromKernelPnt, fromKernelDir, withKernelVec, withKernelPnt, withKernelDir } from 'brepjs'; toKernelVec(v): KernelType // Caller must delete() fromKernelVec(v): Vec3 // reads { x, y, z } or .X()/.Y()/.Z() fromKernelPnt(v): Vec3 fromKernelDir(v): Vec3 // Scoped (auto-cleanup) withKernelVec(v, fn): T // fn receives the kernel vector, deleted after withKernelPnt(v, fn): T withKernelDir(v, fn): T ``` ## Constants - `DEG2RAD` — Multiply degrees to get radians - `RAD2DEG` — Multiply radians to get degrees - `HASH_CODE_MAX` — Maximum hash code value (2147483647) ## Types Reference Key types used across the API: - `Vec3`: `readonly [number, number, number]` - `Vec2`: `readonly [number, number]` - `PointInput`: `Vec3 | Vec2` - `Point2D`: `[number, number]` - `Direction`: `Vec3 | 'X' | 'Y' | 'Z'` - `Plane`: `{ origin: Vec3, xDir: Vec3, yDir: Vec3, zDir: Vec3 }` - `PlaneName`: `'XY' | 'XZ' | 'YZ' | 'ZX' | 'YX' | 'ZY' | 'front' | 'back' | 'left' | 'right' | 'top' | 'bottom'` - `PlaneInput`: `Plane | PlaneName` - `ShapeKind`: `'vertex' | 'edge' | 'wire' | 'face' | 'shell' | 'solid' | 'compsolid' | 'compound'` - `AnyShape`: Union of Vertex, Edge, Wire, Face, Shell, Solid, CompSolid, Compound - `Shape3D`: Shell | Solid | CompSolid | Compound - `Shape1D`: Edge | Wire - `CurveType`: `'LINE' | 'CIRCLE' | 'ELLIPSE' | 'HYPERBOLA' | 'PARABOLA' | 'BEZIER_CURVE' | 'BSPLINE_CURVE' | 'OFFSET_CURVE' | 'OTHER_CURVE'` - `SurfaceType`: `'PLANE' | 'CYLINDRE' | 'CONE' | 'SPHERE' | 'TORUS' | 'BEZIER_SURFACE' | 'BSPLINE_SURFACE' | 'REVOLUTION_SURFACE' | 'EXTRUSION_SURFACE' | 'OFFSET_SURFACE' | 'OTHER_SURFACE'` (what `getSurfaceType()` returns and `ofSurfaceType()` takes) - `Result`: `Ok | Err` (default `E = BrepError`) - `BrepError`: `{ kind: BrepErrorKind, code: string, message: string, suggestion?, cause?, metadata? }` - `ShapeMesh`: `{ triangles: Uint32Array, vertices: Float32Array, normals: Float32Array, uvs: Float32Array, faceGroups }` - `EdgeMesh`: `{ lines: Float32Array, edgeGroups: {start, count, edgeId}[] }` - `MeshOptions`: `{ tolerance?, angularTolerance? (radians), signal? }` (mesh() also accepts `{ skipNormals?, includeUVs?, cache? }`) - `BooleanOptions`: `{ optimisation?, simplify?, strategy?, signal?, fuzzyValue?, trackEvolution? }` - `Camera`: `{ position: Vec3, direction: Vec3, xAxis: Vec3, yAxis: Vec3 }` - `ProjectionPlane`: `'XY' | 'XZ' | 'YZ' | 'YX' | 'ZX' | 'ZY' | 'front' | 'back' | 'top' | 'bottom' | 'left' | 'right'` - `AssemblyNode`: `{ name, shape?, translate?, rotate?: { angle, axis? }, metadata?, children }` - `ModelHistory`: `{ steps: ReadonlyArray, shapes: ReadonlyMap }` - `OperationStep`: `{ id, type, parameters, inputIds, outputId, timestamp, metadata? }` - `OperationFn`: `(inputs: AnyShape[], params: Record) => AnyShape` - `HistoryOperationRegistry`: `{ operations: ReadonlyMap }` - `HealingReport`: `{ isValid, alreadyValid, wiresHealed, facesHealed, solidHealed, steps, diagnostics }` - `InterferenceResult`: `{ hasInterference, minDistance, pointOnShape1, pointOnShape2 }` - `CurvatureResult`: `{ mean, gaussian, maxCurvature, minCurvature, maxDirection, minDirection }` - `ShapeHandle`: `{ wrapped, disposed, delete(), onDispose(cb), [Symbol.dispose]() }` - `Bounds3D`: `{ xMin, xMax, yMin, yMax, zMin, zMax }` - `ShapeDescription`: `{ kind, faceCount, edgeCount, wireCount, vertexCount, valid, bounds }` ## Advanced Examples ### Flanged pipe fitting with booleans, fillets, and a bolt-hole pattern ```typescript import { init, unwrap, cylinder, fuseAll, cut, fillet, circularPattern, edgeFinder, measureVolume, } from 'brepjs'; await init(); // `using` disposes each intermediate at block end; the returned shape survives const pipeFitting = (() => { // Tube + two flanges; simplify merges the coplanar end faces using tube = cylinder(15, 100); using bottomFlange = cylinder(30, 5); using topFlange = cylinder(30, 5, { at: [0, 0, 95] }); using body = unwrap(fuseAll([tube, bottomFlange, topFlange], { simplify: true })); // Fillet the tube-to-flange transitions (the r=15 circles at z=5 and z=95) const transitions = edgeFinder().ofCurveType('CIRCLE').ofLength(2 * Math.PI * 15).findAll(body); using filleted = unwrap(fillet(body, transitions, 3)); // Bore it through, then cut 6 bolt holes through both flanges using bore = cylinder(13, 102, { at: [0, 0, -1] }); using bored = unwrap(cut(filleted, bore)); using pin = cylinder(3, 102, { at: [22, 0, -1] }); using pins = unwrap(circularPattern(pin, [0, 0, 1], 6)); return unwrap(cut(bored, pins)); })(); console.log('Volume:', unwrap(measureVolume(pipeFitting))); ``` ### Enclosure with shell, screw bosses, vent slots, and raised text ```typescript import { init, unwrap, loadFont, box, cylinder, sketchText, fuse, cut, shell, fillet, gridPattern, linearPattern, faceFinder, edgeFinder, } from 'brepjs'; await init(); unwrap(await loadFont('/fonts/Roboto-Regular.ttf', 'Roboto')); const enclosure = (() => { // 80 x 50 x 30 box on z=0 with the four vertical edges rounded using b = box(80, 50, 30, { at: [0, 0, 15] }); using rounded = unwrap(fillet(b, edgeFinder().inDirection('Z'), 5)); // Open the top (the Z-facing face 30 from the origin) and hollow to 2 mm walls using shelled = unwrap(shell(rounded, faceFinder().inDirection('Z').atDistance(30), 2)); // Four screw bosses on the floor, each with a pilot hole using boss = cylinder(3, 25, { at: [-30, -18, 2] }); using bosses = unwrap(gridPattern(boss, [1, 0, 0], [0, 1, 0], 2, 2, 60, 36)); using pilot = cylinder(1.2, 25, { at: [-30, -18, 2] }); using pilots = unwrap(gridPattern(pilot, [1, 0, 0], [0, 1, 0], 2, 2, 60, 36)); using withBosses = unwrap(fuse(shelled, bosses)); using drilled = unwrap(cut(withBosses, pilots)); // Five vent slots through the front wall (y = -25) using slot = box(1.5, 6, 10, { at: [-8, -24, 15] }); using slots = unwrap(linearPattern(slot, [1, 0, 0], 5, 4)); using vented = unwrap(cut(drilled, slots)); // Raised text on the floor: sketchText returns Sketches, whose extrude() gives a Compound using text = sketchText('brepjs', { fontSize: 8, fontFamily: 'Roboto' }, { plane: 'XY', origin: 2 }).extrude(1); return unwrap(fuse(vented, text)); })(); ``` ### Parametric spring with sweep and interference check ```typescript import { init, unwrap, sketchHelix, Sketcher, cylinder, fuse, cut, checkInterference, translate, rotate, } from 'brepjs'; await init(); const spring = (() => { // Helix spine (pitch 8, height 60, radius 20) with a 3 mm round wire swept along it using coil = sketchHelix(8, 60, 20).sweepSketch((plane) => new Sketcher(plane).movePointerTo([-1.5, 0]).sagittaArc(3, 0, 1.5).sagittaArc(-3, 0, 1.5).close() ); // Flat ends using bottomEnd = cylinder(20, 2, { at: [0, 0, -2] }); using topEnd = cylinder(20, 2, { at: [0, 0, 60] }); using withBottom = unwrap(fuse(coil, bottomEnd)); return unwrap(fuse(withBottom, topEnd)); })(); // Does the spring clear the bore of a tube housing (inner radius 23)? using outer = cylinder(25, 70, { at: [0, 0, -5] }); using bore = cylinder(23, 72, { at: [0, 0, -6] }); using housing = unwrap(cut(outer, bore)); const interference = unwrap(checkInterference(spring, housing)); console.log('Fits:', !interference.hasInterference, 'Gap:', interference.minDistance); // Fits: true Gap: ~1.5 // Chain transforms: dispose the intermediate using moved = translate(spring, [100, 0, 0]); const movedSpring = rotate(moved, 90, { axis: [0, 1, 0] }); ``` ### Multi-format export with materials ```typescript import { unwrap, mesh as meshFn, exportGlb, exportOBJ, exportThreeMF, exportDXF, drawProjection, exportAssemblySTEP, toGroupedBufferGeometryData, } from 'brepjs'; // part, body, lid: shapes built earlier // Mesh the shape for rendering (angularTolerance is in radians) const meshData = meshFn(part, { tolerance: 0.1, angularTolerance: 0.3, includeUVs: true }); // glTF with per-face PBR materials const materials = new Map(); for (const group of meshData.faceGroups) { materials.set(group.faceId, { name: `face_${group.faceId}`, baseColor: [0.7, 0.7, 0.8, 1.0], metallic: 0.8, roughness: 0.3, }); } const glb = exportGlb(meshData, { materials }); // 3MF for 3D printing const threemf = exportThreeMF(meshData, { name: 'MyPart', unit: 'millimeter' }); // OBJ for compatibility const obj = exportOBJ(meshData); // STEP with assembly structure and colors (unit: 'mm' | 'cm' | 'm' | 'inch' | 'ft', or uppercase) const step = unwrap(exportAssemblySTEP( [ { shape: body, color: '#336699', name: 'Body' }, { shape: lid, color: '#996633', name: 'Lid', alpha: 0.8 }, ], { unit: 'mm' } )); // 2D projection → SVG; DXF from explicit LINE/POLYLINE entities const { visible } = drawProjection(part, 'top'); const svg = visible.toSVG(); const dxf = exportDXF([ { type: 'POLYLINE', points: [[0,0], [100,0], [100,50], [0,50]], closed: true, layer: 'outline' }, ]); // Three.js integration const bufferData = toGroupedBufferGeometryData(meshData); // Use bufferData.position, .normal, .index, .groups with THREE.BufferGeometry ``` ### Functional API: immutable pipeline with healing ```typescript import { init, unwrap, polygon, extrude, validSolid, cylinder, cut, fillet, shell, edgeFinder, faceFinder, autoHeal, isValid, describe, } from 'brepjs'; await init(); // Every op returns a new shape and leaves its inputs alive: `using` disposes each one using profile = unwrap(polygon([[0, 0, 0], [40, 0, 0], [40, 30, 0], [0, 30, 0]])); // extrude returns Result; fillet and shell take a ValidSolid, so prove it using base = unwrap(validSolid(unwrap(extrude(profile, 20)))); // Round the four vertical edges, then cut a mounting hole using rounded = unwrap(fillet(base, edgeFinder().inDirection('Z'), 2)); using hole = cylinder(3, 20, { at: [10, 15, 0] }); using withHole = unwrap(cut(rounded, hole)); // Open the top face (20 above the profile center) and shell to 1.5 mm walls using shelled = unwrap(shell(withHole, faceFinder().inDirection('Z').atDistance(20, [20, 15, 0]), 1.5)); // Validate; heal only if needed (autoHeal returns { shape, report }) if (!isValid(shelled)) { const { shape, report } = unwrap(autoHeal(shelled)); using healed = shape; console.log('Healed:', report.steps, describe(healed).valid); } const desc = describe(shelled); console.log(`${desc.kind}: ${desc.faceCount} faces, ${desc.edgeCount} edges, valid: ${desc.valid}`); ``` --- # Internal Development Reference For contributors and AI agents working on brepjs internals (not for library consumers). ## Architecture Rules - **Layer boundaries**: imports flow downward only (Layer 0 ← Layer 1 ← Layer 2 ← Layer 3). Enforced by `npm run check:boundaries`. - **Kernel access**: Layer 2+ code calls `getKernel().method(shape.wrapped)` — never access `.wrapped` methods directly. ESLint enforces this. - **Functional API**: All new code goes in `*Fns.ts` files. Never add methods to legacy class wrappers. - **Result types**: Layer 2+ functions return `Result` — never throw. - **ESM imports**: All `.ts` imports must use `.js` extensions. - **`withKernel(id, fn)` is sync-only** — async callbacks silently use the wrong kernel after the first `await`. ## Verification (fastest → slowest) | Command | What | Time | |---------|------|------| | `npm run typecheck` | Type errors | ~5s | | `npm run lint` | Style + banned patterns | ~3s | | `npm run check:boundaries` | Layer imports | ~2s | | `npm run format:check` | Formatting | ~2s | | `npx vitest run tests/.test.ts` | Single test | ~5s | | `npm run test:affected` | Changed file tests | ~10s | | `npm run test:coverage` | Full suite | ~30s | | `npm run validate` | All above except single test and full suite | ~22s | ## Functional API Pattern Template for all `*Fns.ts` files. Follow exactly when adding new operations. ```typescript // src/topology/booleanFns.ts — fuse() is the canonical example /** * Fuse two 3D shapes together (boolean union). Returns a new shape. * * @param a - The first operand. * @param b - The second operand. * @param options - Boolean operation options. * @returns Ok with the fused shape, or Err if the result is not 3D. * * @example * ```ts * const result = fuse(box, cylinder); * if (isOk(result)) console.log(describe(result.value)); * ``` */ export function fuse( a: Shape3D, b: Shape3D, { optimisation = 'none', simplify = false, signal }: BooleanOptions = {} ): Result { if (signal?.aborted) throw signal.reason; // 1. Validate inputs const checkA = validateShape3D(a, 'fuse: first operand'); if (isErr(checkA)) return checkA; const checkB = validateShape3D(b, 'fuse: second operand'); if (isErr(checkB)) return checkB; // 2. Call kernel (never call .wrapped methods directly) const inputFaceHashes = collectInputFaceHashes([a, b]); const { shape: resultShape, evolution } = getKernel().fuseWithHistory( a.wrapped, b.wrapped, inputFaceHashes, HASH_CODE_MAX, { optimisation, simplify } ); // 3. Cast result and check type const fuseResult = castToShape3D(resultShape, 'FUSE_NOT_3D', 'Fuse did not produce a 3D shape'); // 4. Propagate metadata if (fuseResult.ok) { propagateOriginsFromEvolution(evolution, [a, b], fuseResult.value); propagateFaceTagsFromEvolution(evolution, [a, b], fuseResult.value); propagateColorsFromEvolution(evolution, [a, b], fuseResult.value); } return fuseResult; } ``` ## Test Pattern ```typescript import { describe, expect, it, beforeAll } from 'vitest'; import { initKernel } from './setup.js'; import { box, fuse, isOk, unwrap, measureVolume, isSolid } from '../src/index.js'; beforeAll(async () => { await initKernel(); }, 30000); describe('fuse', () => { it('fuses two boxes', () => { const a = box(10, 10, 10); const b = box(10, 10, 10); const result = fuse(a, b); expect(isOk(result)).toBe(true); const shape = unwrap(result); expect(isSolid(shape)).toBe(true); expect(measureVolume(shape)).toBeCloseTo(1000, 0); // geometry: use toBeCloseTo }); }); ``` ## Branded Shape Types (src/core/shapeTypes.ts) ```typescript type Dimension = '2D' | '3D'; // Base types — phantom D parameter for compile-time 2D/3D safety type Vertex = ShapeHandle & { readonly [__brand]: 'vertex'; readonly [__dim]: D }; type Edge = ShapeHandle & { readonly [__brand]: 'edge'; readonly [__dim]: D }; type Wire = ShapeHandle & { readonly [__brand]: 'wire'; readonly [__dim]: D }; type Face = ShapeHandle & { readonly [__brand]: 'face'; readonly [__dim]: D }; type Shell = ShapeHandle & { readonly [__brand]: 'shell' }; type Solid = ShapeHandle & { readonly [__brand]: 'solid' }; type CompSolid = ShapeHandle & { readonly [__brand]: 'compsolid' }; type Compound = ShapeHandle & { readonly [__brand]: 'compound'; readonly [__dim]: D }; type AnyShape = Vertex | Edge | Wire | Face | Shell | Solid | CompSolid | Compound; type Shape1D = Edge | Wire; type Shape3D = Shell | Solid | CompSolid | Compound<'3D'>; // Validity brands — encode topological invariants at compile time type ClosedWire = Wire & { readonly [__closed]: true }; type OrientedFace = Face & { readonly [__oriented]: true }; type ManifoldShell = Shell & { readonly [__manifold]: true }; type ValidSolid = Solid & { readonly [__valid]: true }; (validity smart constructors return Result — see closedWire/orientedFace/manifoldShell/validSolid above; check with isOk, extract with unwrap) // Factories — always use these, never raw casts createVertex(ocShape): Vertex createEdge(ocShape): Edge castShape(ocShape): AnyShape // Auto-detects type // Type guards isVertex(s): s is Vertex isEdge(s): s is Edge isSolid(s): s is Solid isShape3D(s): s is Shape3D is3D(s): s is AnyShape<'3D'> is2D(s): s is AnyShape<'2D'> // Validity smart constructors closedWire(w): Result orientedFace(f): Result manifoldShell(s): Result validSolid(s): Result // Validity type guards isClosedWire(w): w is ClosedWire isOrientedFace(f): f is OrientedFace isManifoldShell(s): s is ManifoldShell isValidSolid(s): s is ValidSolid ``` ## All Error Codes (src/core/errors.ts) ```typescript const BrepErrorCode = { // Kernel operation failures BSPLINE_FAILED, FACE_BUILD_FAILED, SWEEP_FAILED, LOFT_FAILED, FUSE_FAILED, CUT_FAILED, HEAL_NO_EFFECT, // Validation ELLIPSE_RADII, FUSE_ALL_EMPTY, FILLET_NO_EDGES, CHAMFER_NO_EDGES, CHAMFER_ANGLE_NO_EDGES, CHAMFER_ANGLE_BAD_DISTANCE, CHAMFER_ANGLE_BAD_ANGLE, BEZIER_MIN_POINTS, POLYGON_MIN_POINTS, ZERO_LENGTH_EXTRUSION, ZERO_TWIST_ANGLE, LOFT_EMPTY, UNSUPPORTED_PROFILE, UNKNOWN_PLANE, NULL_SHAPE_INPUT, INVALID_FILLET_RADIUS, INVALID_CHAMFER_DISTANCE, INVALID_THICKNESS, ZERO_OFFSET, NO_EDGES, NO_FACES, // Type cast FUSE_NOT_3D, CUT_NOT_3D, INTERSECT_NOT_3D, FUSE_ALL_NOT_3D, CUT_ALL_NOT_3D, LOFT_NOT_3D, SWEEP_NOT_3D, REVOLUTION_NOT_3D, FILLET_NOT_3D, CHAMFER_NOT_3D, CHAMFER_ANGLE_NOT_3D, CHAMFER_ANGLE_FAILED, SHELL_NOT_3D, OFFSET_NOT_3D, NULL_SHAPE, NO_WRAPPER, WELD_NOT_SHELL, SOLID_BUILD_FAILED, OFFSET_NOT_WIRE, UNKNOWN_SURFACE_TYPE, UNKNOWN_CURVE_TYPE, SWEEP_START_NOT_WIRE, SWEEP_END_NOT_WIRE, // I/O STEP_EXPORT_FAILED, STEP_FILE_READ_ERROR, STL_EXPORT_FAILED, STL_FILE_READ_ERROR, STEP_IMPORT_FAILED, STL_IMPORT_FAILED, IGES_EXPORT_FAILED, IGES_IMPORT_FAILED, DXF_IMPORT_FAILED, OBJ_IMPORT_FAILED, THREEMF_IMPORT_FAILED, // Computation PARAMETER_NOT_FOUND, INTERSECTION_FAILED, SELF_INTERSECTION_FAILED, // Compound COMPOUND_NO_FACES, COMPOUND_FACE_NOT_FOUND, // Query FINDER_NOT_UNIQUE, // Hull HULL_EMPTY_INPUT, HULL_FAILED, HULL_DEGENERATE, HULL_NOT_3D, // Minkowski MINKOWSKI_FAILED, MINKOWSKI_NULL_TOOL, MINKOWSKI_NOT_3D, // Polyhedron POLYHEDRON_INSUFFICIENT_POINTS, POLYHEDRON_INSUFFICIENT_FACES, POLYHEDRON_INVALID_INDEX, POLYHEDRON_FAILED, // Domain-specific ROOF_FAILED, MULTI_SWEEP_INSUFFICIENT_SECTIONS, MULTI_SWEEP_FAILED, GUIDED_SWEEP_FAILED, SURFACE_GRID_TOO_SMALL, SURFACE_GRID_JAGGED, SURFACE_FAILED, ASSEMBLY_MATE_INVALID, ASSEMBLY_SOLVE_FAILED, ASSEMBLY_NOT_CONVERGED, } as const; ``` ## KernelAdapter Interface Summary (src/kernel/types.ts) The KernelAdapter has ~164 methods. Key categories: **Boolean ops**: `fuse`, `cut`, `intersect`, `section`, `fuseAll`, `cutAll` **Primitives**: `makeBox`, `makeCylinder`, `makeSphere`, `makeCone`, `makeTorus`, `makeEllipsoid` **Edges/Wires**: `makeLineEdge`, `makeCircleEdge`, `makeArcEdge`, `makeBezierEdge`, `makeHelixWire`, `makeWire` **Extrusion/Sweep**: `extrude`, `revolve`, `loft`, `sweep`, `sweepPipeShell`, `loftAdvanced` **Modification**: `fillet`, `chamfer`, `chamferDistAngle`, `shell`, `thicken`, `offset` **Transforms**: `translate`, `rotate`, `mirror`, `scale`, `generalTransform` **History variants**: `fuseWithHistory`, `cutWithHistory`, `filletWithHistory`, etc. — return `{ shape, evolution }` **Measurement**: `volume`, `area`, `length`, `centerOfMass`, `boundingBox` **Topology**: `iterShapes`, `shapeType`, `isSame`, `hashCode`, `downcast` **Geometry queries**: `surfaceType`, `surfaceNormal`, `curveTangent`, `curveType`, `vertexPosition` **I/O**: `exportSTEP`, `importSTEP`, `exportSTL`, `importSTL`, `exportIGES`, `importIGES` **Repair**: `isValid`, `sew`, `healSolid`, `fixShape`, `fixSelfIntersection` **Meshing**: `mesh`, `meshEdges` All methods accept `KernelShape` (opaque handle) — callers pass `shape.wrapped`. ## Gotchas - OCCT Emscripten returns enum objects with `.value` — extract: `typeof val === 'number' ? val : Number(val?.value ?? val)` - `autoHeal` short-circuits valid shapes: `report.alreadyValid=true`, no sew/heal diagnostics run - Assembly solver composes constraints down a chain: each positioning mate reads the referenced part's solved pose, resolved in topological order from a `fixed` node - `Uint32Array` from WASM: always convert to regular `Array` before passing to kernel methods - `DisposalScope` disposes in LIFO order — register dependencies after their dependees - `noUncheckedIndexedAccess` enabled — array indexing returns `T | undefined`, add bounds checks - `exactOptionalPropertyTypes` enabled — use `prop?: T | undefined` for optional fields