How to check if in an Edit Session using python and arcpy

5444
23
03-08-2017 12:52 PM
VishalShah2
Occasional Contributor II

I am currently working on a tool that uses the updateCursor function but the tool needs to be in an edit session to use. However some who may use the tool may end up not being in an edit session before running the tool and get an error. Rather than come ask me every time about the error, I'd like to implement an if statement in the script in the beginning that checks if the user is currently in an edit session, if they are then run tool. if not, a dialogue box explaining they need to be in an edit session and ask if they want the script to turn editor on. Formatted as such:

#if edit session is on:
    #run tool
#elif edit session is off:
    #dialogue box explaining user has to be in edit session and asking if they want the script to turn editor on (Yes or no option):
        #if yes:
            #run tool
        #if no:
            #quit

I am trying to do this without using the work done by Mark Cederholm as referenced in this post: How do I access ArcObjects from Python?, and code examples provided by Matt Wilkie in his "Snippits.py" file.

0 Kudos
23 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Did you read up on the Editor in the help? It would be best to run your functionality from a tool in arctoolbox, to ensure or validate that you are in an edit session than relying on the user getting into an edit session manually

0 Kudos
VishalShah2
Occasional Contributor II

Dan, I was thinking along the lines of the format I have below. This way users wouldn't have to turn editor on manually but just have to simply click yes or no. Would this work?

if isEditing == True:
    run tool
elif isEditing == False:
    dialogue box pops up informing user to be in edit session and ask if they want script to turn it on for them:
        if yes:
            arcpy.startEditing()
        elif no:
            quit
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I would just bail .... totally untested of course.

# -*- coding: UTF-8 -*-
import sys
import os
import arcpy

#edit_me = sys.argv[1]
fc = r"C:\GIS\array_projects\data\Pro_base.gdb\Line"
workspace = os.path.dirname(fc)
edit = arcpy.da.Editor(workspace)
if edit.isEditing:
    print("you are in an edit session")
else:
    print("you aren't in an edit session")
    sys.exit
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Good idea, unfortunately it doesn't seem to work.  I just tested a file and enterprise geodatabase, and arcpy.da.Editor appears to only be aware of itself, i.e., isEditing() only returns True if the instantiated editor object starts an edit session.

IanMurray
Frequent Contributor

Yea we ran into this question a couple weeks ago where the Map Document and the arcpy Editor object aren't aware of each others editting status and their isn't a way to pythonicly check if an edit session is going on in the map document without using ArcObjects.  I pointed them to the below post but I'm not sure if they got anywhere with it or not.

https://community.esri.com/thread/190456-arcpydaeditor-setup-for-tool

http://gis.stackexchange.com/questions/61708/checking-via-arcpy-if-arcmap-is-in-edit-session

VishalShah2
Occasional Contributor II

So I believe doing it this way may actually work in a sense to get the Message Box using the python add-ins module. However, one issue I am having is doing the check to see which value from the message box the user selected. This is the first thing that happens when the user selects to use this tool. This is an add-in portion of the tool. Any help on figuring out how to call on the value in the Message Box will be helpful. The mb_type here is 4 so that the message box type is yes/no. Other mb_types and information about python add-ins can be found here. So my issue is figuring out how to see if the Message Box value the user selected was a yes or a no.

import arcpy
import pythonaddins
import os

toolPath = r'path to toolboox.tbx'
workspace = 'some workspace'
editor = arcpy.da.Editor(workspace)


if editor.isEditing == 'true':
    pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
elif editor.isEditing == 'false': 
    pythonaddins.MessageBox("You must be in an edit session to run the Custom Export Tool. If you wish to turn an edit session on, please click 'yes'. If you do not wish to turn an edit session on, please click 'no'.", "Turn Editor On?", 4)
        if MessageBox == 'yes':
            editor.startEditing(False, False)
            pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
        elif MessageBox == 'no':
            quit‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The message box function returns a string, so provide a variable for the return string to get stored in:

msg = pythonaddins.MessageBox("You must be in an edit session to run the Custom Export Tool. If you wish to turn an edit session on, please click 'yes'. If you do not wish to turn an edit session on, please click 'no'.", "Turn Editor On?", 4)


 Then check the value stored in the variable.

0 Kudos
VishalShah2
Occasional Contributor II

And would you define this variable at the beginning or where would you implement that variable in the code I have?

0 Kudos
VishalShah2
Occasional Contributor II

Hey Joshua

I just tried what you suggested but the Message Box did not pop up and so the value could not be stored. Any solutions on fixing this?

0 Kudos