Struggling with acpy undoOperation

2178
1
11-15-2013 01:34 PM
FredKellner1
New Contributor
I have developed several toolbars but after some use I decided to try and add undo button to my tool bars. However, I am struggling to implement the arcpy undoOperation function in  just the Arc Python window, let alone one of my toolbars.

I have been trying

workspace='E:\CLWNEZ\LFM_CLWNEZ\M6004\m6004_Classification_Base.gdb'
layer='m6004_Classification_Base'
edit = arcpy.da.Editor(workspace)
edit.startEditing(True, False)
edit.startOperation()
with arcpy.da.Editor(workspace) as edit:
 arcpy.CalculateField_management(layer, "LFM", 5000, "PYTHON")
edit.undoOperation()


But I keep getting this error message when I run this in the Python window.
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'Workspace Operation object' object has no attribute 'undoOperation'


I'm sure I'm doing something incorrect but not sure what yet.

Fred
Tags (2)
0 Kudos
1 Reply
T__WayneWhitley
Frequent Contributor
Just a hunch, I think the problem is you've lost the original 'edit' reference with the implicit 'edit' set with your statement:

with arcpy.da.Editor(workspace) as edit

In other words, I think your statements previous to this block were 'forgotten' (because they're outside the 'scope' of your 'with' block):

edit = arcpy.da.Editor(workspace)
edit.startEditing(True, False)
edit.startOperation()

So, edit.startOperation apparently did not throw an error but the later edit.UndoOperation did.

Notice the sample code structure working with a file gdb (as in your code) given by the webhelp under the subtopic "Edit sessions and with statements" does not use the coupled start/stop statements:
# The below code structure for edit session with statements from
# Editor (arcpy.da)

import arcpy

# Open an edit session and start an edit operation
with arcpy.da.Editor(workspace) as edit:
    # <your edits>

    # If an exception is raised, the operation will be aborted, and 
    #   edit session is closed without saving

    # If no exceptions are raised, stop the operation and save 
    #   and close the edit session


...looking further down to the samples on that same webpage, a try/except block is used in an example to take advantage of any potentially returned ExecuteError exception so that arcpy 'handles' the edit session by aborting and closing, without any further code:
import arcpy

fc = 'C:/Portland/Portland.gdb/Land/Parks'
workspace = 'C:/Portland/Portland.gdb'
layer_name = 'Parks'

try:
    arcpy.MakeFeatureLayer_management(fc, layer_name)
    arcpy.SelectLayerByAttribute_management(
        layer_name, 'NEW_SELECTION',
        """CUSTODIAN = 'City of Portland'""")
    with arcpy.da.Editor(workspace) as edit:
        arcpy.CalculateField_management(
            layer_name, 'Usage', '"PUBLIC"', 'PYTHON')

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))


I think (you'll need to test), you can as needed raise your own error, and likewise raise a specific exception of your own to trigger the same abort and close... that's my hunch.  Working within SDE and versions, I believe I'd implement my own start/stop/abort/etc controls, but for a file gdb (as in your case), it should suffice to use an exception.

Hope that helps,
Wayne
0 Kudos