ArcGIS By Example
上QQ阅读APP看书,第一时间看更新

An introduction to ArcGIS add-ins

In this section, we will introduce the concept of the ArcGIS add-ins building approach. We will then build our first Hello, ArcGIS project, which will help us get started with ArcGIS add-ins. The Hello, ArcGIS project will be a small example that explains the basics of add-ins. As explained in the previous chapter, there are different approaches to building applications on ArcGIS for Desktop. The first and oldest method is Visual Basic for Applications (VBA), the second one is add-in, and the third one is extending ArcObjects, which we will be using in the last two examples.

The reason I started with add-ins is because it is the easiest and most convenient method for development and deployment. When you write and deploy an add-in, ArcGIS detects it and asks you whether you want to install it or not. Add-ins can be later disabled or enabled based on need, which makes them a secure approach for development.

Creating the Hello, ArcGIS add-in project

In this section, we will show how to create an add-in project using Visual Studio. To create an add-in project for ArcGIS, follow these steps:

  1. From the Start menu, locate and run Microsoft Visual Studio.
  2. From the Visual Studio application, point to the File menu and then click on New Project.
  3. Under the Templates node, expand Visual Basic, this is the language we will be programming with, then expand ArcGIS, and click on Desktop Add-ins. You can write add-ins for all ArcGIS for Desktop products as you can see. We want to write add-ins on top of ArcMap, so select ArcMap Add-ins.
  4. In the Name field, type the name of the project HelloArcGIS and point the location to the C:\ArcGISByExample\HelloArcGIS folder, as illustrated in the following screenshot:
    Creating the Hello, ArcGIS add-in project
  5. Click on OK, this will show the ArcGIS Add-in Wizard.
  6. We can use the wizard to autogenerate our controls and other codes, but we are going to do that manually, so simply click on Finish to create the project.
  7. Once the project has been created, you will see two files: config.esriaddinx and an image as an icon for your add-in. The config.esriaddinx file contains metadata about your project like the name and description. To add a functionality to our project, we need to add a control to do this, point to the Project menu and then click on Add Class.
  8. From the Common Items node expand the ArcGIS node and then click on Desktop Add-ins. Click on Add-in Component and call it btnHelloArcGIS, and then click on Add.
  9. From the Add-in Wizard, select Button, for the button caption type Hello ArcGIS and then click on Finish.
  10. This adds two files: a class and an image. Double-click on the btnHelloArcGIS class.
  11. Note that this class inherits from a base class ESRI.ArcGIS.Desktop.AddIns.Button, which will give it the functionality of a button. What is interesting for us here is the onClick event, which is the code that will get executed when one clicks on the button. Write the following code in the onClick event:
    MsgBox("Hello, ArcGIS!")

    For more details, take a look at the following screenshot:

    Creating the Hello, ArcGIS add-in project
  12. It is time to build our project and see the fruits of our work. Point to the Build menu and then click on Build solution. You should get a Build succeeded status message if you have written the code correctly.
  13. Run ArcMap.
  14. From the Customize menu, click on Customize Mode.
  15. Activate the Commands tab and then type Hello ArcGIS in the search box, as illustrated in the following screenshot. Drag the Hello ArcGIS button near the Zoom in button to add it to the toolbar.
    Creating the Hello, ArcGIS add-in project
  16. Close the Customize dialog.
  17. By clicking on your new button, you will notice that the message Hello ArcGIS is displayed.
  18. To add your own toolbar, add a new class to your project and choose Add-in Command Container.
  19. Select Toolbar from the wizard and then select your button to be added to the toolbar from the drop-down list.
  20. Close ArcMap and then close Visual Studio.