基于C#的ArcEngine二次开发教程20:ArcMap Addins 开发的标准流程全解

ArcGIS的插件式开发,可以帮助开发人员对ArcGIS相关产品的现有作业流程进行人工干预,以达到作业效率提升的目的。

近来由于业务需要,本人自学ArcMAP的二次开发,积累浅薄的经验给大家分享,帮助大家快速入门AddIn二次开发快速入门。

注意:本文基于ArcMap10.1和Visual Studio 2010开发实践,相关操作展示。

1 新建工程项目及其注意事项

1.1 以工具集的方式创建项目

点击 [确定] 之后,进入欢迎界面,填写【工具集名称】、【所属单位】、【作者信息】和【工具集描述信息】

之后,直接点 [Fnish],这里不要点Next进行6种插件选择;这么做的优势在于以下两点:

  1.   插件最好是放在自己定义的工具集中会比较好找一些
  2. 如果你定义了单个控件,需要在手动叫将插件拖入到ArcMap的工具栏中;直接采用工具栏的开发方式,可以像正常的ArcMap工具集一样使用

1.2 项目配置文件解析

完成1.1部分的项目创建操作之后,打开 [解决方案管理器]中的Config.esriaddinx文件,内容如下所示:

<ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>我的ArcMap拓展工具集</Name>
  <AddInID>{db88469e-f3b4-457a-9523-2d439039838d}</AddInID>
  <Description>这是一个面向ArcMap的AddIns开发入门流程介绍程序</Description>
  <Version>1.0</Version>
  <Image>Images\ArcMapAddin1.png</Image>
  <Author>天才</Author>
  <Company>光杆司令一个</Company>
  <Date>2020/2/26</Date>
  <Targets>
    <Target name="Desktop" version="10.1" />
  </Targets>
  <AddIn language="CLR4.0" library="ArcMapAddin1.dll" namespace="ArcMapAddin1">
    <ArcMap />
  </AddIn>
</ESRI.Configuration>

这个文件中记录了开发工具集的配置信息,具体说明如下:

  1. 包含之前设置的Name, AddInID, Description, Image, Author, Company,Date等信息
  2. Targets信息记录了这个工具集是针对ArcDesktop 2010开发的
  3. CLR4.0表示是在.Net 4.0下开发的,这一点对调试很重要

1.2.1 The 'language' attribute in the AddIn element is invalid

如果.Net版本不是4.0报错如下:

警告    1    自定义工具警告: The 'language' attribute in the AddIn element is invalid - The value 'CLR4.0'  is invalid according to the project's target framework version - The value should be 'CLR'.    E:\C#\Doctor\ArcCatalogAddin1\ArcCatalogAddin1\Config.esriaddinx    13    10    ArcCatalogAddin1

此时的解决方案是,点击[项目名称] -- [右键属性] -- [应用程序] -- [目标框架] -- 选择 [.NET fRAMEWORK 4.0],弹框选择 [是] 即可

1.2.2 调试是遇到【当前不会命中断点,还没有为该文档加载任何符号】的处理方法

详见之前的博客:

基于C#的ArcEngine二次开发教程18:ArcGIS addIns 调试【当前不会命中断点,还没有为该文档加载任何符号】问题处理办法

2 新建组件与容器

组件是单一的按钮,容器可以存放许多种组件

2.1 组件的创建

2.1.1 添加Button组件

2.1.2 配置文件解析

再次查看刚才的配置文件,你会发现多了如下内容,你每添加一个组件,就会在这里多一行关于你添加按钮的描述信息

<ArcMap>
      <Commands>
        <Button id="光杆司令一个_ArcMapAddin1_ArcGISAddinButton1" class="ArcGISAddinButton1" message="关于按钮的描述,不要也行" caption="测试按钮1" tip="鼠标驻留到该按钮会显示的信息" category="Add-In Controls" image="Images\ArcGISAddinButton1.png" />
      </Commands>
    </ArcMap>

说明:

  1. id是按钮的ID,由公司名 + 你的项目名称 + 按钮类名构成
  2. classs是设置的按钮类的名字
  3. message是刚才填写的Descrption中的信息;注意Message中使用英文分号 [; ] 可以换行
  4. Caption, 按钮的文本名称
  5. tip,按钮的提示文本
  6. category为归属类别
  7. Image为按钮的显示图标,如要修改,参见博文基于C#的ArcEngine二次开发教程19:Addin开发之修改工具栏中的按钮图标图标属性务必设置为[始终拷贝]和[AddInContent]

再添加一个按钮的配置文件:

    <ArcMap>
      <Commands>
        <Button id="光杆司令一个_ArcMapAddin1_ArcGISAddinButton1" class="ArcGISAddinButton1" message="关于按钮的描述,不要也行" caption="测试按钮1" tip="鼠标驻留到该按钮会显示的信息" category="Add-In Controls" image="Images\ArcGISAddinButton1.png" />
        <Button id="光杆司令一个_ArcMapAddin1_ArcGISAddinButton2" class="ArcGISAddinButton2" message="Add-in command generated by Visual Studio project wizard." caption="测试按钮2" tip="Add-in command tooltip." category="Add-In Controls" image="Images\ArcGISAddinButton2.png" />
      </Commands>
    </ArcMap>

此处应注意:如果你手动删除一个组件,这里的配置文件是不会自动删除的,确需删除,需要手动操作

2.1.3 增加代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ArcMapAddin1
{
    public class ArcGISAddinButton2 : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public ArcGISAddinButton2()
        {
            //类的构造函数,填写加载按钮需要设置的信息,可是更改按钮的图标等操作
            //图标最好做成资源文件,这样避免索引不到图标路径,导致ArcMap异常崩溃
        }

        protected override void OnClick()
        {
            MessageBox.Show("测试按钮2的被点击");
        }

        protected override void OnUpdate()
        {
            MessageBox.Show("测试按钮2被更新");
        }
    }
}

关于OnUpdate的解释:【主要功能是确定控件属性的可用性;如果按钮被按下,将会被一直更新】

The OnUpdate method is called periodically by the framework once the control has been created. This provides an opportunity to run some code periodically within your customization. One typical use of OnUpdate is to determine and set the Enabled property of the control. Note that as OnUpdate is called frequently you should avoid lengthy operations in this method, as this would reduce the responsiveness of the application user interface.

2.2 添加容器

添加容器的目的是将多个组件统一到一个容器中,实现功能的集成和分组;

例如,我要通过按钮来干预编辑器拓展的情形,那就必须要按钮和编辑器两个组件协同工作,此时就要使用到容器,会很方便

选择组件

完成后,查看配置文件,会增加如下信息:

      <Toolbars>
        <Toolbar id="光杆司令一个_ArcMapAddin1_My_Toolbar" caption="My Toolbar" showInitially="true">
          <Items>
            <Item refID="光杆司令一个_ArcMapAddin1_ArcGISAddinButton1" />
            <Button refID="光杆司令一个_ArcMapAddin1_ArcGISAddinButton2" separator="true" />
          </Items>
        </Toolbar>
      </Toolbars>

2.3 向已有工具栏添加Combox控件的方法

2.3.1 添加控件到工具条中

如果还想在该容器下增加新的Add-in工具,直接在上边的<Items> ...</Items>中添加插件refID即可,如:

我们要插入一个Combox 其id为 【光杆司令一个_Combox】,我们要将该空间插入到工具集中:

<Items>
            <Item refID="光杆司令一个_ArcMapAddin1_ArcGISAddinButton1" />

            <ComboBox refID="光杆司令一个_Combox" />
            <Button refID="光杆司令一个_ArcMapAddin1_ArcGISAddinButton2" separator="true" />
</Items>

对于添加separator="true"会在该按钮的前边加一个分割线

你想将空间插入到什么位置,就放在什么位置;<Items> ...</Items>的顺序与空间在工具条中的顺序是一致的

自编示例代码:

  1. 向ComboBox添加项目
  2. 指定默认的项目
  3. 获取当前选中的项目
public Combo1()
{
    int c1 = this.Add("苹果");
    this.Add("香蕉");
    this.Add("梨子");
    this.Select(c1);
    selectedStr = this.GetItem(c1);
}
public static string selectedStr;
protected overwrite void OnSelChange(int Cookie)
{
    selectedStr = this.getItem(cookie).Caption;//获取选中项的名字
}

官方示例代码:

publicclass Combo1 : ESRI.ArcGIS.Desktop.AddIns.ComboBox
{
  public Combo1()
  {
    //Add two items to the combo box.
    Point o1 = new Point();
    o1.PutCoords(0, 0);
    int c1 = this.Add("Item1", o1);

    Point o2 = new Point();
    o2.PutCoords(1, 1);
    int c2 = this.Add("Item2", o2);

    //Add the application's caption.
    ESRI.ArcGIS.Framework.IApplication app = this.Hook as ESRI.ArcGIS.Framework.IApplication;
    this.Add(app.Caption);

    //Add one item then removeint c3 = this.Add("Item3");
    this.Remove(c3);

    //Select the second item.this.Select(c2);
  }

  protectedoverridevoid OnSelChange(int cookie)
  {
    if (cookie == -1)
      return;

    //Get the associated object.
    Point tag = this.GetItem(cookie).Tag as Point;
    if (tag != null)
    {
      System.Windows.Forms.MessageBox.Show(tag.X + ", " + tag.Y);
    }
  }

  protectedoverridevoid OnEnter()
  {
    //Loop through the item collection.foreach (ESRI.ArcGIS.Desktop.AddIns.ComboBox.Item item inthis.items)
    {
      if (this.Value == item.Caption)
        return;
    }
    this.Add(this.Value);
  }

  protectedoverridevoid OnEditChange(string editString)
  {
    if (string.Compare(editString, "ABC", true) == 0)
    {
      System.Windows.Forms.MessageBox.Show("editString is " + this.Value);
    }
  }

  protectedoverridevoid OnFocus(bool set)
  {
    if (set)
      System.Diagnostics.Debug.WriteLine("Get focus.");

    if (!set)
      System.Diagnostics.Debug.WriteLine("Lose focus.");
  }

  protectedoverridevoid OnUpdate()
  {
    this.Enabled = ArcMap.Application != null;
  }
}

2.3.2 添加拓展控件

如果你添加的是拓展控件,配置文件中可以看到如下内容:

<!--加入编辑器拓展控件-->
<Editor>
    <Extensions>
     <Extension id="你的拓展ID" class="编辑器拓展控件类名" />
     </Extensions>
</Editor>

如果你的拓展是自己定义的,上述信息是可以自动生成的;但是如果你要整合别人的代码,就需要在配置文件中手动配置该内容,否则你的拓展控件将无法使用。

2.3.3 其他插件配置文件概览

  • DockableWindows

关于悬浮窗的配置信息,记录在<DockableWindows> ... </DockableWindows>中

<DockableWindows>
        <DockableWindow id="ArcMapAddin2_ArcGISAddin1" class="ArcGISAddin1+AddinImpl" caption="My Dockable Window" image="Images\ArcGISAddin1.png">
          <InitialPlacement height="300" width="300" state="pinned" position="left" />
        </DockableWindow>
</DockableWindows>
  • 编辑器拓展
<Editor>
        <Extensions>
          <Extension id="ArcMapAddin2_controls1" class="controls1" />
        </Extensions>
</Editor>
  • MultiItem / Tool
<Commands>
        <MultiItem id="ArcMapAddin2_Controls3" class="Controls3" message="提示信息" caption="项目标题" />
        <Tool id="ArcMapAddin2_Tool1" class="Tool1" message="提示信息,一般写使用帮助" caption="My Tool" tip="Add-in command tooltip." category="Add-In Controls" image="Images\Tool1.png" />
      </Commands>

这里提供一个拓展思路,如果要集成的工具死活调用不起来时,你可以建立一个临时的工程,把你这个拓展新建以下,不写任何代码;看看配置文件有何变化,然后再对比一下你整合工程是不是缺这些文件,这样的话,就可以避免配置文件的问题导致一些意想不到的错误。

3 调试与发布

3.1 调试

3.1.1 启动外部程序调试

选择ArcMap的启动路径:ArcGIS安装路径的bin目录下,我的是D:\Program Files (x86)\ArcGIS\Desktop10.1\bin\ArcMap.exe

3.1.2 附加到进程

打开ArcGIS,然后点击 调试 --> 附加到进程;选择 ArcMap.exe

 

3.1.3 设置和修改.Net 4.0

分两步:

第一步:[项目名称] -- [右键属性] -- [应用程序] -- [目标框架] -- 选择 [.NET fRAMEWORK 4.0],弹框选择 [是] 即可

第二步:修改ArcMap的配置文件,同样是在bin目录下,找到ArcMap.exe.config文件

注释调2.0,解除4.0的注释即可

做完上述操作之后,添加断点,点击调试,进入ArcGIS,会看到如下信息:

 

3.2 发布

在ArcMapAddin1\ArcMapAddin1\bin\Debug目录下,

会在我的文档\ArcGIS\AddIns\Desktop10.1目录下,生成如下文件夹:

文件夹中的文件

3.3 卸载

方法一:直接删除之前刚才提到的文件夹

方法二:在ArcMap中,[自定义] -- [加载项管理] -- [加载项],选择需要卸载的拓展即可;重启后,组件将消失


更多精彩,欢迎关注以下公众号

 

  • 5
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值