When I insert a DBText object into an open drawing, I can set the HorizontalMode to TextMid and it seems to work fine. If I try the same thing to a database that I open, using the ReadDwgFile method, it doesn't seem to work correctly. When I open the drawing, the newly created Text object appears left justified. Viewing the Properties, it says that the justification is Middle center even though it is still left justified.
Is this an AutoCAD bug or PEBKAC?
I put together a small code sample to illustrate the problem.
[Autodesk.AutoCAD.Runtime.CommandMethod("InsertCenteredText")]
public void InsertCenteredText()
{
string sDwgPath = "C:\\Temp\\TestDrawing.dwg";
if( System.IO.File.Exists(sDwgPath))
{
Database db = new Database(false, true);
db.ReadDwgFile(sDwgPath, System.IO.FileShare.ReadWrite, true, null);
Point3d testPoint = new Point3d(20, 20, 0);
string sText = "TextHorizontalMode.TextCenter";
DBText dbArcText = AddCenterJustifiedText(db, testPoint, sText, 45, 20);
db.SaveAs(sDwgPath, DwgVersion.Current);
db.CloseInput(true);
}
}
public DBText AddCenterJustifiedText(Database db, Point3d aPnt, string sText, double dRotation, double dScale)
{
Transaction aT = null;
BlockTable aBT;
BlockTableRecord aBTR = null;
DBText aText = null;
try
{
aText = new DBText();
aText.TextString = sText;
aText.Height = .09;
aText.HorizontalMode = TextHorizontalMode.TextMid;
aText.VerticalMode = TextVerticalMode.TextVerticalMid;
aText.SetDatabaseDefaults(db);
aText.Rotation = dRotation * Math.PI / 180;
aPnt = new Point3d(aPnt.X, aPnt.Y - (aText.Height * dScale * 1.5), aPnt.Z);
aText.Position = aPnt;
aText.AlignmentPoint = aPnt;
Matrix3d matScale = Matrix3d.Scaling(dScale, aText.Position);
aText.TransformBy(matScale);
aT = db.TransactionManager.StartTransaction();
aBT = (BlockTable)aT.GetObject(db.BlockTableId, OpenMode.ForRead);
aBTR = (BlockTableRecord)aT.GetObject(aBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
aBTR.AppendEntity(aText);
aT.AddNewlyCreatedDBObject(aText, true);
aT.Commit();
}
catch (Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(System.Environment.NewLine + ex.Message + System.Environment.NewLine);
}
finally
{
if (aT != null && aT.IsDisposed == false) { aT.Dispose(); }
if (aBTR != null && aBTR.IsDisposed == false) { aBTR.Dispose(); }
}
return aText;
}
Thank you,
Walter Steed