ArcMap中創建Extension的步驟分爲三步:
一.創建Extension。
1,首先在VS2008中,新建一個工程,採用ArcGIS Engine的Class Library模板,本處新建工程的名字是:ExtensionSample,選取工程存放的位置爲:D:\arcEngine。如下圖所示:點擊OK。
2,選擇AE(即ArcGIS Engine)中的所需要的引用命名空間,然後點擊Finish。
3.在生成的解決方案上,點擊右鍵->Add->Class,然後生成下面的界面,選擇ArcGIS下面的ArcGIS Class,此處新建類的名字是:customExtension。然後點擊Add,進入步驟4.
4.,在生成的下面界面裏,選擇如下橢圓所標示的(特別注意綠色橢圓的Extension),點擊Next。進入步驟5.
5,在生成的界面裏勾選ArcMapExtensions,然後點擊Next,進入步驟6
6.在生成的界面上選取:ESRI.ArcGIS.esriSystem.IExtensionConfig,然後點擊Finish。
7,主要修養修改的代碼爲:
(特別注意,此處自動生成的[Guid("f2fe0525-2e89-46a5-a5c1-2045c8d8081a")] ,下面的部分要用的。)
#region IExtensionConfig Members
private esriExtensionState m_extensionState;
private IApplication m_app;
public esriExtensionState State
{
get
{
UID u = new UIDClass();
u.Value = "{215036a4-1781-4b65-91b2-26221a5a1642}";
IDocument pDoc = m_app.Document;
ICommandBars pCmdBars = pDoc.CommandBars;
ICommandBar pCommandBar = pCmdBars.Find(u, false, false) as ICommandBar;
if (pCommandBar == null)
{
return esriExtensionState.esriESUnavailable;
}
if (m_extensionState == esriExtensionState.esriESEnabled)
{
pCommandBar.Dock(esriDockFlags.esriDockShow, pCommandBar);
}
else
{
pCommandBar.Dock(esriDockFlags.esriDockToggle, pCommandBar);
}
return m_extensionState;
}
set
{
m_extensionState = value;
}
}
public string Description
{
get
{
return "This is a sample extension that controls the state of a command.";
}
}
public string ProductName
{
get
{
m_app = initializationData as IApplication;
return "C# Sample Extension";
}
}
#endregion
#region IExtension Members
public string Name
{
get
{
// TODO: Add customExtension.Name getter implementation
return "customExtension";
}
}
public void Shutdown()
{
// TODO: Add customExtension.Shutdown implementation
}
public void Startup(ref object initializationData)
{
m_app = initializationData as IApplication;
}
#endregion
二,創建Toolbar。
1,在解決方案裏面點擊右鍵->Add->New Item…,如下選擇,生成的Toolbar名字ExtensionToolbar。點擊Add,進入,2.
2生成的界面如下,選擇Toolbar,點擊Next。
3.選擇ArcMap CommandBar。點擊Finish。
4.主要代碼如下:
public string Caption
{
get
{
// TODO: Add ExtensionToolbar.Caption getter implementation
return "Sample Extension Toolbar in C#";
}
}
public void GetItemInfo(int pos, ESRI.ArcGIS.SystemUI.IItemDef itemDef)
{
switch (pos)
{
case 0:
// use either the ProgID
itemDef.ID = "esriArcMapUI.AddDataCommand";
break;
case 1:
// or use the GUID
itemDef.ID = "{ea33bbe6-b893-41b9-b065-5530428ed2cf}";//此處的ID,爲後面創建的command的ID。
break;
}
// TODO: Add ExtensionToolbar.GetItemInfo implementation
}
public int ItemCount
{
get
{
// TODO: Add ExtensionToolbar.ItemCount getter implementation
return 2;
}
}
public string Name
{
get
{
// TODO: Add ExtensionToolbar.Name getter implementation
return "ExtensionToolbar";
}
}
#endregion
3.生成自制的command.
1. 在解決方案裏面點擊右鍵->Add->New Item…如下,點選Base Command模板,command的名字爲:ExtensionCommand.然後點擊Add。
2在生成的界面上點擊:ArcMap,MapControl or PageLayoutControl Command,然後點擊OK。
3主要的代碼如下:
private IApplication m_application = null;
private IMxDocument m_mxDocument = null;
private IMap m_map = null;
private IExtensionConfig m_extensionConfig = null;
public ExtensionCommand()
{
//
// TODO: Define values for the public properties
//
base.m_category = "Developer Samples"; //localizable text
base.m_caption = "Sample Extension Command"; //localizable text
base.m_message = "Command that is enabled and disabled by the sample extension."; //localizable text
base.m_toolTip = "Sample Extension Command"; //localizable text
base.m_name = "DeveloperSamples_ExtensionCommand"; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overriden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
try
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.ActiveView == null)
m_hookHelper = null;
}
catch
{
m_hookHelper = null;
}
if (m_hookHelper == null)
base.m_enabled = false;
else
base.m_enabled = true;
m_application = hook as IApplication;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
if (m_application != null)
{
m_mxDocument = m_application.Document as IMxDocument;
m_map = m_mxDocument.FocusMap;
Marshal.ReleaseComObject(m_mxDocument);
IFeatureLayer featureLayer = null;
IEnvelope envelope = null;
// Report the Xmin and YMin of the extent of the first layer in the map
featureLayer = m_map.get_Layer(0) as IFeatureLayer;
envelope = featureLayer.AreaOfInterest;
MessageBox.Show("XMin: " + envelope.XMin + "\nYMin: " + envelope.YMin);
envelope = null;
featureLayer = null;
}
}
public override bool Enabled
{
get
{
bool enabledState = false;
if (m_application != null)
{
m_mxDocument = m_application.Document as IMxDocument;
m_map = m_mxDocument.FocusMap;
Marshal.ReleaseComObject(m_mxDocument);
// Get the extension
UID extensionUID = new UIDClass();
extensionUID.Value = "{f2fe0525-2e89-46a5-a5c1-2045c8d8081a}";//此處的ID就是Extension的ID。
m_extensionConfig = m_application.FindExtensionByCLSID(extensionUID) as IExtensionConfig;
if (m_extensionConfig != null)
{
// Command is enabled only if the extension is turned on and there is data in the map
if ((m_extensionConfig.State == esriExtensionState.esriESEnabled) && (m_map.LayerCount > 0))
enabledState = true;
else
enabledState = false;
}
else
enabledState = false;
}
return enabledState;
}
}
#endregion
}
然後用戶變可以在ArcMap的Tools->Extension中看到所創建的Extension了,如下圖:
Enjoy it……
http://www.cnblogs.com/Medicine/archive/2012/02/23/ArcMap_Extension_PlugInProgram.html