0

I've made an add-in in ArcPy, using add-in assistant, to update attributes of selected features. It has two buttons to populate either 'U' or 'D' as attribute.

The Python code is:

import arcpy
import pythonaddins


class D(object):
"""Implementation for Update_addin.button_1 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        sel = ""
        mxd = arcpy.mapping.MapDocument("CURRENT")
        layers = arcpy.mapping.ListLayers(mxd)
        for layer in layers:
            desc = arcpy.Describe(layer)
            if desc.fidSet:
                sel = layer.name
                break

        if sel != "":
            if arcpy.Describe (sel).FIDSet:
                with arcpy.da.UpdateCursor (sel, "Remark") as curs:
                    for row in curs:
                        row = ("D",)
                        curs.updateRow (row)
                    del curs
                    del row
            else:
                print("No selection")
            arcpy.RefreshActiveView()
        else:
            print("No selection")

class U(object):
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        sel = ""
        mxd = arcpy.mapping.MapDocument("CURRENT")
        layers = arcpy.mapping.ListLayers(mxd)
        for layer in layers:
            desc = arcpy.Describe(layer)
            if desc.fidSet:
                sel = layer.name
                break

        if sel != "":
            if arcpy.Describe (sel).FIDSet:
                with arcpy.da.UpdateCursor (sel, "Remark") as curs:
                    for row in curs:
                        row = ("U",)
                        curs.updateRow (row)
                    del curs
                    del row
            else:
                print("No selection")
            arcpy.RefreshActiveView()
        else:
            print("No selection")

This add-in works perfectly. The only problem is that whenever I press "Undo/Ctrl+Z" it undoes last 2-3 edits done by this button instead of only last edit. However, if I manually update something before pressing Undo, then this problem doesn't occur.

How do I rectify this problem?

I am using ArcMap version 10.3.

  • 1
    Maybe try deleting the cursor after it has run through the "for" statement (it's proper etiquette anyway so I would do it whether it solves the problem or not)? You could also try starting an editing session in the function before you do the row updates with the cursor and then closing the editing session when it is done looping through. I don't know what that would do if you manually open an editing session before using the add-in though. It might close the editing session every time you click the button to update records. Worth a shot though. – MKF Sep 16 at 19:49
  • @MKF no help!, it didn't work. I've edited the question to reflect the changes. – Keshav Sharma Oct 4 at 14:36
  • @MKF starting an editing session and closing it would greatly affect my time so I prefer this step to not use. – Keshav Sharma Oct 4 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.