AnsweredAssumed Answered

Push list of field names from validator into script tool

Question asked by n.ueberschaeresri-rw-esridist Employee on Feb 19, 2019
Latest reply on Feb 25, 2019 by n.ueberschaeresri-rw-esridist

Hi folks, 

 

with help of Implementing cascading drop down lists in a toolbox using validation with Python I gained some understanding how I can make my script tool take conditions and additional information to fill the tool. 

I have three input parameters (a directory, a min and a max value). I would like to have a multiple choice list of those indicators in my txt file (in that directory) that are not empty. 

My validator should check the first txt file in that directory, read the field names, if they are not within a list of excluded field names then read the first value, if it is not None than add the field name to a list. Works perfectly in pyscripter. 

And I also get the correct list displayed in my tool script. Buuut, the tool says the parameter's field type is invalid after displaying the field names. I could imagine that it has something to do with the filter in the parameter which is set to DataType=Field but of course it is not really a field name any longer after returning it from my script. But what else could I use? Or can I somehow tell the self.params that it is still a (list of) field name(s)? 

 

This is my updateParameters. 

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 filedir:
        arcpy.env.workspace = filedir
        list_of_files = arcpy.ListFiles("*.txt")
        firsttable=list_of_files[0]
        print(firsttable)
        print(filedir)
        complete_filename = str(filedir)+"\\"+firsttable

        print(complete_filename)
        table=arcpy.MakeTableView_management(complete_filename, "kivu_tview")


        desc = arcpy.Describe(table)
        #fieldnamesoriginal=[field.name.encode("utf-8") for field in desc.fields] ##collects all fieldnames
        fieldselectlist=[]
        for ofields in desc.fields:
            ftype=ofields.type
            fname=ofields.name.encode("utf-8")
            if ftype=="Double" or ftype=="Long":
                print(fname)
                if fname!="Profile_No" and fname!="Longitude (degrees_east)" and fname!="Latitude (degrees_north)" and fname!="Bot. Depth (m)" and fname!="Depth (m)":
                    with arcpy.da.SearchCursor(table, fname) as calcursor:
                        for calcrow in calcursor:
                            if calcrow[0] is not None:
                               fieldselectlist.append(fname)
                               break
        print(fieldselectlist)
        self.params[3].filter.list =fieldselectlist
        self.params[3].value=fieldselectlist
    return

 

Any hint would be highly appreciated. 

Outcomes