4

I have a toolbar which enables a second and third combobox when a selection is made on the previous. I would like to add a button to 'reset' the toolbar. Basically reload the toolbar to it's original load state. Any thoughts?

Code follows:

import arcpy
import pythonaddins
layer = arcpy.mapping.ListLayers(arcpy.mapping.MapDocument("CURRENT"))[0]
arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")
arcpy.RefreshActiveView()

class Survey(object):
    """Implementation for DatabaseSearch_addin.combobox1 (ComboBox)"""
    def __init__(self):
        #in the initialisation of the addin
        self.items = []
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
        #set the map document reference
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd)[0]
        # check if the layer exists
        if layer:
            #create a column name variable for the search cursor parameters
            column_BlockNo = "L1SURNAM"
            sc = arcpy.SearchCursor(layer,""""L1SURNAM" <> ' '""","",column_BlockNo,column_BlockNo)
            #Due to the multiple values stored in the attribute table a dictionary had to be used
            #Create a dictionary
            dictionary = {}
            #for loop to iterate through the attribute tables getting the values for the column
            for row in sc:
                #set the value as the row value in the column block number
                val = row.getValue(column_BlockNo)
                #the nature of dictionaries does not allow for duplicates thus handling the redundant block numbers in the data table for use in the combobox
                dictionary[val] = val
                #create a list of items for the dictionary's unique keys to be written to
            Items = []
            #for loop to iterate through the keys in the dictionary
            for key in dictionary.iterkeys():
                #append the key values to the list Items
                Items.append(key)
                #append the list of keys to the Blocknumber(self) item list
                self.items.append(key)
            #sort keys in ascending numeric order
            self.items.sort()
        del row
        del sc

    def onSelChange(self, selection):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd)[0]
        arcpy.SelectLayerByAttribute_management(layer,"NEW_SELECTION","L1SURNAM = '" + selection + "'")
        df.zoomToSelectedFeatures()
        arcpy.RefreshActiveView()
        #if the layer exists
        dictionary = {}
        if layer:
            #empty the lot number item list
            combobox2.items = []
            #create a variable for the lot number
            column_Lot = "L2BLOCK"
            #create a search cursor for the layer on the lot number field selecting all the values that are not an empty string
            sc = arcpy.SearchCursor(layer,""""L2BLOCK" <> ' '""","",column_Lot,column_Lot)
            #for loop to read through the values in the lot number field
            for row in sc:
                #set the value as the row value in the column block number
                val = row.getValue(column_Lot)
                #the nature of dictionaries does not allow for duplicates thus handling the redundant block numbers in the data table for use in the combobox
                dictionary[val] = val
                #create a list of items for the dictionary's unique keys to be written to
            #for loop to iterate through the keys in the dictionary
            for key in dictionary.iterkeys():
                #append the key values to the list Items
                combobox2.items.append(key)
                combobox2.items.sort()
                #append the list of keys to the Blocknumber(self) item list
                combobox2.enabled = True
        del row
        del sc
    def onEditChange(self, text):
        #create a variable to store the text in for the selection tool
        SurNam = text
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd)[0]
        arcpy.SelectLayerByAttribute_management(layer,"NEW_SELECTION","L1SURNAM = '" + SurNam + "'")
        df.zoomToSelectedFeatures()

class Block(object):
    """Implementation for DatabaseSearch_addin.combobox2 (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = False
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        #set the map document reference
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd)[0]
        arcpy.SelectLayerByAttribute_management(layer,"SUBSET_SELECTION","L2BLOCK = '" + selection + "'")
        df.zoomToSelectedFeatures()
        arcpy.RefreshActiveView()
        dictionary = {}
        if layer:
            #empty the lot number item list
            combobox3.items = []
            #create a variable for the lot number
            column_Sur = "L3SURNUM"
            sc = arcpy.SearchCursor(layer,""""L3SURNUM" <> ' '""","",column_Sur,column_Sur)
            #for loop to read through the values in the lot number field
            for row in sc:
                #set the value as the row value in the column block number
                val = row.getValue(column_Sur)
                #the nature of dictionaries does not allow for duplicates thus handling the redundant block numbers in the data table for use in the combobox
                dictionary[val] = val
                #create a list of items for the dictionary's unique keys to be written to
            #for loop to iterate through the keys in the dictionary
            for key in dictionary.iterkeys():
                #append the key values to the list Items
                combobox3.items.append(key)
                combobox3.items.sort()
                #append the list of keys to the Blocknumber(self) item list
                combobox3.enabled = True
        del row
        del sc

    def onEditChange(self, text):
        blockNum = text
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd)[0]
        arcpy.SelectLayerByAttribute_management(layer,"SUBSET_SELECTION","L2BLOCK = '" + blockNum + "'")
        df.zoomToSelectedFeatures()

class Section(object):
    """Implementation for DatabaseSearch_addin.combobox3 (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        #only activated when survey name is populated
        self.enabled = False
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd)[0]
        arcpy.SelectLayerByAttribute_management(layer,"SUBSET_SELECTION","L3SURNUM = '" + selection + "'")
        df.zoomToSelectedFeatures()
        arcpy.RefreshActiveView()

class Rest(object):
    """Implementation for DatabaseSearch_addin.button3 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")
        combobox2.items()
        combobox2.enabled = False
        combobox3.items()   
        combobox3.enabled = False
        arcpy.RefreshActiveView()
6
  • 1
    Could you post your code?
    – Fezter
    Commented Feb 6, 2013 at 5:45
  • Could the reset button delete the other comboboxes? Effectively resetting it?
    – Fezter
    Commented Feb 6, 2013 at 5:45
  • I aim to have the reset button clear clear the 2nd and 3rd combobox and reload the first (original state). Here is what have. Thanks for the response! I am providing link to code... I was having trouble posting code to this site? First timer:) forums.arcgis.com/threads/76912-Reset-Python-Add-in @Fezter Commented Feb 6, 2013 at 14:45
  • 1
    I added your code to the question.
    – Fezter
    Commented Feb 6, 2013 at 22:11
  • Thanks @Fezter Any thoughts about a solution? Commented Feb 7, 2013 at 14:16

1 Answer 1

1

I think your only option here is to create a custom global function in which you pass in the toolID as a parameter and then assign a default value to that tool in order to reset it.

I haven't tested this and am just writing it off the top of my head, but it should nudge you in the right direction:

def resetTool(toolID, defaultValue):
    toolID.value = defaultValue

Then on your reset button, simply call that method in your onClick method

class Rest(object):
    """Implementation for DatabaseSearch_addin.button3 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")
        self.resetTool(combobox2, "")
        combobox2.enabled = False
        self.resetTool(combobox3, "")
        combobox3.enabled = False
        arcpy.RefreshActiveView()

Your Answer

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

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