最近有個小需求,就是要把註釋箭頭指向的點的座標提取出來。
剛開始發現Note.GetAttachPos功能,Annotation.GetPosition有這個功能。
但是經過測試發現,當註釋的引線是多折線的時候,上兩個函數就是讀取的位置有時候對,有時候不對。
本來可以想着把所有的第二種換成第一種,那麼獲取的時候就應該正確了,但是發現API裏面還不能直接轉換。
經過大量的調試發現,如果使用的是多折線,但是隻有一條折線信息的時候,可能是對的,如果是多折的,基本上不對。
當鼠標移動到引線上時,默認的一段直線直接顯示的是註釋的圖標,當移動到是多段折線上時,顯示的是另一個圖標,說明它們不是一個對象。而更像是一個組合體。
經過api的檢查,發現了這個多折線有一個專門的對象。裏面是由多個線的信息組成。
然後又發現Annotation中兩個方法 GetMultiJogLeaderCount 和 GetMultiJogLeaders,所以就更顯明瞭。
在獲取註釋對象 Note的引用點位置時,需要做一個判斷。
當GetMultiJogLeaderCount > 0 時,那麼說明它是一個多折線的註釋,需要使用 MultiJogLeader 來獲取信息。遍歷其它的關鍵點座標。
這個座標和視圖的位置與比例有一定關聯,它默認是的整個圖紙的座標。如果需要轉換到視圖中,還需要做個計算。
下面是一段宏的代碼:
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swDraw As DrawingDoc
Dim swSelMgr As SelectionMgr
Dim swNote As Note
Dim swView As View
Dim viewPos As Variant
Dim notePos As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Dim ann As Annotation
If Not swModel Is Nothing Then
If swModel.GetType <> swDocumentTypes_e.swDocDRAWING Then Exit Sub
Set swDraw = swModel
Set swSelMgr = swModel.SelectionManager
Dim selLeaders As SldWorks.MultiJogLeader
Dim lineData As Variant
If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelNOTES Then Exit Sub
Set swNote = swSelMgr.GetSelectedObject6(1, 0)
Set swView = swDraw.ActiveDrawingView
viewPos = swView.Position
notePos = swNote.GetAttachPos
Set ann = swNote.GetAnnotation
If ann.GetMultiJogLeaderCount > 0 Then
Set selLeaders = ann.GetMultiJogLeaders(0)
If selLeaders Is Nothing = False Then
lineData = selLeaders.GetLineAtIndex(0)
For i = 0 To selLeaders.GetLineCount - 1
lineData = selLeaders.GetLineAtIndex(i)
If Not IsEmpty(lineData) Then
Debug.Print ("x:" & Math.Round((lineData(1) - viewPos(0)) * 1000# / swView.ScaleDecimal, 4))
Debug.Print ("y:" & Math.Round((lineData(2) - viewPos(1)) * 1000# / swView.ScaleDecimal, 4))
End If
Next i
End If
Else
End If
' 'ann.UseDocDispLeader = True
'Debug.Print (swView.Angle * 57.295779513)
' Debug.Print (Math.Round((notePos(0) - viewPos(0)) * 1000# / swView.ScaleDecimal, 4))
' Debug.Print (Math.Round((notePos(1) - viewPos(1)) * 1000# / swView.ScaleDecimal, 4))
' Debug.Print
End If
End Sub
反之,按正常的GetAttachPos獲取對象就可以了。