From 880748bc85d05592d80e7b573bbb5db693a050fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= <70905152+haakonflatval-cognite@users.noreply.github.com> Date: Fri, 3 May 2024 15:40:53 +0200 Subject: [PATCH] fix: allow local models in viewer.addModel (#4426) --- .../src/public/migration/Cognite3DViewer.ts | 38 ++++++++++++------- .../data-source/src/LocalDataSource.ts | 2 +- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/viewer/packages/api/src/public/migration/Cognite3DViewer.ts b/viewer/packages/api/src/public/migration/Cognite3DViewer.ts index 2069de41f88..74ce6f163fe 100644 --- a/viewer/packages/api/src/public/migration/Cognite3DViewer.ts +++ b/viewer/packages/api/src/public/migration/Cognite3DViewer.ts @@ -62,7 +62,12 @@ import { CameraStopDelegate, CameraManagerCallbackData } from '@reveal/camera-manager'; -import { CdfModelIdentifier, File3dFormat, Image360DataModelIdentifier } from '@reveal/data-providers'; +import { + CdfModelIdentifier, + File3dFormat, + Image360DataModelIdentifier, + LocalModelIdentifier +} from '@reveal/data-providers'; import { DataSource, CdfDataSource, LocalDataSource } from '@reveal/data-source'; import { IntersectInput, SupportedModelTypes, LoadingState } from '@reveal/model-base'; @@ -705,21 +710,19 @@ export class Cognite3DViewer { * ``` */ async addModel(options: AddModelOptions): Promise { - if (options.localPath !== undefined) { - throw new Error( - 'addModel() only supports CDF hosted models. Use addCadModel() and addPointCloudModel() to use self-hosted models' - ); - } - const modelLoadSequencer = this._addModelSequencer.getNextSequencer(); return (async () => { let type: '' | SupportedModelTypes; try { - type = await this.determineModelType(options.modelId, options.revisionId); + const modelAddOption = + options.localPath !== undefined + ? { type: 'path' as const, localPath: options.localPath } + : { type: 'cdfId' as const, modelId: options.modelId, revisionId: options.revisionId }; + type = await this.determineModelTypeInternal(modelAddOption); } catch (error) { await modelLoadSequencer(() => {}); - throw new Error(`Failed to add model: ${error}`); + throw new Error(`Failed to add model: ${JSON.stringify(error)}`); } switch (type) { case 'cad': @@ -997,11 +1000,20 @@ export class Cognite3DViewer { * ``` */ async determineModelType(modelId: number, revisionId: number): Promise { - if (this._cdfSdkClient === undefined) { - throw new Error(`${this.determineModelType.name}() is only supported when connecting to Cognite Data Fusion`); - } + return this.determineModelTypeInternal({ type: 'cdfId', modelId, revisionId }); + } + + private async determineModelTypeInternal( + modelOptions: { type: 'cdfId'; modelId: number; revisionId: number } | { type: 'path'; localPath: string } + ): Promise { + const modelIdentifier = (() => { + if (modelOptions.type === 'cdfId') { + return new CdfModelIdentifier(modelOptions.modelId, modelOptions.revisionId); + } else { + return new LocalModelIdentifier(modelOptions.localPath); + } + })(); - const modelIdentifier = new CdfModelIdentifier(modelId, revisionId); const outputs = await this._dataSource.getModelMetadataProvider().getModelOutputs(modelIdentifier); const outputFormats = outputs.map(output => output.format); diff --git a/viewer/packages/data-source/src/LocalDataSource.ts b/viewer/packages/data-source/src/LocalDataSource.ts index efd75e47607..d545b7a7c59 100644 --- a/viewer/packages/data-source/src/LocalDataSource.ts +++ b/viewer/packages/data-source/src/LocalDataSource.ts @@ -25,6 +25,6 @@ export class LocalDataSource implements DataSource { } getModelMetadataProvider(): ModelMetadataProvider { - throw new LocalModelMetadataProvider(); + return new LocalModelMetadataProvider(); } }