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.
Joshua is this what you had in mind?
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)
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)
if msg == 'yes':
editor.startEditing(False, False)
pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
elif msg == 'no':
quit
Several comments:
Joshua, I am currently working on another tool where setting the user input equal to a variable is crucial. Is this the right way to do it in terms of setting the variable equal to something the user can do an input and then a conditional to check on what they input?
if editor.isEditing == True:
pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
elif editor.isEditing == False:
pythonaddins.MessageBox("", "", 4)
msg = pythonaddins.MessageBox("", "", 4)
if msg == 'Yes':
Before I get to your question, Line 04 will present the user with a message box, but it doesn't do anything because the return result is never stored. Line 05 is the important line.
I feel like the code you posted isn't directly related to the question you are asking. I get the impression your other tool isn't using just a message box to get user input. If there is another coding question you have, it might be good to start another thread and post the code directly related to your question.
The code is completely different but I wanted to know if the way i have msg = to the message box if that will read whatever the user input to use later in the script? In a sense I wanted to see if I am placing the variable in the right way
If an edit session doesn't exist, when exactly does the user see the error? Right away, or is it after the tool has ran for a while? What is the function or method call that is generating the error? What specifically is the error?
Hi Joshua, so the error pops up milliseconds to like 2 seconds after you start running the tool. The tool is a custom export tool that has the function updateCursor function, which updates the fields to a particular value rather than leaving the blank fields as null values. The error states that updateCursor can not be used outside of the edit session and the rest of the tool does not run. Hence why I want to create this check to see if edit session is active and if not, to provide a prompt for the user that enables the user to select 'Yes' or 'No' to allow the script to turn an edit session on and then run the tool.
iamurray provided some good links, I suggest you read over them.
Given the tools generates an error anywhere from "milliseconds to like 2 seconds," which is pretty quick (it isn't like it runs for tens of minutes and then errors out), I would go with a try except block. Python handles EAFP (easier to ask forgiveness than permission) much better than other languages, so generating exceptions and handling them isn't seen as a big deal. Unlike some of the code examples in the StackExchange thread that Ian referenced, you really don't want to trap all exceptions with a generic except clause. It is better to trap the specific exception relating to edit sessions because you want other errors to still propagate through to the user.
I wonder if instead of trying to check if an edit session is going on, just check if a LOCK file is present for that workspace the file you are hoping to edit is in for the file in question. A file GDB creates a LOCK file of the same name regardless of whether it is being editted within an ArcMap Edit Session or using an Editor Object(FeatureClassName.ed.lock). Its not perfect since obviously more than one person can be accessing the dataset and create a lock on it, but at least you can check if the data is being editted at all.
Just spitballing here.
I've never worked with exceptions before so I was wondering if you have an example of what it may look like and the formatting of the script so I may try that out.