OpenCascade.js Custom Build for Replicad

0.23.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to asynchronously initialize replicad-opencascadejs, inject the resulting OC object into replicad using `setOC`, and then use replicad's API to construct a basic 3D shape (a cylinder with a hole). It highlights the core integration pattern for browser-based CAD applications.

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);

view raw JSON →