我想用它的长度标记一行,以便文本高度可以在运行时中与行的长度成比例地调整。
为此,我编写了以下代码,但我的问题是文本高度保持在“标准”文本样式的常量默认值(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[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();
- }
- }
本帖以下内容被隐藏保护;需要你回复后,才能看到! 游客,如果您要查看本帖隐藏内容请 回复 |