PauseDrawing

Resumen

The PauseDrawing class pauses the drawing of the map view while using it within a context manager (with statement).

Debate

The PauseDrawing class is a context manager that pauses drawing of the map view. Use this class to pause drawing of the map view while updates are applied to a layer or while running geoprocessing tools. The output of geoprocessing tools will be added to the Contents pane but will not be drawn in the map view until the context manager exits. Even if the RefreshLayer function is called within the PauseDrawing context manager, the map view will not refresh until the with block is exited.

Sintaxis

PauseDrawing ()

Muestra de código

PauseDrawing example 1

Use the PauseDrawing class as a context manager while updates are applied with update cursors.

import arcpy
from random import randint

featureclass_1 = r"c:\data\myGDB.gdb\fc1"
featureclass_2 = r"c:\data\myGDB.gdb\fc2"

with arcpy.PauseDrawing():
    with arcpy.da.UpdateCursor(featureclass_1, "class_field") as cursor:
        for row in cursor:
            classification = randint(1, 5)
            row[0] = classification
            cursor.updateRow(row)

    with arcpy.da.UpdateCursor(featureclass_2, "class_field") as cursor:
        for row in cursor:
            classification = randint(1, 5)
            row[0] = classification
            cursor.updateRow(row)
    arcpy.RefreshLayer((featureclass_1, featureclass_2))

# The map view will refresh now that the PauseDrawing context manager has closed.
PauseDrawing example 2

Use the PauseDrawing class as a context manager while inserting new features and running a geoprocessing tool.

import arcpy
from random import randint

featureclass_1 = r"c:\data\myGDB.gdb\fc1"
featureclass_2 = r"c:\data\myGDB.gdb\fc2"

calculation_code_block = """
from random import randint
def f():
    return randint(1, 5)
"""

with arcpy.PauseDrawing():
    # Use cursors to copy geometries from one feature layer to another
    with arcpy.da.InsertCursor(featureclass_1, ["SHAPE@"]) as icur:
        with arcpy.da.SearchCursor(featureclass_2, "SHAPE@") as scur:
            for row in scur:
                icur.insertRow((row[0]))

    # Use CalculateField to update the class field
    arcpy.management.CalculateField(
        in_table=featureclass_1,
        field="class",
        expression="f()",
        expression_type="PYTHON3",
        code_block=calculation_code_block
        )

# The map view will refresh now that the PauseDrawing context manager has closed.