[GIS] Creating Annotation Feature Class using ArcObjects

annotation;arcobjectsc

I want to create an annotation feature class with arcobjects. I do not want my annotation to be linked to source feature class. I have this code but it is not working. It only creates an empty annotation feature class, no labels in it. I tested every thing that I thought might be the problem. still no success. I have a problem with CreateAnnotationClass method. It's not even getting source feature class as a parameter. how does it know which feature class to label in the workspace?! (there is a parameter as srcFeatureClass, but it is only to say if I want my annotation to be linked to source feature class, which I don't, so I put it as null.) can anyone help please?

        public IFeatureClass IFeatureWorkspaceAnno_Example(IFeatureClass featureClass)
    {
        IDataset dataset = (IDataset)featureClass;
        //cast for the feature workspace from the workspace
        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)dataset.Workspace;
        IFeatureWorkspaceAnno featureWorkspaceAnno = dataset.Workspace as IFeatureWorkspaceAnno;
        //set up the reference scale
        ESRI.ArcGIS.Carto.IGraphicsLayerScale graphicLayerScale =
            new ESRI.ArcGIS.Carto.GraphicsLayerScaleClass();
        IGeoDataset geoDataset = (IGeoDataset)dataset;
        graphicLayerScale.Units = ESRI.ArcGIS.esriSystem.esriUnits.esriMeters;
        graphicLayerScale.ReferenceScale = axMapControl1.MapScale; 
        //set up symbol collection
        ESRI.ArcGIS.Display.ISymbolCollection symbolCollection =
            new ESRI.ArcGIS.Display.SymbolCollectionClass();

        #region "MakeText"
        ESRI.ArcGIS.Display.IFormattedTextSymbol myTextSymbol =
            new ESRI.ArcGIS.Display.TextSymbolClass();

        ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
        rgbColor.Red = 150;
        rgbColor.Green = 0;
        rgbColor.Blue = 0;
        myTextSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
        //Set other properties for myTextSymbol
        myTextSymbol.Angle = 0;
        myTextSymbol.RightToLeft = false;
        myTextSymbol.VerticalAlignment = ESRI.ArcGIS.Display.esriTextVerticalAlignment.esriTVABaseline;
        myTextSymbol.HorizontalAlignment = ESRI.ArcGIS.Display.esriTextHorizontalAlignment.esriTHAFull;
        //myTextSymbol.CharacterSpacing = 200;
        myTextSymbol.Case = ESRI.ArcGIS.Display.esriTextCase.esriTCNormal;
        #endregion

        symbolCollection.set_Symbol(0, (ESRI.ArcGIS.Display.ISymbol)myTextSymbol);
        //set up the annotation labeling properties including the expression
        ESRI.ArcGIS.Carto.IAnnotateLayerProperties annoProps = new
            ESRI.ArcGIS.Carto.LabelEngineLayerPropertiesClass();
        annoProps.FeatureLinked = false;
        annoProps.AddUnplacedToGraphicsContainer = true;
        annoProps.CreateUnplacedElements = true;
        annoProps.DisplayAnnotation = true;
        annoProps.UseOutput = true;
        annoProps.LabelWhichFeatures = esriLabelWhichFeatures.esriAllFeatures;

        ESRI.ArcGIS.Carto.ILabelEngineLayerProperties layerEngineLayerProps =
            (ESRI.ArcGIS.Carto.ILabelEngineLayerProperties)annoProps;
        ESRI.ArcGIS.Carto.IAnnotationExpressionEngine annoExpressionEngine =
            new ESRI.ArcGIS.Carto.AnnotationVBScriptEngineClass();
        layerEngineLayerProps.ExpressionParser = annoExpressionEngine;
        layerEngineLayerProps.Expression = "[OBJECTID]";
        layerEngineLayerProps.IsExpressionSimple = true;
        layerEngineLayerProps.Offset = 0;
        layerEngineLayerProps.SymbolID = 0;
        layerEngineLayerProps.Symbol = myTextSymbol;

        ESRI.ArcGIS.Carto.IAnnotateLayerTransformationProperties annoLayerTransProp =
            (ESRI.ArcGIS.Carto.IAnnotateLayerTransformationProperties)annoProps;
        annoLayerTransProp.ReferenceScale = graphicLayerScale.ReferenceScale;
        annoLayerTransProp.Units = graphicLayerScale.Units;
        annoLayerTransProp.ScaleRatio = axMapControl1.MapScale;

        ESRI.ArcGIS.Carto.IAnnotateLayerPropertiesCollection annoPropsColl =
            new ESRI.ArcGIS.Carto.AnnotateLayerPropertiesCollectionClass();
        annoPropsColl.Add(annoProps);
        //use the AnnotationFeatureClassDescription to get the list of required
        //fields and the default name of the shape field
        IObjectClassDescription oCDesc = new ESRI.ArcGIS.Carto.AnnotationFeatureClassDescriptionClass();
        IFeatureClassDescription fCDesc = (IFeatureClassDescription)oCDesc;

        //create the new class
        return featureWorkspaceAnno.CreateAnnotationClass("AnnoTest",
            oCDesc.RequiredFields, oCDesc.InstanceCLSID, oCDesc.ClassExtensionCLSID,
            fCDesc.ShapeFieldName, "", null, null,
            annoPropsColl, graphicLayerScale, symbolCollection, false);
    }

Best Answer

Recently I wrote the method to copy annotation layer. Maybe you will find this code useful. From annotation layer you can get annotation feature class. You just need create instances of ISymbolCollection, IAnnotateLayerPropertiesCollection and IOverposterProperties.

    public static IAnnotationLayer CreateAnnotationLayer(IAnnotationLayer templateLayer, string name, IWorkspace workspace, IFeatureDataset featureDataset)
    {
        var templateFeatureClass = (templateLayer as IDisplayTable)?.DisplayTable as IFeatureClass;
        var annotationClassExtension = templateFeatureClass.Extension as IAnnotationClassExtension;

        //

        var symbolCollection = (annotationClassExtension.SymbolCollection as IClone)?.Clone() as ISymbolCollection;
        var annotationProperties = (annotationClassExtension.AnnoProperties as IClone)?.Clone() as IAnnotateLayerPropertiesCollection;
        var overposterProperties = (annotationClassExtension.OverposterProperties as IClone)?.Clone() as IOverposterProperties;

        //

        IGraphicsLayerScale graphicsLayerScale = new GraphicsLayerScaleClass();
        graphicsLayerScale.ReferenceScale = annotationClassExtension.ReferenceScale;
        graphicsLayerScale.Units = annotationClassExtension.ReferenceScaleUnits;

        //

        IObjectClassDescription ocDescription = new AnnotationFeatureClassDescriptionClass();
        IFeatureClassDescription fcDescription = (IFeatureClassDescription)ocDescription;

        IFields requiredFields = ocDescription.RequiredFields;
        int shapeFieldIndex = requiredFields.FindField(fcDescription.ShapeFieldName);
        IField shapeField = requiredFields.get_Field(shapeFieldIndex);
        IGeometryDef geometryDef = shapeField.GeometryDef;
        IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
        geometryDefEdit.SpatialReference_2 = (templateFeatureClass as IGeoDataset).SpatialReference;

        //

        IAnnotationLayerFactory annotationLayerFactory = new FDOGraphicsLayerFactoryClass();
        return annotationLayerFactory.CreateAnnotationLayer(
            workspace as IFeatureWorkspace,
            featureDataset,
            name,
            geometryDef,
            null,
            annotationProperties,
            graphicsLayerScale,
            symbolCollection,
            false,
            false,
            false,
            true,
            overposterProperties,
            string.Empty);
    }
Related Question