Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Size of the textbox on a drawing 2

Status
Not open for further replies.

tesak

Aerospace
Jul 6, 2011
141
CZ
Hello guys,
I need to get width and height of a textbox in CATIA drawing in order to get a center point of the text regardless of anchor point. Do you have any ideas how to achieve it?

Thanks for your help,

Tesak
- Text along a curve for Catia V5
 
Replies continue below

Recommended for you

Hi.tesak

I have not tried and the processing speed may be slow, but ...
1. Create a temporary DrawingView.
2. Copy and paste any DrawingText.
3. Get size with DrawingView.size.

But your "TxtOnCurve" is wonderful.
I also thought about creating similar things before, but it did not complete ....
 
Try it this.

Code:
'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
 
Hi kantoku,
your idea is really great. Although not very effective (I need to apply it to few hundreds of elements), but it works. Unfortunatelly it seems that there is no direct way to get bounding box (and center point) of text.

I also found one way to get center point of text, but it works only for balloons (enough for me):
[ul]
[li]create leader[/li]
[li]get base point of a leader[/li]
[li]remove leader[/li]
[li]in case of balloons base point is exactly in a center of a circle[/li]
[/ul]

This method however could not be applied to regular text box, because then base point of a leader lies on the side of textbox. So your method is definitelly more versatile.

Thank you very much for your effort, good job :)


Tesak
- Text along a curve for Catia V5
 
Hi.

I've just wasted 20 minutes describing leader approach in this crappy message editor.

Going to be short then:
1. Move text anchor to top-left
2. Create leader, get it's base point
3. Move anchor to bottom-right
4. Create another leader, get it's base point as well

Points from 2 and 4 are located exactly at bounding box opposite corners. Enjoy!

UPD
By the way, the complete code can be found at COE, function is called GetTextBoxWithAnchors (inspired by Rolando Garza, implemented by me)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top