Skip to content

Commit 31a90d1

Browse files
committed
fix area text label for Polygon
1 parent f665ef5 commit 31a90d1

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

packages/itwin/measure-tools/src/api/Polygon.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import type { Ray3d } from "@itwin/core-geometry";
77
import { Point3d, PolygonOps } from "@itwin/core-geometry";
88
import type { DecorateContext, GraphicBuilder} from "@itwin/core-frontend";
9-
import { IModelApp, QuantityType } from "@itwin/core-frontend";
9+
import { IModelApp } from "@itwin/core-frontend";
1010
import { StyleSet, WellKnownGraphicStyleType, WellKnownTextStyleType } from "./GraphicStyle.js";
1111
import type { TextEntry} from "./TextMarker.js";
1212
import { TextMarker } from "./TextMarker.js";
13+
import type { MeasurementFormattingProps } from "./MeasurementProps.js";
1314

1415
export class Polygon {
1516
public isSelected: boolean;
@@ -25,6 +26,8 @@ export class Polygon {
2526
private _overrideText?: string[] | TextEntry[];
2627
private _textMarker: TextMarker;
2728
private _styleSet: StyleSet;
29+
private _areaKoQ: string;
30+
private _areaPersistenceUnitName: string;
2831

2932
public get points(): Point3d[] {
3033
return this._points;
@@ -76,7 +79,27 @@ export class Polygon {
7679
return this._worldScale ?? 1.0;
7780
}
7881

79-
constructor(points: Point3d[], copyPoints: boolean = true, styleSet?: StyleSet, worldScale?: number) {
82+
public get areaKoQ(): string {
83+
return this._areaKoQ;
84+
}
85+
86+
public set areaKoQ(value: string) {
87+
this._areaKoQ = value;
88+
this.recomputeFromPoints();
89+
}
90+
91+
public get areaPersistenceUnitName(): string {
92+
return this._areaPersistenceUnitName;
93+
}
94+
95+
public set areaPersistenceUnitName(value: string) {
96+
this._areaPersistenceUnitName = value;
97+
this.recomputeFromPoints();
98+
}
99+
100+
constructor(points: Point3d[], copyPoints: boolean = true, styleSet?: StyleSet, worldScale?: number, formatting?: MeasurementFormattingProps) {
101+
this._areaKoQ = formatting?.koqName ?? "AecUnits.AREA";
102+
this._areaPersistenceUnitName = formatting?.persistenceUnitName ?? "Units.SQ_M";
80103
this._styleSet = (styleSet !== undefined) ? styleSet : StyleSet.default;
81104
this.drawMarker = true;
82105
this.drawFillArea = true;
@@ -117,12 +140,12 @@ export class Polygon {
117140
this.recomputeFromPoints();
118141
}
119142

120-
private setTextToMarker() {
143+
private async setTextToMarker() {
121144
if (this._overrideText) {
122145
this._textMarker.textLines = this._overrideText;
123146
} else {
124147
const lines: string[] = [];
125-
const areaFormatter = IModelApp.quantityFormatter.findFormatterSpecByQuantityType(QuantityType.Area);
148+
const areaFormatter = IModelApp.quantityFormatter.getSpecsByName(this._areaKoQ)?.formatterSpec;
126149
if (undefined !== areaFormatter)
127150
lines.push(IModelApp.quantityFormatter.formatQuantity(this.worldScale * this.worldScale * this.area, areaFormatter));
128151

packages/itwin/measure-tools/src/measurements/AreaMeasurement.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class AreaMeasurement extends Measurement {
167167
constructor(props?: AreaMeasurementProps) {
168168
super(props);
169169

170-
this._polygon = new Polygon([], false);
170+
this._polygon = new Polygon([], false, undefined, undefined, props?.formatting?.area);
171171
this._polygon.textMarker.setMouseButtonHandler(
172172
this.handleTextMarkerButtonEvent.bind(this)
173173
);
@@ -245,7 +245,7 @@ export class AreaMeasurement extends Measurement {
245245
if (length === 0) return;
246246

247247
const start = this.polygonPoints[length - 1];
248-
this._dynamicEdge = DistanceMeasurement.create(start, point);
248+
this._dynamicEdge = DistanceMeasurement.create(start, point, undefined, { length: { koqName: this._lengthKoQ, persistenceUnitName: this._lengthPersistenceUnitName }});
249249
if (this.drawingMetadata?.origin)
250250
this._dynamicEdge.drawingMetadata = { origin: this.drawingMetadata.origin, worldScale: this.worldScale };
251251
this._dynamicEdge.sheetViewId = this.sheetViewId;
@@ -631,6 +631,16 @@ export class AreaMeasurement extends Measurement {
631631
super.readFromJSON(json);
632632

633633
const jsonArea = json as AreaMeasurementProps;
634+
if (jsonArea.formatting?.area?.koqName) {
635+
this._areaKoQ = jsonArea.formatting.area.koqName;
636+
this._polygon.areaKoQ = this._areaKoQ;
637+
}
638+
if (jsonArea.formatting?.area?.persistenceUnitName) {
639+
this._areaPersistenceUnitName = jsonArea.formatting.area.persistenceUnitName;
640+
this._polygon.areaPersistenceUnitName = this._areaPersistenceUnitName;
641+
}
642+
if (jsonArea.formatting?.length?.koqName) this._lengthKoQ = jsonArea.formatting.length.koqName;
643+
if (jsonArea.formatting?.length?.persistenceUnitName) this._lengthPersistenceUnitName = jsonArea.formatting.length.persistenceUnitName;
634644
if (jsonArea.polygonPoints !== undefined) {
635645
const pts = new Array<Point3d>();
636646
for (const pt of jsonArea.polygonPoints) pts.push(Point3d.fromJSON(pt));
@@ -641,10 +651,7 @@ export class AreaMeasurement extends Measurement {
641651
this.updateDynamicPolygon(this._dynamicEdge.endPointRef);
642652
}
643653

644-
if (jsonArea.formatting?.area?.koqName) this._areaKoQ = jsonArea.formatting.area.koqName;
645-
if (jsonArea.formatting?.area?.persistenceUnitName) this._areaPersistenceUnitName = jsonArea.formatting.area.persistenceUnitName;
646-
if (jsonArea.formatting?.length?.koqName) this._lengthKoQ = jsonArea.formatting.length.koqName;
647-
if (jsonArea.formatting?.length?.persistenceUnitName) this._lengthPersistenceUnitName = jsonArea.formatting.length.persistenceUnitName;
654+
648655
}
649656

650657
/**

0 commit comments

Comments
 (0)