1

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) 
3
  • Have you tried to get your Map Page Grid layer with his datasource? Try getting your layer with layer=arcpy.mapping.Layer(r"c:\datasource")
    – CptGasse
    Mar 28, 2018 at 19:35
  • Sorry for the delay. Where would i add that in the code exactly?
    – kbrenn89
    Apr 5, 2018 at 14:19
  • I would replace the 2 line: layer = arcpy.mapping.ListLayers(mxd, "Inspection Site", df)[0] by layer=arcpy.mapping.Layer(r"c:\your\path\to\InspectionSite.shp")
    – CptGasse
    Apr 5, 2018 at 18:17

1 Answer 1

2

Before focusing the comboBox, click anywhere empty in the Table of Contents (ToC) to unselect anything in the ToC. I think this is the simple solution. Because (I realized that later) if any layer or dataframe (named Layers in your code) is selected, ArcGIS crashes beacuse of python add-in.

If you want to do it by code, refer to Arcpy deselect layer in TOC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.