ProSnippets Labeling - kataya/arcgis-pro-sdk GitHub Wiki

Language:              C#  
Subject:               Labeling  
Contributor:           ArcGIS Pro SDK Team <[email protected]>  
Organization:          esri, http://www.esri.com  
Date:                  12/11/2020  
ArcGIS Pro:            2.7  
Visual Studio:         2017, 2019  
.NET Target Framework: 4.8  

Get the active map's labeling engine - Maplex or Standard labeling engine

//Note: call within QueuedTask.Run()
//Get the active map's definition - CIMMap.
var cimMap = MapView.Active.Map.GetDefinition();
//Get the labeling engine from the map definition
CIMGeneralPlacementProperties labelEngine = cimMap.GeneralPlacementProperties;

Change the active map's labeling engine from Standard to Maplex or vice versa

//Note: call within QueuedTask.Run()
//Get the active map's definition - CIMMap.
var cimMap = MapView.Active.Map.GetDefinition();
//Get the labeling engine from the map definition
var cimGeneralPlacement = cimMap.GeneralPlacementProperties;

if (cimGeneralPlacement is CIMMaplexGeneralPlacementProperties) 
{
    //Current labeling engine is Maplex labeling engine
    //store the current label engine props if needed.
    var maplexLabelEngineProps = cimGeneralPlacement;
    //Create a new standard label engine properties
    var cimStandardPlacementProperties = new CIMStandardGeneralPlacementProperties();
    //Set the CIMMap's GeneralPlacementProperties to the new label engine
    cimMap.GeneralPlacementProperties = cimStandardPlacementProperties;                    
}
else 
{
    //Current labeling engine is Standard labeling engine
    //store the current label engine props if needed.
    var standardLabelEngine = cimGeneralPlacement;
    //Create a new Maplex label engine properties
    var cimMaplexGeneralPlacementProperties = new CIMMaplexGeneralPlacementProperties();
    //Set the CIMMap's GeneralPlacementProperties to the new label engine
    cimMap.GeneralPlacementProperties = cimMaplexGeneralPlacementProperties;
}
//Set the map's definition
MapView.Active.Map.SetDefinition(cimMap);

Apply text symbol to a feature layer

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Set the label classes' symbol to the custom text symbol
//Refer to the ProSnippets-TextSymbols wiki page for help with creating custom text symbols.
//Example: var textSymbol = await CreateTextSymbolWithHaloAsync();
theLabelClass.TextSymbol.Symbol = textSymbol;
lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition
//set the label's visiblity
featureLayer.SetLabelVisibility(true);

Enable labeling of a layer

//Note: call within QueuedTask.Run()
//set the label's visiblity
featureLayer.SetLabelVisibility(true);

Modify the Placement/Position of labels (Point geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();                
//Modify label Placement 
//Check if the label engine is Maplex or standard.
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties) //Current labeling engine is Standard labeling engine               
    theLabelClass.StandardLabelPlacementProperties.PointPlacementMethod = StandardPointPlacementMethod.OnTopPoint;                                
else    //Current labeling engine is Maplex labeling engine            
    theLabelClass.MaplexLabelPlacementProperties.PointPlacementMethod = MaplexPointPlacementMethod.CenteredOnPoint;

lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition

Modify the Placement/Position of labels (Line geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Modify label Placement 
//Check if the label engine is Maplex or standard.
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
    //Current labeling engine is Standard labeling engine
    var lineLablePosition = new CIMStandardLineLabelPosition
    {
        Perpendicular = true,
        Parallel = false,
        ProduceCurvedLabels = false,
        Horizontal = false,
        OnTop = true
    };
    theLabelClass.StandardLabelPlacementProperties.LineLabelPosition = lineLablePosition;               
}
else //Current labeling engine is Maplex labeling engine
{
    theLabelClass.MaplexLabelPlacementProperties.LinePlacementMethod = MaplexLinePlacementMethod.CenteredPerpendicularOnLine;
    theLabelClass.MaplexLabelPlacementProperties.LineFeatureType = MaplexLineFeatureType.General;
}
//theLabelClass.MaplexLabelPlacementProperties.LinePlacementMethod = MaplexLinePlacementMethod.CenteredPerpendicularOnLine;
lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition

Modify the Placement/Position of labels (Polygon geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Modify label Placement 
//Check if the label engine is Maplex or standard.
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
    //Current labeling engine is Standard Labeling engine
    theLabelClass.StandardLabelPlacementProperties.PolygonPlacementMethod = StandardPolygonPlacementMethod.AlwaysHorizontal;
    theLabelClass.StandardLabelPlacementProperties.PlaceOnlyInsidePolygon = true;
}
else
{
    //Current labeling engine is Maplex labeling engine
    theLabelClass.MaplexLabelPlacementProperties.PolygonFeatureType = MaplexPolygonFeatureType.LandParcel;
    theLabelClass.MaplexLabelPlacementProperties.AvoidPolygonHoles = true;
    theLabelClass.MaplexLabelPlacementProperties.PolygonPlacementMethod = MaplexPolygonPlacementMethod.HorizontalInPolygon;
    theLabelClass.MaplexLabelPlacementProperties.CanPlaceLabelOutsidePolygon = true;
}

lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition
//set the label's visiblity
featureLayer.SetLabelVisibility(true);

Modify Orientation of a label using the MaplexEngine (Points and Polygon geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Modify label Orientation                 
theLabelClass.MaplexLabelPlacementProperties.GraticuleAlignment = true;
theLabelClass.MaplexLabelPlacementProperties.GraticuleAlignmentType = MaplexGraticuleAlignmentType.Curved;                                        

lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition

Modify Orientation of a label using the MaplexEngine (Line geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Modify label Orientation
theLabelClass.MaplexLabelPlacementProperties.AlignLabelToLineDirection = true;

lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition

Modify label Rotation (Point geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Modify label Rotation
CIMMaplexRotationProperties rotationProperties = new CIMMaplexRotationProperties
{
    Enable = true, //Enable rotation
    RotationField = "ELEVATION", //Field that is used to define rotation angle
    AdditionalAngle = 15, //Additional rotation 
    RotationType = MaplexLabelRotationType.Arithmetic,
    AlignmentType = MaplexRotationAlignmentType.Perpendicular,
    AlignLabelToAngle = true
};
theLabelClass.MaplexLabelPlacementProperties.RotationProperties = rotationProperties;
lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition

Spread labels across Polygon geometry

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//Spread Labels (words and characters to fill feature)
// Spread words to fill feature
theLabelClass.MaplexLabelPlacementProperties.SpreadWords = true;
//Spread Characters to a fixed limit of 50%
theLabelClass.MaplexLabelPlacementProperties.SpreadCharacters = true;
theLabelClass.MaplexLabelPlacementProperties.MaximumCharacterSpacing = 50.0;
lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition

Modify label's Leader Line Anchor point properties (Polygon geometry)

//Note: call within QueuedTask.Run()
//Get the layer's definition
var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
//Get the label classes - we need the first one
var listLabelClasses = lyrDefn.LabelClasses.ToList();
var theLabelClass = listLabelClasses.FirstOrDefault();
//If TextSymbol is a callout the leader line anachor point can be modified
theLabelClass.MaplexLabelPlacementProperties.PolygonAnchorPointType = MaplexAnchorPointType.Perimeter;
lyrDefn.LabelClasses = listLabelClasses.ToArray(); //Set the labelClasses back
featureLayer.SetDefinition(lyrDefn); //set the layer's definition
⚠️ **GitHub.com Fallback** ⚠️