标签:
1、开发环境
Vs2012+Arcgis10.2+win7 64bit
2、实现代码
首先在VS2012中新建一个解决方案,命名AddInTest。
接着,给解决方案AddInTest新建一个项目:

点击[确定],出现如下界面,点击[Next]。

接着,设置相关信息,并点击[finish]完成。

上图中,class name是类的名称,caption是button显示的文字,category是所属的command的分类,tooltip是鼠标在上面时状态栏显示的文字,description是工具的描述。
项目建成后,文件组织如下图:

将项目的框架改为framework 4.5
Config.esriaddinx是一个XML文件,是一个配置文件,里面包含了项目的相关配置,是自动生成的,内容如下:
01.<esri.configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">02.<name>ArcMapAddinTest</name>03.<addinid>{0f7ec41b-d1e3-4391-8d67-9dea10bea621}</addinid>04.<description>Type in a description for this Add-in.</description>05.<version>1.0</version>06.<img src="" style="display: none;"><img alt="加载中..." title="图片加载中..."src="http://www.it165.net/statics/images/s_nopic.gif">ImagesArcMapAddinTest.png07.<author>Administrator</author>08.<company></company>09.<date>2013/12/12</date>10.<targets>11.<target name="Desktop" version="10.0">12.</target></targets>13.<addin language="CLR" library="ArcMapAddinTest.dll" namespace="ArcMapAddinTest">14.<arcmap>15.<commands><button caption="AddShp" category="Add-In Controls" class="AddShp" id="ArcMapAddinTest_AddShp"image="ImagesAddShp.png" message="点击浏览shp文件并添加" tip="实现添加shp文件的功能"></button></commands></arcmap></addin></esri.configuration>
01.using System;02.using System.Collections.Generic;03.using System.Text;04.using System.IO;05. 06. 07.namespace ArcMapAddinTest08.{09.public class AddShp : ESRI.ArcGIS.Desktop.AddIns.Button10.{11.public AddShp()12.{13.}14. 15.protected override void OnClick()16.{17.}18. 19.protected override void OnUpdate()20.{21.}22.}23.}
里面包含两个方法,onclick和onupdate方法。onclick方法是点击按钮时,我们在里面写添加shp文件的代码。
首先,添加如下引用:
1.using System.Windows.Forms;2.using ESRI.ArcGIS.ArcMapUI;3.using ESRI.ArcGIS.Carto;4.using ESRI.ArcGIS.Geometry;5.using ESRI.ArcGIS.Geodatabase;6.using ESRI.ArcGIS.DataSourcesFile;
onclick方法里,事件的实现代码如下:
01.IMxDocument pMxd;02.public Button1()03.{04.pMxd = ArcMap.Document as IMxDocument;05.}06. 07.protected override void OnClick()08.{ 09.System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();10.openFileDialog.Filter = "shp(*.shp)|*.shp";11.openFileDialog.InitialDirectory = @"D:";12.openFileDialog.Multiselect = false;13.DialogResult pDialogResult = openFileDialog.ShowDialog();14.if (pDialogResult != DialogResult.OK)15.{16.return;17.}18.string pPath = openFileDialog.FileName;19.string pFolder = System.IO.Path.GetDirectoryName(pPath);20.string pFileName = System.IO.Path.GetFileName(pPath);21. 22.IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();23.IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0);24.IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;25.IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName);26.IFeatureLayer pFLayer = new FeatureLayerClass();27.pFLayer.FeatureClass = pFC;28.pFLayer.Name = pFC.AliasName;29.ILayer pLayer = pFLayer as ILayer;30.IMap pMap = pMxd.FocusMap;31.pMap.AddLayer(pLayer);32. 33.//34.// TODO: Sample code showing how to access button host35.//36.ArcMap.Application.CurrentTool = null;37.}
OnUpdate方法事件的代码如下:
1.protected override void OnUpdate()2.{3.Enabled = pMxd.FocusMap.LayerCount >= 0;4.}
代码编写完成后,编译程序,打开编译目录,编译文件如下:

双击.esriAddIn文件,添加工具到Arcmap中。打开Arcmap,打开扩展管理,command选项卡,找到Add-In Controls,这时候你会发现你编写的工具会出现在这一组里面。

点击[close],点点试试……
标签:
原文地址:http://www.cnblogs.com/Robert-huge/p/5845490.html