Friday 25 March 2016

arcgis 10.1 - Performing Tool Validation in Python Toolbox?


I'm trying to make sure there is no '-' in the output filename, and if there is, to set an error (circle with the red 'X') so that the tool can't be run, and have a tooltip that explains the issue. I've tried a bunch of things, but from what I've read, it seems this should work:


if parameters[7].value:
if parameters[7].value.value.count("-") > 0:
parameters[7].setErrorMessage("Output cannot have a '-'." + \
"Please change the dash and rename the output after the tool has run")
else:
parameters[7].clearMessage()

return

I'm not super clear on what is returned from the list of parameters. The paramater type is "DEShapefile". There is no error message being thrown when I use "--" as the filename.


Can someone see why it's not working for me?



Answer



I spoofed up a Python script of 1 parameter and wired it into toolbox and this is how I got it to check a FeatureClass input:


import arcpy

class ToolValidator(object):
"""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()

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 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."""
self.params[0].clearMessage()
if self.params[0].value is None:
self.params[0].clearMessage()
else:
fc = str(self.params[0].value.value)
if fc.count("-") > 0:
self.params[0].setErrorMessage("Output cannot have a dash")
else:
self.params[0].clearMessage()

return

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...