Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm creating a python addin script to be used on some mxd's I have created as templates. Each map contains 2 dataframes. I need to loop through each dataframe and do some processing on each. I set my loop as follows:

for df in arcpy.mapping.ListDataFrames(mxd, "DFMAIN"):
                    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                           # do some processing here
for df in arcpy.mapping.ListDataFrames(mxd, "DFINSET"):
                    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                           # do some processing here

In this scenario, if the second dataframe is the 'active' dataframe, it gets chosen first and then some weird stuff happens. I get layers from my first dataframe copied into the second which baffles me and I am not even using any functionality that would copy layers (i.e. arcpy.mapping.AddLayer or anything similar) I just update some definition queries.

If I change the template so that the first dataframe is the active one, then everything works fine.

I tried indexing the dataframe loops:

for df in arcpy.mapping.ListDataFrames(mxd, "DFMAIN")[0]:

but I get the same weirdness if the second dataframe is active.

I guess the easy answer is, make sure the template has the first dataframe as the active one and save it that way, but I have users who may change this unknowingly. I want the geoprocessing to take care of the active dataframe issue. I can't seen anything in the help menu to set the dataframe as the active one.

share|improve this question
add comment

1 Answer

The bug with layers being added to your inset dataframe is very strange indeed. You may want to report it as a bug to ESRI.

One possible workaround is to set the map document's active data frame using the activeView property of the MapDocument object. Based on the documentation:

Provides the ability to set or get a map document's active view. This is either a single data frame or the page layout. The property works with a string that represents the active data frame name or the PAGE_LAYOUT keyword.

If activeView is set to PAGE_LAYOUT and the map document is saved, the next time the map document is opened, it will be opened in layout mode. If activeView is set to a data frame name and the map document is saved, the next time the map document is opened, it will be opened in data view, and that particular data frame will be the active data frame.

So you'd have to add this to your code beforehand:

#Sets active data frame and switches to data view
mxd.activeView = "DFMAIN"
mxd.save()

#Keeps active data frame, but switches back to layout view
mxd.activeView = "PAGE_LAYOUT"
mxd.save()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.