-
Notifications
You must be signed in to change notification settings - Fork 208
/
SectionDrawingLocationState.ts
215 lines (192 loc) · 9.81 KB
/
SectionDrawingLocationState.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module HyperModeling
*/
import { Id64String } from "@itwin/core-bentley";
import { Placement3d, QueryRowFormat, SectionType } from "@itwin/core-common";
import { DrawingViewState, IModelConnection, SheetViewState, SpatialViewState } from "@itwin/core-frontend";
import { ClipVector, Transform, XYZProps } from "@itwin/core-geometry";
const selectSectionDrawingLocationStatesECSql = `
SELECT
bis.SectionDrawingLocation.ECInstanceId as sectionLocationId,
bis.SectionDrawingLocation.Model.Id as sectionLocationModelId,
bis.SectionDrawingLocation.SectionView.Id as sectionViewId,
bis.SectionDrawingLocation.Category.Id as categoryId,
bis.SectionDrawingLocation.Origin as origin,
bis.SectionDrawingLocation.Yaw as yaw,
bis.SectionDrawingLocation.Pitch as pitch,
bis.SectionDrawingLocation.Roll as roll,
bis.SectionDrawingLocation.BBoxLow as bboxLow,
bis.SectionDrawingLocation.BBoxHigh as bboxHigh,
bis.SectionDrawingLocation.UserLabel as userLabel,
bis.SectionDrawing.SectionType as sectionType,
json_extract(bis.SectionDrawing.jsonProperties, '$.drawingToSpatialTransform') as drawingToSpatialTransform,
bis.SectionDrawing.SpatialView.Id as spatialViewId,
json_extract(bis.SectionDrawing.jsonProperties, '$.sheetToSpatialTransform') as sheetToSpatialTransform,
json_extract(bis.SectionDrawing.jsonProperties, '$.drawingBoundaryClip') as sheetClip,
json_extract(bis.SpatialViewDefinition.jsonProperties, '$.viewDetails.clip') as clipJSON,
bis.ViewAttachment.ECInstanceId as viewAttachmentId,
bis.SheetViewDefinition.ECInstanceId as sheetViewId
FROM bis.SectionDrawingLocation
INNER JOIN bis.ViewDefinition2d on bis.SectionDrawingLocation.SectionView.Id = bis.ViewDefinition2d.ECInstanceId
INNER JOIN bis.SectionDrawing on bis.ViewDefinition2d.BaseModel.Id = bis.SectionDrawing.ECInstanceId
LEFT JOIN bis.ViewAttachment on bis.ViewDefinition2d.ECInstanceId = bis.ViewAttachment.View.Id
LEFT JOIN bis.SheetViewDefinition on bis.SheetViewDefinition.BaseModel.Id = bis.ViewAttachment.Model.Id
INNER JOIN bis.SpatialViewDefinition on bis.SpatialViewDefinition.ECInstanceId = bis.SectionDrawing.SpatialView.Id
`;
/** Raw data representing a [[SectionDrawingLocationState]]. This is an amalgamation of data from several related ECClasses.
* @internal
*/
export interface SectionDrawingLocationStateData {
sectionType: SectionType;
drawingToSpatialTransform: string; // stringified TransformProps
spatialViewId: Id64String;
clipJSON?: string; // stringified ClipVector json
viewAttachmentId?: Id64String;
sheetToSpatialTransform?: string; // stringified TransformProps
sheetClip?: string; // stringified ClipVector json
sheetViewId?: Id64String;
sectionLocationId: Id64String;
sectionLocationModelId: Id64String;
sectionViewId: Id64String;
categoryId: Id64String;
origin?: XYZProps;
yaw?: number;
pitch?: number;
roll?: number;
bboxLow?: XYZProps;
bboxHigh?: XYZProps;
userLabel: string;
}
/** Represents a [ViewAttachment]($backend) that attaches a [[SectionDrawingLocationState]] to a [Sheet]($backend) model.
* @public
*/
export interface SectionViewAttachment {
/** The view attachment's element Id. */
readonly id: Id64String;
/** The Id of a [SheetViewDefinition]($backend) that displays this view attachment.
* This is used when navigating from the spatial view to the view attachment.
*/
readonly viewId?: Id64String;
/** The transform from the [Sheet]($backend) coordinate space to the spatial coordinate space. */
readonly transformToSpatial: Transform;
/** Optional 2d clip vector used to clip out portions of the [Sheet]($backend) containing annotations not relevant to this section drawing. */
readonly clip?: ClipVector;
}
/** Represents a [SectionDrawingLocation]($backend), including data from related elements like [SectionDrawing]($backend) used in a hyper-modeling context.
* @see [[SectionMarker]] for a widget that allows the user to interact with a section drawing location.
* @public
*/
export class SectionDrawingLocationState {
/** The iModel in which this section drawing location resides. */
public readonly iModel: IModelConnection;
/** The element Id of the section drawing location. */
public readonly id: Id64String;
/** The Id of the [GeometricModel3d]($backend) in which this section drawing location resides. */
public readonly model: Id64String;
/** A user-friendly label for the section drawing location. */
public readonly userLabel: string;
/** The Id of the [SpatialCategory]($backend) to which this section drawing location belongs. */
public readonly category: Id64String;
/** The section drawing location's spatial placement. */
public readonly placement: Placement3d;
/** The type of section that generated the associated [SectionDrawing]($backend). */
public readonly sectionType: SectionType;
/** The Id of the [ViewDefinition]($backend) that can display the section graphics. */
public readonly drawingViewId: Id64String;
/** The Id of the [SpatialViewDefinition]($backend) from which the section drawing was generated. */
public readonly spatialViewId: Id64String;
/** The section clip from the [SpatialViewDefinition]($backend) from which the section drawing was generated. */
public readonly clip: ClipVector;
/** The transform from the [GeometricModel]($backend) containing the section graphics to the spatial coordinate space.
* This is used to position the 2d graphics in the context of a spatial view.
*/
public readonly drawingToSpatialTransform: Transform;
/** If the section drawing is attached to a [Sheet]($backend), details about the corresponding [ViewAttachment]($backend).
* When this section drawing location is displayed in a spatial context, annotations in the sheet model can be displayed along with the section graphics.
*/
public readonly viewAttachment?: SectionViewAttachment;
/** @internal */
public constructor(props: SectionDrawingLocationStateData, iModel: IModelConnection) {
this.iModel = iModel;
this.id = props.sectionLocationId;
this.model = props.sectionLocationModelId;
this.userLabel = props.userLabel;
this.category = props.categoryId;
this.sectionType = props.sectionType;
this.drawingViewId = props.sectionViewId;
this.spatialViewId = props.spatialViewId;
this.drawingToSpatialTransform = Transform.fromJSON(JSON.parse(props.drawingToSpatialTransform));
const extractClip = (str: string | undefined) => {
let clip;
if (str) {
try {
clip = ClipVector.fromJSON(JSON.parse(str));
} catch { }
}
return clip;
};
this.clip = extractClip(props.clipJSON) ?? ClipVector.createEmpty();
const placementProps = {
origin: props.origin ?? {},
angles: {
yaw: props.yaw,
pitch: props.pitch,
roll: props.roll,
},
placement: (props.bboxLow && props.bboxHigh) ? { low: props.bboxLow, high: props.bboxHigh } : undefined,
};
this.placement = Placement3d.fromJSON(placementProps);
if (props.viewAttachmentId && props.sheetToSpatialTransform) {
this.viewAttachment = {
id: props.viewAttachmentId,
transformToSpatial: Transform.fromJSON(JSON.parse(props.sheetToSpatialTransform)),
clip: extractClip(props.sheetClip),
viewId: props.sheetViewId,
};
}
}
/** Return a promise that resolves to the [DrawingViewState]($frontend) corresponding to `this.drawingViewId`, or undefined if an exception occurs. */
public async tryLoadDrawingView(): Promise<DrawingViewState | undefined> {
try {
const view = await this.iModel.views.load(this.drawingViewId);
if (view instanceof DrawingViewState)
return view;
} catch { }
return undefined;
}
/** Return a promise that resolves to the [SpatialViewState]($frontend) corresponding to `this.spatialViewId`, or undefined if an exception occurs. */
public async tryLoadSpatialView(): Promise<SpatialViewState | undefined> {
try {
const view = await this.iModel.views.load(this.spatialViewId);
if (view instanceof SpatialViewState)
return view;
} catch { }
return undefined;
}
/** Return a promise that resolves to the [SheetViewState]($frontend) corresponding to `this.viewAttachment.viewId`; or undefined if there is no corresponding [ViewAttachment]($backend) or an exception occurs. */
public async tryLoadSheetView(): Promise<SheetViewState | undefined> {
if (undefined === this.viewAttachment || undefined === this.viewAttachment.viewId)
return undefined;
try {
const view = await this.iModel.views.load(this.viewAttachment.viewId);
if (view instanceof SheetViewState)
return view;
} catch { }
return undefined;
}
/** Query the specified iModel for [SectionDrawingLocation]($backend)s and return a list of corresponding [[SectionDrawingLocationState]]s. */
public static async queryAll(iModel: IModelConnection): Promise<SectionDrawingLocationState[]> {
const states: SectionDrawingLocationState[] = [];
try {
for await (const row of iModel.createQueryReader(selectSectionDrawingLocationStatesECSql, undefined, { rowFormat: QueryRowFormat.UseJsPropertyNames }))
states.push(new SectionDrawingLocationState(row.toRow() as SectionDrawingLocationStateData, iModel));
} catch {
// If the iModel contains a version of BisCore schema older than 1.12.0, the query will produce an exception due to missing SectionDrawingLocation class. That's fine.
}
return states;
}
}