2

The engineers I work with have requested an autosave feature for ArcGIS, so I thought I'd make them a python tool that routinely saves. The script works fine when run in the foreground, but fails when "Always run in foreground" in not checked.

enter image description here

The returned messages:

enter image description here

Is there a workaround for this issue?

The code:

"""Autosave"""
import arcpy
import time
import os
arcpy.env.overwriteOutput = True
autosaveFolder = arcpy.GetParameterAsText (0)
autosaveFile = arcpy.GetParameterAsText (1)
waitTime = arcpy.GetParameter (2)
if not autosaveFile.lower().endswith (".mxd"):
 autosaveFile = autosaveFile + ".mxd"
waitSeconds = int (waitTime * 60)
autosaveMxd = os.path.join (autosaveFolder, autosaveFile)
while True:
 time.sleep (waitSeconds)
 mxd = arcpy.mapping.MapDocument("CURRENT")
 mxd.saveACopy (autosaveMxd)
 del mxd
asked Aug 27, 2015 at 23:01
3
  • Arc is still not thread safe. I have a timer save in C# but I cheated a bit.. the timer runs in background and pops up a box that prompts to save. You don't want to save blindly as the current sketch is discarded on save and the undo buffer is flushed... it really should be up to the user if it's a good time to save or not. Would you like to see the C# code? Commented Aug 27, 2015 at 23:15
  • I'd be a bit surprised if the engineers knew how to undo :) "the current sketch is discarded on save" I'm not sure what this means. Can you explain? I also wouldn't know how to implement the C# code, nor is it quite what the engineers are after, so it probably wouldn't do me much good. Thanks though. Commented Aug 27, 2015 at 23:39
  • If you are currently editing or adding a feature it's not there until you press F2 to finish the sketch... should you hit Save or Save Edits while there is a sketch active the sketch is discarded (which is kind of disheartening if you've spent the last half hour plotting a lake boundary and it vanishes). Try launching a message box with the text "Time to save" - as a reminder; this might be the best you'll get with python. Commented Aug 27, 2015 at 23:43

3 Answers 3

5

My understanding is that Background Geoprocessing runs as a separate process and, unlike Foreground Geoprocessing, is unaware of your current ArcMap environment.

Consequently, I don't think you'll have success with your current approach.

AutoSaving maps is not the same as AutoSaving edits but you could look at Incremental "auto save" in ArcMap edit session and add your voice to the ArcGIS Idea(s) for this functionality.

Rather than a Python Toolbox the place that I would look to try and implement this would be as a Python AddIn Extension, but I have not tried using an extension to do this so an event to trigger your code may or may not be available.

You may also want to look at ArcGIS Pro because that has Autosave on edits, but I am not sure about AutoSaving projects/maps/etc.

answered Aug 27, 2015 at 23:18
4
  • I use a delegate to pick up the event on the main thread, I'm not sure how you can do that in python - a little too advanced for me. ArcGis Pro sounds like it might be worth looking at; does it use the same licensing as ArcGis desktop? Commented Aug 27, 2015 at 23:40
  • @MichaelMiles-Stimson It uses Named Users but but my understanding is that basic principle is every ArcGIS for Desktop user on maintenance has entitlement to use ArcGIS Pro and/or ArcMap. Commented Aug 28, 2015 at 0:11
  • Technically speaking, it's a separate process, not a separate thread. Commented Aug 28, 2015 at 0:23
  • I'll trust your terminology rather than mine @KHibma so I've edited my question. Commented Aug 28, 2015 at 0:26
4

When you are running a script in the background, a python process is spawned that is separate from ArcMap. So essentially, arcpy.mapping.MapDocument("CURRENT") is attempting to open a map document that it cannot see. This makes sense as imagine the issues if you had multiple .mxd open: Python would not know which one you mean by "CURRENT".

You can see this yourself by firing up python (not from ArcMap) and executing the above.

Traceback (most recent call last):
 File "<pyshell#1>", line 1, in <module>
 arcpy.mapping.MapDocument("CURRENT")
 File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects\mixins.py", line 610, in __init__
 super(MapDocumentMethods, self).__init__(mxd)
 File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects\_base.py", line 47, in __init__
 for arg in args))
RuntimeError: Object: CreateObject cannot open map document
answered Aug 27, 2015 at 23:15
2

You could try using XTools Pro, which has an autosave MXD function built in. This removes the need for a custom tool and eliminates the foreground/background issue, while also allowing the user to set their own auto-save interval.

answered Aug 28, 2015 at 4:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.