Using the Calculate Value Tool and messaging in Model Builder

Discussion created by Hornbydd on Jun 30, 2011
Latest reply on Jul 1, 2011 by curtvprice

I came across this blog about using the Calculate Value tool. The blog is for ArcGIS 10 but I was able to adapt an example to 9.3.

I found this blog really useful as I never knew about this particular tool and being able to write if then else logic but still within model builder is extremely useful as it avoids the need for having to build scripts that are ultimately files that sit outside a toolbox.

So I created a simple python function that tested for the existence of a field and output a boolean which is a precondition for what ever. It all worked great.

Now my question is simple, how do you write a message to the output window when a model is running? If you look at the code below I've tried two methods but it nevers writes anything, is it possible?

The code block contained the following code for the Calculate Value tool:

def test(f):
import arcgisscripting
gp = arcgisscripting.create(9.3)
desc = gp.Describe(f)
fi = desc.FieldInfo
f = fi.FindFieldByName("RIVER")
if f == -1:
return "False"
gp.Addmessage("Hello world")
print "Hello Word"
return "True"

The output in the model dialog is:

Executing (Calculate Value): CalculateValue test("%FC%") "def test(f):\n import arcgisscripting\n gp = arcgisscripting.create(9.3)\n desc = gp.Describe(f)\n fi = desc.FieldInfo\n f = fi.FindFieldByName("RIVER")\n if f == -1:\n return "False"\n else:\n gp.Addmessage("Hello world")\n print "Hello Word"\n return "True"" Boolean true
Start Time: Thu Jun 30 17:06:39 2011
Value = True
Executed (Calculate Value) successfully.
End Time: Thu Jun 30 17:06:39 2011 (Elapsed Time: 0.00 seconds)
Executing (Get Count): GetCount "Base data\NRFA_Gauge_Locations" 1661
Start Time: Thu Jun 30 17:06:39 2011
Row Count = 1661
Executed (Get Count) successfully.
End Time: Thu Jun 30 17:06:39 2011 (Elapsed Time: 0.00 seconds)

As you can see I never see Hello World in the output.