-
Notifications
You must be signed in to change notification settings - Fork 208
/
TextAnnotationElement.ts
106 lines (90 loc) · 4.54 KB
/
TextAnnotationElement.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Elements
*/
import { GeometryParams, GeometryStreamBuilder, TextAnnotation, TextAnnotation2dProps, TextAnnotation3dProps } from "@itwin/core-common";
import { IModelDb } from "./IModelDb";
import { AnnotationElement2d, GraphicalElement3d } from "./Element";
import { produceTextAnnotationGeometry } from "./TextAnnotationGeometry";
import { Id64String } from "@itwin/core-bentley";
function updateAnnotation(element: TextAnnotation2d | TextAnnotation3d, annotation: TextAnnotation, subCategory: Id64String | undefined): boolean {
const builder = new GeometryStreamBuilder();
const params = new GeometryParams(element.category, subCategory);
if (!builder.appendGeometryParamsChange(params)) {
return false;
}
const props = produceTextAnnotationGeometry({ iModel: element.iModel, annotation });
if (!builder.appendTextBlock(props)) {
return false;
}
element.geom = builder.geometryStream;
element.jsonProperties.annotation = annotation.toJSON();
return true;
}
/** An element that displays textual content within a 2d model.
* The text is stored as a [TextAnnotation]($common) from which the element's [geometry]($docs/learning/common/GeometryStream.md) and [Placement]($common) are computed.
* @see [[setAnnotation]] to change the textual content.
* @public
*/
export class TextAnnotation2d extends AnnotationElement2d {
/** @internal */
public static override get className(): string { return "TextAnnotation2d"; }
protected constructor(props: TextAnnotation2dProps, iModel: IModelDb) { super(props, iModel); }
public static fromJSON(props: TextAnnotation2dProps, iModel: IModelDb): TextAnnotation2d {
return new TextAnnotation2d(props, iModel);
}
public override toJSON(): TextAnnotation2dProps {
return super.toJSON();
}
/** Extract the textual content, if present.
* @see [[setAnnotation]] to change it.
*/
public getAnnotation(): TextAnnotation | undefined {
const json = this.jsonProperties.annotation;
return json ? TextAnnotation.fromJSON(json) : undefined;
}
/** Change the textual content, updating the element's geometry and placement accordingly.
* @see [[getAnnotation]] to extract the current annotation.
* @param annotation The new annotation
* @param subCategory If specified, the subcategory on which to define the geometry; otherwise, the default subcategory of the element's category is used.
* @returns true if the annotation was successfully updated.
*/
public setAnnotation(annotation: TextAnnotation, subCategory?: Id64String): boolean {
return updateAnnotation(this, annotation, subCategory);
}
}
/** An element that displays textual content within a 3d model.
* The text is stored as a [TextAnnotation]($common) from which the element's [geometry]($docs/learning/common/GeometryStream.md) and [Placement]($common) are computed.
* @see [[setAnnotation]] to change the textual content.
* @public
*/
export class TextAnnotation3d extends GraphicalElement3d {
/** @internal */
public static override get className(): string { return "TextAnnotation3d"; }
protected constructor(props: TextAnnotation3dProps, iModel: IModelDb) { super(props, iModel); }
public static fromJSON(props: TextAnnotation3dProps, iModel: IModelDb): TextAnnotation3d {
return new TextAnnotation3d(props, iModel);
}
public override toJSON(): TextAnnotation3dProps {
return super.toJSON();
}
/** Extract the textual content, if present.
* @see [[setAnnotation]] to change it.
*/
public getAnnotation(): TextAnnotation | undefined {
const json = this.jsonProperties.annotation;
return json ? TextAnnotation.fromJSON(json) : undefined;
}
/** Change the textual content, updating the element's geometry and placement accordingly.
* @see [[getAnnotation]] to extract the current annotation.
* @param annotation The new annotation
* @param subCategory If specified, the subcategory on which to define the geometry; otherwise, the default subcategory of the element's category is used.
* @returns true if the annotation was successfully updated.
*/
public setAnnotation(annotation: TextAnnotation, subCategory?: Id64String): boolean {
return updateAnnotation(this, annotation, subCategory);
}
}