ArcObjects Tutorial II: Add Command to Toolbar

Welcome to second walkthrough on adding custom ArcGIS component (command) to standard tool bar in ArcMap. We have successfully created custom command using C#; adding this command to ArcMap standard toolbar through ESRI IExtension.

 Tools & Language: Visual Studio 2005, C#.NET, ArcGIS 9.2

Manually we can add the command using ArcMap Tools–>Customize–>Click Command Tab, Choose the category and drag the command to toolbar. If we want to save this command permanently in ArcMap; use ‘Save In’ option to ‘Normal.mxt’ . The command will be saved on template.  

The purpose of this walkthrough is to add the custom command to ESRI toolbar programmatically.  ESRI has provided option to develop Application Extension inheriting ESRI IExtension. By inheriting IExtension class we can create our own extension and add the extension in categories manager. Applications loads extension AUTOMATICALLY on Startup. Life cycle of loading extension is a) Application starts b) Extension are loaded c) Document is loaded  d)  Document is closed e) Extensions are unloaded .

There are two types of extension they are Visible and Invisible (present walkthrough). Visible extension inherits IExtensionConfig and it will be added in Extension Manager Window. Looks like other ESRI extension along with its description.

Once you added this class, new class ‘ApplicationExtension1.cs’ will create by inheriting base class IExtension. This class contains functions to Register COM components (Register and UnRegister) and IExtension members like Extension name (Property-String) , Startup, Shutdown methods.

Our objective is to add the developed custom command to existing standard toolbar, i.e. whenever we open Map Document is opened, and command should be added in the toolbar. For achieving this, we have add delegate (IDocumentEvent_NewDocumentEventHandler) and pass a method to be fired on startup.

public void Startup(ref object initializationData)

  {

           //m_application is ArcMap Application

            m_application = initializationData as IApplication;

            if (m_application == null)

                return;

                 IMxDocument pMxDoc;       

            // pMxDoc is ArcMap Map Document 

            pMxDoc = (IMxDocument)m_application.Document;          

 // Creating Event Handler with a Method ’onNewOpenDoc’          

((IDocumentEvents_Event)pMxDoc).NewDocument += new IDocumentEvents_NewDocumentEventHandler(OnNewOpenDoc);

  } 

 

private void OnNewOpenDoc()

{

ICommandBar pCmdBar; 

//Create two new UID objects from UID class

UID pUID = new UID();

UID pCommandUID=new UID(); 

//pUID value is ArcMap Standard ToolBar UID Value  taken from here

 

pUID.Value = “{5DEB1DB8-C2A9-11D1-B9A2-080009EE4E51}”; 

pCmdBar= ICommandBar)m_application.Document.CommandBars.Find(pUID, false, false); 

 

// pCommandUID value is custom command UID value found in the class //created

 

pCommandUID.Value = “{a657d37d-5cdd-4315-8866-87e389f2ffb8}”;

object index=0; 

// Note object called index is set to ‘0′; command will be added as first //command in toolbar 

pCmdBar.Add(pCommandUID,ref index); 

}

In ‘OnNewOpenDoc’ method we have to write code for adding the custom command to toolbar.  This method gets fired on opening every new ArcMap Instance.

 

Build and run the project. Now you can notice your custom command appeared as first item in a standard toolbar (make visible, if not) of ArcMap.   

 

I hope that this tutorial will be of much help to beginner who starts programming in ArcObjects. I will add some useful external links to this post. 

 

 

Attachment: extending-arcgisnet  PDF  

Advertisements

Tags: , ,

2 Responses to “ArcObjects Tutorial II: Add Command to Toolbar”

  1. Beheen Trimble Says:

    I love your posts. Although I am not a VBA/C# programmer, your post helps me to develope in Java. Thanks very much.

    I heard, we can save state(s) of our extensions and the Addins in our arcmap document that when we click on our map document, the saved states puts the map where it was left off! I am not sure if I am describing this right or not and I don’t know how useful this feature, if exists, is for one’s program! Would you please touch on this if it makes any sense to you?

    Regards
    Beheen Trimble

  2. ImranR Says:

    can you please tell me how to toggle visibility of a toolbar using java sdk, my plugin has one main toolbar but I wish to show more

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: