Reset Python Add-in

690
2
02-05-2013 04:58 AM
NoahHuntington
Occasional Contributor
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 clear the selection and return toolbar to the state it was when originally loaded? Any thoughts? Here is what I have...

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()
        
Tags (2)
0 Kudos
2 Replies
KarlHillstrom
New Contributor III
If you know what feature class has a selection on it, you should be able to use a button to reset the enabled toggle on the other buttons and then run the SelectLayerByAttribute tool to clear the selection. All of this would be within the script.
0 Kudos
DanielIsenegger
New Contributor II
Hi,
I am interested in particular issue of your posted code. I would be very glad if you can help me to understand this issue:

In the class Survey in the function onSelChange() you refer to 'combobox2'. I suppose that this is the ID of the comobox defined in the associated xml.
But how does the python script know about this ID and respectively of this addin, so that you can refer to the properties of this addin?

Thanks for your help,
Dani
0 Kudos