Width of DBText

Width of DBText

youssefGC
Advocate Advocate
2,048 Views
8 Replies
Message 1 of 9

Width of DBText

youssefGC
Advocate
Advocate

Hello everyone,

Normally the width of the text depends on the number of characters, I need a method to know the width of a DBText in my drawing, is it possible to do this by programming?

youssefGC_0-1719482925426.png

 

using (DBText Txt = new DBText())
            {
                // ......

                Txt.TextString = "I need to read the width of this text";

                // ......
            }

 

Thank you.

 

 

 

 

0 Likes
Accepted solutions (1)
2,049 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

You can P/Invoke acedTextBox as shown in the DBTextExtension class (GetTextBoxCorners method) from Gile.AutoCAD.Extension library.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 9

daniel_cadext
Advisor
Advisor

AcDbText::getBoundingPoints isn’t in .NET?

you may be able to put the contents in an MText, then

call AcDbMText::getBoundingPoints

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 4 of 9

youssefGC
Advocate
Advocate
Accepted solution

I use "Extents Box" to calculate the dimensions width height of the text :

 

public double GetLengthOfText(DBText txt, ObjectId IdOfStyle)
        {
            double width = 0.0, height = 0.0;
            Autodesk.AutoCAD.GraphicsInterface.TextStyle iStyle = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();
            iStyle.FromTextStyleTableRecord(IdOfStyle);
            Extents2d extents = iStyle.ExtentsBox(txt.TextString, true, true, null);
            width = extents.MaxPoint.X - extents.MinPoint.X;
            height = extents.MaxPoint.Y - extents.MinPoint.Y;

            return width * (txt.Height / height);  
        }

 

 

Message 5 of 9

kerry_w_brown
Advisor
Advisor

 

I use a variation of this :

https://www.theswamp.org/index.php?topic=50493.msg556106#msg556106

 

Handy for determining the space text will occupy, without necessarily having the Text object.

 

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 6 of 9

daniel_cadext
Advisor
Advisor

That’s pretty slick, you could use that to pre calculate table column widths

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 7 of 9

kerry_w_brown
Advisor
Advisor

@daniel_cadext wrote:

That’s pretty slick, you could use that to pre calculate table column widths


Exactly !

 

Also handy when auto-choosing an Item_Balloon which is a suitable length to use for component 'numbers' 

 

2024-06-29_11-48-42.jpg

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 8 of 9

youssefGC
Advocate
Advocate

Thank you, but in my case I have to pass a DbText in the argument to take into consideration the change in text height in my main code.

 

0 Likes
Message 9 of 9

kerry_w_brown
Advisor
Advisor

@youssefGC 

 

That's fine, but not obvious from your original question.

 

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes