clm42

Collapse symbology in TOC

Discussion created by clm42 on Sep 10, 2012
Latest reply on Apr 9, 2019 by dylanh0
We are about to do some major rearranging in our SDE (moving into the Local Gov Info Model) and I've written a script to re-source automatically those feature classes that are moving in any MXDs they appear in. I don't want the users to experience any changes to the way their MXDs look when they open them. I know I can catch whether a layer is visible or not and set that back but I don't see that I can set if the symbology is collapsed in the TOC. Just wanted to verify this. When I update to the new LYR if the TOC has that layer expanded it will save in the edited MXD as expanded and I dont want that if I can avoid it. Below is the code I wrote this morning to make the changes to the map documents.



import arcpy, os

Layer_Replacements={old_path:[new_dataset,lyr]}

for root, dirs, files in os.walk(top_directory_of_search):
    mxds=[x for x in files if x.endswith('.mxd')]
    if len(mxds)!=0:
        for mxd_path in [os.path.join(root,mxd) for mxd in mxds]:
            paths.append(mxd_path)

for mxd_path in paths:
    mxd=arcpy.mapping.MapDocument(mxd_path)
    for df in arcpy.mapping.ListDataFrames(mxd):
        for layer in arcpy.mapping.ListLayers(df):
            if layer.dataSource in Layer_Replacements:
                was_visible=layer.visible
                layer.replaceDataSource(Layer_Replacements[r'Database Connections\OSA@sde@bay-gis.sde','SDE_WORKSPACE',Layer_Replacements[layer.dataSource][0])
                symbology=arcpy.mapping.Layer(Layer_Replacements[layer.dataSource][1])
                arcpy.mapping.UpdateLayer(df,layer,symbology)
                layer.visible=was_visible
    mxd.save

Outcomes