In this topic
Extending ArcGIS for Desktop with custom commands or tools
Commands and tools are the most commonly implemented framework components to extend the functionality of an ArcGIS for Desktop application. You can target your command or tool to work with ArcCatalog, ArcMap, ArcScene, or ArcGlobe by registering your component into the necessary application.
To get started, use the ArcGIS item templates to create a command and tool, then select the ArcGIS for Desktop application where you want to create the command or tool. For more information, see Creating commands and tools.
The Base Command and ArcGIS Base Tool templates override the OnCreate method that is called by the framework when your command or tool is created. The framework also passes the Application object that creates the command or tool as the input hook parameter in this method. Although the hook parameter is a generic type of System.Object, the expected hook is a reference to the Application object that implements the IApplication interface.
The IApplication reference is the top-level entry point into the various ArcGIS for Desktop applications. In most cases, a command or tool only works in a specific application or applications but not all. The following table shows the interface you can test the IApplication reference against:
Desktop application |
Interfaces |
ArcCatalog command or tool |
|
ArcGlobe command or tool |
|
ArcMap command or tool |
|
ArcScene command or tool |
Creating an application-specific command
To create an application-specific command, follow these steps:
-
Create an ArcGIS for Desktop class library project targeting the application of your choice, for example, ArcMap. For more information, see Choosing an ArcGIS for Desktop project template.
-
Create a command using the ArcGIS Base Command templates. Select the ArcGIS for Desktop application option where you want to create the command, for example, Desktop ArcMap command. For more information, see the previously referenced topic, Creating commands and tools.
-
Specify command properties in the constructor, for example, set the category to .NET Samples caption to My Command, and the name to NETSamples_MyCommand.
-
Examine how the code in the OnCreate method handles the incoming hook parameter, for example, IMxApplication for ArcMap.
-
Go to the OnClick method and add code to display the name of the application in which the command resides. See the following code example:
IMessageDialog msgDialog=new MessageDialogClass();
msgDialog.DoModal("Test", "My command works in " + m_app.Name, "", "", 0);
[VB.NET] Dim msgDialog As IMessageDialog=New MessageDialogClass()
msgDialog.DoModal("Test", "My command works in " + m_app.Name, "", "", 0)
- Add a break point in the previous code example (optional).
- Debug the command in your Visual Studio project. Make sure the debug application property of your project points to ArcMap.exe.
- Once ArcMap starts in your debug session, open the Customize dialog box. Search for the custom command in the .NET Samples category, and drag and drop the command onto the application.
- Close the dialog box.
- Click the command. It hits the break point you added in the OnClick method announcing the name of the application.
Creating a generic command
You can repeat the steps in Creating an application-specific command by creating another command in a different desktop application. However, you can also create a single command that works in all applications by modifying the previous code example.
To create a generic command, follow these steps:
- Omit the check for a specific application in the OnCreate method by commenting out or deleting lines of code in the following code example:
//Disable if it is not ArcMap.
//if (hook is IMxApplication)
//base.m_enabled=true;
//else
//base.m_enabled=false;
[VB.NET] 'Disable if it is not ArcMap.
'If TypeOf hook Is IMxApplication Then
'MyBase.m_enabled=True
'Else
'MyBase.m_enabled=False
'End If
- Add code to register your class into all desktop application command categories using the ArcGIS Component Category Registrar dialog box. Select the following categories on the dialog box:
- ArcMap Commands
- ArcCatalog Commands
- ArcGlobe Commands
- ArcScene Commands
- Run or debug your command in any of the desktop applications. When debugging, switch the debug application project property to the appropriate application path.
- Drag and drop the command from the Customize dialog box, and click the command to display a message with the application name.
Passing the hook object to dialog boxes or other .NET components
Sometimes a command is used to display a dialog box that also needs to access the application object.
You can use the same technique to pass the application hook object to other .NET components in your project.
To pass the application object to a form via a write-only property, follow these steps:
- Create a desktop command using the ArcGIS Base Command template. Follow Steps 1–3 in Creating an application-specific command.
- Add a form to your project, for example, Form1.
- Add a button to your form.
- Add a write-only property to the form to accept the application object.
See the following code example:
public partial class Form1: Form
{
private IApplication m_application;
... public IApplication ArcGISApplication
{
set
{
m_application=value;
}
}
...
}
[VB.NET] Public Class Form1
Private m_application As IApplication
Public WriteOnly Property ArcGISApplication() As IApplication
Set(ByVal Value As IApplication)
m_application=Value
End Set
End Property
...
End Class
- Add code to the OnClick event handler of the form button to display a message for the application.
See the following code example:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(m_application.Name);
}
[VB.NET] Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(m_application.Name)
End Sub
- Return to the command file and add the following code to the OnClick method to initialize and hand over the application object to the form:
public sealed class Command1: BaseCommand
{
private Form1 m_form;
... public override void OnClick()
{
//Initialize form.
if (m_form == null || m_form.IsDisposed)
{
m_form=new Form1();
m_form.ArcGISApplication=m_application;
}
//Show form (modeless).
if (!m_form.Visible)
m_form.Show();
}
...
}
[VB.NET] Public NotInheritable Class Command1
Inherits BaseCommand
...
Public Overrides Sub OnClick()
'Initialize form.
If m_form Is Nothing OrElse m_form.IsDisposed Then
m_form=New Form1()
m_form.ArcGISApplication=m_application
End If
'Show form (modeless).
If Not m_form.Visible Then m_form.Show()
End Sub
...
End Class
- Compile and run the code by adding the command to the application, then click it to display the form. Click the form button to announce the application name.
See Also:
Creating commands and toolsTo use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing | Deployment licensing |
---|---|
ArcGIS for Desktop Basic | ArcGIS for Desktop Basic |
ArcGIS for Desktop Standard | ArcGIS for Desktop Standard |
ArcGIS for Desktop Advanced | ArcGIS for Desktop Advanced |