基於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開發之修改工具欄中的按鈕圖標

再添加一個按鈕的配置文件:

    <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控件的方法

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

我們要插入一個Combox 其id爲 【光桿司令一個_Combox】,我們要將該空間插入到工具集中:

<Items>
            <Item refID="光桿司令一個_ArcMapAddin1_ArcGISAddinButton1" />

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

你想將空間插入到什麼位置,就放在什麼位置;<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;
  }
}

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中,[自定義] -- [加載項管理] -- [加載項],選擇需要卸載的拓展即可;重啓後,組件將消失


更多精彩,歡迎關注以下公衆號

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章