Labels to Annotation in another databse

793
5
12-09-2017 10:51 PM
SibghatUllah1
Occasional Contributor

I am trying to convert labels to annotation by using following code.It is working fine and creating annotation in the feature layers database but i want to store in other Geodatabase.

I have tried by adding string path of my geodatabase as a workspace but it is not working.Convert Annotation

string fgdb= folderPath + "\\Test.gdb;

IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(fgdb, 0);

 

static void ConvertLabelsToGDBAnnotationEntireMap(IMap pMap, bool featureLinked)
{
    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.esriDatabaseAnnotation,
        esriLabelWhichFeatures.esriVisibleFeatures, true, pTrackCancel, null);
    IUID pUID = new UIDClass();
    pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; 
        //IGeoFeatureLayer interface ID.
    IMapLayers pMapLayers = pMap as IMapLayers;
    IEnumLayer pEnumLayer = pMapLayers.get_Layers(pUID as UIDClass, true);
    pEnumLayer.Reset();
    IGeoFeatureLayer pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;

    while (pGeoFeatureLayer != null)
    {
        if (pGeoFeatureLayer.Valid == true)
        {
            if (pMapLayers.IsLayerVisible(pGeoFeatureLayer as ILayer))
            //Takes scale and groups layers into account.
            {
                if (pGeoFeatureLayer.DisplayAnnotation == true)
                {
                    IFeatureClass pFeatureClass = pGeoFeatureLayer.FeatureClass;
                    IDataset pDataset = pFeatureClass as IDataset;
                    IFeatureWorkspace pFeatureWorkspace = pDataset.Workspace as
                        IFeatureWorkspace;

                    //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", pFeatureWorkspace,
                        pFeatureClass.FeatureDataset, featureLinked, false, false,
                        true, true, "");
                }
            }
        }
        pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;
    }
    //Do the conversion.
    pConvertLabelsToAnnotation.ConvertLabels();
    IEnumLayer pAnnoEnumLayer = pConvertLabelsToAnnotation.AnnoLayers;
    //Turn off labeling for the layers converted.
    pEnumLayer.Reset();
    pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;
    while (pGeoFeatureLayer != null)
    {
        if (pGeoFeatureLayer.Valid == true)
        {
            if (pMapLayers.IsLayerVisible(pGeoFeatureLayer as ILayer))
            //Takes scale and groups layers into account.
            {
                if (pGeoFeatureLayer.DisplayAnnotation == true)
                    pGeoFeatureLayer.DisplayAnnotation = false;
            }
        }
        pGeoFeatureLayer = pEnumLayer.Next()as IGeoFeatureLayer;
    }
    //Add the result annotation layer to the map.
    pMap.AddLayers(pAnnoEnumLayer, true);
    //Refresh the map to update the display.
    IActiveView pActiveView = pMap as IActiveView;
    pActiveView.Refresh();
}
0 Kudos
5 Replies
DuncanHornby
MVP Notable Contributor

I think the problem may be with this line:

pConvertLabelsToAnnotation.AddFeatureLayer(...)

Reading the API help file for this method the third parameter is The workspace the annotation feature class will reside in. This is stated in the remarks section of the help page. As I read your code pFeatureWorkspace is the workspace of the layer you are adding to the converter, which makes no sense as you need to define the output location.

0 Kudos
SibghatUllah1
Occasional Contributor

Dear Duncan,

I have tried by giving workspace as follow but dint worked.

string fgdb= folderPath + "\\Test.gdb;

IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(fgdb, 0);

0 Kudos
DuncanHornby
MVP Notable Contributor

But that object is called featureWorkspace and you are using pFeatureWorkspace...

0 Kudos
SibghatUllah1
Occasional Contributor

i am using featureWorkspace now.issue is something else

0 Kudos
CiaraRowland-Simms
Esri Contributor

Hello Sibghat, 
Are you trying to create feature-linked annotation by any chance? Feature-linked annotation can only be created in the same geodatabase as the features it is created from so if so this could be the cause of your issues!

0 Kudos