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)
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)
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
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!
#
# 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)