@itwin/measure-tools-react
Version:
Frontend framework and tools for measurements
193 lines • 10.6 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 { AccuDrawHintBuilder, EventHandled, IModelApp, ToolAssistance, ToolAssistanceImage, ToolAssistanceInputMethod, } from "@itwin/core-frontend";
import { FeatureTracking, MeasureToolsFeatures } from "../api/FeatureTracking.js";
import { MeasurementToolBase } from "../api/MeasurementTool.js";
import { MeasurementViewTarget } from "../api/MeasurementViewTarget.js";
import { MeasureTools } from "../MeasureTools.js";
import { MeasureDistanceToolModel } from "../toolmodels/MeasureDistanceToolModel.js";
import { SheetMeasurementsHelper } from "../api/SheetMeasurementHelper.js";
import { PropertyDescriptionHelper } from "@itwin/appui-abstract";
import { ViewHelper } from "../api/ViewHelper.js";
export class MeasureDistanceTool extends MeasurementToolBase {
get allowedDrawingTypes() {
return [SheetMeasurementsHelper.DrawingType.CrossSection, SheetMeasurementsHelper.DrawingType.Plan];
}
static get flyover() {
return MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureDistance.flyover");
}
static get description() {
return MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureDistance.description");
}
static get keyin() {
return MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureDistance.keyin");
}
get feature() {
return MeasureToolsFeatures.Tools_MeasureDistance;
}
constructor(enableSheetMeasurements = false, allowedViewportCallback = (() => true)) {
super(allowedViewportCallback);
this._useMultiPointMeasurement = false;
this._enableSheetMeasurements = enableSheetMeasurements;
}
async onPostInstall() {
await super.onPostInstall();
}
async onRestartTool() {
const tool = new MeasureDistanceTool(this._enableSheetMeasurements, this._allowedViewportCallback);
if (await tool.run())
return;
return this.exitTool();
}
async onReinitialize() {
await super.onReinitialize();
AccuDrawHintBuilder.deactivate();
}
async onDataButtonDown(ev) {
if (!ev.viewport)
return EventHandled.No;
const viewType = MeasurementViewTarget.classifyViewport(ev.viewport);
if (MeasureDistanceToolModel.State.SetMeasurementViewport ===
this.toolModel.currentState) {
this.toolModel.setMeasurementViewport(viewType);
this.toolModel.setStartPoint(viewType, ev.point);
await this.sheetMeasurementsDataButtonDown(ev);
this._sendHintsToAccuDraw(ev);
this.updateToolAssistance();
}
else if (MeasureDistanceToolModel.State.SetEndPoint === this.toolModel.currentState) {
this.toolModel.setEndPoint(viewType, ev.point, false);
if (this._enableSheetMeasurements && ViewHelper.isSheetView(ev.viewport))
FeatureTracking.notifyFeature(MeasureToolsFeatures.Tools_MeasureDistance_createdInSheet);
await this.onReinitialize();
// Trigger another button event to use as the start point of the next measurement
if (this._useMultiPointMeasurement) {
return this.onDataButtonDown(ev);
}
}
ev.viewport.invalidateDecorations();
return EventHandled.Yes;
}
async sheetMeasurementsDataButtonDown(ev) {
if (!ev.viewport)
return;
if (this._enableSheetMeasurements) {
if (this.toolModel.drawingMetadata?.drawingId === undefined && ev.viewport.view.id !== undefined) {
const drawingInfo = await SheetMeasurementsHelper.getDrawingId(this.iModel, ev.viewport.view.id, ev.point);
this.toolModel.sheetViewId = ev.viewport.view.id;
if (drawingInfo?.drawingId !== undefined && drawingInfo.origin !== undefined && drawingInfo.worldScale !== undefined) {
const data = { origin: drawingInfo.origin, drawingId: drawingInfo.drawingId, worldScale: drawingInfo.worldScale, extents: drawingInfo.extents };
this.toolModel.drawingMetadata = data;
}
}
}
}
isValidLocation(ev, isButtonEvent) {
if (!super.isValidLocation(ev, isButtonEvent))
return false;
if (!this._enableSheetMeasurements || !ev.viewport?.view.isSheetView())
return true;
if (!SheetMeasurementsHelper.checkIfAllowedDrawingType(ev.viewport, ev.point, this.allowedDrawingTypes))
return false;
if (this.toolModel.drawingMetadata?.drawingId === undefined || this.toolModel.drawingMetadata?.origin === undefined || this.toolModel.drawingMetadata?.extents === undefined)
return true;
return SheetMeasurementsHelper.checkIfInDrawing(ev.point, this.toolModel.drawingMetadata?.origin, this.toolModel.drawingMetadata?.extents);
}
decorate(context) {
super.decorate(context);
if (this._enableSheetMeasurements && this.toolModel.drawingMetadata?.origin !== undefined && this.toolModel.drawingMetadata?.extents !== undefined) {
context.addDecorationFromBuilder(SheetMeasurementsHelper.getDrawingContourGraphic(context, this.toolModel.drawingMetadata?.origin, this.toolModel.drawingMetadata?.extents));
}
}
_sendHintsToAccuDraw(ev) {
const hints = new AccuDrawHintBuilder();
hints.setOrigin(ev.point);
hints.setModeRectangular();
hints.sendHints(false);
IModelApp.toolAdmin.setCursor(IModelApp.viewManager.crossHairCursor);
}
async onMouseMotion(ev) {
if (undefined === ev.viewport ||
MeasureDistanceToolModel.State.SetEndPoint !== this.toolModel.currentState)
return;
const type = MeasurementViewTarget.classifyViewport(ev.viewport);
this.toolModel.setEndPoint(type, ev.point, true);
ev.viewport.invalidateDecorations();
}
createToolModel() {
return new MeasureDistanceToolModel();
}
updateToolAssistance() {
let promptMainInstruction;
if (MeasureDistanceToolModel.State.SetEndPoint !== this.toolModel.currentState)
promptMainInstruction = MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureDistance.mainInstruction");
else
promptMainInstruction = MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureDistance.mainInstruction2");
const promptClickTap = MeasureTools.localization.getLocalizedString("MeasureTools:tools.GenericPrompts.acceptPoint");
let promptRightClick;
if (undefined !== this.toolModel.dynamicMeasurement)
promptRightClick = MeasureTools.localization.getLocalizedString("MeasureTools:tools.GenericPrompts.clearCurrentMeasurement");
else
promptRightClick = MeasureTools.localization.getLocalizedString("MeasureTools:tools.GenericPrompts.restart");
const mainInstruction = ToolAssistance.createInstruction(this.iconSpec, promptMainInstruction);
const mouseInstructions = [];
const touchInstructions = [];
if (!ToolAssistance.createTouchCursorInstructions(touchInstructions))
touchInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.OneTouchTap, promptClickTap, false, ToolAssistanceInputMethod.Touch));
mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.LeftClick, promptClickTap, false, ToolAssistanceInputMethod.Mouse));
mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.RightClick, promptRightClick, false, ToolAssistanceInputMethod.Mouse));
if (undefined === this.toolModel.dynamicMeasurement) {
if (this.toolModel.canUndo)
mouseInstructions.push(this.createMouseUndoInstruction());
if (this.toolModel.canRedo)
mouseInstructions.push(this.createMouseRedoInstruction());
}
const sections = [
ToolAssistance.createSection(mouseInstructions, ToolAssistance.inputsLabel),
ToolAssistance.createSection(touchInstructions, ToolAssistance.inputsLabel),
];
const instructions = ToolAssistance.createInstructions(mainInstruction, sections);
IModelApp.notifications.setToolAssistance(instructions);
}
async onInstall() {
if (!await super.onInstall()) {
return false;
}
const initialValue = IModelApp.toolAdmin.toolSettingsState.getInitialToolSettingValue(this.toolId, MeasureDistanceTool.useMultiPointPropertyName);
this._useMultiPointMeasurement = true === initialValue?.value;
return true;
}
async onCleanup() {
const propertyName = MeasureDistanceTool.useMultiPointPropertyName;
const value = { value: this._useMultiPointMeasurement };
IModelApp.toolAdmin.toolSettingsState.saveToolSettingProperty(this.toolId, { propertyName, value });
return super.onCleanup();
}
supplyToolSettingsProperties() {
const propertyLabel = MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureDistance.useMultiPoint");
const toolSettings = [{
value: { value: this._useMultiPointMeasurement },
property: PropertyDescriptionHelper.buildToggleDescription(MeasureDistanceTool.useMultiPointPropertyName, propertyLabel),
editorPosition: { rowPriority: 0, columnIndex: 0 },
isDisabled: false,
}];
return toolSettings;
}
async applyToolSettingPropertyChange(updatedValue) {
if (MeasureDistanceTool.useMultiPointPropertyName === updatedValue.propertyName) {
const value = updatedValue.value.value;
if (typeof value !== "boolean") {
return false;
}
this._useMultiPointMeasurement = value;
return true;
}
return super.applyToolSettingPropertyChange(updatedValue);
}
}
MeasureDistanceTool.toolId = "MeasureTools.MeasureDistance";
MeasureDistanceTool.iconSpec = "icon-measure-distance";
MeasureDistanceTool.useMultiPointPropertyName = "useMultiPoint";
//# sourceMappingURL=MeasureDistanceTool.js.map