Nima2018 发表于 2019-1-31 03:25:35

标签行长度

我想用它的长度标记一行,以便文本高度可以在运行时中与行的长度成比例地调整。
为此,我编写了以下代码,但我的问题是文本高度保持在“标准”文本样式的常量默认值(TextSize=0.2)上,并且不会更改。
请建议我 .
   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,
                                                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();
            }
      }

**** Hidden Message *****

Bryco 发表于 2019-1-31 11:49:20

myTextStyleTableRecord.TextSize = 0;我认为你需要这个能够改变高度

Bryco 发表于 2019-1-31 15:22:11

此外,为了补充Byrco指出的内容,我可能记错了,但您可能需要在设置高度之前附加多行文字实体。

pera 发表于 2019-2-1 19:37:00

tsr.FileName=Environment.GetEnvironmentVariable("SystemRoot") + @"\Fonts\"+sFilename;
当我制作字体时,我也添加了路径,这可能就是为什么你得到标准字体的原因。

Nima2018 发表于 2019-2-21 10:13:18

希望不会太晚。您应该将以下行更改为
acMText.Height = 0.035 * line_length;
在您的代码中,使用下一个
acMText.TextHeight = 0.035 * line_length;
文本高度
控制文本大小(高度)。

Nima2018 发表于 2019-3-7 11:14:31

嗨pera,
这正是我的问题,已经解决了。
谢谢你的建议。
页: [1]
查看完整版本: 标签行长度