Author Topic: What is the best way to draw a Single-Line text with assignd font.  (Read 1544 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
Hi.
I want to create a funtion that draw a Single-Line text with assigned font. So,everytime I want to draw some text, I just call this function. Here is what I get.
Code: [Select]
Public Shared Function AddTextArial(ByVal Location As Point3d, ByVal TextString As String, ByVal Height As Double) As ObjectId
        Try

            '' Get the current document and database
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database

            'Set the font
            '' Start a transaction
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                '' Open the current text style for write
                Dim acTextStyleTblRec As TextStyleTableRecord
                acTextStyleTblRec = acTrans.GetObject(acCurDb.Textstyle, _
                                                      OpenMode.ForWrite)

                '' Get the current font settings
                Dim acFont As Autodesk.AutoCAD.GraphicsInterface.FontDescriptor
                acFont = acTextStyleTblRec.Font

                '' Update the text style's typeface with "Arial"
                Dim acNewFont As Autodesk.AutoCAD.GraphicsInterface.FontDescriptor
                acNewFont = New  _
                  Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("Arial", False, False, acFont.CharacterSet, acFont.PitchAndFamily)

                acTextStyleTblRec.Font = acNewFont

                acDoc.Editor.Regen()

                '' Save the changes and dispose of the transaction
                acTrans.Commit()
            End Using

            'Font is set


            'draw the text
            Dim ent As New DBText()
            ent.Position = Location
            ent.TextString = TextString
            ent.Height = Height
            Dim entId As ObjectId = AppendEntity(ent)
            Return entId
        Catch ex As Exception
            Dim NullId As ObjectId = ObjectId.Null
            Return NullId
        End Try
    End Function
I overload the AppendEntity to return the ObjectId of the text. This is how I do it, I do not want to modify my AppendEntity.
Code: [Select]
Public Shared Function AppendEntity(ByVal ent As Entity) As ObjectId
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim entId As ObjectId
        Using trans As Transaction = db.TransactionManager.StartTransaction
            Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            entId = btr.AppendEntity(ent)
            trans.AddNewlyCreatedDBObject(ent, True)
            trans.Commit()
        End Using
        Return entId
    End Function
But It seems that I will have to set the font everytime I call this function. How to improve?
« Last Edit: March 14, 2013, 05:27:20 AM by 闻仲 »

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: What is the best way to draw a Single-Line text with assignd font.
« Reply #1 on: March 14, 2013, 09:17:12 AM »
by changing the current text style you'll also change all instances of text which use it... create a new one if required

waterharbin

  • Guest
Re: What is the best way to draw a Single-Line text with assignd font.
« Reply #2 on: March 15, 2013, 09:52:28 AM »
Hi,Will.
How to create one TextStyle, and set it current? Please give me some codes!

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: What is the best way to draw a Single-Line text with assignd font.
« Reply #3 on: March 15, 2013, 12:38:49 PM »
I've never created one via .NET sorry.  I just know it's a hard pointer from text objects to the TextStyleTableRecord, so changing the style def will change all instances of text using it (much like a block def)

n.yuan

  • Bull Frog
  • Posts: 280
Re: What is the best way to draw a Single-Line text with assignd font.
« Reply #4 on: March 15, 2013, 03:09:27 PM »
Hi,Will.
How to create one TextStyle, and set it current? Please give me some codes!

if you had searched this forum, you would have easily found this posted question and some smaple code:

http://www.theswamp.org/index.php?topic=39214.msg445047#msg445047