Specifying text alignment

<?xml encoding=”UTF-8″>By Daniel Du

Issue

How do I set up or change a text entity’s alignment?

Solution

When aligning text you need to use the TextAlignmentPoint property as shown in
the following sample code:

<font color="#000000">Public Sub test()<br>   Dim Text As AcadText<br>   Dim curnam As String<br>   Dim inpt(0 To 2) As Double<br>   Dim ht As Double<br>   <br>   curnam = "This is a string"<br>   inpt(0) = 5<br>   inpt(1) = 5<br>   inpt(2) = 0<br>  <br>   ht = 0.5<br>   <br>   Set Text = ThisDrawing.ModelSpace.AddText(curnam, inpt, ht)<br>   Text.Alignment = acAlignmentMiddleCenter<br>   Text.TextAlignmentPoint = inpt<br>End Sub</font>

 

For .net API, you can use DBText.AlignmentPoint property.Following information comes from the .net API document:

This function sets pt to be the alignment point for the text object.

If vertical mode is AcDb::kTextBase and horizontal mode is either AcDb::kTextLeft, AcDb::kTextAlign, or AcDb::kTextFit, then the position point (DXF group code 10) is the insertion point for the text object and, for AcDb::kTextLeft, the alignment point is automatically calculated based on the other parameters in the text object. Any setting made by this function are replaced by the newly calculated value.

For all other vertical and horizontal mode combinations, the alignment point is used as the insertion point of the text and the position point is automatically calculated based on the other parameters in the text object.

The alignment point value is the WCS equivalent of DXF group code 11.

Returns Acad::eOk if successful, or Acad::eNotApplicable if the text object’s horizontal mode is AcDb::kTextLeft and vertical mode is AcDb::kTextBase.

Here is the code in .net API:

<font face="新宋体"><span><font color="#0000ff"><font>public</font></font></span><font><font color="#000000"> </font><span><font color="#0000ff">void</font></span><font color="#000000"> MyCommand() </font><span><font color="#008000">// This method can have any name</font></span><br></font></font><font><font face="新宋体"><font color="#000000">{<br>    </font><span><font color="#2b91af">Document</font></span><font color="#000000"> doc = </font><span><font color="#2b91af">Application</font></span></font><font face="新宋体"><font color="#000000">.DocumentManager.MdiActiveDocument;<br>    </font><span><font color="#2b91af">Editor</font></span></font><font face="新宋体"><font color="#000000"> ed = doc.Editor;<br>    </font><span><font color="#2b91af">Database</font></span></font><font face="新宋体"><font color="#000000"> db = doc.Database;<br> <br>    </font><span><font color="#008000">//align point</font></span><br><font color="#000000">    </font><span><font color="#2b91af">Point3d</font></span><font color="#000000"> pt = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">Point3d</font></span></font><font face="新宋体"><font color="#000000">(15, 15, 0);<br> <br>    </font><span><font color="#0000ff">using</font></span><font color="#000000"> (</font><span><font color="#2b91af">DBText</font></span><font color="#000000"> text = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">DBText</font></span></font><font face="新宋体"><font color="#000000">())<br>    {<br>        text.TextString = </font><span><font color="#a31515">"This is a db text"</font></span></font><font face="新宋体"><font color="#000000">;<br> <br>        text.VerticalMode = </font><span><font color="#2b91af">TextVerticalMode</font></span></font><font face="新宋体"><font color="#000000">.TextVerticalMid;<br>        text.HorizontalMode = </font><span><font color="#2b91af">TextHorizontalMode</font></span></font><font face="新宋体"><font color="#000000">.TextCenter;<br>        text.AlignmentPoint = pt;<br>                <br>        AddToModelSpace(text, db);<br>    }<br>          <br>}<br> </font><br><span><font color="#0000ff">private</font></span><font color="#000000"> </font><span><font color="#0000ff">static</font></span><font color="#000000"> </font><span><font color="#0000ff">void</font></span><font color="#000000"> AddToModelSpace(</font><span><font color="#2b91af">Entity</font></span><font color="#000000"> ent, </font><span><font color="#2b91af">Database</font></span></font><font face="新宋体"><font color="#000000"> db)<br>{<br>    </font><span><font color="#0000ff">using</font></span><font color="#000000"> (</font><span><font color="#2b91af">Transaction</font></span></font><font face="新宋体"><font color="#000000"> trans = db.TransactionManager.StartTransaction())<br>    {<br>        </font><span><font color="#2b91af">BlockTable</font></span><font color="#000000"> bt = (</font><span><font color="#2b91af">BlockTable</font></span></font><font face="新宋体"><font color="#000000">)trans.GetObject(<br>            db.BlockTableId, </font><span><font color="#2b91af">OpenMode</font></span></font><font face="新宋体"><font color="#000000">.ForRead);<br>        </font><span><font color="#2b91af">BlockTableRecord</font></span></font><font face="新宋体"><font color="#000000"> modelSpace = trans.GetObject(<br>            bt[</font><span><font color="#2b91af">BlockTableRecord</font></span></font><font face="新宋体"><font color="#000000">.ModelSpace], <br>            </font><span><font color="#2b91af">OpenMode</font></span><font color="#000000">.ForWrite) </font><span><font color="#0000ff">as</font></span><font color="#000000"> </font><span><font color="#2b91af">BlockTableRecord</font></span></font></font><font face="新宋体"><font><font color="#000000">;<br> <br>        modelSpace.AppendEntity(ent);<br>        trans.AddNewlyCreatedDBObject(ent, </font><span><font color="#0000ff">true</font></span><font color="#000000">);<br>        trans.Commit();<br>    }<br>}</font></font></font>

Hope this helps!


Comments

3 responses to “Specifying text alignment”

  1. Jason DeVore Avatar
    Jason DeVore

    Thank you!

  2. Jason DeVore Avatar
    Jason DeVore

    This post single-handedly helped me figure out why I was getting exceptions when attempting to align my text in AutoCAD using the .NET API. It seems that there is a conflict when attempting to set both position and alignmentpoint properties. (Only alignmentpoint needs to be set, apparently.) The developer’s guide doesn’t give much detail to this tidbit!

  3. Prakash B Bajaj Avatar
    Prakash B Bajaj

    Danial Du
    Thank you so much for specifying text alignment

Leave a Reply to Jason DeVoreCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading