Select to view content in your preferred language

Convert labels to annotation

4596
7
05-07-2010 08:02 AM
RDHarles
Regular Contributor
Hi,

In an automated process, I need to label shapefiles or feature classes in a File Geodatabase, then convert those labels to annotation as it's own layer.

I'm a python programmer who knows little about ArcObjects.  This is the one small part of my "production line" that I can't accomplish using python and ESRI tools.  I'm hoping someone can help me out with some code so that I can plug this part into my python script.

Thanks for any help you can give!
0 Kudos
7 Replies
LaimonasLiskauskas
Occasional Contributor
Hi, do you have any luch with solving your task? Thanks in advance. Regards, Laimonas.
0 Kudos
AndrewMay
Emerging Contributor
Here's a VBA example which does what you want:

http://help.arcgis.com/en/sdk/10.0/vba_desktop/conceptualhelp/index.html#/How_to_convert_labels_to_s...

I'm not sure whether you can use this or not, or how you interface with your Python script, but it uses the IConvertLabelsToAnnotation interface.

Hope this helps.
0 Kudos
RDHarles
Regular Contributor
Thanks for posting, Andrew.  I haven't had much luck editing VBA (I know very little about it) and then calling it from python, but it's probably worth another look.



Laimonas,
I was able to solve my task, but certainly not the way I wanted to do it.

These are the steps I ended up taking:
1.)  I had to convert all my shapefiles to ARC/INFO coverages:
gp.FeatureClassToCoverage_conversion()

2.) Then call an AML from python to create anno in each coverage:
cmd = 'arc " &r "'+amlPath+'annopbs.aml"'
subprocess.Popen(cmd, shell=True).wait()

3.) Then have python write AutoCAD scripts to "dxf in" the layers/annotation and make them look right (colors, layer names, etc.)

...not pretty but it worked.



At the ESRI Conference in SD this year, I was told by staff that you could do it like this, but I never got it to work:
1.) convert shapefiles to file geodatabase
2.) open up an mxd template (with the layers already set) with the labels on.
3.) use the tool "Tiled Labels To Annotation" to covert labels to their own annotation layer. {never got this to work correctly}
4.) use the tool "Export to CAD"

Please post back it you come up with a good solution.  I always looking for an upgrade to the way I ended up doing it!
Good luck.
0 Kudos
MLowry
by
Frequent Contributor
I need to do exactly what is in the title of this thread by creating a Custom UI Button that I will add to the toolbar in ArcMap. I want to click on the custom button and it will automatically convert the labels to annotation of the labels that are displayed in the data frame.

I have the C# ArcObjects code I need from the SDK, which is the following:



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();
    }
}




All I need to figure out now is how to get that code to run when I click on the custom UI button.

I have used the samples and gotten them to work but I am having trouble linking the code above to the OnClick event and passing those parameters (pMap and layerIndex) to the function. Can someone please help me?
0 Kudos
JohnHauck
Frequent Contributor
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.
0 Kudos
MLowry
by
Frequent Contributor
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!
0 Kudos
robertfortin1
New Contributor
import arcpy
0 Kudos