Coding an ArcMap Extension
Anybody who has used tools from Spatial Analyst or 3D Analyst is familiar with ArcMap extensions. To enable ArcMap extension, you simply open Tools --> Extensions, and select the appropriate checkbox.
Once you have a custom tool or toolbar written and successfully tested, it is very easy to wrap the tool(s) up as an extension just like Spatial Analyst. Let's take a look.
The first thing you want to do is create a new COM class in the same Visual Studio project as your tools. Your new class will need to implement the IExtension and IExtensionConfig Interfaces. Make sure that you include the necessary imports. Notice the two module level properties m_app and m_extensionState. The first will be used during the implementation of IExtension, and the second for IExtensionConfig.
Looking at the implementation of the IExtension interface below, we see the m_app property which is set during the IExtension's startup method and holds the hook into the ArcMap instance. Here I have also included a name for the extension, but did not add any additional logic to be executed during shutdown.
State property which sets and return the m_extensionState variable. The State method store info on which the extension has been enabled/disabled from the ArcMap tools -> extensions dialog.
To finish development of the extension, I have added an additional method to a utilities class which I use to store functions which will be shared among tools. This new function returns a boolean which indicates whether or not the extension is currently enabled. In this project, I also wanted to ensure layers were present in the map document which is why there is some additional logic. It would probably be better to separate that logic out into its own function to increase reuse-ability for future endeavors.
That wraps up creating the extension. To review, we created a new extension class, implemented the IExtension, and IExtensionConfig interfaces, and rigged up the logic to the ortho tool to enable only when the extension has been enabled.
Creating an Installer/Setup file
Now that we have an extension for the ortho-tool, it is time to create the installer class which will sit inside the same Visual Studio project. To do this just add a new item and select the Installer Class template. Switch to the code view and override the methods shown below. The Install/Uninstall methods will now handle registering/unregistering our tools Assembly with Windows.
New Project, select Other Project Types, and Setup and Deployment Project. My tools were part of a project called "Boston Tools" so I call the setup project "BostonToolsSetup". To link the two project together creating our friendly solution for distributing the tools:
- Click on the new project and customize the manufacturer and product name properties for the tool. These properties will also be used forming the path for where the extension is install on the users computer.
- Right-Click the setup project and select Add->Project Output. You can then select Primary Output and the ArcMap Extension's project name.
- Once you see the dependencies are added to the setup project, right-click the setup project again and select view->custom actions. Double-click on the Application Folder and confirm that Primary Output from
is selected and click OK.