ToolValidator code - script tool output type dataset force .dbf extension

581
2
01-17-2014 06:27 AM
curtvprice
MVP Esteemed Contributor
I have a tool that takes a table view as input and outputs either a feature class or table, depending on the input. Of course the parameter data type is read only so I can't change that in the ToolValidator class

I set the input data type is "Table View" and the output data type to "Dataset", and this seems to work. The problem I'm having is that the validator always adds a .dbf extension for folder workspaces (the output still makes a shapefile and adds the shapefile feature class to the map).

I can change the path to .shp manually in the tool dialog and that seems to work fine. But I'd like it to go to .shp if the output workspace is a folder and the input is a feature layer.

Here is my validation code:

  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[0].value and self.params[1].value:
      ds =  self.params[0].value
      if arcpy.Describe(ds).dataType[:5] != "Table":
        out = str(self.params[1].value)
        if os.path.splitext(out)[1] == ".dbf":
          self.params[1].value = os.path.splitext(out)[0] + ".shp"
        # the above change does not "stick" - the internal validation
        # switches it back to .dbf
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    # I needed add this because the default validation
    # was not finding existing data and warning me.
    if self.params[1].value:
      if arcpy.Exists(self.params[1].value):
        self.params[1].setIDMessage("Error",1251)
    return


Screen shot: [ATTACH=CONFIG]30603[/ATTACH]
Tags (2)
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus
No clue, but perhaps it interprets != "Table" as being true if a shapefile is the input, is there anyway you can check to see whether a *.shp or shapefile or featureclass is checked rather than a table?  I tend to do this thing at the tool level in ArcToolbox rather than using validator scripts.  There may be some order of prescedence on how things are run within Arctoolbox
0 Kudos
curtvprice
MVP Esteemed Contributor
No clue, but perhaps it interprets != "Table" as being true if a shapefile is the input, is there anyway you can check to see whether a *.shp or shapefile or featureclass is checked rather than a table? 


Yes, it's definitely seeing shapefiles as features.
>>> arcpy.Describe("polygon_Diss").dataType
u'FeatureLayer'
>>> arcpy.Describe(r"D:\Users\cprice\work\polygon_Diss.shp").dataType
u'ShapeFile'


When I debug the validation code, I can see that the path IS getting calcualted. The issue is with the internal validation of the output, which is changing the extension back from ".shp" to ".dbf" after I painstakingly change it!
0 Kudos