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.
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.
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?