OpenCascade.js Custom Build for Replicad
replicad-opencascadejs is a specialized, minified build of opencascade.js, which itself is a JavaScript and WebAssembly port of the Open CASCADE Technology (OCCT) CAD kernel. This package is specifically tailored to include only the necessary APIs for the replicad library, aiming to significantly reduce the overall bundle size and improve load times for web-based 3D modeling applications. The current stable version, as per the request, is 0.23.0, though npm often shows slightly older versions like 0.20.2 or 0.21.0 as most recently published, indicating an irregular release cadence tied to upstream opencascade.js updates and replicad's needs. Its key differentiator is providing a lightweight, optimized WebAssembly CAD backend for replicad, enabling efficient browser-based parametric 3D design without the full footprint of the generic opencascade.js library. It leverages the robust geometry capabilities of OCCT, exposed through a developer-friendly API for code-driven 3D model generation.
Common errors
-
TypeError: initOpenCascade is not a function
cause The import statement for replicad-opencascadejs is incorrect (e.g., using `require` in an ESM context, or attempting a named import when it's a default export).fixEnsure you are using `import initOpenCascade from 'replicad-opencascadejs';` for ESM environments. Verify your build system correctly handles default exports. -
WebAssembly.instantiate(): Compilation failed: expected magic word 0x0061736d
cause The browser or Node.js environment could not find or correctly load the `.wasm` file. This often happens if the `locateFile` option in `initOpenCascade` is misconfigured, or the WASM file is not deployed to the correct path on the server/CDN.fixAdjust the `locateFile` function passed to `initOpenCascade` to return the correct URL path for the `replicad-opencascadejs.wasm` file. Ensure the `.wasm` file is deployed to that accessible path relative to your application's entry point. -
Error: replicad has not been initialised. Please run `setOC(opencascade)` first
cause The `replicad` library attempts to perform a CAD operation (e.g., creating a `Sketcher` instance or a shape) before the `opencascade.js` WebAssembly module has been initialized and its core `OC` object injected into `replicad` via `setOC(OC)`.fixEnsure that the asynchronous `initOpenCascade()` call has completed and `setOC(OC)` has been successfully executed *before* any `replicad` functions that depend on the CAD kernel are invoked. Wrap your `replicad` logic in an `async` function and `await` the initialization.
Warnings
- breaking The underlying opencascade.js library, and by extension replicad-opencascadejs, has undergone significant breaking changes in previous major versions (e.g., in beta releases leading to v1.x). These often involve changes to API conventions, especially around static methods and supported classes, due to updates in the Emscripten build process and OpenCascade Technology (OCCT) versions. Users should consult the specific changelogs for opencascade.js if encountering issues with the underlying CAD kernel.
- gotcha Loading the WebAssembly module can be time-consuming and block the main thread, especially for complex models or older devices. For optimal user experience, it is strongly recommended to load and execute CAD computations within a Web Worker.
- gotcha Custom builds of `replicad-opencascadejs` or `opencascade.js` require Docker and `ytt` for configuration file generation. Directly using the npm package avoids this build-time dependency, but attempting to modify or rebuild the WASM module without these tools will fail.
- gotcha OpenCascade Technology (OCCT) itself has a known limitation called the 'Topological Naming Problem' (TNP). When a model is modified in a way that changes its topology (e.g., adding a hole, filleting an edge), the internal identifiers for faces, edges, and vertices can change. While `replicad` introduces 'finders' to mitigate this for higher-level operations, direct interaction with the underlying OCCT objects via `replicad-opencascadejs` might expose this issue.
Install
-
npm install replicad-opencascadejs -
yarn add replicad-opencascadejs -
pnpm add replicad-opencascadejs
Imports
- initOpenCascade
const initOpenCascade = require('replicad-opencascadejs');import initOpenCascade from 'replicad-opencascadejs';
- OC
const OC = await initOpenCascade();
- setOC
import { setOC } from 'replicad';
Quickstart
import initOpenCascade from 'replicad-opencascadejs';
import { setOC, Sketcher, Extrude, FaceFinder, makeCylinder } from 'replicad';
async function initializeAndCreateModel() {
console.log('Initializing replicad-opencascadejs...');
// The locateFile option is often needed to point to the .wasm file if not in the root
const OC = await initOpenCascade({
locateFile: (path) => {
if (path.endsWith('.wasm')) {
// Adjust this path based on your project's build setup
return `/path/to/replicad-opencascadejs.wasm`;
}
return path;
},
});
setOC(OC);
console.log('OpenCascade initialized and injected into replicad.');
// Example: Create a simple cylindrical body with a hole
const baseCylinder = makeCylinder(10, 30);
const holeSketch = new Sketcher('XY')
.circle(3)
.translate([0, 0, 15]) // Center the hole along the cylinder's height
.extrude(35, { direction: [0, 0, 1] });
const finalShape = baseCylinder.cut(holeSketch);
// You would typically use a viewer or export this shape
// For demonstration, we'll log a representation (e.g., its volume or type)
const volume = finalShape.getVolume();
console.log(`Created a shape with volume: ${volume.toFixed(2)} units³`);
// In a browser environment, you might render it:
// const modelViewerElement = document.getElementById('model-viewer');
// if (modelViewerElement) {
// const stlBlob = await finalShape.toStl();
// const url = URL.createObjectURL(stlBlob);
// modelViewerElement.src = url;
// }
// Clean up if needed, though OC is often long-lived
// OC.Module.clean(); // This depends on opencascade.js API
return finalShape;
}
initializeAndCreateModel().catch(console.error);