0

I have a script tool which has string mulivalue paparameter with a filter list.

enter image description here

For example in this case I want to stop script working after it detect that not only "AAA" parameter is selected but other one or more. Also it would be useful if script stop after "AAA" is selected in first box and "1" is selected in second.

I could make hundreds of lines like this:

for s in selected:  
    if "AAA" in selected and "BBB" in selected: 
        arcpy.AddMessage("Don't choose more than one value")
    quit()

But hope that there is an easier way to complete this check.

5
  • Which type is selected variable - list? Clould you split selected variable into two box_1_values and box_2_values? Jan 16, 2017 at 10:56
  • @Mr.Che selected is a result of splitting the values in multivalue parameter, probably it is string Jan 16, 2017 at 10:59
  • @Mr.Che in second box there is a substitute for that, firstly I want to know how to check if there are more than one values selected in first box. Jan 16, 2017 at 11:05
  • You have to know selected viarable type, execute this code in your script and print results here: print type(selected) Jan 16, 2017 at 11:12
  • @Mr.Che you were right, it had list type Jan 16, 2017 at 11:51

3 Answers 3

2

You can disable the script from running by using tool validation (in the script tool properties -> Validation tab). The tool will not run if there are any error messages. Edit the updateMessages() function like:

def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""

    if 'AAA' in str(self.params[0].value) and (len(str(self.params[0].value).split(';')) > 1 or '1' in str(self.params[1].value)):
        self.params[0].setErrorMessage('ERROR!!!')
    return

Results in:

enter image description here

3
  • that works, thanks! I made it in a bit another way to check if two or more values are selected in one box: if ";" in str(self.params[0].value): self.params[0].setErrorMessage("ERROR"). However, I tried to make a condition to check if something is selected in both box1 and box2. Writing a string for each value in box can be complicated, is there any easier way to do it? Jan 17, 2017 at 11:54
  • I think that would be: (len(str(self.params[0].value).split(';')) > 1) or (len(str(self.params[1].value).split(';')) > 1)
    – phloem
    Jan 17, 2017 at 17:34
  • Unfortunately, it doesn't work. I noticed that when script is launched with some selected parameters, the "input" of script tool looks like "Executing: Script AAA 1 #" if "AAA" and "1" are selected. "#" is not a separator and doesn't recognise by .split("#"). I tried to test this if len(str(str(self.params[0].value)+ "#$" + str(self.params[1].value)).split("#")[1]) >1: but in this case it shows error if even something is chosen in the first box Jan 18, 2017 at 8:07
2

This code might help you understand how to call values in arcpy.Parameter().

Full arcpy.Parameter() syntax is here: http://pro.arcgis.com/en/pro-app/arcpy/classes/parameter.htm

import arcpy

def only_one_value_selected(box_value):
    if len(box_value) == 1:
        return True
    else:
        return False

def checker(box_1_values, box_2_values):
    empty = None

    print 
    print u'box_1_values = ', box_1_values.values
    print u'box_2_values = ', box_2_values.values

    if box_1_values.values is not empty and \
       box_2_values.values is empty:
        if only_one_value_selected(box_1_values.values):
            print 'OK - box 1'
        else:
            print 'Error. Too many values selected!'


    elif box_1_values.values is empty and \
         box_2_values.values is not empty:        
        if only_one_value_selected(box_2_values.values):
            print 'OK - box 2'
        else:
            print 'Error. Too many values selected!'        

    elif box_1_values.values is empty and \
         box_2_values.values is empty:
        print 'Error. All boxes are empty!'

    else:
        print 'Error. Too many values selected!'

box_1_values = arcpy.Parameter()
box_2_values = arcpy.Parameter()

box_1_values.multiValue = True
box_2_values.multiValue = True

box_1_values.values = ['AAA', 'BBB', 'CCC', 'DDD']
box_2_values.values = [1, 2, 3, 4]
checker(box_1_values, box_2_values)

box_1_values.values = ['AAA']
box_2_values.values = []
checker(box_1_values, box_2_values)

box_1_values.values = ['AAA', 'CCC']
box_2_values.values = []
checker(box_1_values, box_2_values)

box_1_values.values = []
box_2_values.values = [3]
checker(box_1_values, box_2_values)

box_1_values.values = []
box_2_values.values = [3, 4]
checker(box_1_values, box_2_values)

box_1_values.values = []
box_2_values.values = []
checker(box_1_values, box_2_values)

Script prints:

box_1_values =  [u'AAA', u'BBB', u'CCC', u'DDD']
box_2_values =  [u'1', u'2', u'3', u'4']
Error. Too many values selected!

box_1_values =  [u'AAA']
box_2_values =  None
OK - box 1

box_1_values =  [u'AAA', u'CCC']
box_2_values =  None
Error. Too many values selected!

box_1_values =  None
box_2_values =  [u'3']
OK - box 2

box_1_values =  None
box_2_values =  [u'3', u'4']
Error. Too many values selected!

box_1_values =  None
box_2_values =  None
Error. All boxes are empty!
1
0

Recently I have found an easy way to check it. Thanks to Mr.Che, his answer and main idea that selected value had list type.

I will post the whole code with solution:

import arcpy
import os

from arcpy import env
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame

all_selected = arcpy.GetParameterAsText(0) 
nlevel = arcpy.GetParameterAsText(1)
correct = arcpy.GetParameterAsText(2)

selected = all_selected.split(";")
nsel = nlevel.split(";") 
corr = correct.split (";")

aa = str (selected)
ab = str (nsel)

if ", " in aa:
    arcpy.AddMessage ("Two or more values are are chosen")  
    quit()

if ", " in ab:
    arcpy.AddMessage ("Two or more values are are chosen")  
    quit()

if "u" in aa and "u" in ab:
    arcpy.AddMessage ("Values from box1 and box2 are chosen")
    quit()

The key is that when values are selected from multivalue parameter in script tool, the output (in my case selected) will look like: [u'AAA', u'BBB', u'CCC'] if AAA, BBB and CCC are selected. Converting list type to string will give us an option to find ", " thing in this line so that we can see that not the only one thing is chosen. Same about the check of values selected in two boxes but here we find "u" or "u'" in both box1 and box2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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