Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've come to realize that scripting ArcGIS 10.0 with Python imposes too many limitations for my goals with ArcGIS, so I've decided to bite the bullet and delve into ArcObjects.

Apparently, using ArcObjects in ArcGIS 10.0 has gotten somewhat easier because there is a new framework that avoids having to deal with COM objects. This seems good, but also means less examples for those starting at 10.0

The only detailed ESRI example of beginning with ArcObjects is a walkthrough of how to build an add-in. The instructions get somewhat vague in step 2a) and 3 of the section Adding ZoomToLayer functionality to the custom button:

< 2 > To implement ZoomToLayer functionality in your button, use the ArcGIS Snippet Finder tool.

  • a. Right-click the Visual Studio code editor window at the desired insertion point.
  • b, c, d

< 3 > Invoke the ZoomToLayer method upon OnClick() method implementation.

This is what my ZoomToLayerButton.cs file looks like before step 2:

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

namespace CustomUIElements
{
    public class ZoomToLayerButton : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public ZoomToLayerButton()
        {

        }


        protected override void OnClick()
        {
            ArcMap.Application.CurrentTool = null;
        }
        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }
    }

}

and here is the snippet I grabbed:

 #region"Zoom to Active Layer in TOC"
        // ArcGIS Snippet Title:
        // Zoom to Active Layer in TOC
        // 
        // Long Description:
        // Zooms to the selected layer in the Table of Contents (TOC) associated with the active view.
        // 
        // Add the following references to the project:
        // ESRI.ArcGIS.ArcMapUI
        // ESRI.ArcGIS.Carto
        // ESRI.ArcGIS.Geometry
        // 
        // Intended ArcGIS Products for this snippet:
        // ArcGIS Desktop (ArcEditor, ArcInfo, ArcView)
        // 
        // Applicable ArcGIS Product Versions:
        // 9.2
        // 9.3
        // 9.3.1
        // 10.0
        // 
        // Required ArcGIS Extensions:
        // (NONE)
        // 
        // Notes:
        // This snippet is intended to be inserted at the base level of a Class.
        // It is not intended to be nested within an existing Method.
        // 

        ///<summary>Zooms to the selected layer in the TOC associated with the active view.</summary>
        /// 
        ///<param name="mxDocument">An IMxDocument interface</param>
        ///  
        ///<remarks></remarks>
        public void ZoomToActiveLayerInTOC(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument)
        {
            if (mxDocument == null)
            {
                return;
            }
            ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView;

            // Get the TOC
            ESRI.ArcGIS.ArcMapUI.IContentsView IContentsView = mxDocument.CurrentContentsView;

            // Get the selected layer
            System.Object selectedItem = IContentsView.SelectedItem;
            if (!(selectedItem is ESRI.ArcGIS.Carto.ILayer))
            {
                return;
            }
            ESRI.ArcGIS.Carto.ILayer layer = selectedItem as ESRI.ArcGIS.Carto.ILayer;


            // Zoom to the extent of the layer and refresh the map
            activeView.Extent = layer.AreaOfInterest;
            activeView.Refresh();
        }
        #endregion

Regarding step 2.a)"Right-click the Visual Studio code editor window at the desired insertion point" where I should insert the snippet? Reading the comments in the snippet, I assume it should go after the first method:

public ZoomToLayerButton()
{

}


//here??

protected override void OnClick()
{
    ArcMap.Application.CurrentTool = null;
}

Also, the comments in the snippet also say I should:

// Add the following references to the project:
// ESRI.ArcGIS.ArcMapUI
// ESRI.ArcGIS.Carto
// ESRI.ArcGIS.Geometry 

Where do I add these? At the top of the ZoomTo LayerButton.cs file or my Config.Designer.cs file? (I have tried both of these locations and I get the same error: 'Carto' does not exist in the namespace ESRI.ArcGIS)

Regarding step 3, "Invoke the ZoomToLayer method upon OnClick() method implementation." I assume that the code shown later on after those instructions is what invokes the method:

protected override void OnClick()
{
    ZoomToActiveLayerInTOC(ArcMap.Application.Document as IMxDocument);
}

I assume this also means I should erase the line that was in there before?

ArcMap.Application.CurrentTool = null;

I have not yet gone on to the next step of the walkthrough - Installing the custom button and tool - since I have not got the code so far to build. I knew I was in for a steep learning curve with ArcObjects, and this intro example certainly confirms this! Hopefully, with these clarifications, I will have my first ArcObjects code running soon enough.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Some clarifications:

  • You are still using COM objects, the object model on which ArcObjects is built. The add-in framework merely simplifies the registration of your COM object extensions into component categories and streamlining the install/uninstall process.

    There are a few things that you can't do with add-ins that you can only do with traditional COM object extensions, but typically add-ins will suffice for most desktop customization type projects.

  • References are set per-project in Visual Studio. Currently added references are listed under the "References" folder in the solution explorer. Right click References and click Add, then, under the .NET tab (not the COM tab), add the necessary assemblies.

Other than that it looks like what you're doing should work, although you will want to read up on object-oriented programming fundamentals like classes, interfaces, implementation and inheritance, GUI and user interface design, separation of concerns, etc.

In addition you'll want to start learning as much as you can about the practical basics: the .NET Framework, the Visual Studio IDE, generics, and language syntax (see the C# specification).

Perhaps on a more advanced level, you will do well to learn the ins and outs of COM (particularly marshaling and reference counting), serialization, threading, P/Invoke and the Windows API (for hacking things that ArcObjects or the add-in framework didn't provide).

Of course if you have questions about most of that you would want to look at stackoverflow.com instead of gis.se!

share|improve this answer
    
It worked! many thanks! And great resources you linked to too. Just a few additional things I'm wondering i) is it better coding ethic to write: ZoomToActiveLayerInTOC(ArcMap.Application.Document as IMxDocument); - or - ZoomToActiveLayerInTOC(ArcMap.Application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument); and ii) the references that I had to link to where under the NET tab - the same libraries also exist in the COM tab. Could you point me to something I can read to understand why the two exist and when to use which? (will I always be using the NET tab for Add-ins?). Thanks again! –  GIStack Dec 13 '11 at 18:48
1  
For i) I would use the former as it's cleaner. You would only need to spell it out if there was potential for naming conflicts. For ii) the .NET libraries, called Primary Interop Assemblies, wrap the COM types in .NET types and handle the marshaling between the two environments, making it much easier to use in C#. I think you could use the COM libraries directly but not without a lot more hassle. There is some useful info in the help: help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/0001/… –  blah238 Dec 13 '11 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.