ifc-web-app/ui.js

106 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;
}
/**
* @param {HTMLElement} container The scene container element
* @returns {OBC.Components}
*/
Actions.createScene = function (container) {
this.init(container);
this.world.scene.setup();
// Add a grid to the scene
const grids = this.components.get(OBC.Grids);
const grid = grids.create(this.world);
// (zoom_level, position)
this.world.camera.controls.setLookAt(15, 15, 15, 0, 0, 0);
}
/**
* @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/",
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;
}
/**
* @namespace UI
*/
const UI = {};
/**
*
* @param {HTMLElement} container The scene container element
*/
UI.setScene = function (container) {
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;