【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); }
相关推荐
吾与谁归in几秒前
【C#设计模式(14)——责任链模式( Chain-of-responsibility Pattern)】
设计模式·c#·责任链模式
神仙别闹1 小时前
基于C#和Sql Server 2008实现的(WinForm)订单生成系统
开发语言·c#
向宇it10 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
九鼎科技-Leo11 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
Heaphaestus,RC12 小时前
【Unity3D】获取 GameObject 的完整层级结构
unity·c#
baivfhpwxf202312 小时前
C# 5000 转16进制 字节(激光器串口通讯生成指定格式命令)
开发语言·c#
直裾12 小时前
Scala全文单词统计
开发语言·c#·scala
GIS思维12 小时前
ArcGIS定义投影与投影的区别(数据和底图不套合的原因和解决办法)
arcgis·gis·地理信息·arcgis坐标系·动态投影
ZwaterZ14 小时前
vue el-table表格点击某行触发事件&&操作栏点击和row-click冲突问题
前端·vue.js·elementui·c#·vue
阮少年、16 小时前
ArcGIS Pro的arpx项目在ArcGIS Server中发布要素服务(FeatureServer)
arcgis·arcgis server·gis服务发布·要素服务