How to enable undo in ArcPro via Python window

171
0
04-16-2024 10:53 PM
EdX1
by
New Contributor III

Hello, 

I am updating a script which ran in ArcMap, to ArcPro.

This script used the following code (python 2):

 

 

		edit = arcpy.da.Editor(Workspace)
		#Retire the old assessments
		print "Retiring old assessment(s)..."
		arcpy.SelectLayerByAttribute_management(sTabName, "NEW_SELECTION", sOldWhere)
		edit.startEditing(False, True)
		edit.startOperation()
		with arcpy.da.UpdateCursor(sTabName, ("RecordStatus", "Retired")) as oRows:
			for oRow in oRows:
				oRow[0] = "Retired"
				sretired = "Title retired & replaced by {0}".format(",".join(aNewSIDs))
				if len(sretired) >250:
					oRow[1] = 'Title retired & replaced by Multiple parcels'
				else:
					oRow[1] = sretired
				oRows.updateRow(oRow)

		#Create new assessments
		print "Creating new assessments..."
		with arcpy.da.InsertCursor(sTabName, ('VOLUME','FOLIO','SERVICEID','ServiceType','Property30','WaterTreated','Pressure25','Flow20','Easement','SupplyZoneID','Comments','DateAssessed','Retired','RecordStatus')) as oRows:
			for sSid in aNewSIDs:
				print "  {0}".format(sSid)
				aParts = sSid.split("_")
				sVol = aParts[0]
				sFol = aParts[1]
				sCom = "Assessment generated from {0} due to cadastre change".format(",".join(aOldSIDs))
				today = datetime.datetime.now()
				sDate = today.strftime("%d/%m/%y")
				aNewRow = [sVol, sFol, sSid] + list(oOldAssRow) + [sCom, sDate, None, "Active"]
				oRows.insertRow(aNewRow)
			edit.stopOperation()
#		edit.stopEditing(True)

 

 

 
This script ran, updated some fields and added new fields to a table and as you can see on the last line the edit.stopEditing is commented out. The result of this script in ArcMap/Py2 was that the edits were held in memory but not committed/saved yet. The user would then save the edits or had the option to undo them.

I've updated my script to work in python 3 and ArcPro. But doing the same process of not having edit.StopEditing(true) causes some strange issues, the edits exist in the project, but there is no save option, or undo option. This is obviously a big issue.


My updated code for ArcPro/Py3 is this:

            edit = None  # Initialize edit variable
            edit = arcpy.da.Editor(SDE_Workspace_Path)
            edit.startEditing(True, True) 
            edit.startOperation()
            


            # Retire the old assessments
            print("Retiring old assessment(s)...")
            count = 0
            with arcpy.da.UpdateCursor(sTabName, ("RecordStatus", "Retired"), where_clause=sOldWhere) as oRows:
                for oRow in oRows:
                    oRow[0] = "Retired"
                    sretired = "Title retired & replaced by {0}".format(",".join(aNewSIDs))
                    if len(sretired) > 250:
                        oRow[1] = 'Title retired & replaced by Multiple parcels'
                    else:
                        oRow[1] = sretired
                    oRows.updateRow(oRow)
                count += 1
                

            # Creating new assessments in sTabName
            print("Creating new assessments...")
            with arcpy.da.InsertCursor(sTabName, ('VOLUME', 'FOLIO', 'SERVICEID', 'ServiceType', 'Property30', 'WaterTreated', 'Pressure25', 'Flow20', 'Easement', 'SupplyZoneID', 'Comments', 'DateAssessed', 'Retired', 'RecordStatus')) as oRows:
                for sSid in aNewSIDs:
                    print("  {0}".format(sSid))
                    aParts = sSid.split("_")
                    sVol = aParts[0]
                    sFol = aParts[1]
                    sCom = "Assessment generated from {0} due to cadastre change".format(",".join(aOldSIDs))
                    today = datetime.datetime.now()
                    sDate = today.strftime("%d/%m/%y")
                    aNewRow = [sVol, sFol, sSid] + list(oOldAssRow) + [sCom, sDate, None, "Active"]
                    oRows.insertRow(aNewRow)
            edit.stopOperation()
            edit.stopEditing(True)  # Save changes FALSE
        except Exception as e:
            edit.abortOperation()
            edit.stopEditing(False)  # Discard changes
            print(f"An error occurred, no changes were saved. Error: {e}")
        except arcpy.ExecuteError as epy:
            edit.abortOperation() 
            print(f"An error occurred, no changes were saved. ArcPy Error: {epy.args[0]}")


print("Finished...")

 
TL;DR The problem:
I have enabled undo via: edit.startEditing(True, True), yet after the script runs there is no option to undo the updated/new items. How can I enable this functionality? 


 
0 Kudos
0 Replies