admin | ||
管理員
![]() 性別: 男
帖子: 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 { } } 相關文章
|
|
|