Monday 23 April 2018

arcpy - How to select all values by default in arcgis tool's multivalue parameter using python?


I am using ArcGIS 10.2 and have three parameters, Feature class, field and a multivalue parameter respectively in ArcGIS tool. I populate multivalue parameter with unique values of feature class on selection of feature class and field. Here is the code snippet:


def updateParameters(self):


if self.params[0].value and self.params[1].value:
fc = str(self.params[0].value)
col = str(self.params[1].value)
self.params[2].filter.list = sorted(
set(
row[0] for row in arcpy.da.SearchCursor(fc, [col]) if row[0]
)
)


By default, none of the value is checked in the tool.


Unselected values


How can I check all values of multivalue parameter through ToolValidation class using python 2.7?


All selected



Answer



You can set the value of the parameter to the values you want to be checked, at least when using a Python Toolbox. The same should be true for your case.


For example:


def getParameterInfo(self):
p = arcpy.Parameter()
p.datatype = 'String'

p.multiValue = True
p.name = 'test'
p.displayName = 'Test'
p.parameterType = 'Required'
p.direction = 'Input'
p.filter.list = ['One','Two','Three','Four']
return [p]

def updateParameters(self, parameters):
parameters[0].value = ['Two','Four']

return

enter image description here


edit


For your code example this would look like:


def updateParameters(self):
if self.params[0].value and self.params[1].value:
fc = str(self.params[0].value)
col = str(self.params[1].value)
vals = sorted(set(row[0] for row in arcpy.da.SearchCursor(fc,[col]) if row))

self.params[2].filter.list = vals
self.params[2].value = vals

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...