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……