I have a feature layer, and with ModelBuilder (ArcMap 10.8) I'm trying to ensure that the "Iterate Feature Selection" tool is carried out only if there are selected features; if no feature are selected the tool and next steps shoul be skipped. Following the instruction from the Online Help on Using If-Then-Else logic for branching I create a simple python script with two ouputs. If the output indicating that features are selected is true the process should run.
import arcpy
layerName = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(layerName)
if not desc.FIDSet == '':
arcpy.SetParameterAsText(1,"true")
arcpy.SetParameterAsText(2,"false")
else:
arcpy.SetParameterAsText(1,"false")
arcpy.SetParameterAsText(2,"true")
The issue is when no feature are selected the process still run for every feature in the layer.
Example of model builder with two output options Even when I use 'Iterate Feature Selection' in a separate model, all steps are executed, even if no features are selected
Example of separate model builder for the iteration
How can I resolve?
-
It looks like you are inputting and/or storing three parameters (0, 1, 2) in the first block, but as the selected answer below comments, you haven't chosen to show what you do with them once loaded.JasonInVegas– JasonInVegas2024年09月24日 19:25:27 +00:00Commented Sep 24, 2024 at 19:25
1 Answer 1
It's not possible to answer your question as you do not show how you pass in the parameters into your script tool. I suspect the issue is there.
Below is an easier solution using the calculate value tool.
The Calculate value tool is set up as below:
For your convenience here is the Code Block:
import arcpy
def hasSelection(layer):
desc = arcpy.Describe(layer)
if desc.FIDSet == '':
b = False
else:
b = True
return b
Explore related questions
See similar questions with these tags.