How do you create a new label class?

1266
5
10-18-2018 07:16 AM
MichaelStranovsky
Occasional Contributor

I would like to be able to create a new label class on a featurelayer using the sdk.

0 Kudos
5 Replies
UmaHarano
Esri Regular Contributor

Here is a code snippet to create a new label class and assign it to a feature layer.

Thanks

Uma

QueuedTask.Run(() => {
                var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().First();
                //get the layer's defintion
                var lyrDefn = lyr.GetDefinition() as CIMFeatureLayer;
                //Text symbol to use for labelling
                var textSymbol = SymbolFactory.Instance.ConstructTextSymbol();
                //Create the new label class and initialize props
                var newLabelClass = new CIMLabelClass
                {
                    //Define the label expression engine
                    ExpressionEngine = LabelExpressionEngine.Arcade,
                    //Expression to use to label
                    Expression = "$feature.NAME + $feature.Shape_Area",                    
                    Visibility = true,
                    TextSymbol = textSymbol.MakeSymbolReference(),
                    Name = "MyNewLabelClass"
                };

                //Most of the Maplex and standard engine placement properties get set automatically.
                //But you have to set the feature type explicitly. Line seems default.
                var maplexLabelPlacementProps = new CIMMaplexLabelPlacementProperties
                {
                    FeatureType = LabelFeatureType.Polygon
                };

                var standardLabelPlacementProps = new CIMStandardLabelPlacementProperties
                {
                    FeatureType = LabelFeatureType.Polygon
                };

                //Set the placement prop defined to the label class.
                newLabelClass.MaplexLabelPlacementProperties = maplexLabelPlacementProps;
                newLabelClass.StandardLabelPlacementProperties = standardLabelPlacementProps;

                //Get the layer's label classes
                var lyrsLabelsClasses = lyrDefn.LabelClasses.ToList();
                //Add the new label class to the labels label class
                lyrsLabelsClasses.Add(newLabelClass);
                
                lyrDefn.LabelClasses = lyrsLabelsClasses.ToArray(); //Set the labelClasses back
                lyr.SetDefinition(lyrDefn); //set the layer's definition;


            });
0 Kudos
MichaelStranovsky
Occasional Contributor

I tried it and it did not work. It does not create the label class and assign it on any of the layers.

0 Kudos
MichaelStranovsky
Occasional Contributor

Never mind , I was able to get it to work.

Thank you

0 Kudos
MichaelStranovsky
Occasional Contributor

Hi Uma,

Is there a way to get at the following:

To enable them through the sdk?

Thank you Michael

0 Kudos
MichaelStranovsky
Occasional Contributor

I actually figured it out….AutoReorder on the CIMLegend

Thank you

0 Kudos