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.
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.
Solved! Go to Solution.
lc = cim_lyr.labelClasses[0]
lc.maplexLabelPlacementProperties.neverRemoveLabel = True # or False
Let us know if that works for you.
All the best,
Glen
lc = cim_lyr.labelClasses[0]
lc.maplexLabelPlacementProperties.neverRemoveLabel = True # or False
Let us know if that works for you.
All the best,
Glen
Thanks! That worked like a charm.