Answers:
我没有在ArcPy中看到任何原生内容。最简单的操作可能是使用SendKeys模块将F9击键发送到ArcMap窗口。
我使用此语法进行了测试,并且工作正常:
import SendKeys
# Send the keystroke for F9
SendKeys.SendKeys("{F9}")
唯一需要注意的是,您可能需要在脚本属性的“常规”选项卡上取消选中“始终在前台运行”。否则,脚本进度窗口可能会捕获按键。
我意识到这个问题很早以前就已经结束,但是我有一些旧工具,这是新出现的问题,SendKeys解决方案似乎不再起作用,因此我在试验后推出了自己的解决方案。它不会禁用绘图,而是通过禁用图层并在完成后重新启用它们来创建与之等效的性能。在后台运行脚本无法解决问题(尽管我认为可以解决),所以我尝试关闭所有层-并且可以正常工作!完全加速到空文档中的等效代码。因此,这里有一些代码可以实现这一点。
将这两个功能结合使用时,将关闭文档中的所有图层,并返回图层的保存状态。然后,当您完成操作后,可以通过将已保存的状态提供给第二个功能来重新打开它们。推荐用法:
try:
layer_state = turn_off_all_layers("CURRENT")
# Do interesting things here
finally: # put it in a finally block so that if your interesting code fails, your layers still get reenabled
turn_on_layers("CURRENT", layer_state)
下面是函数-更正,注释,欢迎使用-相当新的代码,因此可能会有一些错误,但是已经过一些测试。
def turn_off_all_layers(document="CURRENT"):
"""
A speedup function for map generation in ArcMap - turns off all layers so that it doesn't try to rerender them while we're using tools (since these tools need
to run in the foreground and background processesing didn't seem to speed it up).
Creates a dictionary keyed on the arcpy layer value longName which contains True or False values for whether or not the layers were enabled before running this.
Allows us to then use turn_on_layers on the same document to reenable those layers
:param document: a map document. defaults to "CURRENT"
:return: dict: a dictionary keyed on layer longName values with True or False values for whether the layer was enabled.
"""
visiblity = {}
doc = arcpy.mapping.MapDocument(document)
for lyr in arcpy.mapping.ListLayers(doc):
if lyr.visible is True:
try:
visiblity[lyr.longName] = True
lyr.visible = False
except NameError:
visiblity[lyr.longName] = False # if we have trouble setting it, then let's not mess with it later
else:
visiblity[lyr.longName] = False
return visiblity
def turn_on_layers(document="CURRENT", storage_dict=None, only_change_visible=True):
if not storage_dict:
raise ValueError("storage_dict must be defined and set to a list of layer names with values of False or True based on whether the layer should be on or off")
doc = arcpy.mapping.MapDocument(document)
for lyr in arcpy.mapping.ListLayers(doc):
if lyr.longName in storage_dict:
if not only_change_visible or (only_change_visible is True and storage_dict[lyr.longName] is True): # if we're only supposed to set the ones we want to make visible and it is one, or if we want to set all
try:
lyr.visible = storage_dict[lyr.longName] # set the visibility back to what we cached
except NameError:
arcpy.AddWarning("Couldn't turn layer %s back on - you may need to turn it on manually" % lyr.longName) # we couldn't turn a layer back on... too bad