Select to view content in your preferred language

Message script tool for ModelBuilder

4588
4
10-18-2012 10:16 AM
curtvprice
MVP Esteemed Contributor

Today I got a message script tool working along the lines of the one discussed in the ArcGIS Blog for use in generating messages inside ModelBuilder.

First, an example of how it works, set up as a script tool with two text arguments, the first "MESSAGE","ERROR","WARNING", the second your message. What's  new about this one over the one linked from the blog is that with this version, you can do error codes by using the format: "id arg1,arg2". (Sure would be nice if a tool was provided that does this in ModelBuilder.)

Updated for ArcGIS 10x / Pro - currently testing


Executing: Message WARNING "id 591 First,Last"
Start Time: Thu Oct 18 11:58:10 2012
Running script Message...
WARNING 000591: First parameter not Last.
Completed script Message...
Succeeded at Thu Oct 18 11:58:10 2012 (Elapsed Time: 0.00 seconds)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

#
# GP Script tool - Message
# for use in ModelBuilder

import arcpy

# Two text parameters:
# 0) Message type: ERROR, INFORMATIVE, WARNING
# 1) Message text
msgType = arcpy.GetParameterAsText(0).upper()
msgText = arcpy.GetParameterAsText(1)

try:
    # id message, format: "id 345 arg1,arg2"
    if msgText[:2].upper() != "ID":
      raise Exception
    else:
        msgList = msgText[3:].split()
        msgID = int(msgList[0])
        # pick up arguments, comma-separated
        msgArgs = " ".join(msgList[1:]).split(",")
        msgArgs = [msgType, msgID] + msgArgs
        arcpy.AddIDMessage(*msgArgs)
except:
    # text messages (no ID message)
    if msgType == "WARNING":
        arcpy.AddWarning(msgText)
    elif msgType == "ERROR":
        arcpy.AddError(msgText)
    else:
        arcpy.AddMessage(msgText)
arcpy.SetParameterAsText(2,True)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
4 Replies
ShitijMehta
Esri Regular Contributor
Thanks for improving it Curt!!!
I bet it will help a lot of people looking for a way to write a custom error, warning or message.


Point under consideration - about the need for a tool in ModelBuilder to do that.
0 Kudos
LindsayRaabe_FPCWA
Honored Contributor

Handy tool! I've just customised to make a simpler version for my own use. Others may find handy. 

#
# GP Script tool - Message
# for use in ModelBuilder

import arcpy

# Two text parameters:
# 0) Message type: ERROR, WARNING, INFORMATIVE
# 1) Message text

msgType = arcpy.GetParameterAsText(0).upper()
msgText = arcpy.GetParameterAsText(1)

# Return msgText and format based on msgType parameter
if msgType.upper() == "ERROR":
  arcpy.AddError(msgText)
elif msgType.upper() == "WARNING":
    arcpy.AddWarning(msgText)
else:
    arcpy.AddMessage(msgText)

LindsayRaabe_FPCWA_0-1704420167908.pngLindsayRaabe_FPCWA_1-1704420198396.png

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
LyonMNGIS
Frequent Contributor

Hello,

I could not find the original blog and I am having trouble following this post.  It looks like this script combines python and model builder to provide feedback to the user.  Does anyone have a basic sample script that illustrates how to use these tools?   I have a model that tests if a layer has only one feature and if a particular value is greater than zero.  It would be great if the model reported to the user errors such as "This model requires a single feature." or "Please populate recorded acres."  I just do not know how to do that yet.

Thanks

0 Kudos
LindsayRaabe_FPCWA
Honored Contributor

The below GIF runs you through the tool creation and use, but if you would prefer to see it in full resolution and without so much cutting, watch it here on YouTube. The code used in the video is posted below. Even better - sample toolbox attached!

Custom Message Tool2.gif

 

#
# GP Script tool - Message
# for use in ModelBuilder

import arcpy

# Two text parameters:
# 0) Message type: ERROR, WARNING, INFORMATIVE
# 1) Message text

msgType = arcpy.GetParameterAsText(0).upper()
msgText = arcpy.GetParameterAsText(1)

# Return msgText and format based on msgType parameter
if msgType.upper() == "ERROR":
  arcpy.AddError(msgText)
elif msgType.upper() == "WARNING":
    arcpy.AddWarning(msgText)
else:
    arcpy.AddMessage(msgText)

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA