ESRI add-in

From Wildsong
Jump to navigationJump to search

An add-in can be used to attach Python to standard controls in ArcMap like buttons or drop down selection lists. It can also be used from Data Driven Pages, so that some python runs each time a DDP page is selected.

An add-in consists of a ZIP file with configuration and python code in it, with the extension changed from .zip to .esriaddin.

The easiest way to create one is to use the ESRI wizard to make a new project and then to modify the files to add your code.

Create an add-in

First create the template using the ESRI Python Add-In Wizard.

Add your code. (Just like that no additional help needed. :-)

This will give you a makeaddin.py script that you can run to build the .esriaddin file.

I use Visual Studio set to run makeaddin.py when I do an F5 to run the project.

I have ArcMap set to search my source directory on startup. (See Data Driven Pages for help.) Then I only need to reload ArcMap to test new versions.

config.xml

One of the files the wizard creates is the config.xml, which you can then further edit.

Mine defines that there is an extension in the add-in, that code appears in the ArcMap section and looks like this:

 <Extensions>
   <Extension autoLoad="true" category="CCMAP tools" class="ddp_extension" description="Watch for changes in DDP and update page layout" id="ccmap.ddp_extension" name="CCMAP DDP extension" productName="CCMAP DDP extension" showInExtensionDialog="true" />
</Extensions>

Note it defines the name of the class "ddp_extension". That code is in ccmap.py.

What's in an esriaddin file?

I opened my DDP extension in emacs. It looks like this:

 Filemode      Length  Date         Time      File
 ----------  --------  -----------  --------  -------------------------------
 -rw-rw-rw-      1897  23-May-2018  15:22:30  config.xml                            esriaddin config file
 -rw-rw-rw-       694  19-Apr-2018  12:18:14  README.txt                            documentation
 -rw-rw-rw-      2834  30-Apr-2018  10:27:38  makeaddin.py                          script that builds the esriaddin
 -rw-rw-rw-     11951  20-Apr-2018  10:19:58  Images/cc_logo_64x64.png              icon
 -rw-rw-rw-     27711  19-Apr-2018  10:12:54  Images/ormap.png                      icon
 -rw-rw-rw-      4543  23-May-2018  12:24:52  Install/ccmap.py                      the actual add-in code
 -rw-rw-rw-      5859  23-May-2018  12:07:48  Install/arc_utilities.py              glue code to make arcpy easier to use
 -rw-rw-rw-      4293  18-May-2018  12:08:28  Install/cancellations.py              manages cancelled taxlot lists
 -rw-rw-rw-      3182  28-Mar-2018  11:36:54  Install/mapnum.py                     map number handling code
 -rw-rw-rw-      4592  11-May-2018  16:28:44  Install/MXDReport_tool.py             UNUSED in this add-in
 -rw-rw-rw-      3932  29-Mar-2018  11:33:26  Install/mxd_report.py                 sample code, not used
 -rw-rw-rw-     10504   3-Apr-2018  16:52:26  Install/ormapnum.py                   A class that handles ORMap string conversions
 -rw-rw-rw-      3400   3-Apr-2018  16:03:32  Install/printMaps.py                  UNUSED in this add-in
 -rw-rw-rw-      7128  21-May-2018  16:30:22  Install/PrintMaps_tool.py             UNUSED in this add-in
 -rw-rw-rw-     13514  18-May-2018  12:37:22  Install/zoomToMapNumber.py            The update_page_layout code is here
 -rw-rw-rw-      4336  21-May-2018  16:30:22  Install/ZoomToMapNumber_tool.py       UNUSED in this add-in
 -rw-rw-rw-       186  12-Apr-2018  16:40:12  Install/__init__.py                   module init
 -rw-rw-rw-      2547  25-Apr-2018  16:49:52  Install/ORMAP_config.py               defines some things like database names and folder names
 ----------  --------  -----------  --------  -------------------------------
               113103                         18 files

If you look in my source code you will see ONLY ccmap.py in the Install/ folder. I tweaked the makeaddin.py script to copy all the other files in there, so that I can share most of the code with other projects. There is probably a good way to do this directly in Visual Studio too.