Using script tool parameters


In this topic


About using script tool parameters

Geoprocessing tools typically have parameters. Parameters or arguments, make programs more generic and flexible so they can be used to vary input data, set other tool parameters used in the program, or control the program's processing logic.
When a program is added to a toolbox as a script tool, a number of parameters can be defined. These parameters correspond to a program's parameters, representing input and output values.
Typically, programs only have input values, since they are usually run in an independent fashion. Script tools must define their outputs so tools work as expected in models built with ModelBuilder or on the command line in the geoprocessing window. Models need an output parameter so they can be used as input for other tools, while tools on the command line allow users to specify the name and location of a tool's output.
System tools built with ArcObjects, populate the properties of output variables in a model to aid subsequently connected tools. These properties are not required for building models but aid in their construction. System tools do this with a validation routine that is called each time a parameter value is changed for a tool. Script tools do not have a validation routine, just one for execution, which is the script. The geoprocessing framework does some validation for script tools automatically, such as ensuring a parameter value matches what is expected for a parameter, including a number for a numeric parameter and a path to a feature class for a feature class parameter. By defining dependencies between parameters, certain behavior can also be built into a tool.
For example, a dependency between a feature class parameter and a field parameter in which the field parameter is dependent on the feature class means the field list is automatically populated on the tool, when the feature class is given a valid value. Another program may not have a new output because it alters a dataset specified as an input parameter. In this case, the script tool still declares an output parameter, with the type set derived with a dependency on the input dataset. The script tool automatically sets the output parameter's value to be the same as the input dataset when used in a model, so the tool can be used in a workflow.

Getting input parameter values

Programming languages provide a mechanism for accessing arguments passed to the program from the caller. Java, for example, provides an array of type Object that represents the arguments passed to a method. A program must use these mechanisms if it is not the source of a script tool. See the following code example:
[Java]
// Set the workspace environment.
gp.SetEnvironmentValue(� � �workspace � � �, args[0]);
The following code example shows how to call the Java executable .jar in the preceding code example to specify an input workspace:
[Java]
 > java - jar clipdata.jar d:  \
    soilsewfoundland
If a program is the source of a script tool, it can use the geoprocessor to access the input parameter values. The following code example, shows how to use the GetParameterValue method to get input parameter values:
[Java]
// Get the input workspace environment value.
Object workspace = gp.getParameterValue(0)gp.setEnvironmentValue(� � �workspace � �
    �, (String)workspace);

Setting output parameters

Output parameter values may not be known until the program is executed. The program must evaluate or calculate an output value based on its input, so script tools must have a way to specify output values after execution so the values can be used in a model workflow.
The setParameterValue method sets the value of an output parameter using either an object, such as a value table, or a text string. An index value is required to indicate the parameter that is updated. See the following code example:
[Java]
// Intialize the geoprocessor.
GeoProcessor gp = new GeoProcessor();
// Check the feature classes and set the output parameter.
gp.setEnvironmentValue(� � �workspace � � �, String(gp.getParameterValue(0)); if 
    (gp.listFeatureClasses("*", "POLYGON", "").next() != null){
    gp.addMessage("Feature type is polygon"); Object param = gp.getParameterValue(0)
        ; gp.setParameterValue(0, "true"); gp.setParameterValue(1, "false"); 
}

else if (gp.listFeatureClasses("*", "POLYLINE", "").next() != null){
    gp.addMessage("Feature type is line"); gp.setParameterValue(0, "false");
        gp.setParameterValue(1, "true"); 
}

else{
    gp.addMessage("Coverage has neither polygons nor lines"); gp.setParameterValue(0,
        "false"); gp.setParameterValue(1, "false"); 
}