【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); }
相关推荐
IT技术分享社区4 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
△曉風殘月〆11 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風13 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_6569747416 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo16 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo18 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发18 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术18 小时前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒19 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
平凡シンプル21 小时前
C# EF 使用
c#