ArcEngine入门开发第3篇:距离/面积量算

内容摘要:

本文提供利用ArcEngine10.7&VS2017设计Windows窗体实现对Mxd地图文档的距离+面积量算功能,并解决“未找到类型或命名空间名”等的报错,往期文章:

ArcEngine入门开发第1篇:ArcEngine10.7&VS2017环境配置

ArcEngine入门开发第2篇:打开/保存Mxd地图文档

本文内容接上篇。

一、开发环境

1.语言:C#

2.编程环境:Visual Studio2017

3.ArcEngine版本:10.7

二、菜单栏设计

在窗体的“文件”菜单下添加两个一级菜单“量算”和“清除量算”,在“量算”下添加两个二级菜单“距离量算”和“面积量算”。

b9a443194c3041789dae1fd16bf2745b.png

三、距离量算和面积量算

本文实现的功能需要用到以下变量,将这些变量添加到public partial class Form1 : Form中:

 int flag = 0;
 private IElement pElement;

双击菜单栏中的“距离量算”,进入Form1.cs,在如下函数中添加代码:

private void 距离量算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

双击菜单栏中的“面积量算”,进入Form1.cs,在如下函数中添加代码:

private void 面积量算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 2;
        }

在窗体中点击MapControl控件,在其属性窗口中点击上端第四个图标⚡,显示事件,点击第二个图标按字母排序,找到“OnMouseDown”事件,双击进入Form1.cs,在如下函数中添加代码:

7508ae6798234b0a99ec699db3ac9654.png

private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
     //量算功能
     switch (flag)
     {
         case 0:
            break;
         case 1:
            IPolyline polyline = DistanceMeasure();
            MessageBox.Show("距离为" + Convert.ToInt64(polyline.Length).ToString());
            break;
         case 2:
            IPolygon polygon = AreaMeasurea();
            IArea pArea = polygon as IArea;
            MessageBox.Show("面积为" + Convert.ToInt64(Math.Abs(pArea.Area)).ToString());
            break;
         default:
             break;
     }
}

public partial class Form1 : Form中实现距离量算DistanceMeasure()函数和面积量算AreaMeasure()函数,添加如下代码:

//距离量算
            private IPolyline DistanceMeasure()
            {
                ILineElement pLineElement;
                IActiveView pActiveView;
                IRgbColor pRgbColor;
                IRubberBand pRubberBand;
                ISimpleLineSymbol pSimpleLineSymbol;
                IPolyline pPloyline;
                pActiveView = axMapControl1.ActiveView;
                pSimpleLineSymbol = new SimpleLineSymbol();
                pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                pRgbColor = new RgbColor();
                pRgbColor.Red = 125;
                pSimpleLineSymbol.Color = pRgbColor;
                pRubberBand = new RubberLine();
                pLineElement = new LineElementClass();
                pLineElement.Symbol = pSimpleLineSymbol;
                pPloyline = pRubberBand.TrackNew(pActiveView.ScreenDisplay, pSimpleLineSymbol as ISymbol) as IPolyline;
                pElement = new LineElement();
                pElement = pLineElement as IElement;
                pElement.Geometry = pPloyline;
                double a = pPloyline.Length;
                IGraphicsContainer pGraphicsContainer = axMapControl1.ActiveView.FocusMap as IGraphicsContainer;
                pGraphicsContainer.AddElement(pElement, 0);
                axMapControl1.ActiveView.Refresh();
                return pPloyline;
            }

//面积量算
            private IPolygon AreaMeasurea()
            {
                IRgbColor pRgbColor;
                IActiveView pActiveView;
                IRubberBand pRubberBand;
                IElement pElement;
                IGraphicsContainer pGraphicsContainer;
                IPolygonElement pPolygonElement;
                ISimpleFillSymbol pSimpleFillSymbol;
                IPolygon pPolygon;
                pActiveView = axMapControl1.ActiveView;
                pSimpleFillSymbol = new SimpleFillSymbol();
                pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSBackwardDiagonal;
                pRgbColor = new RgbColor();
                pRgbColor.Red = 250;
                pSimpleFillSymbol.Color = pRgbColor;
                pRubberBand = new RubberPolygon();
                pPolygonElement = new PolygonElementClass();
                pPolygon = pRubberBand.TrackNew(pActiveView.ScreenDisplay, pSimpleFillSymbol as ISymbol) as IPolygon;
                pElement = new PolygonElement();
                pElement = pPolygonElement as IElement;
                pElement.Geometry = pPolygon;
                pGraphicsContainer = axMapControl1.ActiveView.FocusMap as IGraphicsContainer; pGraphicsContainer.AddElement(pElement, 0);
                axMapControl1.ActiveView.Refresh();
                return pPolygon;
            }

此时系统会显示错误提示如下。

b30346f590f14a8a89aff94c79c8f070.png

这是由于缺少对部分ArcGIS指令的引用,在Form1.cs引用部分添加如下代码可解决:

using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;

若仍然显示报错不存在类型或命名空间名“Display”或“Geometry”,则右键解决方案资源管理器中“引用”,选择“添加引用”,在打开的引用管理器对话框中点击“扩展”,找到ESRI.ArcGIS.Display和ESRI.ArcGIS.Geometry并选中,点击“确定”按钮即可解决。

b13fb9951ba748d4b3330d2c36a0cb41.png

此时仍有两个错误提示如下。

d64a4a41133140968aaff95b88498e00.png

在解决方案资源管理器中点击“引用”,找到“ESRI.ArcGIS.Carto”引用,右键选择“属性”,在属性窗口中将其嵌入互操作类型设置为“False”即可解决。

578625fe0bef410ba0afc0df51dfa1ce.png

e8a06469c6054cb4ade60e6f27046a06.png

四、清除量算

双击菜单栏中的“清除量算”,进入Form1.cs,在如下函数中添加代码:

 private void 清除量算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 0;
            axMapControl1.ActiveView.GraphicsContainer.DeleteAllElements();
            axMapControl1.Refresh();
        }

运行结果如下。

e8cfef006ac442c5809409da4c0c5998.png

8cef92aea52a4316870ec7a366f57220.png

欢迎交流🌹🌹

  • 31
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值