admin
发表于: 2015/1/8 19:06 引用 回复 只看该作者 1# TOP
管理员
性别: 男
积分:397
阅读权限:144
帖子: 60
加入时间: 2011/9/1
最后登录: 2024/4/26
    
       // Place Text to center using calculated text width
        // tested on A2010 .NET Framework 3.5
        [CommandMethod("tcenter")]
        public static void textToCenter()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Autodesk.AutoCAD.GraphicsInterface.TextStyle style = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();
            byte n;

            Transaction tr = db.TransactionManager.StartTransaction();
            try
            {
                using (tr)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    // setup the text 
                    string text = "1234567890";
                    // add new dbtext to current space
                    DBText txt = new DBText();
                    txt.SetDatabaseDefaults();
                    txt.TextString = text;
                    txt.Position = new Point3d(100, 200, 0);
                    btr.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    // get textstyle of newly created text 
                    TextStyleTableRecord txtbtr = (TextStyleTableRecord)tr.GetObject(txt.TextStyleId, OpenMode.ForRead);
                    // copy properties from TextStyleTableRecord and dbtext to temp AcGi.TextStyle (just very limited one for the future calculation)
                    style.FileName = txtbtr.FileName;
                    // then copy properties from existing text
                    style.TextSize = txt.Height;  // txtbtr.TextSize;
                    style.ObliquingAngle = txt.Oblique;
                    style.XScale = txt.WidthFactor;
                    // load temp style record
                    try
                    {
                        n = style.LoadStyleRec;
                    }
                    catch { return; }// something wrong then exit on error

                    // set new position of text center, i.e. some dummy point
                    Point3d cpt = new Point3d(20, -45, 0);

                    // find out the extents 
                    Point2d minpt, maxpt;
                    // get extends of text
                    Extents2d ex = style.ExtentsBox(text, true, true, null);

                    minpt = ex.MinPoint;
                    maxpt = ex.MaxPoint;
                    // work out the insertion point 
                    Point3d newpos = cpt - new Vector3d((minpt.X + maxpt.X) / 2.0, (minpt.Y + maxpt.Y) / 2.0, 0);
                    // change text position to be centered in this point, independently of text alignment mode
                    txt.Position = newpos;
                    style.Dispose();// it's not a database resident, so dispose style, optional
                    tr.Commit();
                }
            }
            catch (System.Exception exc)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(exc.Message + "\n" + exc.StackTrace);
            }
            finally { }
        }

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

快速回复主题