Author Topic: Place Text to center using calculated text width  (Read 1619 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Place Text to center using calculated text width
« on: March 12, 2013, 05:52:23 PM »
   C# 
       
Code: [Select]
   
       // 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 { }
        }

   VB.NET

Code: [Select]
      ' Place Text to center using calculated text width
        ' tested on A2010 .NET Framework 3.5
        <CommandMethod("tcenter")> _
        Public Shared Sub textToCenter()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim style As New Autodesk.AutoCAD.GraphicsInterface.TextStyle()
            Dim n As Byte

            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Try
                Using tr
                    Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                    ' setup the text
                    Dim text As String = "1234567890"
                    ' add new dbtext to current space
                    Dim txt As 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
                    Dim txtbtr As TextStyleTableRecord = DirectCast(tr.GetObject(txt.TextStyleId, OpenMode.ForRead), TextStyleTableRecord)
                    ' 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
                    End Try
                    ' something wrong then exit on error
                    ' set new position of text center, i.e. some dummy point
                    Dim cpt As New Point3d(20, -45, 0)

                    ' find out the extents
                    Dim minpt As Point2d, maxpt As Point2d
                    ' get extends of text
                    Dim ex As Extents2d = style.ExtentsBox(text, True, True, Nothing)

                    minpt = ex.MinPoint
                    maxpt = ex.MaxPoint
                    ' work out the insertion point
                    Dim newpos As Point3d = 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()
                End Using
            Catch exc As System.Exception
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(exc.Message & vbLf & exc.StackTrace)
            Finally
            End Try
        End Sub