Custom toolbar makes selection by series of ComboBox objects

2607
2
03-20-2013 08:16 AM
johnodonnell
New Contributor
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!
Tags (2)
0 Kudos
2 Replies
johnodonnell
New Contributor
For anyone interested, I figured out what was going wrong. When declaring a global variable in the second combo box, I declared the object as the variable, rather than the name of the object. Then, when I tried passing that variable into the next combo box, it failed, since it needed a string input. I'm away from my work computer now, but I will post my code tomorrow.

Cheers!
0 Kudos
johnodonnell
New Contributor
Apologies, I forgot to post my finished script. Here it is for anyone interested. It basically allows an unexperienced ArcGIS user to select by attributes and returns the number of features meeting the specified criteria. It was a fun exercise, but I kind of doubt it will benefit any of you that much.

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):
        
        desc = arcpy.Describe(fc)

        if desc.shapeType == "Point":
            arcpy.MakeFeatureLayer_management(fc, "pointfeats", '"' + attName + '"' + ' = ' + "'" + valName + "'")
            count = arcpy.GetCount_management("pointfeats")
            print str(count) + " FEATURES WITH " + str(attName) + " EQUAL TO " + (str(valName)).upper()
            arcpy.Delete_management("pointfeats")

        elif desc.shapeType == "Polyline":
            arcpy.MakeFeatureLayer_management(fc, "linefeats", '"' + attName + '"' + ' = ' + "'" + valName + "'")
            lengths = []
            curs = arcpy.da.SearchCursor("linefeats", "Shape_Length")
            for cur in curs:
                lengths.append(cur)
            floatLengths = [float(e[0]) for e in lengths]
            totalLength = sum(floatLengths)
            print str(round(totalLength,2)) + " TOTAL LINEAR FEET"
            arcpy.Delete_management("linefeats")

        elif desc.shapeType == "Polygon":
            arcpy.MakeFeatureLayer_management(fc, "polygonfeats", '"' + attName + '"' + ' = ' + "'" + valName + "'")
            areas = []
            curs = arcpy.da.SearchCursor("polygonfeats", "SHAPE_Area")
            for cur in curs:
                areas.append(cur)
            floatAreas = [float(e[0]) for e in areas]
            totalArea = sum(floatAreas)
            print str(round(totalArea,2)) + " TOTAL SQUARE FEET"
            arcpy.Delete_management("polygonfeats")

        else:
            print "Calculation can not be performed"

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 attName
        global attribute
        attribute = arcpy.ListFields(fc, selection)[0]
        attName = attribute.name



class ComboBoxClass3(object):
    """Implementation for Totals_addin.combobox_2 (ComboBox)"""
    def __init__(self):
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'
    def onFocus(self, focused):
        if focused:
            values = [row[0] for row in arcpy.da.SearchCursor(fc, attName)]
            uniqueValues = set(values)
            self.items = []
            for uniqueValue in uniqueValues:
                self.items.append(uniqueValue)
    def onSelChange(self, selection):
        global valName
        val = (self.items, selection)[1]
        valName = str(val)

0 Kudos