Change Drop Down Menu for Parameter In Geoprocessing Pane

209
2
Jump to solution
a month ago
AnjeliDubey
New Contributor II

Hello,

I am currently trying to change the drop down menu for one of the script tools I created. Currently, when I set a parameter to a feature class and I open the tool, the drop down menu for the parameter will show all the feature classes listen in the Contents Pane. I would like to change this so the drop down menu shows all feature classes within a specific geodatabase folder. How would I go about doing this? Thank you!

0 Kudos
1 Solution

Accepted Solutions
EdMorris
Esri Contributor

Hello

So – yes- - it can be done but you will need to write some Python Validation code within the ToolValidator class of the Script Tool properties dialog box and make some changes to your Python script tool dialog

1: Display the Tool Properties dialog box for your script tool.

2: On your form create a new parameter which will allow you to search for a workspace, for example, use the following properties as a guide to search for a file geodatabase:

Label:                  Choose the Workspace

Data Type:          Workspace

Filter:                   Workspace :: Local File System (This allows you to select ONLY file geodatabases)

 

I think you may have a parameter which allows you to select feature layers from the Contents pane?

3: Change this parameter using the following as a guide:

Label:                  Choose the Feature class

Data Type:          String

Filter:                   Value List            (This means your parameter will display values within a drop down list)

Your parameters tab in the Tool Properties dialog  box should look like the following:

EdMorris_0-1712253236489.png

Notice the numbers next to the parameters: 0 is the position of the workspace parameter while 1 is the position of the feature class parameter. You will use these numbers in a moment.

 

4: Click the Validation tab.

This is where you can write Python code which will execute when your form is displayed or when you interact with the parameters on the form.

 

5: Locate the updateParameters() method.

6: Underneath the comment for the method write the following code:

 

wkParam = self.params[0]
if wkParam.altered and not wkParam.hasBeenValidated:
    fcParam = self.params[1]
    arcpy.env.workspace = wkParam.value
    fcs = arcpy.ListFeatureClasses()
    if len(fcs) > 0:
        fcParam.filter.list = fcs
        fcParam.value = fcs[0]

 

Be aware of the importance of indentation!

What the code does is:

  • Get the parameters from a list of parameters from the dialog.
  • Check to see if the workspace parameter has been altered by the user choosing a workspace and that the parameter has not gone through ArcGIS Pro’s validation routines already. If you don’t put this check in then you will be forever in a validation spiral which will affect the performance of your script tool.
  • Get the chosen workspace and a list of feature classes
  • Assign the list of feature classes to the feature class parameter using the filter and list properties on the Parameter object. Remember we set the Filter to be a Value List earlier?
  • Get the 1st item from the list and assign it as the first item for display in the combo box before the user clicks on it.

The method should look like the following:

EdMorris_1-1712253474021.png

7: Press the OK button on the Tool Properties dialog box.

8: Double click the script tool to display the dialog:

EdMorris_2-1712253520100.png

9: Choose a file geodatabase

The Choose Feature Class parameter will now be populated with the feature classes within the chosen file geodatabase.

EdMorris_3-1712253546216.png

If you choose a different file geodatabase then the drop down combo box will be cleared and re-populated with the new feature classes from the geodatabase!

I hope this does what you want it to do.

I will make this into a blog to add a bit of meat to these bones and i'll put the link here once it's written but this should be enough for you....

Many thanks ed

View solution in original post

0 Kudos
2 Replies
EdMorris
Esri Contributor

Hello

So – yes- - it can be done but you will need to write some Python Validation code within the ToolValidator class of the Script Tool properties dialog box and make some changes to your Python script tool dialog

1: Display the Tool Properties dialog box for your script tool.

2: On your form create a new parameter which will allow you to search for a workspace, for example, use the following properties as a guide to search for a file geodatabase:

Label:                  Choose the Workspace

Data Type:          Workspace

Filter:                   Workspace :: Local File System (This allows you to select ONLY file geodatabases)

 

I think you may have a parameter which allows you to select feature layers from the Contents pane?

3: Change this parameter using the following as a guide:

Label:                  Choose the Feature class

Data Type:          String

Filter:                   Value List            (This means your parameter will display values within a drop down list)

Your parameters tab in the Tool Properties dialog  box should look like the following:

EdMorris_0-1712253236489.png

Notice the numbers next to the parameters: 0 is the position of the workspace parameter while 1 is the position of the feature class parameter. You will use these numbers in a moment.

 

4: Click the Validation tab.

This is where you can write Python code which will execute when your form is displayed or when you interact with the parameters on the form.

 

5: Locate the updateParameters() method.

6: Underneath the comment for the method write the following code:

 

wkParam = self.params[0]
if wkParam.altered and not wkParam.hasBeenValidated:
    fcParam = self.params[1]
    arcpy.env.workspace = wkParam.value
    fcs = arcpy.ListFeatureClasses()
    if len(fcs) > 0:
        fcParam.filter.list = fcs
        fcParam.value = fcs[0]

 

Be aware of the importance of indentation!

What the code does is:

  • Get the parameters from a list of parameters from the dialog.
  • Check to see if the workspace parameter has been altered by the user choosing a workspace and that the parameter has not gone through ArcGIS Pro’s validation routines already. If you don’t put this check in then you will be forever in a validation spiral which will affect the performance of your script tool.
  • Get the chosen workspace and a list of feature classes
  • Assign the list of feature classes to the feature class parameter using the filter and list properties on the Parameter object. Remember we set the Filter to be a Value List earlier?
  • Get the 1st item from the list and assign it as the first item for display in the combo box before the user clicks on it.

The method should look like the following:

EdMorris_1-1712253474021.png

7: Press the OK button on the Tool Properties dialog box.

8: Double click the script tool to display the dialog:

EdMorris_2-1712253520100.png

9: Choose a file geodatabase

The Choose Feature Class parameter will now be populated with the feature classes within the chosen file geodatabase.

EdMorris_3-1712253546216.png

If you choose a different file geodatabase then the drop down combo box will be cleared and re-populated with the new feature classes from the geodatabase!

I hope this does what you want it to do.

I will make this into a blog to add a bit of meat to these bones and i'll put the link here once it's written but this should be enough for you....

Many thanks ed

0 Kudos
AnjeliDubey
New Contributor II

Thank you so much! This has been a great help, it's difficult to find resources on validation programming here. 

0 Kudos