Select to view content in your preferred language

Creating a new label class in Arc Pro using the arcpy CIM

2492
5
Jump to solution
11-10-2020 06:40 PM
by Anonymous User
Not applicable

I am attempting to create a new label class for a layer using the arcpy CIM, code below. The new label class gets created, but ArcGIS Pro immediately crashes when I attempt to interact with it in the GUI. I have tried in the python window and in a script. 

aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps("Map")[0]
lyr = m.listLayers("SOME LAYER")[0]
ldef = lyr.getDefinition('V2')

newlblclass = arcpy.cim.CreateCIMObjectFromClassName('CIMLabelClass','V2')
newlblclass.name = "NEW CLASS"
newlblclass.visibility = True
ldef.labelClasses.append(newlblclass)
lyr.setDefinition(ldef)
0 Kudos
2 Solutions

Accepted Solutions
DuncanHornby
MVP Notable Contributor

May be there are required properties that need setting? All you set is the name and visibility, but what about the field that the label is derived from?

View solution in original post

0 Kudos
by Anonymous User
Not applicable

Correct. After researching there are many "nested" CIMobjects that make up a label class cim object. All of these objects need to be created and assigned in order for the label class object to function properly. 

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

View solution in original post

Tags (3)
0 Kudos
5 Replies
DuncanHornby
MVP Notable Contributor

May be there are required properties that need setting? All you set is the name and visibility, but what about the field that the label is derived from?

0 Kudos
by Anonymous User
Not applicable

Correct. After researching there are many "nested" CIMobjects that make up a label class cim object. All of these objects need to be created and assigned in order for the label class object to function properly. 

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

Tags (3)
0 Kudos
anyry
by
Occasional Contributor

Can you please share your label class creation solution? I have the same problem and I can't solve it. Thanks

0 Kudos
DestinyKelley1
Occasional Contributor

My solution is different, and may be of help to you if you are still experiencing this issue.   I have figured out a way to bypass the corruption of the CIM definition when attempting to create a new label class with ArcPy CIM.  To test this out, you can use any layer those default class is named "Class 1." Instead of using arcpy.cim.CreateCIMObjectFromClassName('CIMLabelClass','V2') or arcpy.cim.CIMLabelClass() as suggested by other users, I used cimObject.labelClasses.copy()

It's a little awkward, but I hope it helps!

The code to try out is below:

import arcpy

p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps('Map')[0]
layer = m.listLayers("Your Layer Name Here")[0]
cimObject = layer.getDefinition('V2')
lblClasses = cimObject.labelClasses
    
for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        lblClass_copy = cimObject.labelClasses.copy()[nlc_index]
        cimObject.labelClasses.append(lblClass_copy)
        layer.setDefinition(cimObject)
        break

cimObject = layer.getDefinition('V2')
lblClasses = cimObject.labelClasses        
        
for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        if nlc_index != 0:
            lblClass.name = "Dummy"
            layer.setDefinition(cimObject)

 

Tags (2)
0 Kudos
JingchaoZhou
Occasional Contributor

Just wondering if you can change the label class name before you append it in the first for loop

something like this 

for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        lblClass_copy = cimObject.labelClasses.copy()[nlc_index]
        
        lblClass_copy.name = "new_class_name"
        cimObject.labelClasses.append(lblClass_copy)
        layer.setDefinition(cimObject)
        break

I have not tested this, but just wondering if this will optimize the code and avoid the second for loop....

0 Kudos