Ensure selection before proceeding in model

500
2
Jump to solution
07-30-2019 08:01 AM
JeffThomasILM
Occasional Contributor II

I have created a model that is intended to run on features I've selected interactively (manually) from the map. That is, feature selection is not part of the model - it is the expected input. The problem is, if there is no selection on a particular layer the model runs on every feature in that layer. I want to prevent this inevitable accident by putting a check into the model that stops it from running unless at least one feature in the layer is selected. (It's not obvious if features from the correct layer are selected due to multiple similar layers in the map. I am often toggling selectability and visibility of the layers.)

The Get Count documentation has an example diagram using it as a precondition, which seems to be exactly what I need. But it doesn't work in my model, I assume due to my manual selection workflow. If I test my model with no selection, Get Count simply returns the number of (unselected) features in my layer. Thus, the count is always more than zero and fails to guard against proceeding without features having been selected.

Here's a screenshot of the first part of my model with the attempted precondition:

Is there a way to do what I'm envisioning? Perhaps this must be done with Python? I'm open to that, but I usually try ModelBuilder first since I'm more familiar with it. Thanks!

1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

You can test for a selection on a layer using a little python in a Calculate Value tool and using it's output (set to Boolean) as a precondition.  The tool would be used as shown below:

The Expression is:

hasSelection("%Gauging Stations%")

The Code block is:

def hasSelection(layer):
 desc = arcpy.Describe(layer)
 s = desc.FIDSet
 if len(s) == 0:
   return False
 else:
   return True‍‍‍‍‍‍‍

View solution in original post

2 Replies
DuncanHornby
MVP Notable Contributor

You can test for a selection on a layer using a little python in a Calculate Value tool and using it's output (set to Boolean) as a precondition.  The tool would be used as shown below:

The Expression is:

hasSelection("%Gauging Stations%")

The Code block is:

def hasSelection(layer):
 desc = arcpy.Describe(layer)
 s = desc.FIDSet
 if len(s) == 0:
   return False
 else:
   return True‍‍‍‍‍‍‍
JeffThomasILM
Occasional Contributor II

Thank you, Duncan! This works exactly as expected.

0 Kudos