Install web-ifc via npm
mainTo use the web-ifc library in your JavaScript project, install it using npm:
npm install web-ifcrepository·main·Indexed 21 days ago
https://github.com/thatopen/engine_web-ifcA 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.
To use the web-ifc library in your JavaScript project, install it using npm:
npm install web-ifcIf you need to test the newest fixes before an official release, you can use the current live build.
dist folder into your node_modules/web-ifc directory.If you are using web-ifc-three, the path will be node_modules/web-ifc-three/node_modules/web-ifc.
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);To compile the WASM binaries and the JavaScript API yourself, follow these steps:
emsdk_env is in your PATH)npm install to install dependencies.npm run setup-mingw to configure the environment.npm run setup-env in every new terminal session to set up Emscripten environment variables.npm run build-release (outputs to ./dist).npm run build-debug (enables better inspection of debugging information).npm run dev to launch a development server with a basic IFC file viewer.npm install
npm run setup-env
npm run build-releaseWhen 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.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-updateThe build process generates several files for different environments (Browser, Node.js, Multi-threading):
| File | Description |
|---|---|
web-ifc.wasm | WASM (compiled C++) for the browser |
web-ifc-mt.wasm | WASM (compiled C++) for the browser with multi-threading support |
web-ifc-node.wasm | WASM (compiled C++) for Node.js |
web-ifc-api-node.js | JavaScript wrapper for Node.js |
web-ifc-api-node.d.ts | TypeScript definitions for the Node.js API |
web-ifc-api.js | JavaScript wrapper for the browser |
web-ifc-api.d.ts | TypeScript definitions for the main web-ifc API |
ifc-schema.d.ts | TypeScript definitions for the IFC schema |
helpers/properties.d.ts | TypeScript definitions for the properties aspect |
helpers/log.d.ts | TypeScript definitions for the logger aspect |
web-ifc-mt.worker.js | Webworker script to enable multi-threading in the browser |
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);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>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();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
});You can load geometry using two patterns:
LoadAllGeometry(modelID) returns a Vector<FlatMesh> containing all geometry in the model.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);