iTwin Tools函数拆解
写一个工具大部分是extend PrimitiveTool
一个Tool中主要的函数
isCompatibleViewport
是否是合适的ViewPort,如是否是二维View,或者三维View才执行Tool
public isCompatibleViewport(vp: Viewport | undefined,isSelectedViewChange: boolean): boolean {return (super.isCompatibleViewport(vp, isSelectedViewChange) &&undefined !== vp &&vp.view.isSpatialView());}
requireWriteableTarget
是否需要写iModel
public requireWriteableTarget(): boolean {return false;
}
onPostInstall & setupAndPromptForNextAction
工具启动后执行的逻辑,如打开精确捕捉等,示例如下
public override async onPostInstall() {await super.onPostInstall();this.setupAndPromptForNextAction();}protected setupAndPromptForNextAction(): void {IModelApp.accuSnap.enableSnap(this.wantAccuSnap);this.provideToolAssistance();}
鼠标动作
onMouseMotion
鼠标运动时
下面示例代码表示如果选中元素或者在包围盒内,则鼠标跟随变成十字,其余为默认圆圈
public async onMouseMotion(_ev: BeButtonEvent): Promise<void> {if (undefined === _ev.viewport || this.allowOutsideProjectExtents) return; // Shouldn't really happenconst hit = await IModelApp.locateManager.doLocate(new LocateResponse(), true, _ev.point, _ev.viewport, _ev.inputSource);this._hit = hit; // Save the hit object for later use.if (hit?.isElementHit) {IModelApp.toolAdmin.setCursor("crosshair");} else {if (_ev.viewport.iModel.projectExtents.containsPoint(_ev.point)) {IModelApp.toolAdmin.setCursor("crosshair");} else {IModelApp.toolAdmin.setCursor("default");}}}
onDataButtonDown
鼠标左键
可以用于触发绘制,插入Marker,计算等等,如:
public override async onDataButtonDown(ev: BeButtonEvent): Promise<EventHandled> {this.points.push(ev.point.clone());this.setupAndPromptForNextAction();if (!this.isDynamicsStarted)this.beginDynamics();return EventHandled.No;}
onResetButtonUp
鼠标右键
一般用于退出工具,如:
public override async onResetButtonUp(_ev: BeButtonEvent): Promise<EventHandled> {if (undefined === this._origin)await this.exitTool(); // exit to select tool if we haven't gotten first point...elseawait this.onReinitialize(); // Calls onRestartTool...return EventHandled.No;}
getToolPromotionMessage
提示信息,iTwin UI的框架下,如:
public getToolPromotionMessage() {const promptMainInstruction = "这是一个提示";const promptLeftClickTap = "鼠标左键提示";const promptRightClickTap = "鼠标右键提示";const mainInstruction = ToolAssistance.createInstruction(this.iconSpec, promptMainInstruction);const mouseInstructions = [];const touchInstructions = [];mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.LeftClick, promptLeftClickTap, false, ToolAssistanceInputMethod.Mouse));mouseInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.RightClick, promptRightClickTap, false, ToolAssistanceInputMethod.Mouse));touchInstructions.push(ToolAssistance.createInstruction(ToolAssistanceImage.OneTouchTap, promptLeftClickTap, false, ToolAssistanceInputMethod.Touch));const sections = [ToolAssistance.createSection(mouseInstructions, ToolAssistance.inputsLabel),ToolAssistance.createSection(touchInstructions, ToolAssistance.inputsLabel),];const instructions = ToolAssistance.createInstructions(mainInstruction, sections);IModelApp.notifications.setToolAssistance(instructions);} // This is the message that appears in the status bar when the tool is active
accuSnap
打开精确捕捉
IModelApp.accuSnap.enableSnap(true);
允许捕捉到元素
IModelApp.accuSnap.enableLocate(true);
ToolAdmin
IModelApp.toolAdmin.setCursor("crosshair"); // 设置鼠标为跟随为十字 undefined为去掉鼠标跟随,wait为转圈等待,default为圆圈IModelApp.toolAdmin.setLocateCircleOn(false); // 不显示鼠标跟随的圆圈await IModelApp.toolAdmin.doUndoOperation(); // undoawait IModelApp.toolAdmin.doRedoOperation(); // redo