Creating a custom tool for ArcGis products

ESRI's desktop products can be extended by adding tools and command buttons integrally into the environment. This can be done by using VBA inside the products, or creating controls in .Net using the .Net ADF that's supplied with the product installation.

In order to create a custom command/tool you need to install the desktop products, and the .Net SDK for them. After the installation, new templates are added to visual studio.

To start creating the tool follow these steps:

  1. Open visual studio
  2. File -> New -> project -> Visual C# -> ArcGis -> Desktop -> Class Library (product - ArcMap, ArcCatalog, etc...)
  3. Select Assemblies to reference (If you don't know you can return to this selection by right click on the references icon under the project -> Add ArcGis reference).

Now you have a project that can contain the new controls for the desktop, and it's time to create a new control. There are several types of controls you can create: command,tool, dockable window and more.

Tool or Command?

Tool: receives input from the environment, like mouse events. For example : Zoom-in is a tool. A tool stays selected until you chose another tool.
Command: is like a button. You click on it and something happens. For example: Full-extent is a command. Command does not effect the selected tool, and after you click a command, if a tool was selected before it will stay selected after the command preformed.

Creating a tool

Let's create a simple tool for ArcMap that will display coordinate we clicked on with the mouse.
For starters, create a project of type Class Library (ArcMap) as describe earlier and add the assemblies: Display, Geometry, ArcMap.
Now, add new class: Right click on the project -> New class -> ArcGis -> Base tool, and select ArcMap as the target product.

A new class was generated. It derives from BaseTool which is the base class for tools. There is also BaseCommand for command.

It contains methods for COM registrations. Because the all of ESRI's products are COM based, you need to register the control for COM usage. For this you need the GUID attribute on the class. These methods are triggered when you build your projects. You can also trigger them with an installer (when you deploy the tool as an MSI).

The constructor contains several initializations. You can set the caption on the tool, tooltip, bitmap (for icon on the button), and more important: In which category the tool will be placed (We will go back to that later).

Overrides for the events the tool can handle. You can see there are, among else, mouse events.

Set the variables in the constructor with following values:

base.m_catagory = "YsA tools";  
base.m_caption = "Show position";  
...
base.m_name = "YsATools_ShowPosition";  

In the OnMouseDown event, write this code:

IPoint point = m_application.Display.DisplayTransformation.ToMapPoint(X,Y);  
MessageBox.Show("X : " + point.X + ", Y : " +point.Y);  

To use the tool do the following:

  • Build the solution
  • Open ArcMap
  • Tools -> Customize
  • In the category list choose the category you set in the constructor: "YsA Tools"
  • Search for the tool "Show position" in the commands list, and drag it onto one of the toolbars
  • Close the dialog box
  • Click on the tool button. As you can see it stays active
  • Click on the map and what do you know... A message box will popup and display the coordinates on which you clicked (in decimal format).

This is a very simple example. You can do a lot more with these these controls: open forms and receive user input, use ArcObjects to perform geographic operations, etc... The sky is the limit.

Yossi Shmueli

Keeping it green since 1995

comments powered by Disqus