内容摘要:
本文提供利用ArcEngine10.7&VS2017设计Windows窗体实现对Mxd地图文档的距离+面积量算功能,并解决“未找到类型或命名空间名”等的报错,往期文章:
ArcEngine入门开发第1篇:ArcEngine10.7&VS2017环境配置
本文内容接上篇。
一、开发环境
1.语言:C#
2.编程环境:Visual Studio2017
3.ArcEngine版本:10.7
二、菜单栏设计
在窗体的“文件”菜单下添加两个一级菜单“量算”和“清除量算”,在“量算”下添加两个二级菜单“距离量算”和“面积量算”。
三、距离量算和面积量算
本文实现的功能需要用到以下变量,将这些变量添加到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,在如下函数中添加代码:
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;
}
此时系统会显示错误提示如下。
这是由于缺少对部分ArcGIS指令的引用,在Form1.cs引用部分添加如下代码可解决:
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
若仍然显示报错不存在类型或命名空间名“Display”或“Geometry”,则右键解决方案资源管理器中“引用”,选择“添加引用”,在打开的引用管理器对话框中点击“扩展”,找到ESRI.ArcGIS.Display和ESRI.ArcGIS.Geometry并选中,点击“确定”按钮即可解决。
此时仍有两个错误提示如下。
在解决方案资源管理器中点击“引用”,找到“ESRI.ArcGIS.Carto”引用,右键选择“属性”,在属性窗口中将其嵌入互操作类型设置为“False”即可解决。
四、清除量算
双击菜单栏中的“清除量算”,进入Form1.cs,在如下函数中添加代码:
private void 清除量算ToolStripMenuItem_Click(object sender, EventArgs e)
{
flag = 0;
axMapControl1.ActiveView.GraphicsContainer.DeleteAllElements();
axMapControl1.Refresh();
}
运行结果如下。
欢迎交流🌹🌹