Skip to content

gsdali/OCCTSwiftScripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OCCTSwiftScripts

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.

Quick Start

# Build (first time ~30s, incremental ~1-2s)
swift build

# Edit Sources/Script/main.swift with your geometry code, then:
swift run Script

In the OCCTSwiftViewport demo app (macOS): sidebar > File & Tools > Script Watcher > toggle on. Geometry auto-reloads on each run.

What You Can Do

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

How It Works

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
  1. ScriptContext writes each body as a .brep file (~1ms each)
  2. emit() writes a combined output.step for external tool interop (ezdxf, FreeCAD, etc.)
  3. emit() writes manifest.json last — the file watcher triggers on this

API Reference

Adding Geometry

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)

ScriptContext.add (Shape)

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)

ScriptContext.add (Wire)

Same parameters as Shape (minus roughness/metallic). Wire is converted to a Shape internally — BREP preserves wire topology, displayed as wireframe edges.

ScriptContext.add (Edge)

Same as Wire — single edge converted and preserved.

ScriptContext.emit

try ctx.emit(description: "My parametric design")

Writes manifest.json (viewport trigger) and output.step (external tools). Call last.

Predefined Colors

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.copper

Disabling STEP Export

let ctx = ScriptContext(exportSTEP: false)  // BREP only, faster

Example: Parametric Bracket

import 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")

Output Directory

~/.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)

Requirements

  • macOS 15+
  • Swift 6.0+
  • OCCTSwift (local path dependency at ../OCCTSwift)

About

Script harness for rapid OCCTSwift parametric geometry iteration

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages