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 as ARC self.arcpy = ARC self.paramsIndex = {'networkDataset':0, 'impedance': 1} self.params = arcpy.GetParameterInfo() #Get the parameter objects that will be modified self.networkParam = self.params[self.paramsIndex['networkDataset']] self.impedanceParam = self.params[self.paramsIndex['impedance']] 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 self.networkParam.altered: if self.networkParam.value and not self.networkParam.hasBeenValidated: #Check if the NDS exits try: self.getNetworkProps(self.networkParam.value) except: self.resetNetworkProps() return else: self.resetNetworkProps() return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def getNetworkProps(self,network): '''Update the parameter values based on th network dataset''' desc = self.arcpy.Describe(network) defaultCostAttr = "" costAttributes = [] count = 0 #Build a list of cost attributes and get the default cost attribute attributes = desc.attributes for attribute in attributes: usageType = attribute.usageType name = attribute.name useByDefault = attribute.useByDefault if usageType == "Cost": #Check if the cost attribute has to be used by default. if useByDefault: defaultCostAttr = name costAttributes.append(name) #Set the lists for cost attribute self.impedanceParam.filter.list = costAttributes #Set the default value for the cost attribute if not self.impedanceParam.altered: if defaultCostAttr == "" and costAttributes: #if there is no default use the first one in the list defaultCostAttr = costAttributes[0] self.impedanceParam.value = defaultCostAttr return def resetNetworkProps(self): """Resets the network dataset derived parameters to nothing""" self.impedanceParam.filter.list = [] self.impedanceParam.value = "" return