我创建了一个Python加载项按钮,通过将一个要素类属性复制到另一个要素类来帮助加快我的同事的工作流程。它使用arcpy.UpdateCursor函数更新目标要素类中的一行。现在,无论编辑模式如何,都可以运行此按钮脚本。显然,当脚本在编辑会话中运行时,用户可以选择停止编辑而不保存更改,但是当脚本在编辑会话之外运行时,情况并非如此。
如果ArcMap当前不在编辑会话中,如何向脚本添加检查以停止脚本运行?
这关系到ArcMap 10和10.1
我还想与其他ArcMap用户进行核对,以验证在没有进行编辑会话的情况下,通常不允许对表进行更新。
那么该脚本如何在编辑会话之外运行?
该脚本还提出了另一个问题,即当我从列表更新第二要素类表时,ArcMap执行的看似偶然的选择顺序正好对我有用,但这又是一天了。
这是现在可以正常运行的脚本(没有任何10.1编辑器实现):
如何添加检查以确保用户处于编辑会话中?
def onClick(self):
#Reference mxd
mxd = arcpy.mapping.MapDocument("CURRENT")
#Reference the main Data frame
mm = arcpy.mapping.ListDataFrames(mxd, "MainMap")[0]
#Reference the Water System Valve feature class
waterValves = arcpy.mapping.ListLayers(mxd, "Water System Valve", mm)[0]
#Reference the fire hydrant feature class
fireHydrants = arcpy.mapping.ListLayers(mxd, "Water Hydrant", mm)[0]
#Use the extent of the main DF to select all valves in the current view
dfAsFeature = arcpy.Polygon(arcpy.Array([mm.extent.lowerLeft, mm.extent.lowerRight, mm.extent.upperRight, mm.extent.upperLeft]), mm.spatialReference)
arcpy.SelectLayerByLocation_management(waterValves, "WITHIN", dfAsFeature,"", "NEW_SELECTION")
arcpy.SelectLayerByAttribute_management(waterValves, "SUBSET_SELECTION", "LOCATIONID IS NULL")
fields = ["LOCATIONID"]
row, rows = None, None
rows = arcpy.UpdateCursor(waterValves,fields)
row = rows.next()
valveList = []
append = valveList.append
#Loop through the valves table to update LocationID
while row:
builder = str(row.QSNO)+"-"+ str(row.VALVESEQNO)
row.setValue("LOCATIONID", builder)
append(builder)
rows.updateRow(row)
row = rows.next()
del row, rows
#New selection for fire hydrants
arcpy.SelectLayerByLocation_management(fireHydrants, "WITHIN", dfAsFeature,"", "NEW_SELECTION")
arcpy.SelectLayerByAttribute_management(fireHydrants, "SUBSET_SELECTION", "LOCATIONID IS NULL")
row, rows = None, None
rows = arcpy.UpdateCursor(fireHydrants,fields)
row = rows.next()
#Loop through fire hydrant table to update LocationID
while row:
for locID in valveList:
construct = str(locID) + "-FH"
#print construct
row.setValue("LOCATIONID", construct)
rows.updateRow(row)
row = rows.next()
del row, rows, valveList, mxd