Friday, 12 December 2014

How to create a simple python add-in tool for ArcGIS Desktop

How to create a simple python add-in for ArcGIS Desktop

TOOL USED:  Python Add-in Assistant

Creating a Simple python add-in assistant for ArcGIS Desktop to add a Fishnet table to the ArcMap using the python
Type of Python Add-in: Tool Type
Function: At the end of drawing a rectangle on the ArcMap a fishnet with specified rows and columns should appear on the Screen
A python add-in tool is very similar to a button. But, a tool requires user to interact with the map application based on that interaction some business logic will get executed. The Zoom In tool Description: Zoom In in ArcMap will be a great example, when the user draws a rectangle on the ArcMap ,then the map will be redrawn with that rectangle as an extent.
Below steps will help you to create both Toolbox as well as a Tool on it

Steps to Create a Toolbar for your Tool:
1.     To create your own toolbar. Open the Add-n assistant and Go to Add-in Contents tab and give the information about the add-in which you are going to create.
Note: A tool can reside on a Toolbar but cannot sits on a Menu
2.     Click the Add-In Contents tab and begin declaring your add-in customizations. A tool can reside on a toolbar but not on a menu. See creating an add-in toolbar for more detailed procedure.
3.     To Create the tool on your new Toolbar
Right-click the new toolbar and choose New Tool on the right click Menu.
Python addin assistant window

At once you have finished giving out the information and details about the add-in. Click on Save button at the bottom of the wizard. All the necessary files and folders will get created within the working folder of your add-in assistant.
Now it is the time to work on the main code. Go to the python script located on the install folder and open it using your Python idle. Code will appear as shown below with the class name same as your button name.
Note: Do not change the button name as it is directly linked with the button and registered in the your Config.xml file

Onclick() methof of the addin button class
The Onclick () method shown on idle will get executed on when the user click on the tool . But I our case the fishnet should get created whenever the user draw the tool is selected and draw an rectangle on the Map. So to do that OnRectangle () method of the python add-in will comes in to Picture. Replace on click method with OnRectangle () method as shown below:

Fishnet content added to On Rectangle method

·         The code will check for any preexistence of fishnet on the memory using the Exist() Method and deletes if anyone is already there using the Delete_Management() function.
·         CreateFishnet_management is the core function which is going to do the actual work after supply all the necessary parameters to it
·         Refresh the active view to redraw the Map and return the path of the fishnet to the calling function.

No comments: