I am trying to create a python add-in toolbar with a combo box that lists all of the [SITE_NUMBER] values in my "Site Inspection" polygon layer. The user should be able to select a [SITE_NUMBER] value from the combo box and have the active map view zoom to the extent of the selected site. The problem is that the toolbar doesn't behave consistently. It works correctly if the "Site Inspection" layer is the only one in the MXD. If there is another layer in the MXD then the combobox only works a single time and then stops working completely. I'm new to python and add-ins so I'm not sure where I'm going wrong.
import arcpy
import pythonaddins
class ComboBoxClass1(object):
"""Implementation for Inspection_Site_Zoom_addin.combobox (ComboBox)"""
def __init__(self):
self.editable = True
self.enabled = True
self.dropdownWidth = '1234567'
self.width = '1234567'
def onSelChange(self, selection):
layer = arcpy.mapping.ListLayers(mxd, "Inspection Site", df)[0]
arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "SITE_NUMBER = '" + selection + "'")
df.extent = layer.getSelectedExtent()
arcpy.RefreshActiveView()
def onFocus(self, focused):
global mxd
mxd = arcpy.mapping.MapDocument('current')
global df
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
layer = arcpy.mapping.ListLayers(mxd, "Inspection Site", df)[0]
arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION")
self.items = []
values = [row[0] for row in arcpy.da.SearchCursor(layer, ["SITE_NUMBER"])]
for uniqueVal in sorted(set(values)):
self.items.append(uniqueVal)
layer=arcpy.mapping.Layer(r"c:\datasource")
layer = arcpy.mapping.ListLayers(mxd, "Inspection Site", df)[0]
bylayer=arcpy.mapping.Layer(r"c:\your\path\to\InspectionSite.shp")