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) 
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 27, 2018 at 22:28
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") Commented Mar 28, 2018 at 19:35
  • Sorry for the delay. Where would i add that in the code exactly? Commented 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") Commented 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.

answered Apr 5, 2018 at 15:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.