ifc-web-app/ui.js
Nicolò P 207b09cbb8 Basic IFC load
TODO: separate styles, use CSS library...
2024-05-28 14:39:36 +02:00

110 lines
2.8 KiB
JavaScript

'use strict';
import * as THREE from 'three';
import * as OBC from 'openbim-components';
import * as WEBIFC from 'web-ifc';
/**
* @namespace Actions
*/
const Actions = {};
/**
*
* @param {HTMLElement} container The scene container element
* @returns {object} {components, world}
*/
Actions.init = function (container) {
const components = new OBC.Components();
const worlds = components.get(OBC.Worlds);
const world = worlds.create();
world.scene = new OBC.SimpleScene(components);
world.renderer = new OBC.SimpleRenderer(components, container);
world.camera = new OBC.SimpleCamera(components);
// Starts the app and updates components at 60 fps
components.init();
this.world = world;
this.components = components;
return {components, world};
}
/**
* @todo Serve web-ifc.wasm locally via AJAX
* @param {OBC.Components} components
* @param {ArrayBuffer} buffer The uploaded IFC file
*/
Actions.loadIfc = async function (buffer) {
const fragments = this.components.get(OBC.FragmentsManager);
// Clean memory before uploading new file
fragments.dispose();
const fragmentIfcLoader = this.components.get(OBC.IfcLoader);
// NOTE: loads web-ifc WASM from https://unpkg.com/web-ifc@0.0.53/
await fragmentIfcLoader.setup();
/*
fragmentIfcLoader.settings.wasm = {
path: "./vendor/web-ifc/web-ifc.wasm",
absolute: false
}
*/
// Excludes IFC categories from the fragment
const excludedCats = [
WEBIFC.IFCTENDONANCHOR,
WEBIFC.IFCREINFORCINGBAR,
WEBIFC.IFCREINFORCINGELEMENT,
];
for (const cat of excludedCats) {
fragmentIfcLoader.settings.excludedCategories.add(cat);
}
fragmentIfcLoader.settings.webIfc.COORDINATE_TO_ORIGIN = true;
fragmentIfcLoader.settings.webIfc.OPTIMIZE_PROFILES = true;
const model = await fragmentIfcLoader.load(buffer);
model.name = "Test";
this.world.scene.three.add(model);
return fragments;
}
/**
* @param {HTMLElement} container The scene container element
* @returns {OBC.Components}
*/
Actions.createScene = function (container) {
//const {components, world} =
this.init(container);
this.world.scene.setup();
this.world.camera.controls.setLookAt(3, 3, 3, 0, 0, 0);
//return components;
}
/**
* @namespace UI
*/
const UI = {};
/**
*
* @param {HTMLElement} container The scene container element
*/
UI.setScene = function (container) {
this.components = Actions.createScene(container);
}
/**
* @param {string} uiContainer The UI div's id
*/
UI.ifcLoader = function () {
const loadBtn = document.querySelector(`#load-ifc`);
loadBtn.onchange = async function () {
const files = this.files;
Actions.loadIfc(new Uint8Array(await files[0].arrayBuffer()));
}
};
export default UI;