Creating a text style using VB.NET

By Virupaksha Aithal

Below code shows the procedure to add a new text style to database. Also, the code makes the newly added text style as active/current text style by setting the database property “Database.Textstyle”

       <CommandMethod("TestFont")> _

      Public Shared Sub TestFont()

 

            Dim doc As Document = _

                       Application.DocumentManager.MdiActiveDocument

            Dim db As Database = _

                        doc.Database

            Dim tm As Transaction = _

                            db.TransactionManager.StartTransaction()

 

            Dim ed As Editor = doc.Editor

 

            Using tm

                Dim st As TextStyleTable = CType(tm.GetObject( _

                                db.TextStyleTableId, _

                                OpenMode.ForWrite, False),  _

                                                 TextStyleTable)

                Dim str As TextStyleTableRecord = _

                                        New TextStyleTableRecord()

                str.Name = "MyStyle"

                st.Add(str)

 

                ‘Following are properties to set by default,

                ‘you can also modify them

 

                ‘str.FileName = "txt.shx"

                ‘str.PriorSize = 0.2               

                ‘str.ObliquingAngle = 0.0

                ‘str.XScale = 1.0

                ‘str.TextSize = 0.0

                ‘str.IsVertical = False

                ‘str.IsShapeFile = False

 

                ‘using the font descriptor to set the new font style

                ‘Imports Autodesk.AutoCAD.GraphicsInterface

                str.Font = New FontDescriptor("Times New Roman", _

                                        True, True, Nothing, Nothing)

                tm.AddNewlyCreatedDBObject(str, True)

 

                ‘make as current

                db.Textstyle = str.ObjectId

                tm.Commit()

            End Using

 

        End Sub


Comments

2 responses to “Creating a text style using VB.NET”

  1. Thanks for posting this. I have a couple of questions.
    1. Where do you set the Upside down and backwords values? These are shown on the dialog but not in vb.net from what I can see.
    2. Using the FontDescriptor, it asks for a string, you have entered “Times New Roman”. I have a variable dimed as a string called ‘FontName’ that I want to place here. Each time I try, I get an error saying invalid input. But it works if I put it in the FileName setting. This happens even on True type fonts. And if I want to set a true type font and it’s styles how would I with this method?
    Thanks.

  2. Thank you for share text style code.. it works for my project.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading