Added your method to the code behind for a simple button Add-In. Added the appropriate references, for example IMap is an interface that comes from an ESRI provided assembly (ESRI.ArcGIS.Carto.dll):
IMap (from ESRI Carto)
Added the following references through Project > Add Reference in Visual Studio:
ESRI.ArcGIS.Carto
ESRI.ArcGIS.Geodatabase
ESRI.ArcGIS.Display
Now on the buttons click we want to call this method passing in the appropriate arguments. With Add-ins we have access to a static class called ArcMap. This will represent our initial entry point as with 'ThisDocument', for example, with VBA. Here is an example of calling the method:
protected override void OnClick()
{
ConvertLabelsToAnnotationSingleLayerMapAnno(ArcMap.Document.FocusMap, 0);
....
The code will pass the Active map (dataframe) and the value of 0 to your method. This tells it to process on the first layer in the given map.
The labels are sucessfully converted to Map annotation in a new annotation group.
Oh my goodness, thank you for showing me the light, jmhauck!I had it set up correctly, and the main thing that I just COULD NOT figure out was how to pass that pMap variable into the function during the onClick event! All I needed was the "ArcMap.Document.FocusMap" part!!Thank you again!And for all searchers in the future, this is my code to create a custom button which lets the user convert labels to annotation at the click of a button!
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
namespace ArcMapAddin2
{
public class LabelToAnno : ESRI.ArcGIS.Desktop.AddIns.Button
{
public LabelToAnno()
{
}
protected override void OnClick()
{
//
// TODO: Sample code showing how to access button host
//
ConvertLabelsToAnnotationSingleLayerMapAnno(ArcMap.Document.FocusMap, 0);
ArcMap.Application.CurrentTool = null;
}
protected override void OnUpdate()
{
Enabled = ArcMap.Application != null;
}
static void ConvertLabelsToAnnotationSingleLayerMapAnno(IMap pMap, int layerIndex)
{
IConvertLabelsToAnnotation pConvertLabelsToAnnotation = new
ConvertLabelsToAnnotationClass();
ITrackCancel pTrackCancel = new CancelTrackerClass();
//Change global level options for the conversion by sending in different parameters to the next line.
pConvertLabelsToAnnotation.Initialize(pMap,
esriAnnotationStorageType.esriMapAnnotation,
esriLabelWhichFeatures.esriVisibleFeatures, true, pTrackCancel, null);
ILayer pLayer = pMap.get_Layer(layerIndex);
IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
if (pGeoFeatureLayer != null)
{
IFeatureClass pFeatureClass = pGeoFeatureLayer.FeatureClass;
//Add the layer information to the converter object. Specify the parameters of the output annotation feature class here as well.
pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer,
pGeoFeatureLayer.Name + "_Anno", null, null, false, false, false, false,
false, "");
//Do the conversion.
pConvertLabelsToAnnotation.ConvertLabels();
//Turn off labeling for the layer converted.
pGeoFeatureLayer.DisplayAnnotation = false;
//Refresh the map to update the display.
IActiveView pActiveView = pMap as IActiveView;
pActiveView.Refresh();
}
}
}
}
Upon clicking this, the first layer (Indexed at 0) in the Table of Contents (TOC) will be converted to annotations!