【ArcGIS Pro二次开发】(80):标注_CIMLabelClass

CIMLabelClass(Cartographic Information Model Label Class)是ArcGIS Pro SDK中的一个类。

它主要用于定义标签的样式和属性,如字体、大小、颜色、对齐方式等,以及标签的排列和布局规则。


1、获取当前地图的标签引擎

cs 复制代码
// 获取当前地图的定义【CIMMap】
CIMMap cimMap = MapView.Active.Map.GetDefinition();
// 从【CIMMap】中获取标签引擎
CIMGeneralPlacementProperties labelEngine = cimMap.GeneralPlacementProperties;

2、修改当前地图的标签引擎

cs 复制代码
// 获取当前地图的定义【CIMMap】
CIMMap cimMap = MapView.Active.Map.GetDefinition();
// 从【CIMMap】中获取标签引擎
CIMGeneralPlacementProperties labelEngine = cimMap.GeneralPlacementProperties;
// 创建一个新的Maplex标签引擎
CIMMaplexGeneralPlacementProperties mgp = new CIMMaplexGeneralPlacementProperties();
// 设置标签引擎
cimMap.GeneralPlacementProperties = mgp;
// 应用到地图设置中
MapView.Active.Map.SetDefinition(cimMap);

3、将文字符号TextSymbol应用到要素图层

cs 复制代码
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 创建一个CIMTextSymbol
CIMTextSymbol textSymbol = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlueRGB, 8, "Corbel", "Regular");
// 设置文字样式TextSymbol
theLabelClass.TextSymbol.Symbol = textSymbol;
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
featureLayer.SetLabelVisibility(true);

4、更改标签位置------点

cs 复制代码
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注位置
// 根据标签引擎的不同,需要分别处理
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
    theLabelClass.StandardLabelPlacementProperties.PointPlacementMethod = StandardPointPlacementMethod.OnTopPoint;
}
else
{
    theLabelClass.MaplexLabelPlacementProperties.PointPlacementMethod = MaplexPointPlacementMethod.CenteredOnPoint;
}
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
featureLayer.SetLabelVisibility(true);

5、更改标签位置------线

cs 复制代码
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注位置
// 根据标签引擎的不同,需要分别处理
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
    var lineLablePosition = new CIMStandardLineLabelPosition
    {
        Perpendicular = false,    // 垂直
        Parallel = false,     // 平行
        ProduceCurvedLabels = true,    // 曲线
        Horizontal = false,   // 水平
        OnTop = false    // 位于上方
    };
    theLabelClass.StandardLabelPlacementProperties.LineLabelPosition = lineLablePosition;
}
else
{
    theLabelClass.MaplexLabelPlacementProperties.LinePlacementMethod = MaplexLinePlacementMethod.CenteredPerpendicularOnLine;
    theLabelClass.MaplexLabelPlacementProperties.LineFeatureType = MaplexLineFeatureType.General;
}
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }

6、更改标签位置------面

cs 复制代码
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注位置
// 根据标签引擎的不同,需要分别处理
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
    theLabelClass.StandardLabelPlacementProperties.PolygonPlacementMethod =
             StandardPolygonPlacementMethod.AlwaysHorizontal;
    theLabelClass.StandardLabelPlacementProperties.PlaceOnlyInsidePolygon = true;
}
else
{
    theLabelClass.MaplexLabelPlacementProperties.PolygonFeatureType =
                             MaplexPolygonFeatureType.LandParcel;
    theLabelClass.MaplexLabelPlacementProperties.AvoidPolygonHoles = true;
    theLabelClass.MaplexLabelPlacementProperties.PolygonPlacementMethod =
                        MaplexPolygonPlacementMethod.HorizontalInPolygon;
    theLabelClass.MaplexLabelPlacementProperties.CanPlaceLabelOutsidePolygon = true;
}
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }

7、更改对齐方式【Maplex】

cs 复制代码
// 点、面
theLabelClass.MaplexLabelPlacementProperties.GraticuleAlignment = true;
theLabelClass.MaplexLabelPlacementProperties.GraticuleAlignmentType = MaplexGraticuleAlignmentType.Curved;
// 线
theLabelClass.MaplexLabelPlacementProperties.AlignLabelToLineDirection = true;

8、更改标注旋转方式【Maplex】

cs 复制代码
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注旋转
CIMMaplexRotationProperties rotationProperties = new CIMMaplexRotationProperties
{
    Enable = true,
    RotationField = "ELEVATION", // 按字段
    AdditionalAngle = 15, // 角度 
    RotationType = MaplexLabelRotationType.Arithmetic,
    AlignmentType = MaplexRotationAlignmentType.Perpendicular,
    AlignLabelToAngle = true
};
// 应用属性设置
theLabelClass.MaplexLabelPlacementProperties.RotationProperties = rotationProperties;
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }

9、更改标注表达式

cs 复制代码
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 设置标注语言为python
theLabelClass.ExpressionEngine = LabelExpressionEngine.Python;
// 设置标注内容
string code = $"import......  ";
theLabelClass.Expression = code;
// 应用标注设置
lyrDefn.LabelClasses[0] = theLabelClass; // 假设只有一个标注类别
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }
相关推荐
坐井观老天1 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
pchmi4 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭5 小时前
C#都可以找哪些工作?
开发语言·c#
boligongzhu7 小时前
Dalsa线阵CCD相机使用开发手册
c#
GISerQ.17 小时前
ArcGIS计算土地转移矩阵
arcgis·土地利用·土地转移矩阵
向宇it19 小时前
【从零开始入门unity游戏开发之——C#篇23】C#面向对象继承——`as`类型转化和`is`类型检查、向上转型和向下转型、里氏替换原则(LSP)
java·开发语言·unity·c#·游戏引擎·里氏替换原则
sukalot20 小时前
windows C#-命名实参和可选实参(下)
windows·c#
小码编匠20 小时前
.NET 下 RabbitMQ 队列、死信队列、延时队列及小应用
后端·c#·.net
我不是程序猿儿1 天前
【C#】Debug和Release的区别和使用
开发语言·c#