Convert GraphicsLayer to Annotation Feature Layer

04-25-2013 10:44 PM
New Contributor III
I am trying to create an Annotation Feature Layer from a GraphicsLayer. I have used CompositeGraphicsLayer to create the IGraphicsLayer and have drawn some graphic elements on it (Triangle Element, Line element etc). Trying to convert graphics layer into Annotation Feature Layer using the below code, but I am getting an exception "Object reference not set to an instance of an object" at the mentioned line below. Request someone to please help me in solving this issue or provide me with sample code to convert Graphics Layer to Annotation Layer.

IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(@"J:\GIS\DGN\PersonalGDB.mdb", 0);
ISpatialReferenceFactory3 spRefFact = new SpatialReferenceEnvironmentClass();
ISpatialReference spatialReference = spRefFact.CreateESRISpatialReferenceFromPRJFile(@"J:\GIS\DGN\56k.prj");
m_mapControl.SpatialReference = spatialReference;

IFeatureClass pAnnoFeatureClass = CreateStandardAnnotationClass(featureWorkspace, null, "ANNO_LAYER1001", m_mapControl.SpatialReference, 1, esriUnits.esriDecimalDegrees, "");
IFeature pFeature; //= pAnnoFeatureClass.CreateFeature();
IFeatureCursor pFCursor;
pFCursor = pAnnoFeatureClass.Update(null, false);
IAnnotationFeature pAnnoFeature ;//= pFeature as IAnnotationFeature;
IGraphicsContainer pGrContainer = layer as IGraphicsContainer;
IElement element = pGrContainer.Next();
// IGraphicElement pGraphicElement;
pFeature = pFCursor.NextFeature();
while (element != null)
// pGraphicElement = element as IGraphicElement;
//pGrElement.SpatialReference = m_mapControl.SpatialReference;
//pAnnoFeature.Annotation = pGraphicElement as IElement;
//(pAnnoFeature as IFeature).Store();
element.Geometry.SpatialReference = m_mapControl.SpatialReference;
pAnnoFeature = pFeature as IAnnotationFeature;
// pGraphicElement = pAnnoFeature.Annotation as IGraphicElement;
// pGraphicElement.SpatialReference = m_mapControl.SpatialReference;

// EXCEPTION : Object reference not set to an instance of an object
pAnnoFeature.Annotation = element; //pGraphicElement as IElement;
pFCursor.UpdateFeature(pAnnoFeature as IFeature);

pFeature = pFCursor.NextFeature();
element = pGrContainer.Next();

I have used the same CreateStandardAnnotationClass example provided in the "Creating annotation and dimension feature classes" in ESRI Help to create Annotation Feature Class.

public IFeatureClass CreateStandardAnnotationClass(IFeatureWorkspace featureWorkspace, IFeatureDataset featureDataset,
string className, ISpatialReference spatialReference, int referenceScale, esriUnits
referenceScaleUnits, string configKeyword)
// Create an annotation class and provide it with a name.
ILabelEngineLayerProperties labelEngineLayerProperties = new LabelEngineLayerPropertiesClass();
IAnnotateLayerProperties annotateLayerProperties = (IAnnotateLayerProperties) labelEngineLayerProperties;
annotateLayerProperties.Class = "Annotation Class 1";

// Get the symbol from the annotation class. Make any changes to its properties
// here.
ITextSymbol annotationTextSymbol = labelEngineLayerProperties.Symbol;
ISymbol annotationSymbol = (ISymbol)annotationTextSymbol;

// Create a symbol collection and add the default symbol from the
// annotation class to the collection. Assign the resulting symbol ID
// to the annotation class.
ISymbolCollection symbolCollection = new SymbolCollectionClass();
ISymbolCollection2 symbolCollection2 = (ISymbolCollection2)symbolCollection;
ISymbolIdentifier2 symbolIdentifier2 = null;
symbolCollection2.AddSymbol(annotationSymbol, "Annotation Class 1", out symbolIdentifier2);
labelEngineLayerProperties.SymbolID = symbolIdentifier2.ID;

// Add the annotation class to a collection.
IAnnotateLayerPropertiesCollection annotateLayerPropsCollection = new AnnotateLayerPropertiesCollectionClass();

// Create a graphics layer scale object.
IGraphicsLayerScale graphicsLayerScale = new GraphicsLayerScaleClass();
graphicsLayerScale.ReferenceScale = referenceScale;
graphicsLayerScale.Units = referenceScaleUnits;

// Create the overposter properties for the standard label engine.
IOverposterProperties overposterProperties = new BasicOverposterPropertiesClass();

// Instantiate a class description object.
IObjectClassDescription ocDescription = new AnnotationFeatureClassDescriptionClass();
IFeatureClassDescription fcDescription = (IFeatureClassDescription)ocDescription;

// Get the shape field from the class description's required fields.
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 = spatialReference;

// Create the annotation layer factory.
IAnnotationLayerFactory annotationLayerFactory = new FDOGraphicsLayerFactoryClass();

// Create the annotation feature class and an annotation layer for it.
IAnnotationLayer annotationLayer = annotationLayerFactory.CreateAnnotationLayer
(featureWorkspace, featureDataset, className, geometryDef, null,
annotateLayerPropsCollection, graphicsLayerScale, symbolCollection, false,
false, false, true, overposterProperties, configKeyword);

// Get the feature class from the feature layer.
IFeatureLayer featureLayer = (IFeatureLayer)annotationLayer;
IFeatureClass featureClass = featureLayer.FeatureClass;

return featureClass;
0 Kudos
2 Replies
New Contributor III
Can someone please reply.
0 Kudos
New Contributor II
Did you ever get this to work?  I found your post while researching how to distinguish between text and graphics annotation features (still can't figure that out). 
In your code, it appears that you're assigning the graphics element to an AnnotationFeature object, but the target object hasn't been created.  You need to use an InsertCursor or some other way to first create a new annotationfeature.

I hope this helps.

0 Kudos