Update annotation text

1461
2
09-30-2011 07:24 AM
davido_loughlin
New Contributor
Hi,

I'm trying to change the text string in a existing annotation. In the debugger, I can see the text change take place but I log out and back in and the original text string is still present. When I uncomment the line "'pAnnotationFeature.Store()", I get an null pointer error. My code looks like this:
        pWorkspaceEdit.StartEditing(True)
...
        pStreamCursor = pFCStream.Search(pStreamQueryFilter, False)
        pStreamFeature = pStreamCursor.NextFeature
        While Not (pStreamFeature Is Nothing)
            pAnnotationUpdateFilter = New QueryFilter
            pAnnotationUpdateFilter.WhereClause = "Unique_ID=" & pStreamFeature.Value(indexAnnotationFeatureField)
            pFCursor = pFC.Update(pAnnotationUpdateFilter, False)
            pAnnotationFeature = pFCursor.NextFeature
            While Not (pAnnotationFeature Is Nothing)
                pTextElement = CType(pAnnotationFeature.Annotation, ISymbolCollectionElement)
                length = pStreamFeature.Value(indexLengthField)
                pTextElement.Text = "Hello"
                'pAnnotationFeature.Store()
                pAnnotationFeature = pFCursor.NextFeature
            End While
            pStreamFeature = pStreamCursor.NextFeature

        End While
        pWorkspaceEdit.StopEditing(True)


Can anyone help?
0 Kudos
2 Replies
AlexanderGray
Occasional Contributor III
I am not sure why the store is not working, the store should work.  I don't see your variable declaration so I am not sure what is going on there.  Without store I don't think this is going to work.  When I work with annotation, I clone the annotation, make the changes and set the element back to the annotation property.  Something like this

dim annoClone as IClone
annoClone = directCast( pAnnoFeature.Annotation, IClone)
dim textElem as ITextElement = DirectCast(annoClone.Clone, ITextElement)
textElem.Text = "Hello"
pAnnoFeature.Annotation = textElem
dim feat as IFeature = DirectCast(pAnnoFeature, IFeature)
feat.Store


Another thing is you don't have any edit operations started.  This may or may not cause a problem but worth a try.  If you still get the error on store, you can post the exact error to the thread.
0 Kudos
davido_loughlin
New Contributor
Thanks to Alexander and Phil at ESRI, this worked:

            ...
            pFCursor = pFC.Update(pAnnotationUpdateFilter, False)
            pAnnotationFeature = pFCursor.NextFeature
            While Not (pAnnotationFeature Is Nothing)
                area = (pWetlandFeature.Value(indexWetlandSizeField)) / 43559.66
                pWorkspaceEdit.StartEditing(True)
                pWorkspaceEdit.StartEditOperation()
                'call the sub below
                UpdateText(pAnnotationFeature, "Hello"))
                pWorkspaceEdit.StopEditOperation()
                pWorkspaceEdit.StopEditing(True)
                pAnnotationFeature = pFCursor.NextFeature
            End While
            pFCursor.Flush()
            ...

    Public Sub UpdateText(ByVal pAnnoFeature As IAnnotationFeature, ByVal stringNew As String)

        Dim pElement As IElement
        pElement = pAnnoFeature.Annotation

        If TypeOf pElement Is ISymbolCollectionElement Then
            'use ISymbolCollectionElement for efficient annotation updates editing
            Dim pSymbolCollectionElement As ISymbolCollectionElement
            pSymbolCollectionElement = pElement
            pSymbolCollectionElement.Text = stringNew
            pAnnoFeature.Annotation = pElement
            Dim pFeature As IFeature
            pFeature = pAnnoFeature
            pFeature.Store()
        End If

    End Sub
0 Kudos