一、 开发环境
Windows 7 SP1 64bit
ArcGIS 10.2.2
Microsoft Visual Studio 2010(C# 4.0)
.NET Framework 4.0
二、 入门实例
1. 如何创建简单的工具
1) 创建
在Visual Studio 2010中创建工程,选择模板路径为Visual C# -- ArcGIS – Desktop Add-Ins – ArcMap Add-in,自定义工程名称和代码位置后确定,进行ArcGIS Add-Ins开发向导。
在工程向导中,设置该Add-in插件的基本信息,进入下一步设置。
设置Add-in类型,选中Tool,并设置基本信息,完成设置。
2) 编译
向导已经自动为该工程添加了部分必要的ArcGIS Engine组件引用,并自动生成了部分代码。
执行编译,将生成一个.esriAddIn文件。
3) 安装
双击.esriAddIn文件,进行安装。
提示安装成功。
4) 调用
在ArcMap中,打开Customize Mode菜单的,切换到Commans页签,在Add-In Controls中可以看到刚刚安装的功能。
创建一个自定义的Toolbar。
将创建的Command拖动到该Toolbar上,即可使用。
5) 移除
不再使用到的Add-In插件,可在Add-In Manager中删除。
2. 如何进行功能开发
1) 了解虚类
虚类Tool中定义了一些常见的方法,我们只要根据实际需要去实现部分或全部方法,即可实现工具的功能。
2) 实现虚方法
本例将实现一个在MapControl中绘制多段线并展示其坐标的功能。
3) 功能测试与验证
开发完成后,编译,安装,打开ArcMap,执行该功能。
弹出绘制的多段线的坐标,则功能无误。
附录
本例中ShowCoordinates类的源代码如下:
using System; using System.Collections.Generic; using System.Text; using System.IO; using ESRI.ArcGIS.Display; namespace ArcMapAddin1 { public class ShowCoordinates : ESRI.ArcGIS.Desktop.AddIns.Tool { private ESRI.ArcGIS.Display.INewLineFeedback m_pNewLineFeedback = null; public ShowCoordinates() { } protected override void OnUpdate() { try { Enabled = true; } catch { } finally { base.OnUpdate(); } //Enabled = ArcMap.Application != null; } protected override void OnActivate() { base.OnActivate(); Cursor = System.Windows.Forms.Cursors.Cross; } protected override bool OnDeactivate() { Cursor = System.Windows.Forms.Cursors.Arrow; return base.OnDeactivate(); } protected override void OnDoubleClick() { Func<ESRI.ArcGIS.Geometry.IPolyline, string> PolylineToString = (aPolyline) => { string sResult = ""; if (null != aPolyline) { ESRI.ArcGIS.Geometry.IPointCollection pPointCollection = aPolyline as ESRI.ArcGIS.Geometry.IPointCollection; int nCount = pPointCollection.PointCount; for (int n = 0; n < nCount; n++) { ESRI.ArcGIS.Geometry.IPoint aPoint = pPointCollection.get_Point(n); sResult += string.Format("{0} {1}, ", aPoint.X, aPoint.Y); } if (1 < sResult.Length) sResult = sResult.Substring(0, sResult.Length - 2); } return sResult; }; if (m_pNewLineFeedback != null) { ESRI.ArcGIS.Geometry.IPolyline pPolyline = m_pNewLineFeedback.Stop(); m_pNewLineFeedback = null; System.Windows.Forms.MessageBox.Show(PolylineToString(pPolyline)); } } protected override void OnMouseDown(MouseEventArgs arg) { if (arg.Button == System.Windows.Forms.MouseButtons.Left) { ESRI.ArcGIS.Geometry.IPoint pCursorPoint = ArcMap.Document.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y); if (m_pNewLineFeedback == null) { m_pNewLineFeedback = new NewLineFeedbackClass(); IRgbColor pRGB = new RgbColorClass(); ISimpleLineSymbol pSimpleLineSymbol = m_pNewLineFeedback.Symbol as ISimpleLineSymbol; pRGB.Red = 255; pSimpleLineSymbol.Color = pRGB; pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; pSimpleLineSymbol.Width = 2; m_pNewLineFeedback.Symbol = pSimpleLineSymbol as ISymbol; m_pNewLineFeedback.Display = ArcMap.Document.ActiveView.ScreenDisplay; m_pNewLineFeedback.Start(pCursorPoint); } else { m_pNewLineFeedback.AddPoint(pCursorPoint); } } } protected override void OnMouseUp(MouseEventArgs arg) { if (arg.Button == System.Windows.Forms.MouseButtons.Right) { OnDoubleClick(); } } protected override void OnMouseMove(MouseEventArgs arg) { if (m_pNewLineFeedback != null) { ESRI.ArcGIS.Geometry.IPoint pCursorPoint = ArcMap.Document.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y); m_pNewLineFeedback.MoveTo(pCursorPoint); } } } }