jodonnelliv

Custom toolbar makes selection by series of ComboBox objects

Discussion created by jodonnelliv on Mar 20, 2013
Latest reply on Apr 3, 2013 by jodonnelliv
Hello all-

I'm working on a custom toolbar that will contain three combo boxes and one button. It will basically allow the user to select the layer (first combo box), the attribute (second combo box), and a specific value (third combo box), and return the total length or area of the features in question (button).

I've gotten the first two combo boxes working: the first one populates all the layers in the current mxd, and the second populates all the fields in the first combo box's selection. Where I'm running into trouble is populating the third combo box with all unique values found in the second combo box's selection. I've experimented with using a Search Cursor to add values to a list, then create a set of that list to pare it down to unique values. Below is my working code, I would greatly appreciate any and all feedback.

Please disregard the button object, as I have not begun working on it.

import arcpy
import pythonaddins

class ButtonClass4(object):
    """Implementation for Totals_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pass

class ComboBoxClass1(object):
    """Implementation for Totals_addin.combobox (ComboBox)"""
    def __init__(self):
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'
    def onFocus(self, focused):
        if focused:
            self.mxd = arcpy.mapping.MapDocument('current')
            layers = arcpy.mapping.ListLayers(self.mxd)
            self.items = []
            for layer in layers:
                self.items.append(layer.name)
    def onSelChange(self, selection):
        global fc
        fc = arcpy.mapping.ListLayers(self.mxd, selection)[0]
        
class ComboBoxClass2(object):
    """Implementation for Totals_addin.combobox_1 (ComboBox)"""
    def __init__(self):
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'
    def onFocus(self, focused):
        if focused:
            fields = arcpy.ListFields(fc)
            self.items = []
            for field in fields:
                self.items.append(field.name)
    def onSelChange(self, selection):
        global attribute
        attribute = arcpy.ListFields(self.items, selection)[0]


class ComboBoxClass3(object):
    """Implementation for Totals_addin.combobox_2 (ComboBox)"""
    def __init__(self):
        self.editable = False
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'
    def onFocus(self, focused):
        if focused:
            values = []
            val = arcpy.da.SearchCursor(fc, attribute)
            for v in val:
                values.append(v)
                value_set = set(values)
                for value in value_set:
                    self.items.append(value)
                del val
    def onSelChange(self, selection):
        global value
        value = arcpy.ListValues(self.values, selection)[0]


Thanks!

Outcomes