Select to view content in your preferred language

Scripting that is Event Driven

1333
8
06-15-2011 11:38 PM
RussellPilbeam
New Contributor
In previous versions of arcgis we had some scripting that operated within a template mxt file. When the mxt file was opened some fields on the layout view (author, file name (all now dynamic text)) changed to the current user. We had some VB scripting that worked previously but have had no luck in finding anything similar in python that works on arcgis 10. Not after a complete solution just a pointer in the right direction.

Thanks
Tags (2)
0 Kudos
8 Replies
AndrewChapkowski
Esri Regular Contributor
You can use the arcpy.mapping.MapDocument() to alter those properties. You cannot create an map document from nothing, so you need a blank map document file.  I named is blank.mxd.  You could do things like load base map data, set the relative path, etc... Then alter the properties of the map.  In my example below, I set the author to the current windows user by grabbing the environmental variable. 
Use os.system() to launch arcmap with newly created map document.

You can also create a .NET Event listener to alter your map document. 
See this help.


import arcpy
import os
blankPath = r"c:\temp\blank.mxd"
savePath = r"c:\temp\test.mxd"
mxd = arcpy.mapping.MapDocument(blankPath)
mxd.author = arcpy.GetSystemEnvironment("USERNAME")
mxd.saveACopy(savePath)
del mxd
os.system(savePath)
0 Kudos
MathewCoyle
Honored Contributor
I wrote a script that replicated some of that "lost" functionality with .mxts, I found it surprisingly easy to write/modify text based on what you want. I run it as a stand alone script to export PDFs that dynamically set the author/date/title as well as data populated by the selected area filled into predefined text elements.

I am working on creating a Tkinter GUI for it now to make it a little more user friendly, but this entire process could easily be loaded into a script tool in arc. The reason I didn't go that direction is that I find the overhead of having to open Arc, find and open an .mxd, zoom to where you want to go, then run a tool to fill in the text elements you want, too much when all you want to do is print or export a PDF of a map you've already set up.
0 Kudos
RussellPilbeam
New Contributor
Thank you for your responses.

I'm not sure I was clear in my explanation. The 'event' that drives the script is a process within arcmap. For example, a template mxd is opened and the user changes the view from data view to layout view. This action runs the script in the background creating entries into the text fields on the map layout. Effectively the key is automation, not for someone to have to click on a tool button to run the script.
0 Kudos
ChrisMathers
Deactivated User
Well depending on the content of the text fields you can use the dynamic text functions. Date, User name, data frame aspects, etc. are all variables you can plug into your text strings in an xml sort of way that auto update when you look at the layout.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/What_is_dynamic_text/00s90000000v00000...
0 Kudos
KevinBell
Deactivated User
Thank you for your responses.

I'm not sure I was clear in my explanation. The 'event' that drives the script is a process within arcmap. For example, a template mxd is opened and the user changes the view from data view to layout view. This action runs the script in the background creating entries into the text fields on the map layout. Effectively the key is automation, not for someone to have to click on a tool button to run the script.


You sound like you miss VBA already!  : )

I don't think it's possible to do what you want (yet)...  I would also like to see a high level module for allowing python to react to ArcMap user events, and being able to store the code with the mxd, just like the way the VBA worked.

Use case:  user zooms in to only have a single parcel in view, and an envelope w/ addresses is kicked out of the printer...
0 Kudos
ChrisMathers
Deactivated User
Well you can do the printer thing with python now. You can even make it a button so that you dont even have to open a toolbox. Just zoom in and click on a toolbar.
0 Kudos
LoganPugh
Frequent Contributor
The only "events" (technically more like event handlers) currently implemented in Python are those in the ToolValidator class on script tools. For everything else you need to use C++, .NET or Java to write add-ins or class libraries to respond to ArcGIS application events. It may be possible to use Python and comtypes or ctypes to respond to events but I have not seen any such implementations.
0 Kudos
JasonScheirer
Esri Alum
This video gives some hints on Python Add-Ins, which will give access to a set of common document events.
0 Kudos