Select to view content in your preferred language

Arcpy Edit tool

547
1
07-10-2019 07:19 AM
gopi_chandangaluri
Emerging Contributor

Hi ,

help 

How can we start the arcmap  Edit Session and also activate  the  Edit Tool using Arcpy , programmatically

Requirement :   I have created 2 python addins

                          1. Line tool 2. Button

                         using the line tool i can draw a line feature but after finish the Line drawing need to select an annotation                          text  in  the  map , for this i need to use Edit Tool automatically to select the Annotation . Also                              sample D:\Sample.gdb is created for workspace and CrossLine ( Line featureclass is created in the  gdb) .

 Please make the  corrections in the below code.

 

import arcpy
import pythonaddins
from arcpy import edit as EDIT
class Btn(object):
"""Implementation for Python_Snaptool_addin.button (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
pass
class Edit_Operations(object):
"""Implementation for Python_Snaptool_addin.extension20 (Extension)"""
def __init__(self):
# For performance considerations, please remove all unused methods in this class.
self.enabled = True
#edit = arcpy.da.Editor(r'D:\Sample.gdb')
#self.onStartEditing()
def startup(self):
pass
def onStartEditing(self):
pythonaddins.MessageBox('Select a data frame', 'INFO', 0)
pass
def onStopEditing(self, save_changes):
pass
def onStartOperation(self):
pass
def beforeStopOperation(self):
pass
def onStopOperation(self):
pass
def onSaveEdits(self):
pass
def onCreateFeature(self):
pass
class LineClass(object):
"""Implementation for Python_Snaptool_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "Line" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.

def onMouseDown(self, x, y, button, shift):
pass
def onMouseDownMap(self, x, y, button, shift):
pass
def onMouseUp(self, x, y, button, shift):
pass
def onMouseUpMap(self, x, y, button, shift):
pass
def onMouseMove(self, x, y, button, shift):
pass
def onMouseMoveMap(self, x, y, button, shift):
pass
def onDblClick(self):
pass
def onKeyDown(self, keycode, shift):
pass
def onKeyUp(self, keycode, shift):
pass
def deactivate(self):
pass
def onCircle(self, circle_geometry):
pass
def onLine(self, line_geometry):
global mid_point
edit = arcpy.da.Editor(r'D:\Sample.gdb')

array = arcpy.Array([arcpy.Point(line_geometry.firstPoint.X,line_geometry.firstPoint.Y),arcpy.Point(line_geometry.lastPoint.X,line_geometry.lastPoint.Y)]) 
polyline = arcpy.Polyline(array)
edit.startEditing()
edit.startOperation() 
cursor = arcpy.da.InsertCursor("CrossLine", ["SHAPE@"])
cursor.insertRow([polyline])
del cursor 
mid_point = arcpy.Point() 
mid_point.X= line_geometry.positionAlongLine(0.50,True).firstPoint.X
mid_point.Y= line_geometry.positionAlongLine(0.50,True).firstPoint.Y
edit.stopOperation()
edit.stopEditing(True) ## Stop the edit session with True to save the changes
arcpy.RefreshActiveView()
obj = Edit_Operations()
pass

def onRectangle(self, rectangle_geometry):
pass

Thakns

Gopi

0 Kudos
1 Reply
DanPatterson_Retired
MVP Emeritus

Gopi, could you format your code please.  As posted it won't work and there are no line numbers to reference

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos