replicad

repository·main·Indexed 20 days ago

https://github.com/sgenoud/replicad

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

Tokens
44.3K
Snippets
183
Records
223
Agent score
71%

What's inside replicad

  1. Introduction to replicad

    main
    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.
  2. What is replicad

    main

    replicad 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:

    1. Model Generation: Using the library's API to programmatically define and build 3D geometry.
    2. Application Integration: Integrating the replicad engine into a custom web application to provide CAD-like functionality to end-users.
  3. Use `replicad-evaluator` for portable code evaluation

    main
    The replicad-evaluator package provides portable utilities for evaluating replicad code 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 execute replicad scripts or generate shapes outside of the standard Studio interface.
  4. Understand the purpose of Replicad recipes

    main
    Replicad 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.
  5. Define parametric models with `defaultParams`

    main

    You can make your models interactive by defining parameters that users can adjust via a UI in the share application.

    To do this, define a defaultParams object in your script. This object must be defined alongside your main function. The properties within defaultParams will be passed as the second argument to your main function.

    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...
    }
  6. Combine, negate, or use OR conditions with finders

    main

    Finders allow for complex selection logic through chaining, negation, and logical OR operations:

    • Chaining (AND): By default, chaining methods acts as an AND condition. All conditions must be met.
      • Example: e.ofCurveType("CIRCLE").inPlane("XZ") finds edges that are both circles AND in the XZ plane.
    • 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)])
    • 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"))
  7. Follow the Watering Can tutorial

    main

    The Watering Can tutorial is a step-by-step guide designed to teach you how to use the replicad APIs 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.
  8. Initialize opencascadejs for replicad

    main

    replicad depends on the opencascadejs WebAssembly module. To use replicad, you must initialize opencascadejs and then inject the resulting instance into replicad using the setOC function.

    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();
  9. Serialize and deserialize replicad objects

    main

    Replicad 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:
      1. To a string (Recommended for most use cases).
      2. 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.
  10. Use the replicad online visualiser

    main

    You can prototype and test your 3D models using the online replicad visualiser.

    To use it:

    1. Write your model code in a local file.
    2. Select that file in the visualiser tool.
    3. 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 main function that receives the replicad library as its first argument.

    const main = ({ Sketcher }) => {
      // Your model logic here
      return someShape;
    };