博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Arcgis Add-In开发入门实例(转载)
阅读量:4701 次
发布时间:2019-06-09

本文共 4019 字,大约阅读时间需要 13 分钟。

转自:http://blog.csdn.net/gisshixisheng/article/details/17284383

作为一个本科侧重于应用,工作之后却做了开发的程序员来说,做GIS,开发应该是一门必修课,只是,苦于各种原因吧,做GIS应用的人会开发的很少,做GIS开发的大部分都是计算机出身,痛心疾首啊……

 

不好意思,刚开始,就唠叨两句,还望大家见谅。在Arcgis10的版本之后,ESRI公司推出了很方便的Add-In插件式开发,下面就Add-In做一个简单的介绍:

 

Add-In是一种能够快速扩展桌面应用程序功能的全新扩展方式。

作为ArcGIS 10中全新定制方法,它有以下特点:

(1)容易创建:ArcGIS提供了很多创建Add-In模板,并提供了详细的接口以实现各种功能。

(2)更易共享:Add-In本质上是一个Zip压缩文件这个压缩文件里面包含了,易于网络、邮件传输、易于局域网内共享。

(3)更加安全:可以对Add-In文件进行数字签名,使用的安全性得到保障。

(4)更易安装管理:只要安装有桌面软件,系统就能自动识别Add-In文件,双击即可安装部署,同时desktop也有用于管理Add-In的管理工具。

ArcGISAdd-In支持的类型

 按钮和工具

 组合框

 菜单和右键菜单

 Multi-items

 工具条

 Tool palettes

 可停靠窗体

Application extensions

Editor extensions

 

在做Add-In开发需要安装ArcObjects for .net的开发包。下面就详细的介绍一个简单的例子:在Arcmap界面添加一个按钮,按钮对应的事件为添加shp数据。

 

1、开发环境

Vs2010+Arcgis10.0+win8 64bit

 

2、实现代码

首先在VS2010中新建一个解决方案,命名AddInTest。

 

接着,给解决方案AddInTest新建一个项目:

点击[确定],出现如下界面,直接点击[finish]。

接着,给项目ArcMapAddinTest新建一个项目

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

上图中,class name是类的名称,caption是button显示的文字,category是所属的command的分类,tooltip是鼠标在上面时状态栏显示的文字,description是工具的描述。

 

项目建成后,文件组织如下图:

Config.esriaddinx是一个XML文件,是一个配置文件,里面包含了项目的相关配置,是自动生成的,内容如下:

 

[html]   
 
 
  1. <ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  2.   <Name>ArcMapAddinTest</Name>  
  3.   <AddInID>{0f7ec41b-d1e3-4391-8d67-9dea10bea621}</AddInID>  
  4.   <Description>Type in a description for this Add-in.</Description>  
  5.   <Version>1.0</Version>  
  6.   <Image>Images\ArcMapAddinTest.png</Image>  
  7.   <Author>Administrator</Author>  
  8.   <Company></Company>  
  9.   <Date>2013/12/12</Date>  
  10.   <Targets>  
  11.     <Target name="Desktop" version="10.0" />  
  12.   </Targets>  
  13.   <AddIn language="CLR" library="ArcMapAddinTest.dll" namespace="ArcMapAddinTest">  
  14.     <ArcMap>  
  15.       <Commands>  
  16.         <Button id="ArcMapAddinTest_AddShp" class="AddShp" message="点击浏览shp文件并添加" caption="AddShp" tip="实现添加shp文件的功能" category="Add-In Controls" image="Images\AddShp.png" />  
  17.       </Commands>  
  18.     </ArcMap>  
  19.   </AddIn>  
  20. </ESRI.Configuration>  

新建完成之后,addshp的内容如下:

 

 

[csharp]   
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.IO;  
  5.   
  6.   
  7. namespace ArcMapAddinTest  
  8. {  
  9.     public class AddShp : ESRI.ArcGIS.Desktop.AddIns.Button  
  10.     {  
  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文件的代码。

 

首先,添加如下引用:

 

[csharp]   
 
 
  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方法里,事件的实现代码如下:

 

 

[csharp]   
 
 
  1. <span style="white-space:pre">  </span>IMxDocument pMxd;  
  2.         public Button1()  
  3.         {  
  4.             pMxd = ArcMap.Document as IMxDocument;  
  5.         }  
  6.   
  7.         protected override void OnClick()  
  8.         {              
  9.             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 host  
  35.             //  
  36.             ArcMap.Application.CurrentTool = null;  
  37.         }  

OnUpdate方法事件的代码如下:

 

 

[csharp]   
 
 
  1. <span style="white-space:pre">  </span>protected override void OnUpdate()  
  2.         {  
  3.             Enabled = pMxd.FocusMap.LayerCount >= 0;  
  4.         }  

代码编写完成后,编译程序,打开编译目录,编译文件如下:

 

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

点击[close],点点试试……

转载于:https://www.cnblogs.com/Joetao/articles/5697242.html

你可能感兴趣的文章
Stanford Local Programming Contest 2011
查看>>
多线程中,NSOperationQueue和GCD的区别
查看>>
python生成.exe文件
查看>>
PHP面向对象(OOP)----分页类
查看>>
监听SD卡状态
查看>>
vs2017 EFCore 迁移数据库命令
查看>>
serialVersionUID的作用
查看>>
liunx trac 插件使用之GanttCalendarPlugin
查看>>
(14)嵌入式软件开发工程师技能要求总结
查看>>
[hackerrank]Closest Number
查看>>
volatile关键字
查看>>
[Android] TabLayout设置下划线(Indicator)宽度
查看>>
<潭州教育>-Python学习笔记@条件与循环
查看>>
web自动化之验证码识别解决方案
查看>>
netty接收大文件的方法
查看>>
软件工程设计之四则运算
查看>>
SpringMVC @ResponseBody 406
查看>>
HDOJ---2824 The Euler function[欧拉函数]
查看>>
KMP算法
查看>>
Atlas学习之开始篇[转]
查看>>