Select to view content in your preferred language

How to get unplaced labels set to "Never Remove" in Python for ArcGIS Pro?

278
2
Jump to solution
04-01-2025 04:03 PM
DN_GIS_Analyst
Emerging Contributor

I'm writing an automation script that forces some layers to set the unplaced labels to "Never Remove (place overlapping)" as seen in the label class dialog.

DN_GIS_Analyst_0-1743548299675.png

However, I want to do this in python as well.

aprx = arcpy.mp.ArcGISProject("CURRENT")

for m in aprx.listMaps():
    if m.name == "Overview Map":
        print(f"Processing {m.name}:")
        
        for layer in m.listLayers():
            if layer.name == "Pipes by Pressure Zones":
                print(f" - Processing {layer.name}:")
                try:
                    cim_lyr = layer.getDefinition("V3")
                    cim_lyr.labelVisibility = False
                    layer.setDefinition(cim_lyr)
                    print(f" -- Labels turned off for {layer.name} via CIM.")
                except Exception as e:
                    print(f" -- Failed to modify CIM for {layer.name}: {e}")

            if layer.name == "MapBook Pages":
                print(f" - Processing {layer.name}:")
                layer.visible = False
                print(f" -- Turned off {layer.name}")

            if layer.name in ["MapBook Page Sections", "Hydrants"]:
                print(f" - Processing {layer.name}:")
                try:
                    fillRGBColor = arcpy.cim.CreateCIMObjectFromClassName('CIMRGBColor', 'V3')
                    fillRGBColor.values = [255, 255, 255, 100]  # White

                    solFill = arcpy.cim.CreateCIMObjectFromClassName('CIMSolidFill', 'V3')
                    solFill.color = fillRGBColor
                    solFill.enable = True
                    solFill.colorlocked = False
                    solFill.overprint = False

                    sym = arcpy.cim.CreateCIMObjectFromClassName('CIMPolygonSymbol', 'V3')
                    sym.symbolLayers = [solFill]

                    cim_lyr = layer.getDefinition("V3")
                    lc = cim_lyr.labelClasses[0]
                    if layer.name == "MapBook Page Sections":
                        lc.canRemoveOverlappingLabel = False
                        lc.allowOverlappingLabels = True
                        lc.unplacedLabelStrategy = "ForcePlace"
                        
                    lc.visible = True
                    lc.textSymbol.symbol.haloSize = 2
                    lc.textSymbol.symbol.haloSymbol = sym

                    cim_lyr.labelVisibility = True
                    layer.showLabels = True
                    layer.setDefinition(cim_lyr)

                    print(f" -- Labels turned on and halo enabled for {layer.name} via CIM.")
                except Exception as e:
                    print(f" -- Failed to modify CIM for {layer.name}: {e}")

 

I'm unable to get the unplaced labels to be set to "Never Remove (place overlapping)" or something similar to that. The checkbox was not checked.

My goal is to get this ironed and modified to work on all Pro projects within a folder, so that I don't have to do this manually for each Pro project. Don't worry, I already got code worked out for this last part.

Thanks for whatever help you can give out.

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
MVP Regular Contributor

Hi @DN_GIS_Analyst 

lc = cim_lyr.labelClasses[0]
lc.maplexLabelPlacementProperties.neverRemoveLabel = True # or False

 

Let us know if that works for you.

 

All the best,

Glen

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
MVP Regular Contributor

Hi @DN_GIS_Analyst 

lc = cim_lyr.labelClasses[0]
lc.maplexLabelPlacementProperties.neverRemoveLabel = True # or False

 

Let us know if that works for you.

 

All the best,

Glen

~ learn.finaldraftmapping.com
DN_GIS_Analyst
Emerging Contributor

Thanks! That worked like a charm.

0 Kudos