A script harness for rapid iteration on OCCTSwift parametric geometry. Edit a Swift script using the full OCCTSwift API, run it, and see results instantly in the OCCTSwiftViewport demo app.
This is the OCCTSwift equivalent of CadQuery or OpenSCAD — write parametric, constraint-based CAD code and get visual + file feedback immediately.
# Build (first time ~30s, incremental ~1-2s)
swift build
# Edit Sources/Script/main.swift with your geometry code, then:
swift run ScriptIn the OCCTSwiftViewport demo app (macOS): sidebar > File & Tools > Script Watcher > toggle on. Geometry auto-reloads on each run.
The script has access to the entire OCCTSwift API (~400+ methods):
| Category | Examples |
|---|---|
| Primitives | Shape.box, .cylinder, .sphere, .cone, .torus, .wedge |
| Sketches | Wire.rectangle, .circle, .polygon, .ellipse, .arc, .helix |
| Extrude/Revolve | Shape.extrude(profile:direction:length:), .revolve(axis:angle:profile:) |
| Sweep/Loft | Shape.sweep(profile:along:), .loft(profiles:), .pipeShell(...) |
| Booleans | .union(with:), .subtracting(_:), .intersection(with:), .split(by:) |
| Fillets/Chamfers | .filleted(radius:), .chamfered(distance:), .blendedEdges(...) |
| Holes/Features | .drilled(at:direction:radius:depth:), .withPocket(...), .withBoss(...) |
| Offset/Shell | .offset(by:), .shelled(thickness:) |
| Transforms | .translated(by:), .rotated(axis:angle:), .scaled(by:), .mirrored(...) |
| Patterns | .linearPattern(direction:spacing:count:), .circularPattern(...) |
| Analysis | .volume, .surfaceArea, .centerOfMass, .bounds, .distance(to:) |
| Healing | .healed(), .fixed(tolerance:), .unified(...), .simplified(...) |
| Curves | Curve2D, Curve3D — bezier, bspline, approximate, intersect |
| Surfaces | Surface.plane, .bezier, .bspline, .pipe, .revolution, .extrusion |
| 2D Solvers | Curve2D.GccAna — tangent circles/lines, constraint solvers |
| File I/O | Shape.load(from:), .loadSTEP, .loadBREP, Document (XDE assembly) |
| GD&T | Document.dimensions, .geomTolerances, .datums |
Sources/Script/main.swift ──swift run──> ~/.occtswift-scripts/output/
├─ manifest.json (trigger file)
├─ body-0.brep (wire sketch)
├─ body-1.brep (filleted solid)
├─ body-2.brep (bolt assembly)
└─ output.step (combined, for external tools)
│
kqueue watcher (demo app)
│
viewport displays
ScriptContextwrites each body as a.brepfile (~1ms each)emit()writes a combinedoutput.stepfor external tool interop (ezdxf, FreeCAD, etc.)emit()writesmanifest.jsonlast — the file watcher triggers on this
let ctx = ScriptContext()
let C = ScriptContext.Colors.self
// Solid shapes
try ctx.add(shape, id: "part", color: C.steel, name: "Bracket")
// Wire profiles / sketches (displayed as wireframe)
try ctx.add(wire, id: "sketch", color: C.yellow)
// Single edges
try ctx.add(edge, id: "axis", color: C.red)
// Multiple shapes as compound
try ctx.addCompound([shape1, shape2], id: "assembly", color: C.gray)| Parameter | Type | Description |
|---|---|---|
shape |
Shape |
Any OCCTSwift shape (solid, shell, compound, face) |
id |
String? |
Body identifier (default: "body-N") |
color |
[Float]? |
RGBA as [r, g, b, a] (0-1 range) |
name |
String? |
Display name |
roughness |
Float? |
PBR roughness (reserved) |
metallic |
Float? |
PBR metallic (reserved) |
Same parameters as Shape (minus roughness/metallic). Wire is converted to a Shape internally — BREP preserves wire topology, displayed as wireframe edges.
Same as Wire — single edge converted and preserved.
try ctx.emit(description: "My parametric design")Writes manifest.json (viewport trigger) and output.step (external tools). Call last.
let C = ScriptContext.Colors.self
// C.red, C.green, C.blue, C.yellow, C.orange, C.purple,
// C.cyan, C.white, C.gray, C.steel, C.brass, C.copperlet ctx = ScriptContext(exportSTEP: false) // BREP only, fasterimport OCCTSwift
import ScriptHarness
let ctx = ScriptContext()
let C = ScriptContext.Colors.self
// 1. Sketch L-shaped profile
let profile = Wire.polygon([
SIMD2(0, 0), SIMD2(40, 0), SIMD2(40, 5),
SIMD2(5, 5), SIMD2(5, 25), SIMD2(0, 25),
])!
try ctx.add(profile, id: "sketch", color: C.yellow)
// 2. Extrude → fillet → drill
let solid = Shape.extrude(profile: profile, direction: SIMD3(0, 0, 1), length: 20)!
let filleted = solid.filleted(radius: 1.5) ?? solid
let bracket = filleted.drilled(at: SIMD3(20, 2.5, 10), direction: SIMD3(0, 1, 0), radius: 3, depth: 10) ?? filleted
try ctx.add(bracket, id: "bracket", color: C.steel)
// 3. Pattern bolt holes
let hole2 = bracket.drilled(at: SIMD3(30, 2.5, 10), direction: SIMD3(0, 1, 0), radius: 3, depth: 10) ?? bracket
try ctx.add(hole2, id: "final", color: C.steel)
try ctx.emit(description: "Parametric L-bracket")~/.occtswift-scripts/output/ — cleaned on each run.
- BREP files: loaded by viewport app, preserves exact B-Rep topology
- output.step: combined geometry for external tools (FreeCAD, ezdxf, STEPUtils, etc.)
- manifest.json: body metadata (IDs, colors, names)
- macOS 15+
- Swift 6.0+
- OCCTSwift (local path dependency at
../OCCTSwift)