Monday 25 July 2016

arcgis 10.0 - First attempt using ArcObjects: ESRI's Add-ins example unclear


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.
//

///Zooms to the selected layer in the TOC associated with the active view.

///
///An IMxDocument interface
///
///
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.



Answer



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!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...