ArcGIS Pro二次开发画注记
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ArcGIS.Core.CIM; using ArcGIS.Core.Data; using ArcGIS.Core.Geometry; using ArcGIS.Desktop.Catalog; using ArcGIS.Desktop.Core; using ArcGIS.Desktop.Editing; using ArcGIS.Desktop.Extensions; using ArcGIS.Desktop.Framework; using ArcGIS.Desktop.Framework.Contracts; using ArcGIS.Desktop.Framework.Dialogs; using ArcGIS.Desktop.Framework.Threading.Tasks; using ArcGIS.Desktop.Mapping; using System.Windows.Input; namespace ProAppModule1 { internal class MapToolSelect : MapTool { public MapToolSelect() { IsSketchTool = true; UseSnapping = true; // set the sketch type to line SketchType = SketchGeometryType.Line; } /// <summary> /// Restrict the sketch to a two point line /// </summary> /// <returns></returns> protected override async Task<bool> OnSketchModifiedAsync() { // restrict the sketch to a 2 point line bool finish = await QueuedTask.Run(async () => { // get the current sketch var geometry = await base.GetCurrentSketchAsync(); // cast to a polyline - the geometry will always be a polyline because the SketchType (set in the constructor) is set to Line. var geom = geometry as ArcGIS.Core.Geometry.Polyline; // check the point count return geom?.PointCount >= 2; }); // call FinishSketchAsync if we have 2 points if (finish) finish = await base.FinishSketchAsync(); return finish; } /// <summary> /// Called when the sketch finishes. This is where we will create the edit operation and then execute it. /// </summary> /// <param name="geometry">The geometry created by the sketch.</param> /// <returns>A Task returning a Boolean indicating if the sketch complete event was successfully handled.</returns> protected override async Task<bool> OnSketchCompleteAsync(Geometry geometry) { if (CurrentTemplate == null || geometry == null) return false; bool result = await QueuedTask.Run(() => { // get the anno layer AnnotationLayer annoLayer = CurrentTemplate.Layer as AnnotationLayer; if (annoLayer == null) return false; // get the anno feature class var fc = annoLayer.GetFeatureClass() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClass; if (fc == null) return false; // get the featureclass CIM definition which contains the labels, symbols var cimDefinition = fc.GetDefinition() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClassDefinition; var labels = cimDefinition.GetLabelClassCollection(); var symbols = cimDefinition.GetSymbolCollection(); // make sure there are labels, symbols if ((labels.Count == 0) || (symbols.Count == 0)) return false; // find the label class required // typically you would use a subtype name or some other characteristic // use the first label class var label = labels[0]; if (labels.Count > 1) { // find a label class based on template name foreach (var LabelClass in labels) { if (LabelClass.Name == CurrentTemplate.Name) { label = LabelClass; break; } } } // each label has a textSymbol // the symbolName *should* be the symbolID to be used var symbolName = label.TextSymbol.SymbolName; int symbolID = -1; if (!int.TryParse(symbolName, out symbolID)) { // int.TryParse fails - attempt to find the symbolName in the symbol collection foreach (var symbol in symbols) { if (symbol.Name == symbolName) { symbolID = symbol.ID; break; } } } // no symbol? if (symbolID == -1) return false; // use the template's inspector object var inspector = CurrentTemplate.Inspector; // get the annotation properties var annoProperties = inspector.GetAnnotationProperties(); // AnnotationClassID, SymbolID and Shape are the bare minimum for an annotation feature // use the inspector[fieldName] to set the annotationClassid - this is allowed since annotationClassID is a guaranteed field in the annotation schema inspector["AnnotationClassID"] = label.ID; // set the symbolID too inspector["SymbolID"] = symbolID; // use the annotation properties to set the other attributes annoProperties.TextString = "My annotation feature"; annoProperties.Color = ColorFactory.Instance.GreenRGB; annoProperties.VerticalAlignment = ArcGIS.Core.CIM.VerticalAlignment.Top; annoProperties.Underline = true; // set the geometry to be the sketched line // when creating annotation features the shape to be passed in the create operation is the CIMTextGraphic shape annoProperties.Shape = geometry; // set the annotation properties back on the inspector inspector.SetAnnotationProperties(annoProperties); // Create an edit operation var createOperation = new EditOperation(); createOperation.Name = string.Format("Create {0}", CurrentTemplate.Layer.Name); createOperation.SelectNewFeatures = true; // create and execute using the inspector createOperation.Create(CurrentTemplate.Layer, inspector); return createOperation.Execute(); }); return result; } } }
分类:
C#
, ArcGIS Pro二次开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2019-03-11 arcgis 获得工具箱工具的个数
2018-03-11 excel 鼠标上下左右移动
2018-03-11 excel 批注