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.