Text box size

Satish Ramchandra Kolekar

Text box size

Hello everyone,

I am working on program, to locate the Text box location on the drawing. With Macro, i am able to find the location fo text box (i.e. Anchor point of the Text box). Now based on anchor point location i need to get center of the text box, but i dont know, how to get the text box size.

Example: i have text "N1" on the drawing and i need to locate center point (half the width & half the hight of text box)

Thanks,

Satish

Vinicius Santin

RE: Text box size
(in response to Satish Ramchandra Kolekar)

Hi Satish,

 

You can not set the ancor point as "MiddleCenter" and then measuring the coordinates??

This way:

...
Dim drawingText1  As DrawingText
Set drawingText1 = drawingTexts1.Item("xxx")

drawingText1.AnchorPosition = catMiddleCenter
MsgBox "X =" & drawingText1.X & " Y =" & drawingText1.Y

Rolando Garza

RE: Text box size
(in response to Vinicius Santin)

Since you write "location on the drawing", I think you'll want to add oView.xAxisData to drawingText1.X to get the box's distance from the page origin and not just the view origin.

If you use fixed length text boxes (each box can have its own fixed length - they do not all have to be the same size) toggle ON the "Word Wrap" property. If it is ON, you can use drawingText1.WrappingWidth to get the text box's size.


Edited By:
Rolando Garza[Teco-Westinghouse] @ Aug 08, 2012 - 03:38 PM (America/Central)
Rolando Garza[Teco-Westinghouse] @ Aug 08, 2012 - 03:51 PM (America/Central)

Satish Ramchandra Kolekar

RE: Text box size
(in response to Vinicius Santin)

Hi,

My problem here is i am not going to add any text on the drawing, those text are already available on the drawing and as default setting, they are anchored on left top corner which is 1st option under anchor position. Even if i change the anchor position of each text box, the will shift thier position.

 

Thanks

Satish

Satish Ramchandra Kolekar

RE: Text box size
(in response to Rolando Garza)

Rolando,

I can try for this property to get total width . but how about getting hight, Any idea how the text box size and text size are related ?

Thanks,

Satish

Little Cthulhu

RE: Text box size
(in response to Satish Ramchandra Kolekar)
Hi. There are no methods for retrievingtext bou ding box in Automation. But it all depends on font and text settings. If you are able to measure each font char then it's possible to calculate correct height and width. I've done it with postscript font once.

Rolando Garza

RE: Text box size
(in response to Satish Ramchandra Kolekar)

How about this:

  1. add leader to the text (point othe view origin)
  2. use GetPoints to find its path.
  3. delete the leader
  4. change anchor point to catMiddleCenter
  5. add leader to the text (again, point othe view origin)
  6. use GetPoints to find the second leader's path.
  7. compare the two paths to find your dX and dY.

This is just a thought. I have not tried this myself.

Little Cthulhu

RE: Text box size
(in response to Rolando Garza)

Great idea Rolando!

Will try to come up with the solution.

 

UPD. 

I've refreshed my own investigations of leaders and found that we can't anchor a leader to the certain point. It always stays at the left-right side of the text and also may float dynamically based on header point coordinates. Also there are some additional leader settings (such as margin) that are set up in standards. Still I believe it is possible to measure at least width of a text.

 

UPD2 + 3 

Here we go (code is intended to be run from VBA Editor): 

Option Explicit
Sub CATMain()
Dim oSel As Selection
Set oSel = CATIA.ActiveDocument.Selection
Dim txtSelected As DrawingText
Set txtSelected = oSel.Item(1).Value
MsgBox "Text width = " + CStr(getTextWidthWithLeaders(txtSelected))
MsgBox "Text height = " + CStr(getTextHeightWithLeaders(txtSelected))
End Sub
'==============================================================================
' Suggested by Rolando Garza.
'
' Calculates drawing text bounding box WIDTH using temporary leaders.
' Leaders are created on the left side of a text and on the right side.
' Distance between leaders base point is considered to be text width.
'
'
' Returns:
' Double - text width.
'==============================================================================
Private Function getTextWidthWithLeaders(text As DrawingText) As Double
' quickly check method arguments
If (text Is Nothing) Then
getTextWidthWithLeaders = 0
Exit Function
End If
'-------------------------
' CREATE TEMPORARY LEADERS
'-------------------------
Dim ldrLeft As DrawingLeader
Set ldrLeft = text.Leaders.Add(text.X - 100, text.Y)
Dim ldrRight As DrawingLeader
Set ldrRight = text.Leaders.Add(text.X + 100, text.Y)
'------------------------------------------------
' CALCULATE DISTANCE BETWEEN LEADERS' BASE POINTS
'------------------------------------------------
Dim dPointLeftX As Double
Dim dPointLeftY As Double
ldrLeft.GetPoint 0, dPointLeftX, dPointLeftY
Dim dPointRightX As Double
Dim dPointRightY As Double
ldrRight.GetPoint 0, dPointRightX, dPointRightY
Dim dDistance As Double
dDistance = dPointRightX - dPointLeftX
'-------------------------
' REMOVE TEMPORARY LEADERS
'-------------------------
' WARNING! Remove method is bugged. It actually works but throws an error.
' Place erroneous calls in a sort of {try - catch} block
On Error Resume Next
text.Leaders.Remove text.Leaders.Count
text.Leaders.Remove text.Leaders.Count
On Error GoTo 0
' return calculated distance
getTextWidthWithLeaders = dDistance
End Function
'==============================================================================
' Suggested by Rolando Garza.
'
' Calculates drawing text bounding box HEIGHT using temporary leaders.
' Leaders are created on the top side of a text and on the bottom side.
' Distance between leaders base point is considered to be text width.
'
' One way to make leader base point "stick" to the bounding box is to put
' a frame around the text.
'
'
' Returns:
' Double - text height.
'==============================================================================
Private Function getTextHeightWithLeaders(text As DrawingText) As Double
' quickly check method arguments
If (text Is Nothing) Then
getTextHeightWithLeaders = 0
Exit Function
End If
'-------------------------
' CREATE TEMPORARY LEADERS
'-------------------------
' place a temporary frame around the text
Dim iTextFrame As Integer
iTextFrame = text.FrameType
text.FrameType = catRectangle
Dim ldrTop As DrawingLeader
Set ldrTop = text.Leaders.Add(text.X, text.Y + 100)
Dim ldrBottom As DrawingLeader
Set ldrBottom = text.Leaders.Add(text.X, text.Y - 100)
'------------------------------------------------
' CALCULATE DISTANCE BETWEEN LEADERS' BASE POINTS
'------------------------------------------------
Dim dPointTopX As Double
Dim dPointTopY As Double
ldrTop.GetPoint 0, dPointTopX, dPointTopY
Dim dPointBottomX As Double
Dim dPointBottomY As Double
ldrBottom.GetPoint 0, dPointBottomX, dPointBottomY
Dim dDistance As Double
dDistance = dPointTopY - dPointBottomY
'-------------------------
' REMOVE TEMPORARY LEADERS
'-------------------------
' WARNING! Remove method is bugged. It actually works but throws an error.
' Place erroneous calls in a sort of {try - catch} block
On Error Resume Next
text.Leaders.Remove text.Leaders.Count
text.Leaders.Remove text.Leaders.Count
On Error GoTo 0
' return original frame type
text.FrameType = iTextFrame
' return calculated distance
getTextHeightWithLeaders = dDistance
End Function

 

 

 

Edited By:
Little Cthulhu[Sikorsky Aircraft] @ Aug 09, 2012 - 11:48 PM (Europe/Moscow)
Little Cthulhu[Sikorsky Aircraft] @ Aug 10, 2012 - 12:05 AM (Europe/Moscow)
Little Cthulhu[Sikorsky Aircraft] @ Aug 10, 2012 - 12:20 AM (Europe/Moscow)
Little Cthulhu[Sikorsky Aircraft] @ Aug 10, 2012 - 12:21 AM (Europe/Moscow)

Satish Ramchandra Kolekar

RE: Text box size
(in response to Little Cthulhu)

Thanks Little Cthulhu,


I will add this code in my program and see what it gives me..

 

paul clark

RE: Text box size
(in response to Little Cthulhu)
To Rolando Garza & Little Cthulhu Just to let you know that this is an excellent little gem of code. I have used this succesfully, both in 2D and 3D and works really well. This has resovled a problem that I've had for a long time, but not only that has suggested more lateral ways to resolve problems. Thanks again

Little Cthulhu

RE: Text box size
(in response to paul clark)

You're welcome. Great that someone made use of it.

Mahadev02 M

RE: Text box size
(in response to Little Cthulhu)

Hi Little Cthulhu,

I tried to use your code (no modification) to find the text box size, i can able to get the width where as i'm not able to get the height of the text as the leaders were getting created at the same point.

Even after putting a frame also nothing is working.

Kindly help, I'm strugling to find the reason.

Regards,

Mahadev

Cliff Johnson, Cliff

RE: Text box size
(in response to Mahadev02 M)

Late update to this thread... 

I stumbled upon a need for this useful code and found like Mahadev that I could not compute the height.

The reason is the drafting standard at use here does not permit the leaders to be attached to the top of the text.

However I found that if I made a circle frame instead of a rectangle I could find the center of the text (which is what I wanted) as the midpoint of the two attachment points and it requires only one call.

So after changing the signature to a sub returning x and y be reference:

Sub GetTextCenter(text as DrawingText, byRef x as double, byRef y as double)
Dim iFrame as Integer
iFrame = text.FrameType
text.FrameType = catCircle
' CREATE TEMPORARY LEADERS'-------------------------
Dim ldrLeft As DrawingLeader
Set ldrLeft = text.Leaders.Add(text.X - 100, text.Y)
Dim ldrRight As DrawingLeader
Set ldrRight = text.Leaders.Add(text.X + 100, text.Y)
' Get BASE POINTS'------------------------------------------------
Dim dPointLeftX As Double
Dim dPointLeftY As Double
ldrLeft.GetPoint 0, dPointLeftX, dPointLeftY
Dim dPointRightX As Double
Dim dPointRightY As Double
ldrRight.GetPoint 0, dPointRightX, dPointRightY
'COMPUTE CENTER OF TEXT--------------------
x = (dPointLeftX + dPointRightX)/2
y = (dPointLeftY + dPointRightY)/2
' REMOVE TEMPORARY LEADERS'-------------------------
On Error Resume Next
text.Leaders.Remove text.Leaders.Count
text.Leaders.Remove text.Leaders.Count
On Error GoTo 0
text.FrameType = iFrame
End Sub

Dave Frank

RE: Text box size
(in response to Rolando Garza)

Hi friends,

I have added a message box, and it pops up in the middle of the screen.

Can you force the location?

This is my code

MsgBox "OK for next itteration", vbOKOnly

This is what I am trying to do:

MsgBox  location =x400 pixels, y=150 pixels   "OK for next itteration", vbOKOnly

Thanks in advance

Dave


Dave Frank  *    Aerospace Dynamics International, Valencia, Ca

5-Axis CNC Programmer

COE Product Co-Chairman, Digital Numerical Control

 

Samarinder Singh

RE: Text box size
(in response to Dave Frank)



In Reply to Dave Frank:

Hi friends,

I have added a message box, and it pops up in the middle of the screen.

Can you force the location?

This is my code

MsgBox "OK for next itteration", vbOKOnly

This is what I am trying to do:

MsgBox  location =x400 pixels, y=150 pixels   "OK for next itteration", vbOKOnly

Thanks in advance

Dave


Dave Frank  *    Aerospace Dynamics International, Valencia, Ca

5-Axis CNC Programmer

COE Product Co-Chairman, Digital Numerical Control

 


Dave,

You need to start a new thread for that. Message box in VB and Text box in CATDrawing are two different things.

Regards,
Samarinder Singh