'vba test_Get_DrawingText_Center
Option Explicit
Sub CATMain()
'check
Call CanExec
'select DrawingText
Dim filter As Variant: filter = Array("DrawingText")
Dim msg As String: msg = "select DrawingText / ESC-Cancel"
Dim selElm As SelectedElement: Set selElm = GetSelectElm(msg, filter)
If selElm Is Nothing Then Exit Sub
'get obj
Dim dr_txt As DrawingText: Set dr_txt = selElm.value
Dim dr_view As DrawingView: Set dr_view = dr_txt.Parent.Parent
'copy&paste
Dim tmp_view As DrawingView
Set tmp_view = dr_view.Parent.Add("AutomaticNaming")
Dim sel As Selection: Set sel = CATIA.ActiveDocument.Selection
With sel
.Copy
.Clear
.Add tmp_view
.Paste
End With
dr_view.Activate
'get size
Dim txt_Center As Variant
txt_Center = GetViewCenterPos(tmp_view)
'remove tmp_view
With sel
.Clear
.Add tmp_view
.Delete
End With
'Calculating Position
Dim txtpos_world As Variant
txtpos_world = Array(dr_view.x + txt_Center(0), _
dr_view.y + txt_Center(1))
'finish
msg = "** Select DrawingText Info **" & vbNewLine & _
"Name : " & dr_txt.Name & vbNewLine & _
"Text : " & dr_txt.Text & vbNewLine & _
"CenterPosition in view : " & Join(txt_Center, " , ") & vbNewLine & _
"CenterPosition in world : " & Join(txtpos_world, " , ")
MsgBox msg
Debug.Print msg
End Sub
'return ary(x_pos,y_pos)
Private Function GetViewCenterPos(ByVal view As Variant) As Variant
Dim size(3) As Variant
Call view.size(size)
GetViewCenterPos = Array((size(0) + size(1)) * 0.5, _
(size(2) + size(3)) * 0.5)
End Function
Private Function GetSelectElm(ByVal msg$, ByVal filter) As SelectedElement
Set GetSelectElm = Nothing
Dim sel As Variant ' Selection
Set sel = CATIA.ActiveDocument.Selection
sel.Clear
Select Case sel.SelectElement2(filter, msg, False)
Case "Cancel", "Undo", "Redo"
Exit Function
End Select
Set GetSelectElm = sel.Item(1)
End Function
Private Sub CanExec()
Dim msg As String: msg = vbNullString
Select Case True
Case CATIA.Windows.count < 1
msg = "Please open the CATDrawing file"
Case Not TypeName(CATIA.ActiveDocument) = "DrawingDocument"
msg = "Please Activate the CATDrawing file"
End Select
If msg = vbNullString Then Exit Sub
MsgBox msg, vbExclamation
End
End Sub