Arcmap 脚本不会在 arcmap 控制台中打印消息

Arcmap script will not print messages in arcmap console

我有一个我为 Arcmap 编写的 Python 脚本。我正在尝试创建一个工具,将工作区内的所有特征 classes 重新投影到指定的特征 class。

我遇到的问题是我无法让 Arcmap 打印 "completed" 消息。当我对变量进行硬编码并将其 运行 作为脚本时,我想要显示的消息将打印出来,但它们不会在 Arcmap 中打印出来。您可以在下面的代码中看到我想要打印特定的打印消息,但它们不会出现。

代码:

#Import modules
import arcpy, os

#Set workspace directory
from arcpy import env

#Define workspace
inWorkspace = arcpy.GetParameterAsText(0)
env.workspace = inWorkspace
env.overwriteOutput = True

try:
    #Define local feature class to reproject to:
    targetFeature = arcpy.GetParameterAsText(1)

    #Describe the input feature class
    inFc = arcpy.Describe(targetFeature)
    sRef = inFc.spatialReference

    #Describe input feature class
    fcList = arcpy.ListFeatureClasses()

    #Loop to re-define the feature classes and print the messages:
    for fc in fcList:
        desc = arcpy.Describe(fc)
        if desc.spatialReference.name != sRef.name:
            print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:\n"
            newFc = arcpy.Project_management(fc, "projected_" + fc, sRef)
            newFeat = arcpy.Describe(newFc)
            count = arcpy.GetMessageCount()
            print "The reprojection of " + str(newFeat.baseName) + " " + arcpy.GetMessage(count-1) + "\n"

    #Find out which feature classes have been reprojected
    outFc = arcpy.ListFeatureClasses("projected_*")

    #Print a custom messagae describing which feature classes were reprojected
    for fc in outFc:
        desc = arcpy.Describe(fc)
        name = desc.name
        name = name[:name.find(".")]
    name = name.split("_")
    name = name[1] + "_" + name[0]
    print "The new file that has been reprojected is named " + name + "\n"

except arcpy.ExecuteError:
    pass

severity = arcpy.GetMaxSeverity()

if severity == 2:
    print "Error occurred:\n{0}".format(arcpy.GetMessage(2))
elif severity == 1:
    print "Warning raised:\n{1}".format(arcpy.GetMessage(1))
else:
    print "Script complete"

当我将脚本上传到 Arcmap 工具箱时,不会打印以下行(来自上面的代码):

print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:\n"

print "The reprojection of " + str(newFeat.baseName) + " " + arcpy.GetMessage(count-1) + "\n"

print "The new file that has been reprojected is named " + name + "\n"

我该如何解决这个问题?

print 仅在您的脚本在 Python 解释器中 运行 时打印消息。为了在 ArcGIS Toolbox 中脚本为 运行 时打印日志,您需要使用 arcpy.AddMessage()

arcpy.AddMessage("Projection of {0} is {1}, so re-defining projection now: ".format(str(fc), desc.spatialReference.name)