Width of text

Width of text

fieldguy
Advisor Advisor
3,324 Views
3 Replies
Message 1 of 4

Width of text

fieldguy
Advisor
Advisor

I am trying to determine the width of the longest text string in order to set a table column width. Geometric extents only work if the text is added to the database - I'd rather not do that.

 

There are some examples of "ExtentsBox" (like >>this one<<< from Phillipe Leefsma) but that is sort of old (May 2012).  If I need to use that, how do I determine the "EntryPoint"?

 

There is another example >>here<< from Balaji Ramamoorthy that includes instructions on how to obtain the "mangled name" using "dumpbin" but I am not sure what to look for.

 

I need this:

 [DllImport("acdb18.dll",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint =
"?fromAcDbTextStyle@@YA?AW4ErrorStatus@Acad@@AAVAcGiTextStyle@@PB_W@Z")]
private static extern ErrorStatus fromAcDbTextStyle(
System.IntPtr style, string styleName);

 

translated to something I can use in Civil3D 2016 (I believe the entrypoint is in acdb20.dll).

 

TIA

0 Likes
Accepted solutions (1)
3,325 Views
3 Replies
Replies (3)
Message 2 of 4

fieldguy
Advisor
Advisor
Accepted solution

I was able to get what I needed using System.Drawing.  The font we are using is Arial.

 

using (var image = new Bitmap(1, 1))
{
using (var g = Graphics.FromImage(image))
{
Font usefont = new Font("Arial", 10); // 10 is pixels
SizeF txtwidth = new SizeF();
Single widesttxt = 0.0F;
List<string> checkwidths = new List<string>() { "s1", "s2", "morestrings" };
foreach (string s in checkwidths)
{
txtwidth = g.MeasureString(s, usefont);
Single twidth = txtwidth.Width;
if (twidth > widesttxt)
widesttxt = twidth;
}
}
}

// round up to nearest 10, convert to int, and add 10
int columnwidth = (int)(Math.Ceiling(widesttxt / 10.0d) * 10) + 10;

0 Likes
Message 3 of 4

SENL1362
Advisor
Advisor

Obviosly too late but nevertheless like to share my solution with you.

 

 

In my Table i'll add content first and remember the longest size.

Then at the end set the column width to the greatest value.

 

I'll use one MText to get the sizes  but there is no need to add this MText to the database

 

using (var mText = new MText())
                {
                    mText.SetDatabaseDefaults(_db);
                    mText.Location = Point3d.Origin;
                    mText.Attachment = AttachmentPoint.BottomLeft;

 

...

Loop

                           mText.TextStyleId = titleCell.TextStyleId.Value;
                            mText.Width = 0;
                            mText.Contents = titleCell.Contents[0].TextString;
                           wCol = (mText.ActualWidth > wCol) ? mText.ActualWidth : wCol;

 

...

    wCol += 2 * TableTextMargin + 1;

   table.Columns[i].Width=wCol;

 

 

table.GenerateLayout();
 //table.RecordGraphicsModified(true);
table.RecomputeTableBlock(true);

 

 

 

 

Message 4 of 4

kerry_w_brown
Advisor
Advisor

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

 

Using  

Autodesk.AutoCAD.GraphicsInterface.TextStyle();

 

without the mangling

and the text does not need to be in the database

 

        //================================================
        [CommandMethod("TextExtentsTest2")]
        static public void TextWidth() {
            var ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            var ts = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();
            ts.FromTextStyleTableRecord("Standard");
            ts.TextSize = 25;
            ed.WriteMessage("AP " + ts.ExtentsBox("AP", true, false, null).MaxPoint.X);
            ed.WriteMessage("\nAPPLE " + ts.ExtentsBox("APPLE", true, false, null).MaxPoint.X);
            ed.WriteMessage("\nAPPLESAUCE " + ts.ExtentsBox("APPLESAUCE", true, false, null).MaxPoint.X);
        }
        //================================================
 

 


// 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