Wednesday, 9 December 2015

arcobjects - Activating custom ITool from form without adding it to ArcMap AddIn toolbar?


I'm working on an add-in for ArcMap 10.0 that adds a toolbar to ArcMap. One command (OpenModelessDialogCommand) button on that toolbar opens a modeless WinForms dialog, from which a tool (MyTool) can be activated in order to e.g. select a feature on the map.


My Config.esriaddinx contains these command and toolbar declarations:





What I'm having problems with is activating MyTool in the form. All I've found on the internet is code samples along the lines of:



// get a reference to an instance of MyTool:
ICommandItem myTool = ArcMap.Application.Document.CommandBars.Find("MyTool");
// activate MyTool:
ArcMap.Application.CurrentTool = myTool;

However, this apparently requires that MyTool actually appears in a command bar (e.g. toolbar) of my add-in. But that's not the case. So, I've tried this next:


ITool myTool = new MyTool();
ArcMap.Application.CurrentTool = myTool; // Type mismatch! An ICommandItem is expected.

I've even looked into adding an invisible AxToolbarControl to my form and adding a button for MyTool there; but then I'm running into problems on how to connect that toolbar (via SetBuddyControl) to the opened document's map. I don't want the tool to function in a separate AxMapControl, I want it to work directly with the main map shown in ArcMap.



Question:
How do I activate a custom tool that is not added to any toolbar (or other command bar, for that matter)?



Answer



This worked for me using ArcGIS 10 SP1. My custom tool is not on a toolbar:


    Dim UIDCls As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
' id property of menu from Config.esriaddinx document
UIDCls.Value = "ClassLibraryAddin_MyTool"
Dim document As ESRI.ArcGIS.Framework.IDocument = My.ArcMap.Document
Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem = TryCast(document.CommandBars.Find(UIDCls), ESRI.ArcGIS.Framework.ICommandItem)
If commandItem Is Nothing Then

Exit Sub
End If
My.ArcMap.Application.CurrentTool = commandItem

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...