Collapse symbology in TOC

2704
8
09-10-2012 12:51 PM
ChrisMathers
Regular Contributor II
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
Tags (2)
0 Kudos
8 Replies
JeffBarrette
Esri Regular Contributor
We do not have an arcpy.mapping Layer property that allows us to determine/set this.  UpdateLayer will apply the symbology however it is persisted in the source layer (i.e., collapse yes/no).

In your case, why are you using UpdateLayer?   Are you just trying to replace data sources or are you also changing out symbology?

Jeff
0 Kudos
MathewCoyle
Frequent Contributor
You could accomplish this with the evil uncle of arcpy, spoken here only in hushed whispers. ArcObjects
0 Kudos
ChrisMathers
Regular Contributor II
That's what I thought. I didn't see it but wanted to be sure. In the move to LGIM we are changing the schema and name of many feature classes so the old symbology will not work with the new attributes. Will UpdateLayer also update the data source as well if I set symbology_only=false?
0 Kudos
JeffBarrette
Esri Regular Contributor
Yes, it will update the data source.

Jeff
0 Kudos
DylanHarwell
Occasional Contributor

To re-open this old thread, as this is exactly what I am trying to find out... Has ESRI added the ability to determine/set whether a layer in the table of contents is collapsed/expanded yet?? I am writing a script that will 'touch' over a thousand layers in my organization's web maps housed in a Pro project, and from what I have read on the Pro/Py3/mp help docs I don't see this as a property of the Layer object yet. 

Please, someone tell me I am wrong and that functionality exists!

0 Kudos
JeffBarrette
Esri Regular Contributor

This will not be supported for arcpy.mapping (ArcMap).  But for Pro 2.4 we added finer grained access to many more capabilities via the CIM - Esri's Cartographic Information Model.  Here is a link to the help topic: 

 

Python CIM access—ArcPy | Documentation 

 

And here is a sample:

#The following script will expand all group layers in a map's TOC
# but will collapse all feature and raster layers.

p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
for l in m.listLayers():
if l.isGroupLayer:
l_cim = l.getDefinition('V2') #get the layer's CIM definition
l_cim.expanded = True #expand group layer
l.setDefinition(l_cim) #set the layer's CIM definition
if l.isFeatureLayer or l.isRasterLayer:
l_cim = l.getDefinition('V2')
l_cim.expanded = False #collapse layers
l.setDefinition(l_cim)

Jeff

DylanHarwell
Occasional Contributor

Thanks Jeff, that is definitely exciting to see!

0 Kudos
JeffBarrette
Esri Regular Contributor

Starting with Pro 2.4, Python CIM access is available which will provide finer-grained access to more settings/capabilities.

 

Help topic: https://pro.arcgis.com/en/pro-app/arcpy/mapping/python-cim-access.htm

Video: https://www.youtube.com/watch?v=8wgt8bKD0Ww&feature=youtu.be

Specific to collapsing layers in the TOC, here is an example:

p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers()[0]

l_cim = l.getDefinition('V2')     #Get the layer's CIM definition

if l_cim.expanded == True:
l_cim.expanded = False        #Collapse the layer

l.setDefinition(l_cim)      #Set the updated layer definition