TitlePages

Wednesday, 19 November 2014

ESRI Addin Button to Zoom to Selected Layer in TOC

Software Required:
  1. Microsoft Visual Studio 2010 +
  2. ArcMap 10.2 or more
  3. ArcObjects SDK for .Net Installed

Procedure:

  • Open Visual Studio 2010
  • Create a New Project > Visual C#> ArcGIS>Desktop Addins >ArcMap Add In
  • ArcMap Addin Wizard will appear on the Screen.Give a desired name for your Addin and click Next
  • In the Addin Types Enable Button checkbox and provide description for it.This will appear as a button tooltip in your ArcMap application.Click finish to end the wizard.
  • Now go the Solution Explorer(View>Soln Explorer) and open the Addinname.cs file if it doesn't appear in your screen.
  • Replace the content of the file with the following data on the onclick() method of the button.cs file.
  • Add the References(Right click references in Solution Explorer and select Add References) based on your classes usage
C#

protected override void OnClick()
        {
IMxDocument mdoc = ArcMap.Application.Document as IMxDocument;

//the following two lines of code are the same
IActiveView activeView = mdoc.ActiveView;
//IActiveView activeView = mdoc.FocusMap as IActiveView;
//get the selected layer in the Table of Contents
ILayer lr = mdoc.SelectedLayer;
//check if there is a selected layer or not
if (lr != null)
{
activeView.Extent = lr.AreaOfInterest;
//invoking Refresh method
//results in redrawing the whole view
activeView.Refresh();
}
}
  • Save your Current project .Don't run your Code.
  • Go to Properties>Addinname Properties>Debug option.Set your ArcMap as the External program for debugging the code
  • Run the Code.
  • ArcMap Application will now open.Go to Customize>Customize mode>Command.Right now you will see your Addin name in the category list click on it, respective button will get displayed on the rightcommands window.
  • Drag the Button and put it in the Menu bar.
  • You get the ESRI Addin file bin>Debug folder of your project location
  • Now its party time :) Share your work with other through the Addin file

No comments: