Friday, January 20, 2012

ArcGIS Toolbox: Loading script tool parameter defaults from a config file.

Lets say we create a script tool to run in ArcGIS Desktop and it has 10 parameters. On average the user only needs to change one of those parameters to run the tool 98% of the time.

We make each parameter available because hard coded  parameters can kill your tool 2% of the time. Or maybe more like 10%.... Whatever.

Its best to have all the parameters exposed in the tool dialog and loaded with the defaults your going to use 98% of the time. If these defaults are the same across many tools you don't want to be going into each tool's parameters to set the defaults. You want one location to set all the defaults. That's your config file bro.

So here is an example of one tool's tool validator that loads the defaults from a config file located in the same directory as the toolbox we are running the tool from.


import sys
import os
import ConfigParser
import arcpy

# Finds the path of the toolbox
scriptPath = sys.path[0]
# The config file should be in the same directory as the toolbox
configFile = scriptPath + "\\amp_config.cfg"

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."""
    self.params = arcpy.GetParameterInfo()
    #Group the following parameters in the dialog box
    self.params[2].category = "Defaults"
    self.params[3].category = "Defaults"
    self.params[4].category = "Defaults"
    self.params[5].category = "Defaults"
    self.params[6].category = "Defaults"
    self.params[7].category = "Defaults"

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

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
    # If the config file is not found the parameters remain blank.
    if not os.path.exists(configFile):
     self.params[2].value = ""
     self.params[3].value = ""
     self.params[4].value = ""
     self.params[5].value = ""
     self.params[6].value = ""
     self.params[7].value = ""
     return
   
    #If the config file is found it moves on to populate the parameters.
    # Adds defalut values from the config file when the first paramter is filled for the first time.
    if self.params[0].altered and not self.params[0].hasBeenValidated:
      if not self.params[0].value == None:
        # Loads the config file into memory
        config = ConfigParser.SafeConfigParser()
        config.readfp(open(configFile))
       # Gets the values from the config file.
        scratchDir = config.get('environment','scratchDir')
        coverageName = os.path.basename(str(self.params[0].value))
        logFile = scratchDir + "\\" + coverageName + "_log.txt"    
        self.params[3].value = logFile
        self.params[4].value = coverageName
        self.params[6].value = scratchDir + "\\" + coverageName + "_cov.gdb"
        self.params[2].value = scratchDir
        self.params[5].value = config.get('dataConversion','coverageList')
        self.params[7].value = config.get('dataConversion','annotationLayerList')
# If parameter 1 is false then parameter 7 is greyed out.
    if self.params[1].value == False:
      self.params[7].enabled = False
    else:
      self.params[7].enabled = True
    return

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

No comments:

Post a Comment