Can print statements raised from a custom exception be displayed in the Geoprocessing Results window?
I modified a script that I found that takes two inputs from a user, finds the associated feature, then zooms to the feature. The tool works fine. I am trying to make it more user-friendly by automatically formatting the inputs to have the correct amount of characters. I added a messagebox that tells the user if they have too many characters in the input box, but I used easygui to create that (a Python library for GUIs). Instead, I would like to use the gp results window to display the message. I thought that I formatted the raise and except statements correctly but the tool runs and I get a completed result and not my error message. I tried the arcpy help from esri and did not get the results I was looking for.
class BadInputError(Exception):
pass
# Import arcpy module
import arcpy, sys, string
from arcpy import env
try:
def checkInput(inp):
mNCount = len(inp)
trigger = 0
while trigger == 0:
if mNCount == 4:
trigger = 1
elif mNCount > 4:
inp = ''
trigger = -1
raise BadInputError
elif mNCount == 3:
inp = '0' + inp
trigger = 1
elif mNCount == 2:
inp = '00' + inp
trigger = 1
elif mNCount == 1:
inp = '000' + inp
trigger = 1
return inp
# Script arguments
MapNumber = arcpy.GetParameterAsText(0)
MapNumber = checkInput(MapNumber)
ParcelNumber = arcpy.GetParameterAsText(1)
ParcelNumber = checkInput(ParcelNumber)
Expression = (""" "MAP" = '%s' AND "PARCEL" = '%s'""") %(MapNumber,ParcelNumber)
# Local variables:
GISData_GISDATA_CAD_PARCEL2009_WHOLE = "Database Connections\\GISUser.sde\\GISData.GISDATA.CAD_PARCEL2009_WHOLE"
Output_Layer_Name = Expression
Output_Layer = "ParcelSelection"
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management("2009 Parcel Points", "NEW_SELECTION", Expression)
# Zoom to Selected Features
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()
df.scale = 8000
arcpy.RefreshActiveView()
except BadInputError:
print "Too many characters. Run again"
except Exception, e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print "Line %i" % tb.tb_lineno
print e.message
Best Answer
I think you're looking for arcpy.AddMessage.