2

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. I want to do it differently because I am trying to find a simplified way of doing such without having to rely on other scripts or downloading anything or importing outside scripts. I do not know what is in the entire scripts and do not need to corrupt my files and such.

So I currently sit here at this code in terms of what I am looking. This has not been tested but I thought to get my general idea out there. This code will go in the add-in script I have for other tools and the check to see if in edit session will be the first thing that happens when the user clicks to user this tool. One issue I am currently having after having done some research is not entirely sure as how to check to see which answer was provided in the message box. I use the mb_type of 4 for a yes/no message box but can use some help to check against the answer given. My code is the following:

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

OR THIS?

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 mb_type == 'yes':
            editor.startEditing(False, False)
            pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
        elif mb_type == 'no':
            quit

where mb_type is the third parameter in the pythonadd Message Box.

This was what I came down to where msg stores the string that is the output from the Message Box and then using the if statement after that to check against the value stored in the message box. But for some reason my message box is not popping up. And this is done in the add-in portion of the code and is the first thing that happens when the tool runs.

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

closed as unclear what you're asking by PolyGeo Jun 24 '17 at 4:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You say you're trying to do this without using the answers from the duplicate question. Please edit your question to outline why you need to do it another way - what doesn't work about those answers that makes this not a duplicate question? – Midavalo Mar 8 '17 at 20:44
  • Have you read the editor help? It sounds like you want the isEditing property. Mind you once you execute a tool theres no interaction with the user. Your only option checking inside the tool would be to throw a big red error message saying enable edit and run the tool again! – KHibma Mar 8 '17 at 21:20
  • KHibma I think isEditing is the right portion to use for me in terms of what I want. My plan was if editor was not on, to pop up a dialogue box that could be intractable with the user. Is that not possible? – Vishal Mar 8 '17 at 21:24
  • 1
    If you have a tool, no you cannot pop up a dialog box. Once you execute the tool, the tool will either finish successfully, error, or the user can cancel it. Theres no interactive nature of back and forth throughout tool execution. If you want that behavior, you should look at making an add-in. I think you can get all the nice back and forth prompting you want with an add-in – KHibma Mar 8 '17 at 21:32
  • @KHibma Are you talking about a "Python Add-In" or an arcobjects "Esri Add-In"? – Midavalo Mar 8 '17 at 21:47