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.
The returned messages:
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
-
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?Michael Stimson– Michael Stimson2015年08月27日 23:15:18 +00:00Commented 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.Emil Brundage– Emil Brundage2015年08月27日 23:39:53 +00:00Commented 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.Michael Stimson– Michael Stimson2015年08月27日 23:43:18 +00:00Commented Aug 27, 2015 at 23:43
3 Answers 3
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.
-
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?Michael Stimson– Michael Stimson2015年08月27日 23:40:02 +00:00Commented 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.2015年08月28日 00:11:31 +00:00Commented Aug 28, 2015 at 0:11
-
Technically speaking, it's a separate process, not a separate thread.KHibma– KHibma2015年08月28日 00:23:38 +00:00Commented Aug 28, 2015 at 0:23
-
I'll trust your terminology rather than mine @KHibma so I've edited my question.2015年08月28日 00:26:19 +00:00Commented Aug 28, 2015 at 0:26
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
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.
Explore related questions
See similar questions with these tags.