If you haven’t already, take a quick look at:
Part 1 on examples of using the Calculate Value tool to create branches using if-else logic.
Part 2 builds on Part1 highlighting how to: create a script tool from the Python script, a value list, expose tool parameters and create model parameters.
Part 3 highlights an example of using if-then-else script tool and a tool to generate custom errors, messages and warnings.
Part 4 highlights an example of using if-then-else script tool, feature set, value lists, preconditions, inline variable substitution, creating complex SQL expressions and a model tool.
This part 5 of the blog highlights:
- Branching using a script tool with if-then-else logic
- Using Iterator
- Using preconditions
- Using inline variable substitution
- Creating nested models/model within a model
Download the script tool and models from here.
We start with
A simple script tool that checks the projection system of an input feature class against a defined projection system or another feature class (as shown below).
Illustration 1- Does Projection Exists Script tool>
This script tool is used in a nested model/sub-model/model within a model as shown below.
Illustration 2- Does Projection Exists model example
In this example the model uses a script tool with if-then-else logic to check if the spatial reference of the input feature class is the same as the case spatial reference (provided by the user of this tool from the script tool dialog), and outputs two Boolean and a feature layer parameter. If it is similar, the Same as…. Boolean parameter is set to True, the model takes no action and passes the input feature class to the Merge Branch tool. If the projection is different, the feature class is projected and passed to the Merge Branch tool. The model uses a Feature Class iterator to iterate over more than one feature class in the given workspace.
Creating the model
The model was created by:
- Creating the script tool (see blog part 2 to learn about creating a script tool). As shown below the script uses the Arcpy Describe function to check the Spatial Reference property-‘name‘ of the input feature class against that of a specified coordinate system or another feature class and then sets the True-False logic for the two branches.
Illustration 3- Python script for the tool
- Adding and connecting the tools in the two models as shown in illustration 2.
- Setting a precondition on the Project tool to only run if the Not Same as… Boolean parameter is true (see illustration 3 below).
- Connecting the outputs of the script and the Project tool to the Merge Branch tool. The Merge Branch tool merges the two branches coming from the script tool and outputs any branch that is valid at the run time based on the if-then-else case.
- Making the Input Workspace and the output of the Collect Values tool a model parameter. Making its output a model parameter in the sub-model makes it a model variable when it is added to the main model. (Read further to understand).
Illustration 4- Setting precondition on the Project tool in the sub-model
- All tools that are required to run more than once and for each iteration are added to the same model with the iterator. This model is the sub-model or a model tool and is dragged and dropped into the main model just like any other system tool. The main model runs the Merge tool to merge all the inputs coming from the sub-model. The output of the sub-model when added to another model is nothing but the output of the Collect Value tool made into a model parameter (described in the paragraph above). Since there is no iterator in the main model this model will run once which only makes sense as we do not want to run the Merge tool more than once (it would be redundant). This main model calls the sub model that iterates and uses the script tool.
Read more about creating nested models from here.
End of 5 part blog. Phew!!
Enjoy building models!