如何在ArcEngine中使用代码实现旋转AnnotationFeature要素

时间:2022-11-16 14:58:45

我们可以使用ITransform2D来进行旋转,主要是提示:Rotate方法传入的是弧度,而不是角度,可参照如下代码:

Sub GetAnnotationAttributes()

  Dim n As Integer
  Dim rAngle As Integer
  Dim pDoc As IMxDocument
  Set pDoc = ThisDocument
  Dim pMap As IMap
  Set pMap = pDoc.FocusMap
  Dim pMxDoc As IMxDocument
  Dim pElement As IElement
  Set pMxDoc = ThisDocument
  Dim pActiveView As IActiveView
  Set pActiveView = pMxDoc.ActivatedView

  Set pMap = pActiveView

  Dim pLayer As ILayer
  Dim pFLayer As IFeatureLayer
  Dim pFeat As IFeature
  Set pLayer = pMap.Layer(0)
  If TypeOf pLayer Is IFeatureLayer Then
    Set pFLayer = pLayer
  Else
    MsgBox "The first layer must be an annotation layer"
    Exit Sub
  End If

  Dim pFC As IFeatureClass
  Set pFC = pFLayer.FeatureClass
  If Not pFC.FeatureType = esriFTAnnotation Then
    MsgBox "The first layer must be an annotation layer"
    Exit Sub
  End If

  Dim FCursor As IFeatureCursor
  Set FCursor = pFC.Update(Nothing, False)

  Dim pAnnoFeat As IAnnotationFeature
  Dim pTextElt As ITextElement
  Dim pTextSym As IFormattedTextSymbol
  Set pAnnoFeat = FCursor.NextFeature
 
  Do While Not pAnnoFeat Is Nothing
    Set pTextElt = pAnnoFeat.Annotation
    Set pTextSym = pTextElt.Symbol
    Set pFeat = pAnnoFeat
    Dim pEnv As IEnvelope
    Set pEnv = pFeat.Shape.Envelope 'pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.FittedBounds
    pEnv.Expand 0.01, 0.01, True
    Set pElement = pTextElt
    pElement.Geometry = pEnv.UpperLeft

    n = 6
   
    ' Have to change the TextSymbol.Angle to the TextElement

    If (pTextSym.Angle < (0 + n)) Then
      rAngle = 0
    End If

    If (pTextSym.Angle > (365 - n)) Then
      rAngle = 0
    End If

    If ((pTextSym.Angle < (270 + n)) And (pTextSym.Angle > (270 - n))) Then
      rAngle = 90
    End If

    If ((pTextSym.Angle < (90 + n)) And (pTextSym.Angle > (90 - n))) Then
      rAngle = 90
    End If

    ' here's the workaround
    Dim pTrans2D As ITransform2D
    Set pTrans2D = pElement
    pTrans2D.Rotate pEnv.UpperLeft, rAngle ' any point would do
    ' end of workaround
    pMxDoc.ActiveView.PartialRefresh esriViewGraphics, pElement, Nothing
    Set pAnnoFeat = FCursor.NextFeature
  Loop
 
  pMxDoc.ActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
 
  MsgBox "Finished Rotating Text", vbInformation, "Text Rotation"

End Su

Dim pAnnoElement As IElement
Set pAnnoElement = pAnnoFeature.Annotation

Dim dAngle as Double
dAngle = GetAngle(pAnnoElement.Geometry)


Private Function GetAngle(ByVal pPolyline As IPolyline) As Double
  Const PI As Double = 3.14159
 
  Dim ppolycurve As IPolycurve
  Dim pTangentLine As ILine
  Dim Length As Double
  Dim radAngle As Double
  Dim degAngle As Double
 
  Set pTangentLine = New Line
  Length = pPolyline.Length
  pPolyline.QueryTangent esriNoExtension, 0.5, True, Length, pTangentLine

  radAngle = pTangentLine.Angle
    degAngle = radAngle * 57.2958279087978  '57 = (180 / PI)
 
  Do While degAngle < 0
    degAngle = degAngle + 360
  Loop
  GetAngle = degAngle
End Function