replicad
repository·main·Indexed 20 days ago
https://github.com/sgenoud/replicadA library for programmatic 3D modeling in the browser that provides a high-level abstraction over the OpenCascade CAD engine. It includes tools such as the replicad-cli for evaluating models and exporting to STL, STEP, JSON, or SVG, and the replicad-threejs-helper for synchronizing shapes with Three.js BufferGeometries. The ecosystem also features replicad-evaluator for portable code execution and replicad-opencascadejs for a lightweight WebAssembly module.
What's inside replicad
- replicad is a library designed for building browser-based 3D models using code. It follows the principles of code-based CAD but is built as a library first, providing an abstraction over OpenCascade. This allows developers to either use it as a standalone tool for modeling or integrate it directly into web applications for features like generative 3D design or custom editors.
What is replicad
mainreplicad is a library designed for building 3D models using code directly in the browser. It acts as an abstraction over OpenCascade, allowing developers to leverage powerful CAD capabilities within web applications.
Users typically interact with replicad in one of two ways:
- Model Generation: Using the library's API to programmatically define and build 3D geometry.
- Application Integration: Integrating the replicad engine into a custom web application to provide CAD-like functionality to end-users.
Use `replicad-evaluator` for portable code evaluation
mainThereplicad-evaluatorpackage provides portable utilities for evaluatingreplicadcode and building shapes. It extracts the evaluator logic used in the Replicad Studio worker into a reusable API designed to run in both browser and Node.js environments. Use this package when you need to programmatically executereplicadscripts or generate shapes outside of the standard Studio interface.Understand the purpose of Replicad recipes
mainReplicad uses 'recipes' to handle common CAD operations that are conceptually simple but involve many configuration options and edge cases. Instead of providing a single, overly complex function with a massive signature (which would be difficult to use and maintain), Replicad provides modular code snippets called recipes. Developers are encouraged to copy these recipes into their own projects and tweak them to suit their specific design requirements.Use replicad as a library
mainreplicad is designed as a core library that allows you to build your own custom viewers, editors, or configurators. While it can export models to STL or STEP formats for use in external applications, it is intended to be integrated into web applications for interactive 3D modeling.Define parametric models with `defaultParams`
mainYou can make your models interactive by defining parameters that users can adjust via a UI in the share application.
To do this, define a
defaultParamsobject in your script. This object must be defined alongside yourmainfunction. The properties withindefaultParamswill be passed as the second argument to yourmainfunction.const defaultParams = { height: 85.0, width: 120.0, thickness: 2.0, holeDia: 50.0, hookHeight: 10.0, }; function main( { Sketcher, FaceFinder, EdgeFinder, sketchCircle }, { width, height, thickness, holeDia, hookHeight } ) { // Use the parameters here... }Combine, negate, or use OR conditions with finders
mainFinders allow for complex selection logic through chaining, negation, and logical OR operations:
- Chaining (AND): By default, chaining methods acts as an
ANDcondition. All conditions must be met.- Example:
e.ofCurveType("CIRCLE").inPlane("XZ")finds edges that are both circles AND in the XZ plane.
- Example:
- Either (OR): Use the
.either([...])method to pass an array of filter functions. The feature is selected if it matches any of the provided conditions.- Example:
f.either([(f) => f.inPlane("YZ", 50), (f) => f.inPlane("YZ", -50)])
- Example:
- Negation (NOT): Use the
.not(filterFunction)method to select features that do not match the specified condition.- Example:
e.ofCurveType("CIRCLE").not((f) => f.inPlane("XZ"))
- Example:
- Chaining (AND): By default, chaining methods acts as an
Follow the Watering Can tutorial
mainThe Watering Can tutorial is a step-by-step guide designed to teach you how to use the
replicadAPIs by building a plunge watering can model. You can follow the implementation steps by interacting with the provided code examples in the documentation.// The tutorial implementation is contained in watering-can.js // which is rendered in the documentation iframe.Run the documentation website in local development mode
mainTo start a local development server for the documentation website, use the
yarn startcommand. This will open a browser window and support live reloading for most changes.$ yarn startInitialize opencascadejs for replicad
mainreplicad depends on the
opencascadejsWebAssembly module. To use replicad, you must initializeopencascadejsand then inject the resulting instance into replicad using thesetOCfunction.It is highly recommended to perform this initialization and model computation inside a Web Worker. This prevents the heavy WASM computations from blocking the main UI thread, allowing for a reactive interface.
let loaded = false; const init = async () => { if (loaded) return Promise.resolve(true); // Initialize opencascadejs const OC = await opencascade({ locateFile: () => opencascadeWasm, }); loaded = true; // Inject the instance into replicad setOC(OC); return true; }; const started = init();Serialize and deserialize replicad objects
mainReplicad supports the serialization and deserialization of both 2D drawings and 3D shapes. Each type uses its own specific schema:
- 2D Drawings: Use a dedicated schema for 2D geometry.
- 3D Shapes: Use a dedicated schema for 3D geometry. Shapes can be serialized in two ways:
- To a string (Recommended for most use cases).
- Directly to a file using raw OpenCascade (OC) utilities.
To implement serialization in your application, you should follow the patterns demonstrated in the replicad examples, which typically involve converting the geometry data into a portable format (like a JSON string or a file) and reconstructing the object from that data later.
// Note: The specific implementation details are contained within the // example component source used in the documentation. // Refer to the replicad examples for the exact API calls used to // transform shapes to strings or files.Use the replicad online visualiser
mainYou can prototype and test your 3D models using the online replicad visualiser.
To use it:
- Write your model code in a local file.
- Select that file in the visualiser tool.
- The tool will build your model. If you are using Chrome, the model recomputes automatically as you save changes to your local file.
Note: Your code must define a
mainfunction that receives thereplicadlibrary as its first argument.const main = ({ Sketcher }) => { // Your model logic here return someShape; };