Select to view content in your preferred language

making an if-then script for modelbuilder that is based on the model iteration number

596
1
01-27-2011 09:42 AM
JohnGallo
Deactivated User
I am trying to make a script that can be used to do if-then branching in model builder based on the iteration number of the underlying model. 

I modeled it after the if-then script example in esri help (http://webhelp.esri.com/ARCGISDESKTOP/9.3/index.cfm?TopicName=Branching%3A_Implementing_If-Then-Else... )

Here is what I came up with.

I wonder if the %i% convention that modelbuilder uses to get the iteration number of the model transfers into python as a variable?  That might be my problem.

The %i% trick comes from this page: http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=822&pid=821&topicname=An_overview_of_model_it...

any suggestions?

# ---------------------------------------------------------------------------
# iteration-if-then1.py
# Created on: Thu Jan 27 2011 09:47:59 AM
#   (generated by ArcGIS/ModelBuilder) and then John Gallo
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Local variables...Try to get the iteration number that the model is on.
IterationNum = "%i%"

#branch depending on iteration number.
if IterationNum=1:
      gp.Addmessage("Iteration number = 1")
      gp.SetParameterAsText(2, "True")
      gp.SetparameterAsTExt(3, "False")
else:
      gp.Addmessage("Iteration number NOT = 1")
      gp.SetParameterAsText(2, "False")
      gp.SetparameterAsTExt(3, "True")

(Oh, and to make things worse, I have tried for over an hour to install pythonwin, to no luck.  it wants a mfc71.dlll , but even when I download it, then copy it and paste it into system32 directory PyhonWin still says it needs it...)
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor
John,

I use PyScripter as my Python development environment, it's very good.

OK first you need to create your script and set it up as shown below:

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Local variables...Try to get the iteration number that the model is on.
IterationNum = gp.GetParameterAsText(0)

# Do stuff...
gp.addmessage("it = " + IterationNum)


Now add your script to the tool box and ensure that it has a single parameter of type text.

Create your model and set your model to run X times. Drag your script which is now a tool onto your model and expose the parameter and set it's value to %n%. Your script will then be able to use it but be aware that a string is passed to the script.

Duncan
0 Kudos