AnsweredAssumed Answered

Python Add-In, Combo Box and Zoom to Selected Unstable

Question asked by emilyhill1 on Mar 24, 2014
Latest reply on Mar 22, 2018 by Kevin_Brenninkmeijer
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:

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.