I am horribly new to coding with python. I made a toolbar using the wizard and old bits of VBA code. I created a button to search by map number, a tool to calculate area from UI and a second tool that would clear the area selected.
I get the following icons when I install the add in to test.
The following is what I have for my code:
import arcpy
import sys
import os
import pythonaddins
class ZoomToMapNumber(object):
"""Implementation for Test Addin_addin.button (Button) Everything from Import to del mxd is external code / deleted pass after 2nd def"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
mapno = sys.argv[1]
mxd = arcpy.mapping.MapDocument("Current")
lyr = arcpy.mapping.ListLayers(mxd,"SDEOWNER.MapIndex")[0]
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"MAPNO\" = " + str(mapno))
result = int(arcpy.GetCount_management(lyr).getOutput(0))
arcpy.AddMessage("Number of selected records: " + str(result))
if result > 0
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.zoomToSelectedFeatures()
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
del df
else:
arcpy.AddError("No record selected for map number " + str(mapno))
del lyr
del mxd
class CalcDrainArea(object):
"""Implementation for Test Addin_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "Line" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
inputs = []
def onMouseDownMap(self, x, y, button, shift): # Inputs x,y coordinate pair to inputs for every mouse click
for coordinate_pairs in (x, y):
inputs.append(x, y)
def onDblClick(self, x, y, button, shift): # Inputs the final x,y pair and the first x,y pair as last four values into the 'inputs' array
final_x = inputs[0]
final_y = inputs[1]
inputs.append(x, y)
inputs.appent(final_x, final_y)
return inputs
def calc_drainage_area(inputs) #takes (x1, y1, x2, y2, ... xn, yn)
cArea = 0
# Calculate the area based on the Surveyor's formula
for i in range(0, len(x)-1):
cArea = cArea + x[i]*y[i+1] - x[i+1]*y[i]
cArea = abs(cArea / 2)
class ClearArea(object):
"""Implementation for Test Addin_addin.tool_1 (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
mxd = arcpy.mapping.MapDocument("Current")
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
elm.delete()
arcpy.RefreshActiveView()
del mxd
The following is the xml that results
<?xml version="1.0"?>
-<ESRI.Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.esri.com/Desktop/AddIns">
<Name>Drainage Test 2</Name>
<AddInID>{51a4d912-ff75-4245-a32c-242ea4bfa5dc}</AddInID>
<Description>Drainage Test 2</Description>
<Version>0.1</Version>
<Image/>
<Author>Drainage Test 2</Author>
<Company>Drainage Test 2</Company>
<Date>07/14/2015</Date>
-<Targets>
<Target version="10.1" name="Desktop"/>
</Targets>
-<AddIn language="PYTHON" namespace="Drainage Test Addin 2_addin" library="Drainage Test Addin 2_addin.py">
-<ArcMap>
-<Commands>
-<Button tip="ZoomToMapNumber" message="ZoomToMapNumber" image="" id="Drainage Test Addin 2_addin.button" class="ZoomToMapNumber" category="Drainage Test 2" caption="ZoomToMapNumber">
<Help heading="ZoomToMapNumber">ZoomToMapNumber</Help>
</Button>
-<Tool tip="CalcDrainArea" message="CalcDrainArea" image="" id="Drainage Test Addin 2_addin.tool" class="CalcDrainArea" category="Drainage Test 2" caption="CalcDrainArea">
<Help heading="CalcDrainArea">CalcDrainArea</Help>
</Tool>
-<Tool tip="ClearArea" message="ClearArea" image="" id="Drainage Test Addin 2_addin.tool_1" class="ClearArea" category="Drainage Test 2" caption="ClearArea">
<Help heading="ClearArea">ClearArea</Help>
</Tool>
</Commands>
<Extensions> </Extensions>
-<Toolbars>
-<Toolbar id="Drainage Test Addin 2_addin.toolbar" category="Drainage Test 2" caption="Toolbar" showInitially="true">
-<Items>
<Button refID="Drainage Test Addin 2_addin.button"/>
<Tool refID="Drainage Test Addin 2_addin.tool"/>
<Tool refID="Drainage Test Addin 2_addin.tool_1"/>
</Items>
</Toolbar>
</Toolbars>
<Menus> </Menus>
</ArcMap>
</AddIn>
</ESRI.Configuration>