@itwin/measure-tools-react
Version:
Frontend framework and tools for measurements
110 lines • 5.51 kB
JavaScript
import { EventHandled, IModelApp, ToolAssistance, ToolAssistanceImage, ToolAssistanceInputMethod, } from "@itwin/core-frontend";
import { MeasureToolsFeatures } from "../api/FeatureTracking.js";
import { MeasurementToolBase } from "../api/MeasurementTool.js";
import { MeasurementViewTarget } from "../api/MeasurementViewTarget.js";
import { MeasureTools } from "../MeasureTools.js";
import { MeasureRadiusToolModel } from "../toolmodels/MeasureRadiusToolModel.js";
/** Tool for measuring radius using 3-points */
export class MeasureRadiusTool extends MeasurementToolBase {
createToolModel() {
return new MeasureRadiusToolModel();
}
static get flyover() {
return MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureRadius.flyover");
}
static get description() {
return MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureRadius.description");
}
static get keyin() {
return MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureRadius.keyin");
}
get feature() {
return MeasureToolsFeatures.Tools_MeasureRadius;
}
constructor(allowedViewportCallback = (() => true)) {
super(allowedViewportCallback);
}
async onRestartTool() {
const tool = new MeasureRadiusTool(this._allowedViewportCallback);
if (await tool.run())
return;
return this.exitTool();
}
/** Show tool assistance messages to user */
showPrompt() {
const identifyStartMessage = MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureRadius.identifyStart");
const identifyCenterMessage = MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureRadius.identifyMidpoint");
const identifyEndMessage = MeasureTools.localization.getLocalizedString("MeasureTools:tools.MeasureRadius.identifyEnd");
let currentMsg = "";
if (this.toolModel.currentState ===
MeasureRadiusToolModel.State.SetMeasurementViewport ||
this.toolModel.currentState === MeasureRadiusToolModel.State.SetStartPoint) {
currentMsg = identifyStartMessage;
}
else if (this.toolModel.currentState === MeasureRadiusToolModel.State.SetMidPoint) {
currentMsg = identifyCenterMessage;
}
else if (this.toolModel.currentState === MeasureRadiusToolModel.State.SetEndPoint) {
currentMsg = identifyEndMessage;
}
const mainInstruction = ToolAssistance.createInstruction(this.iconSpec, currentMsg);
const mouseInstructions = [];
mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.LeftClick, identifyStartMessage, false, ToolAssistanceInputMethod.Mouse));
mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.LeftClick, identifyCenterMessage, false, ToolAssistanceInputMethod.Mouse));
mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.LeftClick, identifyEndMessage, 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 = [];
sections.push(ToolAssistance.createSection(mouseInstructions, ToolAssistance.inputsLabel));
const instructions = ToolAssistance.createInstructions(mainInstruction, sections);
IModelApp.notifications.setToolAssistance(instructions);
}
/** Setup for next tool step */
updateToolAssistance() {
IModelApp.accuSnap.enableSnap(true);
this.showPrompt();
}
async onMouseMotion(ev) {
if (undefined === ev.viewport ||
MeasureRadiusToolModel.State.SetEndPoint !== this.toolModel.currentState)
return;
const type = MeasurementViewTarget.classifyViewport(ev.viewport);
this.toolModel.setEndPoint(type, ev.point, true);
ev.viewport.invalidateDecorations();
}
/** Process mouse presses */
async onDataButtonDown(ev) {
if (!ev.viewport)
return EventHandled.No;
const viewType = MeasurementViewTarget.classifyViewport(ev.viewport);
let success = false;
switch (this.toolModel.currentState) {
case MeasureRadiusToolModel.State.SetMeasurementViewport: {
success = this.toolModel.setMeasurementViewport(viewType);
if (success)
success = this.toolModel.setStartPoint(viewType, ev.point, false);
break;
}
case MeasureRadiusToolModel.State.SetMidPoint: {
success = this.toolModel.setMidPoint(viewType, ev.point, false);
break;
}
case MeasureRadiusToolModel.State.SetEndPoint: {
success = this.toolModel.setEndPoint(viewType, ev.point, false);
break;
}
}
if (success)
this.updateToolAssistance();
ev.viewport.invalidateDecorations();
return EventHandled.Yes;
}
}
MeasureRadiusTool.toolId = "MeasureTools.MeasureRadius";
// TODO: Change icon once UX team provides icon
MeasureRadiusTool.iconSpec = "icon-three-points-circular-arc";
//# sourceMappingURL=MeasureRadiusTool.js.map