/
ModelState.ts
328 lines (283 loc) · 12.7 KB
/
ModelState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module ModelState
*/
import { Id64, Id64String, JsonUtils } from "@itwin/core-bentley";
import {
GeometricModel2dProps, GeometricModel3dProps, GeometricModelProps, ModelProps, RealityDataFormat, RealityDataSourceKey, RealityModelDisplaySettings, RelatedElement,
} from "@itwin/core-common";
import { Point2d, Range3d } from "@itwin/core-geometry";
import { EntityState } from "./EntityState";
import { HitDetail } from "./HitDetail";
import { IModelConnection } from "./IModelConnection";
import { RealityDataSource } from "./RealityDataSource";
import { createOrbitGtTileTreeReference, createPrimaryTileTreeReference, createRealityTileTreeReference, TileTreeReference } from "./tile/internal";
import { ViewState } from "./ViewState";
import { SpatialClassifiersState } from "./SpatialClassifiersState";
/** Represents the front-end state of a [Model]($backend).
* @public
* @extensions
*/
export class ModelState extends EntityState implements ModelProps {
public static override get className() { return "Model"; }
public readonly modeledElement: RelatedElement;
public readonly name: string;
public parentModel: Id64String;
public readonly isPrivate: boolean;
public readonly isTemplate: boolean;
constructor(props: ModelProps, iModel: IModelConnection, state?: ModelState) {
super(props, iModel, state);
this.modeledElement = RelatedElement.fromJSON(props.modeledElement)!;
this.name = props.name ? props.name : "";
this.parentModel = Id64.fromJSON(props.parentModel)!; // NB! Must always match the model of the modeledElement!
this.isPrivate = JsonUtils.asBool(props.isPrivate);
this.isTemplate = JsonUtils.asBool(props.isTemplate);
}
/** Add all custom-handled properties of a Model to a json object. */
public override toJSON(): ModelProps {
const val = super.toJSON() as ModelProps;
val.modeledElement = this.modeledElement;
val.parentModel = this.parentModel;
val.name = this.name;
if (this.isPrivate)
val.isPrivate = this.isPrivate;
if (this.isTemplate)
val.isTemplate = this.isTemplate;
return val;
}
/** Determine whether this is a GeometricModel */
public get isGeometricModel(): boolean { return false; }
/** Attempts to cast this model to a geometric model. */
public get asGeometricModel(): GeometricModelState | undefined { return undefined; }
/** Attempts to cast this model to a 3d geometric model. */
public get asGeometricModel3d(): GeometricModel3dState | undefined { return undefined; }
/** Attempts to cast this model to a 2d geometric model. */
public get asGeometricModel2d(): GeometricModel2dState | undefined { return undefined; }
/** Attempts to cast this model to a spatial model. */
public get asSpatialModel(): SpatialModelState | undefined { return undefined; }
/**
* Return the tool tip for this model. This is called only if the hit does not return a tooltip.
* @internal
*/
public getToolTip(_hit: HitDetail): HTMLElement | string | undefined { return undefined; }
}
/** Represents the front-end state of a [GeometricModel]($backend).
* The contents of a GeometricModelState can be rendered inside a [[Viewport]].
* @public
* @extensions
*/
export abstract class GeometricModelState extends ModelState implements GeometricModelProps {
public static override get className() { return "GeometricModel"; }
/** @internal */
public geometryGuid?: string;
private _modelRange?: Range3d;
constructor(props: GeometricModelProps, iModel: IModelConnection, state?: GeometricModelState) {
super(props, iModel, state);
this.geometryGuid = props.geometryGuid;
}
/** Returns true if this is a 3d model (a [[GeometricModel3dState]]). */
public abstract get is3d(): boolean;
/** @internal */
public override get asGeometricModel(): GeometricModelState { return this; }
/** Returns true if this is a 2d model (a [[GeometricModel2dState]]). */
public get is2d(): boolean { return !this.is3d; }
/** @internal */
public override get isGeometricModel(): boolean { return true; }
/** @internal */
public get treeModelId(): Id64String { return this.id; }
/** Query for the union of the ranges of all the elements in this GeometricModel.
* @note This value is cached after the first time it is called.
* @public
*/
public async queryModelRange(): Promise<Range3d> {
if (undefined === this._modelRange) {
const ranges = await this.iModel.models.queryExtents(this.id);
this._modelRange = Range3d.fromJSON(ranges[0]?.extents);
}
return this._modelRange;
}
/** @internal */
public createTileTreeReference(view: ViewState): TileTreeReference {
// If this is a reality model, its tile tree is obtained from reality data service URL.
const spatialModel = this.asSpatialModel;
const rdSourceKey = this.jsonProperties.rdSourceKey;
const getDisplaySettings = () => view.displayStyle.settings.getRealityModelDisplaySettings(this.id) ?? RealityModelDisplaySettings.defaults;
if (rdSourceKey) {
const useOrbitGtTileTreeReference = rdSourceKey.format === RealityDataFormat.OPC;
const treeRef = (!useOrbitGtTileTreeReference) ?
createRealityTileTreeReference({
rdSourceKey,
iModel: this.iModel,
source: view,
modelId: this.id,
// url: tilesetUrl, // If rdSourceKey is defined, url is not used
classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
getDisplaySettings,
}) :
createOrbitGtTileTreeReference({
rdSourceKey,
iModel: this.iModel,
source: view,
modelId: this.id,
// orbitGtBlob: props.orbitGtBlob!, // If rdSourceKey is defined, orbitGtBlob is not used
classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
getDisplaySettings,
});
return treeRef;
}
const orbitGtBlob = this.jsonProperties.orbitGtBlob;
// If this is an OrbitGt reality model, create it's reference
if(orbitGtBlob) {
let orbitGtName = "";
if (orbitGtBlob.blobFileName !== "") {
if (orbitGtBlob.blobFileName[0] === "/")
orbitGtName = orbitGtBlob.blobFileName.substring(1);
else
orbitGtName = orbitGtBlob.blobFileName;
}
// Create rdSourceKey if not provided
const rdSourceKeyOGT: RealityDataSourceKey = RealityDataSource.createKeyFromOrbitGtBlobProps(orbitGtBlob);
return createOrbitGtTileTreeReference({
rdSourceKey: rdSourceKeyOGT,
iModel: this.iModel,
source: view,
modelId: this.id,
orbitGtBlob,
name: orbitGtName,
classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
getDisplaySettings,
});
}
// If this is a TileTree reality model, create it's reference
const tilesetUrl = this.jsonProperties.tilesetUrl;
if(tilesetUrl) {
const rdSourceKeyCS = RealityDataSource.createKeyFromUrl(tilesetUrl);
return createRealityTileTreeReference({
rdSourceKey: rdSourceKeyCS,
url : tilesetUrl,
iModel: this.iModel,
source: view,
modelId: this.id,
tilesetToDbTransform: this.jsonProperties.tilesetToDbTransform,
classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
getDisplaySettings,
});
}
return createPrimaryTileTreeReference(view, this);
}
}
/** Represents the front-end state of a [GeometricModel2d]($backend).
* @public
* @extensions
*/
export class GeometricModel2dState extends GeometricModelState implements GeometricModel2dProps {
public static override get className() { return "GeometricModel2d"; }
/** @internal */
public readonly globalOrigin: Point2d;
constructor(props: GeometricModel2dProps, iModel: IModelConnection, state?: GeometricModel2dState) {
super(props, iModel, state);
this.globalOrigin = Point2d.fromJSON(props.globalOrigin);
}
/** @internal */
public get is3d(): boolean { return false; }
/** @internal */
public override get asGeometricModel2d(): GeometricModel2dState { return this; }
public override toJSON(): GeometricModel2dProps {
const val = super.toJSON() as GeometricModel2dProps;
val.globalOrigin = this.globalOrigin;
return val;
}
}
/** Represents the front-end state of a [GeometricModel3d]($backend).
* @public
* @extensions
*/
export class GeometricModel3dState extends GeometricModelState {
public static override get className() { return "GeometricModel3d"; }
constructor(props: GeometricModel3dProps, iModel: IModelConnection, state?: GeometricModel3dState) {
super(props, iModel, state);
this.isNotSpatiallyLocated = JsonUtils.asBool(props.isNotSpatiallyLocated);
this.isPlanProjection = JsonUtils.asBool(props.isPlanProjection);
}
/** @internal */
public override toJSON(): GeometricModel3dProps {
const val = super.toJSON() as GeometricModel3dProps;
if (this.isNotSpatiallyLocated)
val.isNotSpatiallyLocated = true;
if (this.isPlanProjection)
val.isPlanProjection = true;
return val;
}
/** @internal */
public get is3d(): boolean { return true; }
/** @internal */
public override get asGeometricModel3d(): GeometricModel3dState { return this; }
/** If true, then the elements in this GeometricModel3dState are expected to be in an XY plane.
* @note The associated ECProperty was added to the BisCore schema in version 1.0.8
*/
public readonly isPlanProjection: boolean;
/** If true, then the elements in this GeometricModel3dState are not in real-world coordinates and will not be in the spatial index.
* @note The associated ECProperty was added to the BisCore schema in version 1.0.8
*/
public readonly isNotSpatiallyLocated: boolean;
/** If true, then the elements in this GeometricModel3dState are in real-world coordinates and will be in the spatial index. */
public get isSpatiallyLocated(): boolean { return !this.isNotSpatiallyLocated; }
}
/** Represents the front-end state of a [SheetModel]($backend).
* @public
* @extensions
*/
export class SheetModelState extends GeometricModel2dState {
public static override get className() { return "SheetModel"; }
}
/** Represents the front-end state of a [SpatialModel]($backend).
* @public
* @extensions
*/
export class SpatialModelState extends GeometricModel3dState {
/** If this is a reality model, provides access to a list of available spatial classifiers that can be applied to it. */
public readonly classifiers?: SpatialClassifiersState;
public static override get className() { return "SpatialModel"; }
/** @internal */
public override get asSpatialModel(): SpatialModelState { return this; }
public constructor(props: ModelProps, iModel: IModelConnection, state?: SpatialModelState) {
super(props, iModel, state);
if (this.isRealityModel)
this.classifiers = SpatialClassifiersState.create(this.jsonProperties);
}
/** Return true if this is a reality model (represented by a 3d tile set). */
public get isRealityModel(): boolean {
return !!this.jsonProperties.tilesetUrl || !!this.jsonProperties.rdSourceKey || !!this.jsonProperties.orbitGtBlob;
}
}
/** Represents the front-end state of a [PhysicalModel]($backend).
* @public
* @extensions
*/
export class PhysicalModelState extends SpatialModelState {
public static override get className() { return "PhysicalModel"; }
}
/** Represents the front-end state of a [SpatialLocationModel]($backend).
* @public
* @extensions
*/
export class SpatialLocationModelState extends SpatialModelState {
public static override get className() { return "SpatialLocationModel"; }
}
/** Represents the front-end state of a [DrawingModel]($backend).
* @public
* @extensions
*/
export class DrawingModelState extends GeometricModel2dState {
public static override get className() { return "DrawingModel"; }
}
/** Represents the front-end state of a [SectionDrawingModel]($backend).
* @public
* @extensions
*/
export class SectionDrawingModelState extends DrawingModelState {
public static override get className() { return "SectionDrawingModel"; }
}