findAndReplaceWorkspacePaths doesn't work w/multiple data frames in mxd??

1210
3
08-30-2012 01:05 PM
ChrisStump
New Contributor II
Hello all,

Got stuck trying to fix broken links in a series of mxds.  I found if a map has more than one data frame, the findandreplaceworkspacepaths method is not working, or at least the mxd.save() call isn't saving the changes, even though the code runs without error.  Even if I ran code similar below through the arcpy window using the 'current' map object call, and clicking the save icon in ArcMap or performing File->Save from the File menu in ArcMap does not save any changes.  Once I removed all but one data frame from an mxd, the code and all save/save as operations worked fine.  Is this a bug, or am I doing something wrong?  Thanks ahead of time.  I'm using ArcGIS v10sp4, and am just trying to change shapefile workspace paths in an mxd. 

Here is a quick code sample: 
import arcpy
mxd = arcpy.mapping.MapDocument(r'\\filelocation\map.mxd')
mxd.findAndReplaceWorkspacePaths(r'\\filelocation1', r'\\filelocation2')
arcpy.RefreshTOC
mxd.save()
del mxd
Tags (2)
0 Kudos
3 Replies
ChristopherThompson
Occasional Contributor III
Could you use arcpy.mapping.ListDataframes and iterate through the list that returns to get layers for each DF and run your findandreplace code against those?
0 Kudos
ChrisStump
New Contributor II
Thanks for the reply.  That's actually how I initially tried to perform this, but can't get the map object to save.  The function I have is this to return the list of layers in the data frame:

def list_layers_mxd(self, mxd_path, data_frame = "", layers = ""):
mxd = arcpy.mapping.MapDocument(mxd_path)
df = arcpy.mapping.ListDataFrames(mxd, data_frame)[0]
layers = arcpy.mapping.ListLayers(mxd, layers, df)
layers.sort()
del mxd
del df
return layers

so I reference that function in:

def findreplace_paths_mxd(self, mxd_path, new_path):
layers = deq_gis().list_laerys_mxd(mxd_path)
mxd = arcpy.mapping.MapDocument(mxd_path)
for lyr in layers[0]:
old_source = lyr.workspacePath
mxd.findAndReplaceWorkspacePaths(old_source, new_path, 'False')
arcpy.RefreshActiveView()
arcpy.RefrechTOC()
mxd.save()
del mxd

in my mind this should save the changes made to the .mxd, but when I open the saved .mxd nothing has changed.  Sometimes it works if referenced in the arcpy window in arcmap, using the 'current' reference.  I removed all but one data frame, which helps if run in the desktop but still doesn't do anything in standalone code.  Also, saveacopy() creates a new copy but doesn't save the workspace path change.  Can't figure out if I'm doing something wrong or if there's a bug in the arcpy class library.
0 Kudos
JeffBarrette
Esri Regular Contributor
I can not reproduce this problem.  Our internal tests include MXDs with many DFs.  I'm having a difficult time reading your sample code - I see several possible issues with it.  Try replacing your data sources with a simple script that does not include functions (not that that is the problem).

See the samples in the following topic:
http://resources.arcgis.com/en/help/main/10.1/#/Updating_and_fixing_data_sources_with_arcpy_mapping/...

Jeff
0 Kudos