Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Modify WebIFCLoaderPlugin to expect externally-provided web-ifc API #1401

Merged
merged 3 commits into from
Mar 6, 2024

Conversation

xeolabs
Copy link
Member

@xeolabs xeolabs commented Mar 6, 2024

This PR removes the bundled web-ifc API implementation from xeokit-sdk, so that WebIFCLoaderPlugin now expects the user to instantiate that API themselves externally and inject it into the WebIFCLoaderPlugin constructor.

Previously, the web-ifc API was instantiated and managed internally within WebIFCLoaderPlugin. This meant that we needed to bundle the API within the xeokit-sdk library, which had the disadvantage of increasing the size of the xeokit-sdk, as well as coupling it to a specific version of web-ifc.

After this PR, users must instantiate and initialize the web-ifc API themselves, then inject it into the WebIFCLoaderPlugin constructor, as shown in the code snippet below.

This is a breaking change in xeokit-sdk for users of our WebIFCLoaderPlugin, but is not a breaking change for xeokit-bim-viewer, which does not use WebIFCLoaderPlugin.

Screenshot from 2024-03-06 20-19-27)

import {Viewer, WebIFCLoaderPlugin} from "xeokit-sdk.es.js";
import * as WebIFC from "https://cdn.jsdelivr.net/npm/[email protected]/web-ifc-api.js";

//------------------------------------------------------------------------------------------------------------------
// 1. Create a Viewer,
// 2. Arrange the camera
//------------------------------------------------------------------------------------------------------------------

// 1
const viewer = new Viewer({
     canvasId: "myCanvas",
     transparent: true
});

// 2
viewer.camera.eye = [-2.56, 8.38, 8.27];
viewer.camera.look = [13.44, 3.31, -14.83];
viewer.camera.up = [0.10, 0.98, -0.14];

//------------------------------------------------------------------------------------------------------------------
// 1. Create a web-ifc API, which will parse IFC for our WebIFCLoaderPlugin
// 2. Connect the API to the web-ifc WASM module, which powers the parsing
// 3. Initialize the web-ifc API
//------------------------------------------------------------------------------------------------------------------

// 1

const IfcAPI = new WebIFC.IfcAPI();

// 2

IfcAPI.SetWasmPath("https://cdn.jsdelivr.net/npm/[email protected]/");

// 3

IfcAPI.Init().then(() => {

     //------------------------------------------------------------------------------------------------------------
     // 1. Create a WebIFCLoaderPlugin, configured with the web-ifc module and the web-ifc API instance
     // 2. Load a BIM model fom an IFC file, excluding its IfcSpace elements, and highlighting edges
     //------------------------------------------------------------------------------------------------------------

    const ifcLoader = new WebIFCLoaderPlugin(viewer, {
        WebIFC,
        IfcAPI
    });

    // 2
    const model = ifcLoader.load({          // Returns an Entity that represents the model
        id: "myModel",
        src: "../assets/models/ifc/Duplex.ifc",
        excludeTypes: ["IfcSpace"],
        edges: true
    });

    model.on("loaded", () => {

        //----------------------------------------------------------------------------------------------------------
        // 1. Find metadata on the bottom storey
        // 2. X-ray all the objects except for the bottom storey
        // 3. Fit the bottom storey in view
        //----------------------------------------------------------------------------------------------------------

        // 1
        const metaModel = viewer.metaScene.metaModels["myModel"];       // MetaModel with ID "myModel"
        const metaObject
                = viewer.metaScene.metaObjects["1xS3BCk291UvhgP2dvNsgp"];  // MetaObject with ID "1xS3BCk291UvhgP2dvNsgp"

        const name = metaObject.name;                                   // "01 eerste verdieping"
        const type = metaObject.type;                                   // "IfcBuildingStorey"
        const parent = metaObject.parent;                               // MetaObject with type "IfcBuilding"
        const children = metaObject.children;                           // Array of child MetaObjects
        const objectId = metaObject.id;                                 // "1xS3BCk291UvhgP2dvNsgp"
        const objectIds = viewer.metaScene.getObjectIDsInSubtree(objectId);   // IDs of leaf sub-objects
        const aabb = viewer.scene.getAABB(objectIds);                   // Axis-aligned boundary of the leaf sub-objects

        // 2
        viewer.scene.setObjectsXRayed(viewer.scene.objectIds, true);
        viewer.scene.setObjectsXRayed(objectIds, false);

        // 3
        viewer.cameraFlight.flyTo(aabb);

        // Find the model Entity by ID
        model = viewer.scene.models["myModel"];

        // Destroy the model
        model.destroy();
    });
});

@xeolabs xeolabs changed the title Modify WebIFCLoaderPlugin to expect externally-provided web-ifc API [BREAKING] Modify WebIFCLoaderPlugin to expect externally-provided web-ifc API Mar 6, 2024
@xeolabs xeolabs merged commit 468992f into master Mar 6, 2024
2 of 4 checks passed
@xeolabs xeolabs added this to the 2.6.0 milestone Apr 21, 2024
@xeolabs xeolabs mentioned this pull request Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant