Skip to content

Commit

Permalink
fix: allow local models in viewer.addModel (#4426)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite authored May 3, 2024
1 parent 1c21eeb commit 880748b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions viewer/packages/api/src/public/migration/Cognite3DViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -705,21 +710,19 @@ export class Cognite3DViewer {
* ```
*/
async addModel(options: AddModelOptions): Promise<CogniteModel> {
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<void>();

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':
Expand Down Expand Up @@ -997,11 +1000,20 @@ export class Cognite3DViewer {
* ```
*/
async determineModelType(modelId: number, revisionId: number): Promise<SupportedModelTypes | ''> {
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<SupportedModelTypes | ''> {
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);

Expand Down
2 changes: 1 addition & 1 deletion viewer/packages/data-source/src/LocalDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export class LocalDataSource implements DataSource {
}

getModelMetadataProvider(): ModelMetadataProvider {
throw new LocalModelMetadataProvider();
return new LocalModelMetadataProvider();
}
}

0 comments on commit 880748b

Please sign in to comment.