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

3381
4
Jump to solution
03-24-2014 12:51 PM
by Anonymous User
Not applicable
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.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanEvans
Occasional Contributor II
Hi Emily,

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:

layer_path = layer.dataSource


then pass the layer_path to the code that finds the unique values, like this:

values = [row[0] for row in arcpy.da.SearchCursor(layer_path, ["SITE_NUM"])]


That should allow you to remove the clear selection from onFocus()

I think that should work, let me know if it doesn't!

Dan

View solution in original post

4 Replies
DanEvans
Occasional Contributor II
Hi, I'm fairly new to this as well, and have been working on a very similar add-in recently.

I think what might be happening is that you're getting the reference to the mxd and dataframe globally, outside the classes, so that code is only being executed once, so when you load a new mxd, your script doesn't have a reference to it. Try moving this code into the onFocus() function, that way whenever the user clicks the combo box, it will update the reference to the mxd and the dataframe, as well as refreshing the list of SITE_NUMs.

If the layer you are selecting from already exists in the mxd, you can do something like:

layer = arcpy.mapping.ListLayers(mxd, "Name of Layer", df)[0]


to create a reference to the first layer in the mxd called "Name of Layer", and then pass the layer variable to the arcpy.SelectLayerByAttribute_management() function to avoid creating a new selection layer.

Note also that the zoomToSelectedFeatures() function will zoom to the extent of all selected features on ALL layers, which may not be what you want; you can also do:

df.extent = layer.getSelectedExtent()
arcpy.RefreshActiveView()



Hope that helps!

Dan
0 Kudos
by Anonymous User
Not applicable
Thanks so much Dan!  I've implemented your suggestions into my code and it's working much better!

I have another question; the combo box is populated with all SITE_NUM's when the user selects their first site.  But the other sites that were available before disappear when the user tries to select a second site.  That's why I originally created the "Selection" layer, above.  One (partial) solution is to clear my selection in the onFocus function, but it's not ideal.  Ideally I would like to maintain the selection so the user can generate a site report, etc. based on their selection, but still have the complete list of sites available to them for further selection later.  Any thoughts?

My new and improved code... 🙂 

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 onSelChange(self, selection):
        layer = arcpy.mapping.ListLayers(mxd, "SITE", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "SITE_NUM = '" + 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, "SITE", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION")
        self.items = []
        values = [row[0] for row in arcpy.da.SearchCursor(layer, ["SITE_NUM"])]
        for uniqueVal in sorted(set(values)):
            self.items.append(uniqueVal)


Thanks again.
DanEvans
Occasional Contributor II
Hi Emily,

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:

layer_path = layer.dataSource


then pass the layer_path to the code that finds the unique values, like this:

values = [row[0] for row in arcpy.da.SearchCursor(layer_path, ["SITE_NUM"])]


That should allow you to remove the clear selection from onFocus()

I think that should work, let me know if it doesn't!

Dan
by Anonymous User
Not applicable
Worked like a charm!  Thanks so much!