This blog was supposed to be the last of a 3-part blog. But we discovered that we had more to say, so this third part isn’t the last; we’ve got a couple more to go.
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, creating a value list, exposing tool parameters and creating model parameters.
This part 3 of the blog highlights:
- Branching using a script tool with if-then-else logic
- Working with Value lists values
- Using custom messaging tool
Download the script tool and model from here.
We start with
The model is created for sharing, but it requires the 3D Analyst extension which might not be available with all those the model is shared with. To account for such a case a simple “if” script tool is added to check the availability of the extension. If the extension is not available, the model uses a custom messaging tool to inform the user about the unavailability of the license and that the model cannot be run.
Illustration 1- Does Extension Exists model
Illustration 2- Does Extension Exists Script tool portion highlighting the ArcPy functions
Creating the model
The model is created by:
- Creating a script tool (see blog part 2 to about creating a script tool)
- Creating a value list filter
To make this script tool a generic tool that can be used to check the availability of any extension, a value list filter is set on the Extension to Check parameter from the script tool properties as shown below.
Illustration 3- Does Extension Exists value list parameter
The list that appears in the drop down list on the tool dialog are the actual names of the extensions available with ArcGIS such as, 3D Analyst, Business Analyst, etc. However, these extensions have a code name that is used to look up the extension license by the script tool. To account for this difference in the name that appears on the tool dialog and the name code that is used in the ArcGIS system, the script tool uses a list of if-then-else statements as shown below.
Illustration 4- Handling extension names and their codes in the script
- Adding and connecting the script tool to other tools in ModelBuilder
- Setting precondition
The Exists Boolean parameter is set as a precondition to the Hillshade tool. This ensures that the Hillshade tool will run only if the Exists Boolean parameter is True. The Does not Exist Boolean parameter is connected as a precondition to another script tool that adds a custom warning if the extension is not available. This custom script tool can be created easily with just three lines of code as shown below:
Illustration 5- Simple Add Warning script tool
To create a more generic tool that can add an error, a message or a warning see the Add Error, Message or a Warning script tool with the download. This tool has three optional input parameters, one each for a custom error, a message or a warning. The script uses if-then-else statements to check which input parameter has a value and based on that creates an appropriate output.
Illustration 6- Generic Add Error, Message or Warning script tool
Any inline variable can be used as an input to this script tool parameters. For example to add an error using the name of the extension that the user of this tool has selected from the drop down list the variable Extension to Check is used as inline variable as shown below:
Illustration 7- Using inline variable substitution in the error message