web-ifc

repository·main·Indexed 21 days ago

https://github.com/thatopen/engine_web-ifc

A high-performance JavaScript library for reading and writing IFC (Industry Foundation Classes) files using WebAssembly. Part of the That Open Company ecosystem, it provides an API (IfcAPI) to load models, retrieve geometry and properties, manage 3D cross sections and alignments, and stream meshes. It supports both browser and Node.js environments with options for multi-threading and custom loader settings.

Tokens
3.7K
Snippets
18
Records
22
Agent score
27%

What's inside web-ifc

  1. Use the latest live build of web-ifc

    main

    If you need to test the newest fixes before an official release, you can use the current live build.

    1. Download the build zip from here.
    2. Manually place the contents of the dist folder into your node_modules/web-ifc directory.
    3. Important: You must replace both the JavaScript files and the WASM files.

    If you are using web-ifc-three, the path will be node_modules/web-ifc-three/node_modules/web-ifc.

  2. Quick setup with web-ifc API

    main

    To use web-ifc, you need to import the API, initialize an instance of IfcAPI, and call Init(). You can then open IFC models using OpenModel() and must remember to free memory by calling CloseModel() when finished.

    const WebIFC = require("web-ifc/web-ifc-api.js");
    
    // initialize the API
    const ifcApi = new WebIFC.IfcAPI();
    
    // initialize the library
    await ifcApi.Init();
    
    // open a model from data
    let modelID = ifcApi.OpenModel(/* IFC data as a string or UInt8Array */, /* optional settings object */, );
    
    // the model is now loaded! use modelID to fetch geometry or properties
    // checkout examples/usage for some details on how to read/write IFC
    
    // close the model, all memory is freed
    ifcApi.CloseModel(modelID);
  3. Build the web-ifc WASM module from source

    main

    To compile the WASM binaries and the JavaScript API yourself, follow these steps:

    Prerequisites

    • Node v16 or later
    • NPM v7 or later
    • EMSCRIPTEN v4.0.23 or later (ensure emsdk_env is in your PATH)
    • CMAKE v3.18 or later
    • MINGW (on Windows)

    Build Steps

    1. Setup Environment:
      • Run npm install to install dependencies.
      • On Windows, run npm run setup-mingw to configure the environment.
      • Run npm run setup-env in every new terminal session to set up Emscripten environment variables.
    2. Compile:
      • For a release version: npm run build-release (outputs to ./dist).
      • For a debug version: npm run build-debug (enables better inspection of debugging information).
    3. Development:
      • Run npm run dev to launch a development server with a basic IFC file viewer.
    npm install
    npm run setup-env
    npm run build-release
  4. Configure LoaderSettings for IFC loading

    main

    When opening models, you can pass a LoaderSettings object to fine-tune the parser and geometry generation.

    Key properties include:

    • COORDINATE_TO_ORIGIN: (boolean) If true, translates the model to the origin.
    • CIRCLE_SEGMENTS: (number) Number of segments used to approximate circles.
    • MEMORY_LIMIT: (number) Maximum memory in bytes to be reserved for IFC data.
    • TOLERANCE_PLANE_INTERSECTION: (number) Numerical tolerance for plane intersections.
    • TOLERANCE_SCALAR_EQUALITY: (number) Tolerance used to compare scalar values.
    • BOOLEAN_UNION_THRESHOLD: (number) Minimum number of solids before triggering a boolean union operation.
  5. Run regression tests

    main

    To ensure geometry consistency, you can run regression tests against the sample models in the tests/public folder:

    • npm run regression: Runs tests and alerts you if sample model geometry has changed.
    • npm run regression-update: Refreshes the regression tests if the changes were intentional.
    npm run regression
    npm run regression-update
  6. Reference: web-ifc Build Outputs

    main

    The build process generates several files for different environments (Browser, Node.js, Multi-threading):

    FileDescription
    web-ifc.wasmWASM (compiled C++) for the browser
    web-ifc-mt.wasmWASM (compiled C++) for the browser with multi-threading support
    web-ifc-node.wasmWASM (compiled C++) for Node.js
    web-ifc-api-node.jsJavaScript wrapper for Node.js
    web-ifc-api-node.d.tsTypeScript definitions for the Node.js API
    web-ifc-api.jsJavaScript wrapper for the browser
    web-ifc-api.d.tsTypeScript definitions for the main web-ifc API
    ifc-schema.d.tsTypeScript definitions for the IFC schema
    helpers/properties.d.tsTypeScript definitions for the properties aspect
    helpers/log.d.tsTypeScript definitions for the logger aspect
    web-ifc-mt.worker.jsWebworker script to enable multi-threading in the browser
  7. Configure the WASM path and log level

    main

    Before using the API, you may need to configure the environment:

    • SetWasmPath(path, absolute): Sets the location of the .wasm file. Use absolute: true if the path is not relative to the executing script.
    • SetLogLevel(level): Sets the logging verbosity for both the JS wrapper and the WASM module.
    ifcApi.SetWasmPath('./wasm/web-ifc.wasm', false);
    ifcApi.SetLogLevel(LogLevel.LOG_LEVEL_INFO);
  8. Retrieve 3D cross sections from IFC entities

    main

    Use GetAllCrossSections3D(modelID) to retrieve all 3D cross sections contained in IFCSECTIONEDSOLID, IFCSECTIONEDSURFACE, or IFCSECTIONEDSOLIDHORIZONTAL (IFC4x3 or superior) entities. The method returns an array of CrossSection objects, where each object contains a coordination matrix and a list of curves (as sets of points) associated with their respective expressIDs.

    const crossSections = ifcApi.GetAllCrossSections3D(modelID);
    // Returns Array<CrossSection>
  9. Initialize the IfcAPI WASM module

    main

    Before using any functionality in web-ifc, you must initialize the IfcAPI instance using the Init method. This method loads the underlying WebAssembly (WASM) module. You can optionally provide a customLocateFileHandler to specify where the WASM files are located, which is useful when using bundlers or specific server configurations.

    If forceSingleThread is set to true, the API will bypass multi-threading attempts and use the single-threaded WASM module.

    import { IfcAPI } from 'web-ifc';
    
    const ifcApi = new IfcAPI();
    await ifcApi.Init();
  10. Stream meshes from an IFC model

    main

    To avoid loading all geometry into memory at once, use the streaming APIs. These methods use a callback function that is invoked for each mesh processed.

    • StreamMeshes(modelID, expressIDs, meshCallback): Streams meshes for a specific set of expressIDs.
    • StreamAllMeshes(modelID, meshCallback): Streams every mesh in the model.
    • StreamAllMeshesWithTypes(modelID, types, meshCallback): Streams all meshes that match a specific list of IFC type IDs.
    ifcApi.StreamAllMeshes(modelID, (mesh, index, total) => {
      console.log(`Processing mesh ${index + 1} of ${total}`);
      // Handle FlatMesh
    });
  11. Load geometry for elements

    main

    You can load geometry using two patterns:

    1. Bulk Load: LoadAllGeometry(modelID) returns a Vector<FlatMesh> containing all geometry in the model.
    2. Single Element: GetFlatMesh(modelID, expressID) returns the FlatMesh for a specific element identified by its expressID.
    // Get a specific mesh
    const mesh = ifcApi.GetFlatMesh(modelID, expressID);
    
    // Get all meshes
    const allMeshes = ifcApi.LoadAllGeometry(modelID);