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

Your Answer

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

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