Best way to work on selected features

190
2
2 weeks ago
vankerck
New Contributor

I have a Python toolbox in Pro that contains several tools that have a GPFeatureLayer as an input. I'd like to be able to run the tool on just the selected features if any are selected, or on the whole feature class if none are selected. Is the best way to use getSelectionSet to see if any features are selected, then do a query for those features with the right objectid values?

0 Kudos
2 Replies
Tom_Laue
New Contributor III

 My understanding is if you point a tool at a layer in the Table of Contents, it will run on the selected records (if any are selected) and on the entire layer if none are selected.

99% of my Python scripts run outside ArcMap but that's the way it works in standalone scripts.

vankerck
New Contributor

Thanks Tom_Laue. I think I finally figured it out and it was a matter of how I got the actual layer. I was initially using parameters[0].value.dataSource to get the feature class and running it on that, but that applied the logic to the entire feature class, and not just the selected features. I then tried to use parameters[0].value, but that would give me a schema lock issue. Finally, I went with the following, which seemed to work as intended:

lyr_name = parameters[0].valueAsText
lyr = arcpy.management.MakeFeatureLayer(lyr_name, 'selected_layer')
0 Kudos