OCCTSwiftViewport Cookbook
Task-oriented, example-rich guides for the OCCTSwiftViewport Swift API — one page per area, each a short bit of prose followed by runnable Swift snippets. The Swift-side counterpart to OCCTSwift’s own cookbook: make the viewport surface answerable by context7.
Conventions
- Every example is runnable Swift in a
```swiftblock, using the real current API. Fallible factories and optionals are unwrapped withguard/if let, never force-unwrapped. Examples double as living docs; keep them compiling against the shipped API. - One canonical place per topic. These pages hold usage; the exhaustive per-member surface lives in the API Reference. Link, don’t duplicate.
- SwiftUI-first. The viewport’s entry point is
MetalViewportView(a SwiftUIView) driven by aViewportController(@MainActor,ObservableObject). Examples assume aViewportControllerinstance namedcontrollerunless shown otherwise.
Figures
The viewport is the renderer, so figures on the visual pages are produced by its own headless OffscreenRenderer — the same path a host app would use for thumbnails or exports. They are committed under images/ and generated by the DocFigures example (Examples/DocFigures), so the picture and the documented API never drift. Rebuild them with:
cd Examples/DocFigures && swift run DocFigures ../../docs/guides/cookbook/images
Pages
- Getting Started —
MetalViewportViewin SwiftUI, theViewportController, and putting your first body on screen. - Display Modes — wireframe, shaded, shaded-with-edges, flat, and unlit/flat-colour for diagnostic renders.
- Camera & Navigation — orbit / pan / zoom, the arcball / turntable / first-person rotation styles, standard views, fit-to-scene, and animated transitions.
- ViewCube — the interactive orientation cube and navigation cube: click-to-snap faces / edges / corners, drag-to-orbit, and placement.
- Lighting & Materials —
LightingConfigurationpresets, per-bodyPBRMaterial/MaterialLibrary, and HDR image-based lighting. - Picking & Selection — GPU pick buffer, body / face / edge / vertex picks,
SelectionFilterchains, ray-casting, andisPickable. - Loading Geometry — the
ViewportBodycontainer, interleaved vertex data, edges & analytic arcs, primitives, point clouds, per-body transform & colour, and bridging from OCCTSwift. - Measurements —
measurementMode, tap-to-measure, theMeasurementOverlay, and distance / angle / radius dimensions. - Clip Planes & Sections —
ClipPlanesection views and capping. - Offscreen Rendering —
OffscreenRenderer/OffscreenRenderOptionsfor headless PNGs, display modes, lighting, and measurement compositing. - Gestures & Input —
GestureConfigurationpresets (.default/.blender/.fusion360), the portableViewportInputEventmodel, and modifier keys. - HUD Overlays — the orientation gnomon and the scale bar.