# Using the Calculate Value Tool and messaging in Model Builder

5422
8
06-30-2011 08:07 AM
MVP Frequent Contributor
All,

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"
else:
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.

Duncan
Tags (3)
8 Replies
MVP Esteemed Contributor
I posted this same question here and have no response yet... I'm pretty sure it can't be done. The best I've been able to come up with -- outside of writing a script tool - is this:

Expression: "\n\n** YOUR PARAMETER IS WEAK **\n** TRY AGAIN **\n"
(no code block)

This shows up in the messaging as:

```Executing (Message): CalculateValue "\n\n** YOUR PARAMETER IS WEAK **\n** TRY AGAIN\n" # Variant
Start Time: Thu Jun 30 10:51:10 2011
Value =

** YOUR PARAMETER IS WEAK **
** TRY AGAIN **

Succeeded at Thu Jun 30 10:51:10 2011 (Elapsed Time: 0.00 seconds)```

You could set this up with a precondition to have it print the message under certain conditions (perhaps determined by another Calculate Value tool boolean result).

All,

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 too 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"
else:
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.

Duncan
MVP Frequent Contributor
Curtis,

Oh dear, thanks for your idea, a bit of a fudge but I guess a tool should really exist, I suggested this on the ideas website.

Duncan
MVP Esteemed Contributor
In Arc 10 you could create a python script and import it into the toolbox - this is a new functionality that will do what you want. But I do agree that there should be a standard tool for this and voted up your idea!

Here's the online" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#... help on embedding scripts in toolboxes.
Esri Regular Contributor
Try the attached script tool to add custom message in ModelBuilder.
MVP Frequent Contributor
Curtis,

Ah! This is good news about embedding scripts into the toolbox. I don't have v10 although there is a v10 machine in the office but I have yet to get up to date with whats new.  So what happens if you embed a python script then realise you've got a bug in it (highly likely as I hate using Python!), is the process of embedding a script a one way task or can you un-embed it to edit it?

Duncan

In Arc 10 you could create a python script and import it into the toolbox - this is a new functionality that will do what you want. But I do agree that there should be a standard tool for this and voted up your idea!

Regular Contributor
The ability to create script tools ("embed them in a toolbox") has been around since 9.0.  If you're on 9.3, you can start with this help topic : An overview of creating script tools
MVP Esteemed Contributor
Here's an attempt to do up a nice tool for this, with documentation. This tool works in 9.3 or 10.x, and has validation associated with it. It has an output status variable (0,1,2) you can use for a precondition or a model variable.

I know you may hate having to code, but the Python script interface is pretty nice.

(Thanks, shitijmehta, for the idea.)

`Usage: ModelMessage <Message_text> {NONE | WARNING | ERROR} {0 | 1 | 2} `

Here's how it looks when you run it in a model in the geoprocessing message stream:

```Executing: MessageModel
Start Time: Fri Jul 01 15:41:02 2011
Executing (Message): ModelMessage "\nHello, world!\n" NONE 0 0
Start Time: Fri Jul 01 15:41:03 2011
Running script ModelMessage...

Hello, world!

Completed script ModelMessage...
Executed (Message) successfully.
End Time: Fri Jul 01 15:41:03 2011 (Elapsed Time: 0.00 seconds)
Executed (MessageModel) successfully.
End Time: Fri Jul 01 15:41:03 2011 (Elapsed Time: 1.00 seconds)
```
MVP Esteemed Contributor
The ability to create script tools ("embed them in a toolbox") has been around since 9.0.  If you're on 9.3, you can start with this help topic : An overview of creating script tools