admin
发表于: 2015/1/8 19:06 引用 回复 只看该作者 1# TOP
管理员
性别: 男
积分:397
阅读权限:144
帖子: 60
加入时间: 2011/9/1
最后登录: 2024/4/26
01     
02       // Place Text to center using calculated text width
03        // tested on A2010 .NET Framework 3.5
04        [CommandMethod("tcenter")]
05        public static void textToCenter()
06        {
07            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
08            Database db = doc.Database;
09            Autodesk.AutoCAD.GraphicsInterface.TextStyle style = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();
10            byte n;
11 
12            Transaction tr = db.TransactionManager.StartTransaction();
13            try
14            {
15                using (tr)
16                {
17                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
18                    // setup the text 
19                    string text = "1234567890";
20                    // add new dbtext to current space
21                    DBText txt = new DBText();
22                    txt.SetDatabaseDefaults();
23                    txt.TextString = text;
24                    txt.Position = new Point3d(100, 200, 0);
25                    btr.AppendEntity(txt);
26                    tr.AddNewlyCreatedDBObject(txt, true);
27                    // get textstyle of newly created text 
28                    TextStyleTableRecord txtbtr = (TextStyleTableRecord)tr.GetObject(txt.TextStyleId, OpenMode.ForRead);
29                    // copy properties from TextStyleTableRecord and dbtext to temp AcGi.TextStyle (just very limited one for the future calculation)
30                    style.FileName = txtbtr.FileName;
31                    // then copy properties from existing text
32                    style.TextSize = txt.Height;  // txtbtr.TextSize;
33                    style.ObliquingAngle = txt.Oblique;
34                    style.XScale = txt.WidthFactor;
35                    // load temp style record
36                    try
37                    {
38                        n = style.LoadStyleRec;
39                    }
40                    catch return; }// something wrong then exit on error
41 
42                    // set new position of text center, i.e. some dummy point
43                    Point3d cpt = new Point3d(20, -45, 0);
44 
45                    // find out the extents 
46                    Point2d minpt, maxpt;
47                    // get extends of text
48                    Extents2d ex = style.ExtentsBox(text, truetruenull);
49 
50                    minpt = ex.MinPoint;
51                    maxpt = ex.MaxPoint;
52                    // work out the insertion point 
53                    Point3d newpos = cpt - new Vector3d((minpt.X + maxpt.X) / 2.0, (minpt.Y + maxpt.Y) / 2.0, 0);
54                    // change text position to be centered in this point, independently of text alignment mode
55                    txt.Position = newpos;
56                    style.Dispose();// it's not a database resident, so dispose style, optional
57                    tr.Commit();
58                }
59            }
60            catch (System.Exception exc)
61            {
62                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(exc.Message + "\n" + exc.StackTrace);
63            }
64            finally { }
65        }

关键词 DBText 居中, CAD二次开发 修改tag
相关文章

快速回复主题

字体
字号
回顶部
用户名
密 码
用户注册
忘记密码