TheSwamp
Code Red => .NET => Topic started by: waterharbin on March 14, 2013, 05:23:09 AM
-
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?
-
by changing the current text style you'll also change all instances of text which use it... create a new one if required
-
Hi,Will.
How to create one TextStyle, and set it current? Please give me some codes!
-
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)
-
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 (http://www.theswamp.org/index.php?topic=39214.msg445047#msg445047)