Hi folks,
with help of https://community.esri.com/people/xander_bakker/blog/2016/07/19/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.
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">updateParameters</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""</SPAN>
<SPAN class="keyword token">if</SPAN> filedir<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> filedir
list_of_files <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFiles<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*.txt"</SPAN><SPAN class="punctuation token">)</SPAN>
firsttable<SPAN class="operator token">=</SPAN>list_of_files<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>firsttable<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>filedir<SPAN class="punctuation token">)</SPAN>
complete_filename <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>filedir<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>firsttable
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>complete_filename<SPAN class="punctuation token">)</SPAN>
table<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>MakeTableView_management<SPAN class="punctuation token">(</SPAN>complete_filename<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"kivu_tview"</SPAN><SPAN class="punctuation token">)</SPAN>
desc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#fieldnamesoriginal=[field.name.encode("utf-8") for field in desc.fields] ##collects all fieldnames</SPAN>
fieldselectlist<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> ofields <SPAN class="keyword token">in</SPAN> desc<SPAN class="punctuation token">.</SPAN>fields<SPAN class="punctuation token">:</SPAN>
ftype<SPAN class="operator token">=</SPAN>ofields<SPAN class="punctuation token">.</SPAN>type
fname<SPAN class="operator token">=</SPAN>ofields<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"utf-8"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> ftype<SPAN class="operator token">==</SPAN><SPAN class="string token">"Double"</SPAN> <SPAN class="operator token">or</SPAN> ftype<SPAN class="operator token">==</SPAN><SPAN class="string token">"Long"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>fname<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> fname<SPAN class="operator token">!=</SPAN><SPAN class="string token">"Profile_No"</SPAN> <SPAN class="operator token">and</SPAN> fname<SPAN class="operator token">!=</SPAN><SPAN class="string token">"Longitude (degrees_east)"</SPAN> <SPAN class="operator token">and</SPAN> fname<SPAN class="operator token">!=</SPAN><SPAN class="string token">"Latitude (degrees_north)"</SPAN> <SPAN class="operator token">and</SPAN> fname<SPAN class="operator token">!=</SPAN><SPAN class="string token">"Bot. Depth (m)"</SPAN> <SPAN class="operator token">and</SPAN> fname<SPAN class="operator token">!=</SPAN><SPAN class="string token">"Depth (m)"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN> fname<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> calcursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> calcrow <SPAN class="keyword token">in</SPAN> calcursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> calcrow<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> None<SPAN class="punctuation token">:</SPAN>
fieldselectlist<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>fname<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">break</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>fieldselectlist<SPAN class="punctuation token">)</SPAN>
self<SPAN class="punctuation token">.</SPAN>params<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>filter<SPAN class="punctuation token">.</SPAN>list <SPAN class="operator token">=</SPAN>fieldselectlist
self<SPAN class="punctuation token">.</SPAN>params<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>value<SPAN class="operator token">=</SPAN>fieldselectlist
<SPAN class="keyword token">return</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Any hint would be highly appreciated.