Friday, June 26, 2009

Using a ToolValidator to create a Value List

New at 9.3, ESRI has introduced the ToolValidator. To many this is old news, because we are on 9.3.1, but to some, it's new due to the fact that it's not talked about much. The toolvalidator allows for more control of the GUI without messing around with ArcObjects or .NET.

More information on ToolValidators can be found here.

Moving on let's get to an example. Say you want to populate a string field with a predefined list of object or what is known as a value list to the GP world. Simply right click your script and click on the Validation Tab. Next click on edit, and you are ready to begin.

By default, IDLE should open, unless you customized your system to use pythonwin. You should see something like this:


Since you want the value like to show up when the GUI is fired, use the initializeParameters(self) method.

Now just create a simple search cursor, and append in the values from the table you want to force the user to choose like such:

def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
import os, sys, arcgisscripting as ARC
self.GP = ARC.create(9.3)
self.rows = self.GP.searchcursor(r"\CountryCodes.dbf")
self.row = self.rows.next()
self.holder = []
while self.row:
self.holder.append(self.row.getvalue('CC'))
self.row = self.rows.next()
self.holder.append('*')
self.holder.sort()
self.params[1].Filter.List = self.holder
return


Now you have a drop down menu from which a user is forced to select from. The best thing is that it's dynamic, so when the table changes so does the selection.

Enjoy