I am trying to create a python add-in toolbar with a combo box. The combo box lists all of the unique [SITE_NUM] values in my SITE feature class. The user should be able to select a SITE_NUM from the combo box and have the active map view zoom to the selected site. The problem is that the toolbar doesn't behave consistently. It will work fine in one map session and then won't work at all when the same document is opened a minute later with no saved changes. I'm new to python and add-ins so I'm not sure where I'm going wrong.
My code is as follows:
If anyone has any suggestions as to how to improve my code and forgo the creation of an entirely new "Selection" layer, but keep the combo box populated with all of the SITE_NUM values after a selection is made, I would greatly appreciate it. I'm truly stumped.
My code is as follows:
import arcpy import pythonaddins global mxd mxd = arcpy.mapping.MapDocument('current') global df df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0] class ComboBoxClass1(object): """Implementation for Sites_addin.combobox (ComboBox)""" def __init__(self): self.editable = True self.enabled = True self.dropdownWidth = 'WWWWWWWWWW' self.width = 'WWWWWWWWWW' def onFocus(self, focused): layer = r"C:\...\SITE" self.items = [] values = [row[0] for row in arcpy.da.SearchCursor(layer, ["SITE_NUM"])] for uniqueVal in sorted(set(values)): self.items.append(uniqueVal) def onSelChange(self, selection): layer = r"C:\...\SITE" arcpy.MakeFeatureLayer_management(layer, "Selection") arcpy.SelectLayerByAttribute_management("Selection", "NEW_SELECTION", "SITE_NUM = '" + selection + "'") arcpy.RefreshActiveView() df.zoomToSelectedFeatures() arcpy.RefreshActiveView()
If anyone has any suggestions as to how to improve my code and forgo the creation of an entirely new "Selection" layer, but keep the combo box populated with all of the SITE_NUM values after a selection is made, I would greatly appreciate it. I'm truly stumped.
I think what you need to do is populate the combo box from the underlying SITES feature class, rather than the layer. You can get the path for the data source of the layer like this:
then pass the layer_path to the code that finds the unique values, like this:
That should allow you to remove the clear selection from onFocus()
I think that should work, let me know if it doesn't!
Dan