I'm writing a python script tool that finds and replaces a layer in a set of MXDs in a folder. It creates a sub-direcory containing the new set of MXDs.The original MXDs are not changed. The user selects a layer file which is then used to style the new layer in the MXDs.
When the symbology_only parameter is set to True the script works fine but when it is set to False it gives an error.
`import arcpy, os
-- Parameters --
path = arcpy.GetParameterAsText(0)
dropLyr = arcpy.GetParameterAsText(1)
newLyrTxt = arcpy.GetParameterAsText(2)
newDirName = arcpy.GetParameterAsText(3)
styleLayerTxt = arcpy.GetParameterAsText(4)
-----------------
newLyr = arcpy.mapping.Layer(newLyrTxt)
styleLayer = arcpy.mapping.Layer(styleLayerTxt)
destPath = path+'\'+newDirName
try: os.makedirs(destPath) except OSError: if not os.path.isdir(destPath): raise
count1 = 0 mxdList = [f for f in os.listdir(path) if f.endswith('.mxd')]
loop through the MXDs in the user selected folder
for mxd in mxdList: pathMxd = path+'\'+mxd mapDoc = arcpy.mapping.MapDocument(pathMxd) count1 +=1 arcpy.AddMessage('MXD '+str(count1)+' of '+str(len(mxdList))+' -- '+mxd)
#loop through the data frames in each MXD numRepl = 0 for df in arcpy.mapping.ListDataFrames(mapDoc): lyrList = arcpy.mapping.ListLayers(mxd, "", df) #loop through the layers in each data frame to find the layer to remove #if the layer is found then insert the new layer, style it and then remove the old one for lyr in lyrList: if str(lyr) == dropLyr: numRepl +=1 arcpy.mapping.InsertLayer(df, lyr, newLyr ) #offending line below arcpy.mapping.UpdateLayer(df, newLyr, styleLayer, False) arcpy.mapping.RemoveLayer(df, lyr) #finally, save the mxd arcpy.AddMessage(' Replaced '+str(numRepl)+'\n') newMxd = destPath+'\\'+os.path.split(mxd)[1] mapDoc.saveACopy(newMxd)` Here is the error code: Traceback (most recent call last): File "C:\Users\rob\Documents\Coding Projects\MXD Mods\Replace_Layer.py", line 54, in arcpy.mapping.UpdateLayer(df, newLyr, styleLayer, False) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1876, in UpdateLayer tc.ReplaceLayer(tl, rl) ValueError: DataFrameObject: Unexpected error
Failed to execute (FindReplaceMXDLayer).
Can anyone see why this is please?
أكثر...
When the symbology_only parameter is set to True the script works fine but when it is set to False it gives an error.
`import arcpy, os
-- Parameters --
path = arcpy.GetParameterAsText(0)
dropLyr = arcpy.GetParameterAsText(1)
newLyrTxt = arcpy.GetParameterAsText(2)
newDirName = arcpy.GetParameterAsText(3)
styleLayerTxt = arcpy.GetParameterAsText(4)
-----------------
newLyr = arcpy.mapping.Layer(newLyrTxt)
styleLayer = arcpy.mapping.Layer(styleLayerTxt)
destPath = path+'\'+newDirName
try: os.makedirs(destPath) except OSError: if not os.path.isdir(destPath): raise
count1 = 0 mxdList = [f for f in os.listdir(path) if f.endswith('.mxd')]
loop through the MXDs in the user selected folder
for mxd in mxdList: pathMxd = path+'\'+mxd mapDoc = arcpy.mapping.MapDocument(pathMxd) count1 +=1 arcpy.AddMessage('MXD '+str(count1)+' of '+str(len(mxdList))+' -- '+mxd)
#loop through the data frames in each MXD numRepl = 0 for df in arcpy.mapping.ListDataFrames(mapDoc): lyrList = arcpy.mapping.ListLayers(mxd, "", df) #loop through the layers in each data frame to find the layer to remove #if the layer is found then insert the new layer, style it and then remove the old one for lyr in lyrList: if str(lyr) == dropLyr: numRepl +=1 arcpy.mapping.InsertLayer(df, lyr, newLyr ) #offending line below arcpy.mapping.UpdateLayer(df, newLyr, styleLayer, False) arcpy.mapping.RemoveLayer(df, lyr) #finally, save the mxd arcpy.AddMessage(' Replaced '+str(numRepl)+'\n') newMxd = destPath+'\\'+os.path.split(mxd)[1] mapDoc.saveACopy(newMxd)` Here is the error code: Traceback (most recent call last): File "C:\Users\rob\Documents\Coding Projects\MXD Mods\Replace_Layer.py", line 54, in arcpy.mapping.UpdateLayer(df, newLyr, styleLayer, False) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1876, in UpdateLayer tc.ReplaceLayer(tl, rl) ValueError: DataFrameObject: Unexpected error
Failed to execute (FindReplaceMXDLayer).
Can anyone see why this is please?
أكثر...