import { BeButtonEvent , CoordinateLockOverrides , EventHandled , HitDetail , IModelApp , LocateFilterStatus , LocateResponse , PrimitiveTool , SelectionMethod , SelectionMode , } from "@itwin/core-frontend" ; import { Point3d } from "@itwin/core-geometry" ; import { UiFramework } from "@itwin/appui-react" ; import { ToolbarItemUtilities } from "@itwin/appui-abstract" ; import genericToolSvg from "./generic-tool.svg?sprite" ; import { UiItemsProvidersTest } from "../ui-items-providers-test" ; /** Sample Primitive tool where user selects an element for processing */ export class GenericLocateTool extends PrimitiveTool { public userPoint : Point3d | undefined ; public elementId : string | undefined ; public static override get toolId ( ) { return "uiItemsProvidersTest-GenericLocateTool" ; } public static get toolStringKey ( ) { return `tools.${ GenericLocateTool . toolId }.` ; } public static override iconSpec = `svg:${ genericToolSvg }` ; public static useDefaultPosition = false ; public override autoLockTarget ( ) { } protected wantSelectionClearOnMiss ( _ev ) { return SelectionMode . Replace === this . getSelectionMode ( ) ; } protected wantPickableDecorations ( ) { return false ; } protected getSelectionMethod ( ) { return SelectionMethod . Pick ; } protected getSelectionMode ( ) { return SelectionMode . Replace ; } public override requireWriteableTarget ( ) { return false ; } public override async filterHit ( _hit , _out ? ) { return Promise . resolve ( LocateFilterStatus . Accept ) ; } public static getPrompt ( name ) { const key = `tools.${ this . toolId }.Prompts.${ name }` ; return UiItemsProvidersTest . translate ( key ) ; } public async process ( _elementId , _point ? ) { return this . exitTool ( ) ; } public override async onDataButtonUp ( ev ) { const hit = await IModelApp . locateManager . doLocate ( new LocateResponse ( ) , true , ev . point , ev . viewport , ev . inputSource ) ; if ( this . elementId === undefined ) { if ( hit !== undefined ) { this . elementId = hit . sourceId ; if ( GenericLocateTool . useDefaultPosition ) await this . process ( hit . sourceId ) ; else this . setupAndPromptForNextAction ( ) ; } } else if ( ! GenericLocateTool . useDefaultPosition ) { this . userPoint = ev . point ; if ( hit !== undefined ) this . userPoint = hit . hitPoint ; await this . process ( this . elementId , this . userPoint ) ; } return EventHandled . Yes ; } public override async onResetButtonUp ( _ev ) { await this . onReinitialize ( ) ; return EventHandled . No ; } protected outputMarkupPrompt ( msg ) { IModelApp . notifications . outputPrompt ( GenericLocateTool . getPrompt ( msg ) ) ; } public setupAndPromptForNextAction ( ) { if ( ! this . elementId ) { this . outputMarkupPrompt ( "identifyElement" ) ; } else if ( ! GenericLocateTool . useDefaultPosition ) { this . outputMarkupPrompt ( "identifyPosition" ) ; IModelApp . accuSnap . enableSnap ( true ) ; } } public override async onPostInstall ( ) { await super . onPostInstall ( ) ; const iModelConnection = UiFramework . getIModelConnection ( ) ; if ( ! iModelConnection ) return ; if ( iModelConnection . selectionSet . size === 1 ) { iModelConnection . selectionSet . elements . forEach ( ( elementId , _val , _set ) => { this . process ( elementId ) ; } ) ; await IModelApp . toolAdmin . startDefaultTool ( ) ; } else { iModelConnection . selectionSet . emptyAll ( ) ; this . outputMarkupPrompt ( "identifyElement" ) ; IModelApp . toolAdmin . setCursor ( "arrow" ) ; IModelApp . toolAdmin . setLocateCircleOn ( true ) ; IModelApp . toolAdmin . toolState . coordLockOvr = CoordinateLockOverrides . All ; IModelApp . locateManager . initToolLocate ( ) ; IModelApp . locateManager . options . allowDecorations = true ; IModelApp . accuSnap . enableLocate ( true ) ; IModelApp . accuSnap . enableSnap ( false ) ; } } public async onRestartTool ( ) { return this . exitTool ( ) ; } public static async startTool ( ) { return ( new GenericLocateTool ( ) ) . run ( ) ; } public static getActionButtonDef ( itemPriority , groupPriority ? ) { const overrides = undefined !== groupPriority ? { groupPriority } : { } ; return ToolbarItemUtilities . createActionButton ( GenericLocateTool . toolId , itemPriority , GenericLocateTool . iconSpec , GenericLocateTool . flyover , async ( ) => { await IModelApp . tools . run ( GenericLocateTool . toolId , IModelApp . viewManager . selectedView , true ) ; } , overrides ) ; } }