Python Add-In Edit a Feature Class

3366
2
04-02-2013 09:56 AM
CaseyBentz
Occasional Contributor II
I am working on my first Python Add-In and am having a couple issues.  What I am trying to do is create a tool that will allow the user to click somewhere on the map and take the map coordinates to create a circle with a predefined radius, fill out some default values, insert it into a SDE feature class, then select the feature to display it's attributes in the attribute editor.  This all works pretty well.

The problem is related to editing.  I would like for the tool to be disabled if the feature class is not editable or if the layer is not in the current map document.  I am also struggling with the edit state.  If I start editing and run my tool I get this message: The requested operation is invalid on a closed state.  If I make at least one edit, I can run the tool just fine. 

def onMouseDownMap(self, x, y, button, shift):
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
        fc = ''


        for lyr in arcpy.mapping.ListLayers(mxd,'*', df):
            if lyr.supports("dataSource"):
                lyrSource = lyr.dataSource.split("\\")
                if lyrSource[3] == 'zodiac.GIS.IPCoROW':
                    fc = lyr.dataSource
                    rowLayer = lyr
                    break
        # set the radius of the circle
        radius = 20
        # set the number of sides/vertices the circle will have
        side_count = 40
        # create the degrees for the circle
        degrees = 360 / side_count
        # create a list to hold each vertice point for the circle
        cir_poly = []
        # loop through the number of sides
        for i in range(side_count):
            # set the angle for the current vertice
            angle = (degrees * i) * (math.pi / 180)
            # do some math to set the xFactor and the yFactor
            xFactor = math.sin(angle)
            yFactor = math.cos(angle)
            # set the x and y vertices
            xVal = radius * xFactor + x
            yVal = radius * yFactor + y
            # create a arcpy point
            pnt = arcpy.Point(xVal,yVal)
            # add the point to the list
            cir_poly.append(pnt)
        # create an array and add the list to it
        array = arcpy.Array([cir_poly])
        # create a polygon from the array of points
        polygon = arcpy.Polygon(array)
        
        # set the path to the IPCoROW layer
        #fc = r"Database Connections\Connection to landbaseProd.sde\zodiac.GIS.Land\zodiac.GIS.IPCoROW"
        if len(fc) > 0:
            
            try:
                # create the insert cursor to be used to add our new circle
                print 'Create Insert Cursor'
                with arcpy.da.InsertCursor(fc, ["SHAPE@","OwnerType","ConvType","SOURCE","Comments"]) as c:
                    # insert the circle with attributes into the IPCoROW layer
                    print 'Insert Row'
                    i = c.insertRow([polygon, 'ROW','Easement','DIGITIZED','INSUFFICIENT DATA'])
                    print 'OID =',i
                    
                # refresh the active view to show the new feature
                arcpy.RefreshActiveView()
                print 'Select Features'
                arcpy.SelectLayerByAttribute_management (rowLayer, "NEW_SELECTION", "OBJECTID = " + str(i))


            except Exception, e:
                # if there was an error, print it here
                # common errors include:
                # attempting to edit outside an edit session
                # attempting to edit prior to making any other edits
                print 'Error -', e
            finally:
                pass
        else:
            print 'Could not find zodiac.GIS.IPCoROW'
Tags (2)
0 Kudos
2 Replies
JoelCalhoun
New Contributor III
I am also struggling with the edit state. If I start editing and run my tool I get this message: The requested operation is invalid on a closed state. If I make at least one edit, I can run the tool just fine.


I have been experiencing this problem too.  I tried starting an edit session with Python first but that didn't seem to work either.
I am very interested in a solution to this problem.
0 Kudos
RichardJarvis
New Contributor II
I'm having trouble finding where you've initiated the edit operation.  Have you checked out http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000005000000

I have had similar issues before until I implemented arcpy.da.editor...

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

# <your edits>

edit.stopOperation()


You can also use the with structure...

# 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


Good luck,
Richard
0 Kudos