1

I'm new to ToolValidator for a script tool and I'd like to make two checks which then adds an error if either rule is violated. I need to make sure parameter 2 and 3 (feature classes) are within parameter 1 (feature dataset). The tool will fail (roughly 3/4 of the way through) if the feature classes are not contained in the feature dataset.

Also, the tool needs to display an error is the feature class is empty.

I know Tool Validator is the way to go. I don't have much and am willing to read stuff. Also, this doesn't work, but I've managed to piece it together with some other posts.

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."""
    import arcpy    
    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."""
    if self.params[3].value==True:
        self.params[3].enabled==True
        fc=self.params[3]
        rows = arcpy.SearchCursor(fc)
    if rows.next():
        self.params[3].enabled==True
    else:
        self.params[3].enabled==False
    return

def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter.  This method is called after internal validation."""
return
    if self.params[3].hasBeenValidated:
        self.params[3].clearMessage()
    else:
        self.params[3].setErrorMessage('Your feature class is empty. 300 Tool will not fail.')
1

Tool validators are not easy to program, there's no debugging or text output so all you get is does/doesn't work. I notice that your if rows.next(): is on an incorrect indent level which could be why it isn't working. I also notice you're equating the self.params[3].value==True: where the value should be a path to a feature class.. not sure what's going on there.

As I see your problem you want to know if parameter 2 & 3 start with parameter 1 which should be string manipulation and to test if parameter 2 & 3 both contain some features. This should get you moving in the right direction..

# I have expanded the terms to make it easier to follow the logic
# make them all uppercase to make comparison easier
P1_val = self.params[1].value.upper()
P2_val = self.params[2].value.upper()
P3_val = self.params[3].value.upper()

P1_len = len(P1_val) # the length of the first parameter
# Python substrings are specified like this: source[start:end]
# so "abcd"[0:2] = "ab".. makes this next bit easier to read
if P2_val[0:P1_len] == P1_val and P3_val[0:P1_len] == P1_val:
    # both p2 and p3 contain p1
    # move forward to getting the counts
    cntObj = arcpy.GetCount_management(P2_val) # GetCount returns an object
    P2_cnt = int(cntObj.getOutput(0))          # get the count from the object

    cntObj = arcpy.GetCount_management(P3_val) # GetCount returns an object
    P3_cnt = int(cntObj.getOutput(0))          # get the count from the object

    if P3_cnt != 0 and P2_cnt !=0:
        # all good!

This will find if both parameter 2 (P2) and parameter 3 (P3) are inside parameter 1 (P1) and that both have feature counts not equal to 0 (can't be negative).

The next question is why does your script fall over about 3/4 the way through if they're not in the same feature dataset?

  • Topology comes in 3/4 of the way through. Which means another person would not know their inputs are incorrectly chosen until topology. – Jennifer May 20 '15 at 14:06
  • As for your other concerns, ie. What's going on there?, I usually manage to piece together scripts reading a whole lot of help menu and stack exchange. Eventually, I find enough information and try enough things that I get it to work. But tool validator doesn't have many questions. I did find a post about empty feature classes and trying to use a search cursor, and another post used a method that did an update after the parameter was validated by tool validator. I knew what I had was wrong, but it was a start. But like you said, tool validator will not give errors if things do not work. – Jennifer May 20 '15 at 14:10
  • Yes Jennifier, you wont find much on tool validators because they're such a pain to work with (as you've found out no doubt); it's much easier (to debug) to skip the validator and do the testing in the script. You're certainly learning a lot doing what you're doing so keep up the good work! That's how I learned python for ArcGis. One trick I learned is to do the validation first in the script then once that was working migrate it to a validator (change the object names of course). What python editor do you use to write your code? a better editor will show inconsistent indent and save you time. – Michael Stimson May 20 '15 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.