Author Topic: Label Line Length  (Read 2845 times)

0 Members and 1 Guest are viewing this topic.

Nima2018

  • Newt
  • Posts: 30
Label Line Length
« on: January 31, 2019, 03:25:35 AM »
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 .

Code: [Select]
   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();
            }
        }

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Label Line Length
« Reply #1 on: January 31, 2019, 11:49:20 AM »
myTextStyleTableRecord.TextSize = 0;   i THINK YOU NEED THIS TO BE ABLE TO CHANGE THE HEIGHT

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Label Line Length
« Reply #2 on: January 31, 2019, 03:22:11 PM »
Also to add what Byrco pointed out and I might be remembering incorrectly but you might need to append the mtext entity before you set the height.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Label Line Length
« Reply #3 on: February 01, 2019, 07:37:00 PM »
tsr.FileName =Environment.GetEnvironmentVariable("SystemRoot") + @"\Fonts\" + sFilename;

 when I make a font I add the path as well, this is probably why you get the standard one instead.

pera

  • Mosquito
  • Posts: 3
Re: Label Line Length
« Reply #4 on: February 21, 2019, 10:13:18 AM »
Hope it's not to late. You should change following line
Code: [Select]
acMText.Height = 0.035 * line_length;in your code, with next one
Code: [Select]
acMText.TextHeight = 0.035 * line_length;
TextHeight controls text size (height).

Nima2018

  • Newt
  • Posts: 30
Re: Label Line Length
« Reply #5 on: March 07, 2019, 11:14:31 AM »
Hi pera,

It was exactly my problem and it was solved .
Thank you for your advice .