@itwin/measure-tools-react
Version:
Frontend framework and tools for measurements
134 lines • 5.01 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { Point3d, PolygonOps } from "@itwin/core-geometry";
import { IModelApp, QuantityType } from "@itwin/core-frontend";
import { StyleSet, WellKnownGraphicStyleType, WellKnownTextStyleType } from "./GraphicStyle.js";
import { TextMarker } from "./TextMarker.js";
export class Polygon {
get points() {
return this._points;
}
get perimeter() {
return this._perimeter;
}
get area() {
return this._area;
}
get areaXY() {
return this._areaXY;
}
get overrideText() {
return this._overrideText;
}
set overrideText(text) {
this._overrideText = text;
this.setTextToMarker();
}
get center() {
return this._textMarker.worldLocation;
}
get textMarker() {
return this._textMarker;
}
get styleSet() {
return this._styleSet;
}
set styleSet(value) {
this._styleSet = value;
this._textMarker.applyStyle(this._styleSet.getTextStyle(WellKnownTextStyleType.AreaMeasurement));
}
set worldScale(scale) {
this._worldScale = scale;
}
get worldScale() {
return this._worldScale ?? 1.0;
}
constructor(points, copyPoints = true, styleSet, worldScale) {
this._styleSet = (styleSet !== undefined) ? styleSet : StyleSet.default;
this.drawMarker = true;
this.drawFillArea = true;
this._worldScale = worldScale;
this._points = (copyPoints) ? this.copyPoints(points) : points;
this._perimeter = this.calculatePerimeter(this.points);
this._area = Math.abs(PolygonOps.area(this.points));
this._areaXY = Math.abs(PolygonOps.areaXY(this.points));
const center = this.getCenter(this.points);
this._textMarker = TextMarker.createStyled([], center, this._styleSet.getTextStyle(WellKnownTextStyleType.AreaMeasurement));
this._textMarker.pickable = false;
this._textMarker.setMouseEnterHandler(() => { this.isSelected = true; });
this._textMarker.setMouseLeaveHandler(() => { this.isSelected = false; });
this.isSelected = false;
this.recomputeFromPoints();
}
recomputeFromPoints() {
this._perimeter = this.calculatePerimeter(this.points);
this._area = Math.abs(PolygonOps.area(this.points));
this._areaXY = Math.abs(PolygonOps.areaXY(this.points));
const center = this.getCenter(this.points);
this._textMarker.worldLocation = center;
this.setTextToMarker();
}
setPoints(points, copyPts = true, recompute = true) {
if (copyPts) {
this._points = [];
for (const pt of points)
this._points.push(pt.clone());
}
else {
this._points = points;
}
if (recompute)
this.recomputeFromPoints();
}
setTextToMarker() {
if (this._overrideText) {
this._textMarker.textLines = this._overrideText;
}
else {
const lines = [];
const areaFormatter = IModelApp.quantityFormatter.findFormatterSpecByQuantityType(QuantityType.Area);
if (undefined !== areaFormatter)
lines.push(IModelApp.quantityFormatter.formatQuantity(this.worldScale * this.worldScale * this.area, areaFormatter));
this._textMarker.textLines = lines;
}
}
copyPoints(points) {
const pts = new Array();
for (const pt of points)
pts.push(pt.clone());
return pts;
}
drawTextMarker(context) {
if (this.drawMarker)
this._textMarker.addDecoration(context);
}
addToGraphicBuilder(gBuilder, styleOverride) {
const style = this._styleSet.getGraphicStyle(styleOverride || WellKnownGraphicStyleType.AreaMeasurement);
// add points
style.addStyledPointString(gBuilder, this.points, false);
style.addStyledLineString(gBuilder, this.points, true);
// add area
if (this.drawFillArea) {
gBuilder.setBlankingFill(style.fillColorDef);
gBuilder.addShape(this.points);
}
}
makeSelectable(isSelectable) {
this._textMarker.pickable = isSelectable;
}
getCenter(points) {
if (points.length === 0)
return Point3d.createZero();
const ray3d = PolygonOps.centroidAreaNormal(points);
return (ray3d) ? ray3d.origin : points[0];
}
calculatePerimeter(points) {
let sum = 0.0;
for (let i = 1; i < points.length; ++i)
sum += points[i - 1].distance(points[i]);
return sum;
}
}
//# sourceMappingURL=Polygon.js.map