I am trying to find a way to execute a few lines of (preferably python-)code whenever I save a specific mxd (my template.mxd)
I'm working with arcgis desktop 10.3.1 (advanced, all extentions)
What I am trying to achieve is the following: I wrote a python script tool which enables the user to automatically export a lot of maps in pdf format. In order to do that, the user can choose certain basemaps from a value list. This list is generated in the validations of the script tool when the tool starts:
def updateParameters(self): mxd = arcpy.mapping.MapDocument(mxd_path) df = arcpy.mapping.ListDataFrames(mxd, "Detailframe",) basemapList = arcpy.mapping.ListLayers(mxd,"basemap*",df) . . .
So now the user can choose each basemap which is integrated in the template mxd. But this slows up the tool startup (~20-30 seconds). Which bothers me.
Info: In the validations (def updateParameters) I made sure, that this list-generation runs only once instead of "whenever a parameter has been changed".
my idea is something like this:
The user alters the template-mxd by i.e. adding some new basemap layers and saves the mxd. At this point a second tool is triggered and lists all the layers with the suffix "basemap" and finally writes them to a simple textfile. This text-file should be much quicker to access in the python script tool. So if the user opens the python script tool it reads out the previously generated textfile and gets all the latest layersnames from there. (the layernames is all I need...so a string or a list is fine)
I managed to do this in the label expressions in python code. But then the code of the second script runs every time when the user pans the map which massively slows up the template-mxd.
def FindLabel ( [text] ): mxd = arcpy.mapping.MapDocument(mxd_path) df = arcpy.mapping.ListDataFrames(mxd, "Detailframe",) basemapList = arcpy.mapping.ListLayers(mxd,"basemap*",df) log = "c:/log/basemaptextfile.txt" txt = open(log, "w") for x in basemapList: txt.write('%s \n' % (x)) txt.close() return
I read about arcgis addins which have similair functions which are called i.e. when a mxd is opened --> def openDocument(self): How could I achieve that some code runs when the mxd is saved?
Of course this would ONCE slow down the template mxd...at the time, when the saving is done. But this would be much better than having to wait 30 seconds each time my tool starts.
Any ideas how to approach? I am not familiar with arcobjects, but I would give it a try if it isn't too complicated.
UPDATE: the final construct shall have 4 components:
1) a template mxd which containes basemaps with the suffix "basemap". The user shall be able to alter this document by adding ne basemap layers or delete existing
2) python script tool which functions as a gui and reads out all the layers which have the suffix "basemap" and puts them into a value list, so the user can select which basemaps shall be exported to pdf. At this time this is achieved by listing the layers in the template mxd, but this takes too long. In the future this should be done by reading the layers from a txt.
3) a second code (maybe arcgis addin) which lists all the layers and writes them as a list or string into a simple txt
4) the python standalone .py which takes all these basemaps and creates pdf's from it(this works pretty well so far)
so what needs to be done is to find a solution for detecting if the user adds, removes or renames basemap layers in the template mxd and writing them to a txt. I thought this could be done by detecting if the template mxd was saved, because the user is supposed to save the template mxd before he runs the python script tool. But now I'm thinking this can maybe done with some functions from the arcgis addins. please feel free to suggest different approaches...