3

I want to perform a selection on tool startup in order to obtain a choice list for parameter derived from selected features. Code kind of works, as I get my choice list with correct values, however it works in a strange way:

  1. After pasting code into validation window and clicking OK, selection starts and is performed several times.
  2. On the first tool startup, selection is done again several times.
  3. Choice list gets remembered as Value List and doesn't change with further tool startups (in fact on next startups, nothing new happens - no selection or any other actions from initializeParameters).

What am I doing wrong?

Here is a code:

import arcpy
class ToolValidator(object):
 """Class for validating a tool's parameter values and controlling
 the behavior of the tool's dialog."""
def __init__(self):
 """Setup arcpy and the list of tool parameters."""
 self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
 """Refine the properties of a tool's parameters. This method is
 called when the tool is opened."""
 arcpy.SelectLayerByLocation_management(in_layer="OBR", overlap_type="CONTAINS", select_features="POW")
 choice_list = []
 with arcpy.da.SearchCursor("OBR", ["OBR", "NAZWA"]) as cursor:
 for row in cursor:
 choice_list.append(row[0])
 arcpy.SelectLayerByAttribute_management("OBR", "CLEAR_SELECTION")
 self.params[0].filter.list = choice_list
 return
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."""
 return
def updateMessages(self):
 """Modify the messages created by internal validation for each tool
 parameter. This method is called after internal validation."""
 return
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 4, 2017 at 10:03
0

1 Answer 1

1

If you want to carry any state from one run of the tool to the next then I think you may need to save/read that state to/from a table or text file.

I have done this when wanting to use a Python AddIn to call Python Toolbox Tools that load a required state.

answered May 8, 2018 at 9:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.