Generating lists for parameters in a script tool

312
1
11-09-2011 10:17 AM
TylerQuick
New Contributor
I am trying to create a custom tool in AcrMap 10 that baically allows me to do a select by attribute within the tool. I have a database of river features and want to select features using the river name listed in a column in the attribute table. I can get the tool to the point where, when run, it will prompt me for river name and I can manually type it in and that works, but is there a way to populate a list of options for the parameter so I don't have to know the exact form of the river name to use the tool? Any help would be appreciated.
Tags (2)
0 Kudos
1 Reply
Luke_Pinner
MVP Regular Contributor
You can use a ToolValidator class. This is set in the script tool properties dialog in the Validation tab.

Something like:
class ToolValidator:
    """Class for validating a tool's parameter values and controlling
    the behavior of the tool's dialog."""

    def __init__(self):
        """Setup arcpy and the list of tool parameters."""
        import arcpy
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        """Refine the properties of a tool's parameters.  This method is
        called when the tool is opened."""

        names=set() #Sets only handle unique values
        rows = arcpy.SearchCursor('Rivers','','','NAME')
        for row in rows:
            names.add(row.getValue('NAME'))

        for param in self.params:
            if param.name == 'RiverName':
                param.filter.list=list(names)
                break

        return

    def updateParameters(self):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return
0 Kudos