From 1e4e93188668febd45cbb89838e58b60d287f530 Mon Sep 17 00:00:00 2001 From: vladyslavtk Date: Mon, 13 Jan 2025 17:26:17 +0200 Subject: [PATCH] Added support of IFC upload --- src/catalogs/demoCatalog.ts | 2 - .../cesium/ngv-plugin-cesium-upload.ts | 42 +++++++++++++++++-- src/plugins/ui/ngv-upload.ts | 13 +++--- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/catalogs/demoCatalog.ts b/src/catalogs/demoCatalog.ts index 738e230..d5f63cb 100644 --- a/src/catalogs/demoCatalog.ts +++ b/src/catalogs/demoCatalog.ts @@ -13,7 +13,6 @@ export const catalog: INGVCatalog = { }, }, position: [6.625727097014207, 46.50662035273721], - // todo apply terrain height automatically? height: 374, rotation: 45, }, @@ -24,7 +23,6 @@ export const catalog: INGVCatalog = { credit: 'Khonos', }, position: [6.625858407650085, 46.50649671101955], - // todo apply terrain height automatically? height: 374, rotation: 239, }, diff --git a/src/plugins/cesium/ngv-plugin-cesium-upload.ts b/src/plugins/cesium/ngv-plugin-cesium-upload.ts index 4c2c2e9..72cf249 100644 --- a/src/plugins/cesium/ngv-plugin-cesium-upload.ts +++ b/src/plugins/cesium/ngv-plugin-cesium-upload.ts @@ -37,8 +37,42 @@ export class NgvPluginCesiumUpload extends LitElement { private uploadedModel: INGVCesiumModel | undefined; async upload(fileDetails: FileUploadDetails): Promise { - const response = await fetch(fileDetails.url); - const arrayBuffer = await response.arrayBuffer(); + let url = fileDetails.url; + let arrayBuffer = + fileDetails.file && (await fileDetails.file.arrayBuffer()); + if (!arrayBuffer && url) { + const response = await fetch(url); + arrayBuffer = await response.arrayBuffer(); + } + if (!arrayBuffer) { + return; + } + + const decoder = new TextDecoder('utf-8'); + const text = decoder.decode(arrayBuffer.slice(0, 15)); + const isIfc = text.startsWith('ISO-10303-21'); + + let blob = new Blob([arrayBuffer]); + url = URL.createObjectURL(blob); + if (isIfc) { + const {ifcToGLTF} = await import('@geoblocks/ifc-gltf'); + try { + const {glb} = await ifcToGLTF({ + url: url, + webIfcSettings: { + wasm: { + path: '/', + absolute: true, + }, + }, + }); + blob = new Blob([glb]); + url = URL.createObjectURL(blob); + } catch (e) { + console.error('Error during file handle or wrong type.', e); + return; + } + } const modelMatrix = Matrix4.fromTranslationRotationScale( new TranslationRotationScale( @@ -52,7 +86,7 @@ export class NgvPluginCesiumUpload extends LitElement { this.uploadedModel = await instantiateModel({ type: 'model', options: { - url: fileDetails.url, + url: url, scene: this.viewer.scene, modelMatrix, id: { @@ -122,7 +156,7 @@ export class NgvPluginCesiumUpload extends LitElement { render(): HTMLTemplateResult { return html` { + @change=${(e: Event) => { const target = e.target; if (!target || !target.files?.length) return; - await this.onFileUpload(target.files[0]); + this.onFileUpload(target.files[0]); }} />