Set ColumnType of MText on creation of MText Object

<?xml encoding=”UTF-8″>By Deepak Nadig

We recently had a query regarding eNotApplicable runtime exception thrown trying to set ColumnType when MText is created as in the snippet: 

MText mytext = new MText();
mytext.SetDatabaseDefaults();
mytext.Contents = "mytext";
mytext.Layer = "0";
mytext.ColorIndex = 3;
mytext.Location = new Point3d(0.0, 0.0, 0.0);
mytext.ColumnType = ColumnType.NoColumns;

To avoid this exception it is required to set MText.Width value greater than 0.0 before setting the ColumnType. Below command sets ColumnType 

[CommandMethod(<span>"TESTMTEXT"</span>)]
<span>public</span> <span>static</span> <span>void</span> testMtext()
{
Document doc <span>=</span> Autodesk.AutoCAD.ApplicationServices.Core.<span>Application</span>.DocumentManager.MdiActiveDocument;
<span>if</span> (doc <span>=</span><span>=</span> <span>null</span>)
<span>return</span>;
<span>try</span>
{
using (Transaction tr <span>=</span> doc.TransactionManager.StartTransaction())
{
MText mytext <span>=</span> <span>new</span> MText();
mytext.SetDatabaseDefaults();
mytext.Contents <span>=</span> <span>"mytext"</span>;
mytext.Layer <span>=</span> <span>"0"</span>;
mytext.ColorIndex <span>=</span> <span>3</span>;
mytext.Location <span>=</span> <span>new</span> Point3d(<span>0.0</span>, <span>0.0</span>, <span>0.0</span>);
mytext.Width <span>=</span> <span>0.0</span>; <span>// don't forget me </span>
<span>if</span>(mytext.Width > <span>0.0</span>)
mytext.ColumnType <span>=</span> ColumnType.NoColumns;
BlockTable bt <span>=</span> (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr <span>=</span> (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(mytext);
tr.AddNewlyCreatedDBObject(mytext, <span>true</span>);
tr.Commit();
}
}
<span>catch</span> (<span>System</span>.Exception ex)
{
doc.Editor.WriteMessage(<span>"<span>n</span>Error: "</span> <span>+</span> ex.ToString());
}
}

It is notable that trying to change ColumnType of selected MText without setting Defined width value above 0.0 is not possible in UI :

MtextSelection


Comments

One response to “Set ColumnType of MText on creation of MText Object”

  1. Evgueny Fyodorov Avatar
    Evgueny Fyodorov

    Great thanx!
    I myself would not have guessed.
    Did so:
    mText.Width = 1;
    mText.ColumnType = ColumnType.NoColumns;
    mText.Width = 0;

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading