这个方法的作用是以输入点为坐标原点,指定角度(以弧度为单位)方向上,与输入点指定距离的位置生成一个点。
代码示例:
public void ConstructAngleDistance()
{
// 获取当前应用程序的文档对象,并转换为IMxDocument接口
IMxDocument mxDocument = m_application.Document as IMxDocument;
// 获取文档对象中的焦点地图
IMap map = mxDocument.FocusMap;
// 获取焦点地图上的要素选择
ISelection featureSelection = map.FeatureSelection;
// 将要素选择转换为枚举器,用于遍历选中的要素
IEnumFeature featureEnumerator = featureSelection as IEnumFeature;
// 重置枚举器,确保从头开始遍历
featureEnumerator.Reset();
// 获取第一个要素
IFeature currentFeature = featureEnumerator.Next();
// 定义距离和角度
double distance = 250;
double angle = 45;
// 遍历所有选中的要素
while (currentFeature != null)
{
// 检查当前要素的几何类型是否为点
if (currentFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
{
// 将要素的形状转换为点
IPoint startPoint = currentFeature.Shape as IPoint;
// 弹出消息框显示起始点的坐标、角度和距离
System.Windows.Forms.MessageBox.Show("Start Point: " + startPoint.X + ", " + startPoint.Y + "\\n" +
"Angle: " + angle + "\\n" + "Distance: " + distance);
// 调用私有方法计算新点,并显示新点的坐标
IPoint point = _ConstructAngleDistance(startPoint, angle, distance);
System.Windows.Forms.MessageBox.Show("x,y = " + point.X + "," + point.Y);
}
// 获取下一个要素
currentFeature = featureEnumerator.Next();
}
}
// 声明一个私有方法_ConstructAngleDistance,用于根据给定的点、角度和距离计算新点
private IPoint _ConstructAngleDistance(IPoint point, double angle, double distance)
{
// 将角度从度转换为弧度
double angleRad = angle * 2 * Math.PI / 360;
// 创建一个新的点对象,用于构造新点
IConstructPoint construcionPoint = new PointClass();
// 使用ConstructAngleDistance方法根据给定的角度、距离和参考点构造新点
construcionPoint.ConstructAngleDistance(point, angleRad, distance);
// 返回构造的新点
return construcionPoint as IPoint;
}
应用场景项目实例:
一、创建add-in项目
添加引用,并把嵌入互操作类型改为false
二、重写OnMouseDown()方法
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ArcMapAddin2
{
public class Tool1 : ESRI.ArcGIS.Desktop.AddIns.Tool
{
public Tool1()
{
}
protected override void OnUpdate()
{
// 如果当前应用程序对象不为空,则工具可用
Enabled = ArcMap.Application != null;
}
// 当鼠标按下时调用
protected override void OnMouseDown(MouseEventArgs arg)
{
try
{
// 获取当前应用程序对象
IApplication app = ArcMap.Application;
// 获取当前地图文档Map、ActiveView
IMxDocument mxDocument = app.Document as IMxDocument;
IMap map = mxDocument.FocusMap;
IActiveView activeView = map as IActiveView;
activeView.ContentsChanged(); // 标记地图内容已更改,触发重绘
// 获取地图中的第一个图层
ILayer layer = map.get_Layer(0);
// 将图层转换为要素图层
IFeatureLayer featureLayer = layer as IFeatureLayer;
// 创建一个橡皮筋工具,用于绘制矩形
IRubberBand pRubberBand = new RubberRectangularPolygonClass();
// 获取用户绘制的矩形框
IGeometry pQueryGeometry = pRubberBand.TrackNew(activeView.ScreenDisplay, null);
// 使用矩形框选择地图中的要素
map.SelectByShape(pQueryGeometry, null, true);
// 获取要素选择对象
IFeatureSelection pFeatureSelection = featureLayer as IFeatureSelection;
ICursor pCursor;
// 搜索所有框选中的要素,并返回一个游标对象 pCursor
pFeatureSelection.SelectionSet.Search(null, true, out pCursor);
// 如果有选中的要素
if (pFeatureSelection.SelectionSet.Count > 0)
{
// 获取要素游标
IFeatureCursor pFeatureCursor = pCursor as IFeatureCursor;
// 获取第一个要素
IFeature pFeature = pFeatureCursor.NextFeature();
// 获取拍摄角度字段的索引
int fieldpsjd = pFeature.Fields.FindField("PSJD");
// 初始化角度值
double angle_number = 0.0;
// 如果字段存在,则获取其值
if (fieldpsjd != -1)
angle_number = (double)pFeature.get_Value(fieldpsjd);
// 获取要素的几何形状
IGeometry shapeCopy = pFeature.ShapeCopy;
// 调用方法构造新点
IPoint point = shapeCopy as IPoint;
IPoint new_point1 = _ConstructAngleDistance(point, angle_number, 8);
IPoint new_point2 = _ConstructAngleDistance(point, angle_number + 15, 4);
IPoint new_point3 = _ConstructAngleDistance(point, angle_number - 15, 4);
// 创建点集合
IPointCollection pointCollection = (IPointCollection)new PolylineClass();
// 添加点到点集合
pointCollection.AddPoint(point);
pointCollection.AddPoint(new_point1);
pointCollection.AddPoint(new_point2);
pointCollection.AddPoint(new_point1);
pointCollection.AddPoint(new_point3);
pointCollection.AddPoint(new_point2);
pointCollection.AddPoint(new_point3);
// 创建折线
IPolyline polyline = pointCollection as IPolyline;
// 创建线元素
ILineElement lineElement = (ILineElement)new LineElementClass();
// 创建简单线符号
ISimpleLineSymbol simpleLineSymbol = (ISimpleLineSymbol)new SimpleLineSymbolClass();
// 创建颜色对象
IRgbColor color = new RgbColorClass();
// 设置颜色为红色
color.Red = 255;
color.Green = 0;
color.Blue = 0;
// 将颜色应用到线符号
simpleLineSymbol.Color = (IColor)color;
simpleLineSymbol.Width = 2; // 设置线宽
lineElement.Symbol = (ILineSymbol)simpleLineSymbol;
// 获取地图的图形容器
IGraphicsContainer pGraphicsContainer = map as IGraphicsContainer;
pGraphicsContainer.Reset(); // 重置图形容器
// 遍历并删除名称为 "ele_polyline" 的图形元素
for (IElement Element = pGraphicsContainer.Next(); Element != null; Element = pGraphicsContainer.Next())
{
if ((Element as IElementProperties).Name == "ele_polyline")
pGraphicsContainer.DeleteElement(Element);
}
// 创建图形元素
IElement element = lineElement as IElement;
element.Geometry = polyline as IGeometry; // 设置图形元素的几何形状
// 将图形元素添加到地图的图形图层
pGraphicsContainer.AddElement(element, 0);
// 设置图形元素的名称
IElementProperties elementProperties = element as IElementProperties;
elementProperties.Name = "ele_polyline";
// 刷新地图视图以显示新添加的图形元素
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, (object)null, (IEnvelope)null);
}
pCursor = null; // 清除游标对象
}
catch (Exception)
{
// 异常处理,这里留空
}
}
// 构造点的方法,根据给定的角度和距离
private IPoint _ConstructAngleDistance(IPoint point, double angle, double distance)
{
// 将角度转换为弧度
double angleRad = Math.PI / 180.0 * (360.0 - angle + 90.0);
// 创建构造点对象
IConstructPoint construcionPoint = new PointClass();
// 根据角度和距离构造新点
construcionPoint.ConstructAngleDistance(point, angleRad, distance);
// 返回新点
return construcionPoint as IPoint;
}
}
}
代码中角度转弧度的公式做了调整,因为PSJD字段存储的角度是从正北(90度)方向顺时针旋转的,而ConstructAngleDistance方法是从正东(0度)方向逆时针旋转。