I want to label a line with its length so that text height can be adjusted in proportion to the length of the line in run time .
To this end, I wrote the following code, but my problem is that the text height stays on the constant default value (TextSize=0.2) of "Standard" Text Style and does not change.
Please advice me .
private static void AddLine_Blue(double x1,double y1,double x2,double y2)
{
// Get the current document and database
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
double line_length;
double line_angle;
using (Line acLine = new Line(new Point3d(x1, y1, 0),
new Point3d(x2, y2, 0)))
{
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
line_length=Math.Round(acLine.Length,2);
line_angle = Math.Round(acLine.Angle, 6);
acLine.Color =Autodesk.AutoCAD.Colors. Color.FromColorIndex(ColorMethod.ByAci, 5);
TextStyleTable myTextStyleTable = acTrans.GetObject(acDoc.Database.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
if (!myTextStyleTable.Has("Arial"))
{
myTextStyleTable.UpgradeOpen();
TextStyleTableRecord myTextStyleTableRecord = new TextStyleTableRecord();
myTextStyleTableRecord.FileName = "Arial.ttf";
myTextStyleTableRecord.Name = "Arial";
myTextStyleTableRecord.TextSize = Math.Round(line_length, 2) * 0.035;
myTextStyleTable.Add(myTextStyleTableRecord);
acTrans.AddNewlyCreatedDBObject(myTextStyleTableRecord, true);
}
else
{
myTextStyleTableRecord.TextSize = Math.Round(line_length, 2) * 0.035;
}
var curTextstyle = acCurDb.Textstyle;
if (myTextStyleTable.Has("Arial") )
{
curTextstyle = myTextStyleTable["Arial"];
}
// Create a multiline text object
using (MText acMText = new MText())
{
double xm = (x1 + x2) / 2;
double ym = (y2 + y1) / 2;
acMText.Location = new Point3d(xm, ym, 0);
if (Math.Round(line_length, 2) - (int)(Math.Round(line_length, 2)) == 0)
{
acMText.Contents = Convert.ToString(line_length) + ".00";
}
else
{
acMText.Contents = Convert.ToString(line_length);
}
acMText.Rotation = line_angle;
acMText.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 3);
acMText.Attachment = AttachmentPoint.BottomCenter;
acMText.SetDatabaseDefaults();
acMText.TextStyleId = curTextstyle;
acMText.Height = 0.035 * line_length;
acBlkTblRec.AppendEntity(acMText);
acTrans.AddNewlyCreatedDBObject(acMText, true);
}
}
// Save the new object to the database
acTrans.Commit();
}
}