ArcObjects (C#.Net) - How to copy and move Annotation Feature with ITransform2D?

2095
2
08-15-2019 08:33 PM
MarkMindlin
Occasional Contributor III

We need to copy an annotation feature (not feature linked) and move 10 metres.

Two issues:

  • A new annotation is drawn behind old one
  • A new annotation is not moved

IFeatureClass pFeatCls = pSourceAnnotationFeature.Class as IFeatureClass;
IFeature pNewAnnoFeature = pFeatCls.CreateFeature();
CopyAttributes(pSourceAnnotationFeature, ref pNewAnnoFeature);

IClone pCloneGeometry = (IClone)pSourceAnnotationFeature.Shape;
IGeometry pShapeCopyGeometry = (IGeometry)pCloneGeometry.Clone();
ITransform2D pTransform2D = pShapeCopyGeometry as ITransform2D;
pTransform2D.Move( 10, 0);
pNewAnnoFeature.Shape = pShapeCopyGeometry;
(pNewAnnoFeature as IAnnotationFeature).Annotation.Geometry = pShapeCopyGeometry;
pNewAnnoFeature.Store();

ArcMap.Document.ActiveView.Refresh();

0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor

I can't spot anything immediately wrong with the code but what you could do is put a message box in to display the XY coordinate of say the centroid before and after the move and just before the store(). If they are what you expected then the issue is with the store step and is probably related to your data source. As the code is generic no one can tell if it is a shapefile or enterprise database such as arc server.

0 Kudos
MarkMindlin
Occasional Contributor III

This may solve the issue

IFeature pNewAnnoFeature = pFeatureClass.CreateFeature();
if (CopyAttributes(pOldFeature, ref pNewAnnoFeature, out outErrMsg) == false)
{
MessageBox.Show(outErrMsg);
return;
}


IClone pFirstAnnotation = (IClone)(pOldFeature as IAnnotationFeature).Annotation;
IElement pSecondElement = pFirstAnnotation.Clone() as IElement;
ITextElement pSecondTextElement = pSecondElement as ITextElement;
try
{
//ensure the alignment of the cloned text is lower left
ISymbolCollectionElement txtSymElemTwo = (ISymbolCollectionElement)pSecondTextElement;
txtSymElemTwo.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;
txtSymElemTwo.VerticalAlignment = esriTextVerticalAlignment.esriTVABottom;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

ITransform2D pTransform2D = (pSecondTextElement as ITransform2D);
pTransform2D.Move(100, 100);

(pNewAnnoFeature as IAnnotationFeature).Annotation = pSecondElement;
pNewAnnoFeature.Store();

0 Kudos