/
GeometryStream.ts
758 lines (677 loc) · 33.9 KB
/
GeometryStream.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Geometry
*/
import { Id64, Id64String, IModelStatus } from "@itwin/core-bentley";
import {
Angle, AnyGeometryQuery, GeometryQuery, IModelJson as GeomJson, LineSegment3d, LowAndHighXYZ, Matrix3d, Point2d, Point3d, Range3d, Transform, TransformProps,
Vector3d, XYZProps, YawPitchRollAngles, YawPitchRollProps,
} from "@itwin/core-geometry";
import { ColorDef, ColorDefProps } from "../ColorDef";
import { GeometricElement2dProps, GeometricElement3dProps, GeometryPartProps, isPlacement2dProps, PlacementProps } from "../ElementProps";
import { BackgroundFill, FillDisplay, GeometryClass, GeometryParams } from "../GeometryParams";
import { Gradient } from "../Gradient";
import { IModelError } from "../IModelError";
import { AreaPattern } from "./AreaPattern";
import { ImageGraphic, ImageGraphicProps } from "./ImageGraphic";
import { LineStyle } from "./LineStyle";
import { TextString, TextStringProps } from "./TextString";
import { Base64EncodedString } from "../Base64EncodedString";
import { Placement2d, Placement3d } from "./Placement";
import { TextBlockGeometryProps } from "../annotation/TextBlockGeometryProps";
/** Establish a non-default [[SubCategory]] or to override [[SubCategoryAppearance]] for the geometry that follows.
* A GeometryAppearanceProps always signifies a reset to the [[SubCategoryAppearance]] for subsequent [[GeometryStreamProps]] entries for undefined values.
* @see [[GeometryStreamEntryProps]]
* @public
* @extensions
*/
export interface GeometryAppearanceProps {
/** Optional [[SubCategory]] id for subsequent geometry. Use to create a GeometryStream with geometry that is not on the default [[SubCategory]] for the element's [[Category]] or is has geometry on multiple subCategories. */
subCategory?: Id64String;
/** Optional color to override [[SubCategoryAppearance.color]] for subsequent geometry. */
color?: ColorDefProps;
/** Optional weight to override [[SubCategoryAppearance.weight]] for subsequent geometry. */
weight?: number;
/** Optional style to override [[SubCategoryAppearance.styleId]] for subsequent geometry. */
style?: Id64String;
/** Optional transparency, 0.0 if undefined. Effective transparency is a combination of this value and [[SubCategoryAppearance.color]]. */
transparency?: number;
/** Optional display priority (2d only), 0 if undefined. Effective display priority is a combination of this value and [[SubCategoryAppearance.priority]]. */
displayPriority?: number;
/** Optional GeometryClass (for DGN compatibility, subCategories preferred), [[GeometryClass.Primary]] if undefined. */
geometryClass?: GeometryClass;
}
/** Add a [[gradient]], [[backgroundFill]], or solid [[color]] fill to subsequent planar regions (or meshes).
* Only one value among [[gradient]], [[backgroundFill]], and [[color]] should be set.
* @see [[GeometryStreamEntryProps]]
* @public
* @extensions
*/
export interface AreaFillProps {
/** Fill display type, must be set to something other than [[FillDisplay.Never]] to display fill */
display: FillDisplay;
/** Optional fill transparency, will be the same as outline transparency if undefined. Allows for different fill and outline transparencies */
transparency?: number;
/** Set fill color to view background color. Use [[BackgroundFill.Solid]] for an opaque fill and [[BackgroundFill.Outline]] to display an outline using the line color */
backgroundFill?: BackgroundFill;
/** Set fill color to a specific color. If the fill color the same as the line color, it is an opaque fill, otherwise it is an outline fill */
color?: ColorDefProps;
/** Set fill using gradient properties. */
gradient?: Gradient.SymbProps;
}
/** Override [[SubCategoryAppearance.materialId]] for subsequent surface and solid geometry.
* @see [[GeometryStreamEntryProps]]
* @public
* @extensions
*/
export interface MaterialProps {
/** Material id to use, specify an invalid [[Id64]] to override [[SubCategoryAppearance.materialId]] with no material. */
materialId?: Id64String;
/** @internal */
origin?: XYZProps;
/** @internal */
size?: XYZProps;
/** @internal */
rotation?: YawPitchRollProps;
}
/** JSON representation of a brep GeometryStream entry.
* @public
*/
export namespace BRepEntity {
/** Enum for type of solid kernel entity this represents */
export enum Type {
/** Body consisting of at least one solid region */
Solid = 0,
/** Body consisting of connected sets of faces having edges that are shared by a maximum of two faces */
Sheet = 1,
/** Body consisting of connected sets of edges having vertices that are shared by a maximum of two edges */
Wire = 2,
}
/** Optional symbology that can be assigned to individual faces of a solid or sheet body */
export interface FaceSymbologyProps {
/** Optional color override for face */
color?: ColorDefProps;
/** Optional transparency override for face */
transparency?: number;
/** Optional material override for face */
materialId?: Id64String;
}
/** Geometry entry representing raw brep data.
* @see [[GeometryStreamEntryProps]]
*/
export interface DataProps {
/** data as Base64 encoded string. Must be specifically requested using [[ElementLoadProps.wantBRepData]]. */
data?: Base64EncodedString;
/** body type, default is Solid */
type?: Type;
/** body transform, default is identity */
transform?: TransformProps;
/** body face attachments */
faceSymbology?: FaceSymbologyProps[];
}
}
/** Add a reference to a [[GeometryPart]] from the GeometryStream of a [[GeometricElement]].
* @see [[GeometryStreamEntryProps]]
* @public
* @extensions
*/
export interface GeometryPartInstanceProps {
/** GeometryPart id */
part: Id64String;
/** Optional translation relative to element's placement, 0.0,0.0,0.0 if undefined. For a 2d element/translation, supply non-zero x and y only */
origin?: XYZProps;
/** Optional rotation relative to element's placement, 0.0,0.0,0.0 if undefined. For a 2d element/rotation, supply a non-zero yaw angle only */
rotation?: YawPitchRollProps;
/** Optional scale to apply to part, 1.0 if undefined */
scale?: number;
}
/** Flags applied to the entire contents of a [[GeometryStreamProps]].
* @see GeometryStreamHeaderProps
* @public
* @extensions
*/
export enum GeometryStreamFlags {
/** No flags. */
None = 0,
/** When the geometry is displayed, it is always oriented to face the viewer. The placement origin of the element associated with the geometry is used as the rotation point.
* If the placement origin is outside of the view, the geometry will not necessarily be displayed, even if rotating it to face the viewer would cause its range to intersect the viewed volume.
*/
ViewIndependent = 1 << 0,
}
/** An entry in a [[GeometryStreamProps]] containing [[GeometryStreamFlags]] that apply to the geometry stream as a whole.
* If this entry exists in the [[GeometryStreamProps]] array, it will always be the *first* entry.
* @public
* @extensions
*/
export interface GeometryStreamHeaderProps {
/** The flags applied to the geometry stream. */
flags: GeometryStreamFlags;
}
/** Allowed GeometryStream entries - should only set one value.
* @see [GeometryStream]($docs/learning/common/geometrystream.md)
* @public
* @extensions
*/
export interface GeometryStreamEntryProps extends GeomJson.GeometryProps {
header?: GeometryStreamHeaderProps;
appearance?: GeometryAppearanceProps;
styleMod?: LineStyle.ModifierProps;
fill?: AreaFillProps;
pattern?: AreaPattern.ParamsProps;
material?: MaterialProps;
geomPart?: GeometryPartInstanceProps;
textString?: TextStringProps;
brep?: BRepEntity.DataProps;
image?: ImageGraphicProps;
subRange?: LowAndHighXYZ;
}
/** A [[GeometricElement]]'s GeometryStream is represented by an array of [[GeometryStreamEntryProps]].
* @public
* @extensions
*/
export type GeometryStreamProps = GeometryStreamEntryProps[];
/** GeometryStreamBuilder is a helper class for populating the [[GeometryStreamProps]] array needed to create a [[GeometricElement]] or [[GeometryPart]].
* @public
*/
export class GeometryStreamBuilder {
/** Current inverse placement transform, used for converting world coordinate input to be placement relative */
private _worldToLocal?: Transform;
/** GeometryStream entries */
public readonly geometryStream: GeometryStreamProps = [];
/** Supply optional local to world transform. Used to transform world coordinate input relative to element placement.
* For a [[GeometricElement]]'s placement to be meaningful, world coordinate geometry should never be appended to an element with an identity placement.
* Can be called with undefined or identity transform to start appending geometry supplied in local coordinates again.
*/
public setLocalToWorld(localToWorld?: Transform) {
this._worldToLocal = (undefined === localToWorld || localToWorld.isIdentity ? undefined : localToWorld.inverse());
}
/** Supply local to world transform from a Point3d and optional YawPitchRollAngles.
* @see [[Placement3d]]
*/
public setLocalToWorld3d(origin: Point3d, angles: YawPitchRollAngles = YawPitchRollAngles.createDegrees(0.0, 0.0, 0.0)) {
this.setLocalToWorld(Transform.createOriginAndMatrix(origin, angles.toMatrix3d()));
}
/** Supply local to world transform from a Point2d and optional Angle.
* @see [[Placement2d]]
*/
public setLocalToWorld2d(origin: Point2d, angle: Angle = Angle.createDegrees(0.0)) {
this.setLocalToWorld(Transform.createOriginAndMatrix(Point3d.createFrom(origin), Matrix3d.createRotationAroundVector(Vector3d.unitZ(), angle)));
}
/** Supply local to world transform from a PlacementProps2d or PlacementProps3d.
* @see [[PlacementProps]]
*/
public setLocalToWorldFromPlacement(props: PlacementProps) {
const placement = isPlacement2dProps(props) ? Placement2d.fromJSON(props) : Placement3d.fromJSON(props);
this.setLocalToWorld(placement.transform);
}
/** Store local ranges in GeometryStream for all subsequent geometry appended. Can improve performance of range testing for elements with a GeometryStream
* containing more than one [[GeometryQuery]] differentiable by range. Not useful for a single [[GeometryQuery]] as its range and that of the [[GeometricElement]] are the same.
* Ignored when defining a [[GeometryPart]] and not needed when only appending [[GeometryPart]] instances to a [[GeometricElement]] as these store their own range.
*/
public appendGeometryRanges() {
this.geometryStream.push({ subRange: Range3d.createNull() });
}
/** Change [[SubCategory]] or reset to [[SubCategoryAppearance]] for subsequent geometry.
* An invalid sub-category id can be supplied to force a reset to the current [[SubCategoryAppearance]].
* It is not valid to change the sub-category when defining a [[GeometryPart]]. A [[GeometryPart]] inherit the symbology of their instance for anything not explicitly overridden.
*/
public appendSubCategoryChange(subCategoryId: Id64String): boolean {
this.geometryStream.push({ appearance: { subCategory: subCategoryId } });
return true;
}
/** Change [[GeometryParams]] for subsequent geometry.
* It is not valid to change the sub-category when defining a [[GeometryPart]]. A [[GeometryPart]] inherits the symbology of their instance for anything not explicitly overridden.
*/
public appendGeometryParamsChange(geomParams: GeometryParams): boolean {
const appearance: GeometryAppearanceProps = {
subCategory: geomParams.subCategoryId,
color: geomParams.lineColor?.toJSON(),
weight: geomParams.weight,
style: geomParams.styleInfo?.styleId,
transparency: geomParams.elmTransparency,
displayPriority: geomParams.elmPriority,
geometryClass: geomParams.geometryClass,
};
this.geometryStream.push({ appearance });
if (undefined !== geomParams.materialId)
this.geometryStream.push({ material: { materialId: geomParams.materialId } });
if (undefined !== geomParams.fillDisplay && FillDisplay.Never !== geomParams.fillDisplay) {
const fill: AreaFillProps = {
display: geomParams.fillDisplay,
transparency: geomParams.fillTransparency,
};
if (undefined !== geomParams.gradient && Gradient.Mode.None !== geomParams.gradient.mode)
fill.gradient = geomParams.gradient?.toJSON();
else if (undefined !== geomParams.backgroundFill && BackgroundFill.None !== geomParams.backgroundFill)
fill.backgroundFill = geomParams.backgroundFill;
else if (undefined !== geomParams.fillColor)
fill.color = geomParams.fillColor.toJSON();
this.geometryStream.push({ fill });
}
if (undefined !== geomParams.pattern) {
const localPattern = this._worldToLocal ? geomParams.pattern.clone() : geomParams.pattern;
if (undefined !== this._worldToLocal && !localPattern.applyTransform(this._worldToLocal))
return false;
this.geometryStream.push({ pattern: localPattern.toJSON() });
}
if (undefined !== geomParams.styleInfo && undefined !== geomParams.styleInfo.styleMod) {
const localStyleMod = new LineStyle.Modifier(geomParams.styleInfo.styleMod);
if (undefined !== this._worldToLocal && !localStyleMod.applyTransform(this._worldToLocal))
return false;
this.geometryStream.push({ styleMod: localStyleMod });
}
return true;
}
/** Append a [[GeometryPart]] instance with relative position, orientation, and scale to a [[GeometryStreamProps]] array for creating a [[GeometricElement3d]].
* Not valid when defining a [[GeometryPart]] as nesting of parts is not supported.
*/
public appendGeometryPart3d(partId: Id64String, instanceOrigin?: Point3d, instanceRotation?: YawPitchRollAngles, instanceScale?: number): boolean {
if (undefined === this._worldToLocal) {
this.geometryStream.push({ geomPart: { part: partId, origin: instanceOrigin, rotation: instanceRotation, scale: instanceScale } });
return true;
}
const partTrans = Transform.createOriginAndMatrix(instanceOrigin, instanceRotation ? instanceRotation.toMatrix3d() : Matrix3d.createIdentity());
if (undefined !== instanceScale)
partTrans.matrix.scaleColumnsInPlace(instanceScale, instanceScale, instanceScale);
const resultTrans = this._worldToLocal.multiplyTransformTransform(partTrans);
const scales = new Vector3d();
if (!resultTrans.matrix.normalizeColumnsInPlace(scales))
return false;
const newRotation = YawPitchRollAngles.createFromMatrix3d(resultTrans.matrix);
if (undefined === newRotation)
return false;
this.geometryStream.push({ geomPart: { part: partId, origin: resultTrans.getOrigin(), rotation: newRotation, scale: scales.x } });
return true;
}
/** Append a [[GeometryPart]] instance with relative position, orientation, and scale to a [[GeometryStreamProps]] array for creating a [[GeometricElement2d]].
* Not valid when defining a [[GeometryPart]] as nesting of parts is not supported.
*/
public appendGeometryPart2d(partId: Id64String, instanceOrigin?: Point2d, instanceRotation?: Angle, instanceScale?: number): boolean {
return this.appendGeometryPart3d(partId, instanceOrigin ? Point3d.createFrom(instanceOrigin) : undefined, instanceRotation ? new YawPitchRollAngles(instanceRotation) : undefined, instanceScale);
}
/** Append a [[TextString]] supplied in either local or world coordinates to the [[GeometryStreamProps]] array */
public appendTextString(textString: TextString): boolean {
if (this._worldToLocal) {
textString = new TextString(textString);
if (!textString.transformInPlace(this._worldToLocal)) {
return false;
}
}
this.geometryStream.push({ textString: textString.toJSON() });
return true;
}
/** Append a series of entries representing a [[TextBlock]] to the [[GeometryStreamProps]] array.
* @beta
*/
public appendTextBlock(block: TextBlockGeometryProps): boolean {
for (const entry of block.entries) {
let result: boolean;
if (entry.text) {
result = this.appendTextString(new TextString(entry.text));
} else if (entry.color) {
if (entry.color === "subcategory") {
result = this.appendSubCategoryChange(Id64.invalid);
} else {
this.geometryStream.push({ appearance: { color: entry.color } });
result = true;
}
} else {
result = this.appendGeometry(LineSegment3d.fromJSON(entry.separator));
}
if (!result) {
return false;
}
}
return true;
}
/** Append an [[ImageGraphic]] supplied in either local or world coordinates. */
public appendImage(image: ImageGraphic): boolean {
if (undefined !== this._worldToLocal)
image = image.cloneTransformed(this._worldToLocal);
this.geometryStream.push({ image: image.toJSON() });
return true;
}
/** Append a [[GeometryQuery]] supplied in either local or world coordinates to the [[GeometryStreamProps]] array */
public appendGeometry(geometry: GeometryQuery): boolean {
if (undefined === this._worldToLocal) {
const geomData = GeomJson.Writer.toIModelJson(geometry);
if (undefined === geomData)
return false;
this.geometryStream.push(geomData);
return true;
}
const localGeometry = geometry.cloneTransformed(this._worldToLocal);
if (undefined === localGeometry)
return false;
const localGeomData = GeomJson.Writer.toIModelJson(localGeometry);
if (undefined === localGeomData)
return false;
this.geometryStream.push(localGeomData);
return true;
}
/** Append [[BRepEntity.DataProps]] supplied in either local or world coordinates to the [[GeometryStreamProps]] array
* @beta
*/
public appendBRepData(brep: BRepEntity.DataProps): boolean {
if (undefined === this._worldToLocal) {
this.geometryStream.push({ brep });
return true;
}
const entityTrans = Transform.fromJSON(brep.transform);
const localTrans = entityTrans.multiplyTransformTransform(this._worldToLocal);
const localBrep: BRepEntity.DataProps = {
data: brep.data,
type: brep.type,
transform: localTrans.isIdentity ? undefined : localTrans.toJSON(),
faceSymbology: brep.faceSymbology,
};
this.geometryStream.push({ brep: localBrep });
return true;
}
/** @internal */
public getHeader(): GeometryStreamHeaderProps | undefined {
return 0 < this.geometryStream.length ? this.geometryStream[0].header : undefined;
}
/** @internal */
public obtainHeader(): GeometryStreamHeaderProps {
const hdr = this.getHeader();
if (undefined !== hdr)
return hdr;
const entry = { header: { flags: GeometryStreamFlags.None } };
this.geometryStream.unshift(entry);
return entry.header;
}
/** Controls whether or not the geometry in the stream should be displayed as view-independent.
* When view-independent geometry is displayed, it is always oriented to face the viewer, using the placement origin of the element as the rotation point.
* If the placement origin is outside of the view, the geometry will not necessarily be displayed, even if rotating it to face the viewer would cause its range to intersect the viewed volume
* @public
*/
public get isViewIndependent(): boolean {
const hdr = this.getHeader();
return undefined !== hdr && GeometryStreamFlags.None !== (hdr.flags & GeometryStreamFlags.ViewIndependent);
}
public set isViewIndependent(viewIndependent: boolean) {
if (viewIndependent === this.isViewIndependent)
return;
const hdr = this.obtainHeader();
if (viewIndependent)
hdr.flags |= GeometryStreamFlags.ViewIndependent;
else
hdr.flags &= ~GeometryStreamFlags.ViewIndependent;
}
}
/** Represents a text string within a GeometryStream.
* @public
* @extensions
*/
export interface TextStringPrimitive {
type: "textString";
readonly textString: TextString;
}
/** Represents an image within a GeometryStream.
* @public
* @extensions
*/
export interface ImagePrimitive {
type: "image";
readonly image: ImageGraphic;
}
/** Represents a reference to a GeometryPart within a GeometryStream.
* @public
* @extensions
*/
export interface PartReference {
type: "partReference";
part: {
id: Id64String;
readonly toLocal?: Transform;
};
}
/** Represents a BRep within a GeometryStream.
* @public
* @extensions
*/
export interface BRepPrimitive {
type: "brep";
/** @beta */
readonly brep: BRepEntity.DataProps;
}
/** Represents one of a variety of GeometryQuery objects within a GeometryStream.
* @public
* @extensions
*/
export interface GeometryPrimitive {
type: "geometryQuery";
readonly geometry: AnyGeometryQuery;
}
/** Union of all possible geometric primitive types that may appear within a GeometryStream.
* @public
* @extensions
*/
export type GeometryStreamPrimitive = TextStringPrimitive | PartReference | BRepPrimitive | GeometryPrimitive | ImagePrimitive;
/** Holds current state information for [[GeometryStreamIterator]]. Each entry represents exactly one geometry primitive in the stream.
* @public
* @extensions
*/
export interface GeometryStreamIteratorEntry {
/** A [[GeometryParams]] representing the appearance of the current geometric entry */
readonly geomParams: GeometryParams;
/** Placement transform, used for converting placement relative, local coordinate entries to world */
readonly localToWorld?: Transform;
/** Optional stored local range for the current geometric entry */
readonly localRange?: Range3d;
/** Returns the geometric primitive represented by this entry. */
readonly primitive: GeometryStreamPrimitive;
}
class IteratorEntry implements GeometryStreamIteratorEntry {
private _primitive?: GeometryStreamPrimitive;
public readonly geomParams: GeometryParams;
public readonly localToWorld?: Transform;
public localRange?: Range3d;
public constructor(appearance: Id64String | GeometryParams, localToWorld?: Transform) {
this.geomParams = typeof appearance === "string" ? new GeometryParams(appearance) : appearance;
this.localToWorld = localToWorld;
}
public get primitive() { return this._primitive!; }
public set primitive(primitive: GeometryStreamPrimitive) { this._primitive = primitive; }
public setGeometryQuery(geometry: AnyGeometryQuery) { this._primitive = { type: "geometryQuery", geometry }; }
public setTextString(textString: TextString) { this._primitive = { type: "textString", textString }; }
public setBRep(brep: BRepEntity.DataProps) { this._primitive = { type: "brep", brep }; }
public setImage(image: ImageGraphic) { this._primitive = { type: "image", image }; }
public setPartReference(id: Id64String, toLocal?: Transform) {
this._primitive = {
type: "partReference",
part: { id, toLocal },
};
}
}
/** GeometryStreamIterator is a helper class for iterating a [[GeometryStreamProps]].
* A [[GeometricElement]]'s GeometryStream must be specifically requested using [[ElementLoadProps.wantGeometry]].
* Each [[GeometryStreamIteratorEntry]] returned by the iterator represents exactly one geometric primitive in the stream.
* @public
*/
export class GeometryStreamIterator implements IterableIterator<GeometryStreamIteratorEntry> {
/** GeometryStream entries */
public geometryStream: GeometryStreamProps;
/** Flags applied to the entire geometry stream. */
public readonly flags: GeometryStreamFlags;
/** Current entry position */
private _index = 0;
/** Allocated on first call to next() and reused thereafter. */
private _entry?: IteratorEntry;
/** Used to initialize this._entry. */
private readonly _appearance: Id64String | GeometryParams;
private readonly _localToWorld?: Transform;
/** Construct a new GeometryStreamIterator given a [[GeometryStreamProps]] from either a [[GeometricElement3d]], [[GeometricElement2d]], or [[GeometryPart]].
* Supply the [[GeometricElement]]'s category to initialize the appearance information for each geometric entry.
*/
public constructor(geometryStream: GeometryStreamProps, categoryOrGeometryParams?: Id64String | GeometryParams, localToWorld?: Transform) {
this.geometryStream = geometryStream;
this._appearance = categoryOrGeometryParams ?? Id64.invalid;
this._localToWorld = localToWorld;
if (0 < geometryStream.length && undefined !== geometryStream[0].header) {
this.flags = geometryStream[0].header.flags;
++this._index;
} else {
this.flags = GeometryStreamFlags.None;
}
}
// eslint-disable-next-line @typescript-eslint/naming-convention
private get entry() {
if (undefined === this._entry)
this._entry = new IteratorEntry(this._appearance, this._localToWorld);
return this._entry;
}
/** Create a new GeometryStream iterator for a [[GeometricElement3d]].
* If [[GeometricElement3dProps.placement]] is not undefined, placement relative entries will be returned transformed to world coordinates.
* @throws [[IModelError]] if element.geom is undefined.
*/
public static fromGeometricElement3d(element: Pick<GeometricElement3dProps, "geom" | "placement" | "category">) {
if (element.geom === undefined)
throw new IModelError(IModelStatus.NoGeometry, "GeometricElement has no geometry or geometry wasn't requested");
let transform;
if (element.placement !== undefined)
transform = Transform.createOriginAndMatrix(Point3d.fromJSON(element.placement.origin), YawPitchRollAngles.fromJSON(element.placement.angles).toMatrix3d());
return new GeometryStreamIterator(element.geom, element.category, transform);
}
/** Create a new GeometryStream iterator for a [[GeometricElement2d]].
* If [[GeometricElement2dProps.placement]] is not undefined, placement relative entries will be returned transformed to world coordinates.
* @throws [[IModelError]] if element.geom is undefined.
*/
public static fromGeometricElement2d(element: Pick<GeometricElement2dProps, "geom" | "placement" | "category">) {
if (element.geom === undefined)
throw new IModelError(IModelStatus.NoGeometry, "GeometricElement has no geometry or geometry wasn't requested");
let transform;
if (element.placement !== undefined) {
const origin = Point3d.createFrom(Point2d.fromJSON(element.placement.origin));
const matrix = Matrix3d.createRotationAroundVector(Vector3d.unitZ(), Angle.fromJSON(element.placement.angle))!;
transform = Transform.createOriginAndMatrix(origin, matrix);
}
return new GeometryStreamIterator(element.geom, element.category, transform);
}
/** Create a new GeometryStream iterator for a [[GeometryPart]].
* To iterate a part's GeometryStream in the context of a part instance found for a [[GeometricElement]], provide the optional [[GeometryParams]] and Transform from the [[GeometricElement]]'s [[GeometryStreamIterator]].
* Supply the [[GeometryParams]] to return appearance information as inherited from the [[GeometricElement]].
* Supply the partToWorld transform to return the part geometry in world coordinates.
* Supply the partToLocal transform to return the part geometry relative to the [[GeometricElement]]'s placement.
* @throws [[IModelError]] if geomPart.geom is undefined.
*/
public static fromGeometryPart(geomPart: Pick<GeometryPartProps, "geom">, geomParams?: GeometryParams, partTransform?: Transform) {
if (geomPart.geom === undefined)
throw new IModelError(IModelStatus.NoGeometry, "GeometryPart has no geometry or geometry wasn't requested");
return new GeometryStreamIterator(geomPart.geom, geomParams?.clone(), partTransform);
}
/** Get the transform that if applied to a [[GeometryPart]]'s GeometryStream entries would return them in world coordinates. */
public partToWorld(): Transform | undefined {
if (undefined === this._entry)
return this._localToWorld;
const partToLocal = "partReference" === this._entry.primitive.type ? this._entry.primitive.part.toLocal : undefined;
if (this._entry.localToWorld === undefined || partToLocal === undefined)
return this._entry.localToWorld;
return this._entry.localToWorld.multiplyTransformTransform(partToLocal);
}
/** Advance to next displayable geometric entry while updating the current [[GeometryParams]] from appearance related entries.
* Geometric entries are [[TextString]], [[GeometryQuery]], [[GeometryPart]], [[ImageGraphic]], and [[BRepEntity.DataProps]].
*/
public next(): IteratorResult<GeometryStreamIteratorEntry> {
// NOTE: localRange remains valid until we encounter either a new subRange entry or a geometry part reference.
while (this._index < this.geometryStream.length) {
const entry = this.geometryStream[this._index++];
if (entry.appearance) {
this.entry.geomParams.resetAppearance();
if (entry.appearance.subCategory)
this.entry.geomParams.subCategoryId = Id64.fromJSON(entry.appearance.subCategory);
if (undefined !== entry.appearance.color)
this.entry.geomParams.lineColor = ColorDef.fromJSON(entry.appearance.color);
if (undefined !== entry.appearance.weight)
this.entry.geomParams.weight = entry.appearance.weight;
if (undefined !== entry.appearance.style)
this.entry.geomParams.styleInfo = new LineStyle.Info(Id64.fromJSON(entry.appearance.style));
if (undefined !== entry.appearance.transparency)
this.entry.geomParams.elmTransparency = entry.appearance.transparency;
if (undefined !== entry.appearance.displayPriority)
this.entry.geomParams.elmPriority = entry.appearance.displayPriority;
if (undefined !== entry.appearance.geometryClass)
this.entry.geomParams.geometryClass = entry.appearance.geometryClass;
} else if (entry.styleMod) {
if (this.entry.geomParams.styleInfo === undefined)
continue;
const styleMod = new LineStyle.Modifier(entry.styleMod);
if (this.entry.localToWorld !== undefined)
styleMod.applyTransform(this.entry.localToWorld);
this.entry.geomParams.styleInfo = new LineStyle.Info(this.entry.geomParams.styleInfo.styleId, styleMod);
} else if (entry.fill) {
if (entry.fill.display)
this.entry.geomParams.fillDisplay = entry.fill.display;
if (entry.fill.transparency)
this.entry.geomParams.fillTransparency = entry.fill.transparency;
if (entry.fill.gradient)
this.entry.geomParams.gradient = Gradient.Symb.fromJSON(entry.fill.gradient);
else if (entry.fill.backgroundFill)
this.entry.geomParams.backgroundFill = entry.fill.backgroundFill;
else if (entry.fill.color)
this.entry.geomParams.fillColor = ColorDef.fromJSON(entry.fill.color);
} else if (entry.pattern) {
const params = AreaPattern.Params.fromJSON(entry.pattern);
if (this.entry.localToWorld !== undefined)
params.applyTransform(this.entry.localToWorld);
this.entry.geomParams.pattern = params;
} else if (entry.material) {
if (entry.material.materialId)
this.entry.geomParams.materialId = Id64.fromJSON(entry.material.materialId);
} else if (entry.subRange) {
this.entry.localRange = Range3d.fromJSON(entry.subRange);
} else if (entry.geomPart) {
let transform;
if (entry.geomPart.origin !== undefined || entry.geomPart.rotation !== undefined || entry.geomPart.scale !== undefined) {
const origin = entry.geomPart.origin ? Point3d.fromJSON(entry.geomPart.origin) : Point3d.createZero();
const rotation = entry.geomPart.rotation ? YawPitchRollAngles.fromJSON(entry.geomPart.rotation).toMatrix3d() : Matrix3d.createIdentity();
transform = Transform.createRefs(origin, rotation);
if (entry.geomPart.scale)
transform.multiplyTransformTransform(Transform.createRefs(Point3d.createZero(), Matrix3d.createUniformScale(entry.geomPart.scale)), transform);
}
// Subgraphic range doesn't apply to parts. A sane geometry stream (i.e., any that has been through the native layers or GeometryStreamBuilder)
// will have a new subgraphic range for any geometric primitive following the part.
this.entry.localRange = undefined;
this.entry.setPartReference(Id64.fromJSON(entry.geomPart.part), transform);
return { value: this.entry, done: false };
} else if (entry.textString) {
const textString = new TextString(entry.textString);
if (this.entry.localToWorld !== undefined)
textString.transformInPlace(this.entry.localToWorld);
this.entry.setTextString(textString);
return { value: this.entry, done: false };
} else if (entry.image) {
const image = ImageGraphic.fromJSON(entry.image);
if (undefined !== this.entry.localToWorld)
image.transformInPlace(this.entry.localToWorld);
this.entry.setImage(image);
return { value: this.entry, done: false };
} else if (entry.brep) {
if (this.entry.localToWorld !== undefined) {
const entityTrans = Transform.fromJSON(entry.brep.transform);
entry.brep.transform = entityTrans.multiplyTransformTransform(this.entry.localToWorld).toJSON();
}
this.entry.setBRep(entry.brep);
return { value: this.entry, done: false };
} else {
const geometryQuery = GeomJson.Reader.parse(entry);
if (!(geometryQuery instanceof GeometryQuery))
continue;
if (this.entry.localToWorld !== undefined)
geometryQuery.tryTransformInPlace(this.entry.localToWorld);
this.entry.setGeometryQuery(geometryQuery);
return { value: this.entry, done: false };
}
}
return { value: this.entry, done: true };
}
public [Symbol.iterator](): IterableIterator<GeometryStreamIteratorEntry> {
return this;
}
/** @internal */
public get isViewIndependent(): boolean { return GeometryStreamFlags.None !== (this.flags & GeometryStreamFlags.ViewIndependent); }
}