Execute a command programmatically

1190
6
02-24-2011 01:14 PM
GagagDa_Morvi
New Contributor
I have developed a command that implements ICommand and ITool. This command is registered in the ESRI MX Commands category. How can I execute this command programmatically without having to add it to a toolbar or a commandbar?
0 Kudos
6 Replies
JamesCrandall
MVP Frequent Contributor
I have developed a command that implements ICommand and ITool. This command is registered in the ESRI MX Commands category. How can I execute this command programmatically without having to add it to a toolbar or a commandbar?


Not totally sure you can do this, but if it's registered already then you should be able to execute it via it's GUID just like any other registered command.  For example, this code would initiate the Sketch Tool:

       'set the current tool to be the editor Sketch tool
        Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem
        Dim pUID As New ESRI.ArcGIS.esriSystem.UID

        pUID.value = "esriCore.SketchTool"

        pCommandItem = m_pApp.Document.CommandBars.Find(pUID)
        m_pApp.CurrentTool = pCommandItem
        m_pEditEvents = pEditor
0 Kudos
GagagDa_Morvi
New Contributor
Not totally sure you can do this, but if it's registered already then you should be able to execute it via it's GUID just like any other registered command.  For example, this code would initiate the Sketch Tool:

       'set the current tool to be the editor Sketch tool
        Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem
        Dim pUID As New ESRI.ArcGIS.esriSystem.UID

        pUID.value = "esriCore.SketchTool"

        pCommandItem = m_pApp.Document.CommandBars.Find(pUID)
        m_pApp.CurrentTool = pCommandItem
        m_pEditEvents = pEditor

Thanks, I will give it a try, all online examples show this technique. But would it work even if the command has not been added to the CommandBar - because the example tries to find the command by ICommandBars.Find().
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks, I will give it a try, all online examples show this technique. But would it work even if the command has not been added to the CommandBar - because the example tries to find the command by ICommandBars.Find().


oooh not sure.  I've always implemented an IToolBarDef class, so I am unsure of success in doing what you are attempting.  Hopefully someone who has experienced this can jump in and explain.
0 Kudos
NeilClemmons
Regular Contributor III
This code uses the tool's ProgId:
pUID.value = "esriCore.SketchTool"

It has been my experience that using the ProgId requires the command or tool to be on a toolbar or menu somewhere.

If you use the actual class GUID, then the command or tool does not have to be on a toolbar or menu:
pUID.value = "{" & yourClass.ClassId & "}"

The code above is .NET and assumes the command/tool class is in your code project.  You can also just use the hard-coded GUID (you'll have to do this for built-in tools and tools whose code is not in your project).
0 Kudos
GagagDa_Morvi
New Contributor
This code uses the tool's ProgId:
pUID.value = "esriCore.SketchTool"

It has been my experience that using the ProgId requires the command or tool to be on a toolbar or menu somewhere.

If you use the actual class GUID, then the command or tool does not have to be on a toolbar or menu:
pUID.value = "{" & yourClass.ClassId & "}"

The code above is .NET and assumes the command/tool class is in your code project.  You can also just use the hard-coded GUID (you'll have to do this for built-in tools and tools whose code is not in your project).

Thanks Neil, I will give it a try and report.
0 Kudos
GagagDa_Morvi
New Contributor
Thanks Neil, I will give it a try and report.

Thank you , Neil. It has worked as per your suggestion.
0 Kudos