.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Specifying Text Style

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
2209 Views, 3 Replies

Specifying Text Style

I am trying to set the text style of a DBText object.

The following code fails when it gets to the line where I try to set the text style (this is done using VB.NET 4.0 and AutoCAD 2012 64-bit).

 

Error reads as follows:

Method not found: 'Void

Autodesk.AutoCAD.DatabaseServices.DBText.set_TextStyle

(Autodesk.AutoCAD.DatabaseServices.ObjectId)'.

 

Code:

Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

Dim acCurDb As Database = acDoc.Database

Using acLockDoc As DocumentLock = acDoc.LockDocument

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction

        Dim acBlkTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

        Dim acBlkTblRec As BlockTableRecord = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

 

        Dim acTxtStylTbl As TextStyleTable = acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForRead)

        Dim sStyleName As String = "STANDARD"

        If acTxtStylTbl.Has("STYLENAME") Then

                sStyleName = "STYLENAME"

        End If

 

        Dim acText As New DBText

        acText.SetDatabaseDefaults()

        acText.TextStyle = acTxtStylTbl(sStyleName)

        acText.TextString = "Some Text Here"

        acText.Height = 12

        acText.Position = New Point3d(0,0,0)

        acText.HorizontalMode = TextHorizontalMode.TextCenter

        acText.VerticalMode = TextVerticalMode.TextVerticalMid

        acText.AlignmentPoint = New Point3d(0,0,0)

        acBlkTblRec.AppendEntity(acText)

        acTrans.AddNewlyCreatedDBObject(acText, True)

        acTrans.Commit()

    End Using

End Using

 

Thank in advance for any information,

Scott

 

3 REPLIES 3
Message 2 of 4
Alfred.NESWADBA
in reply to: Anonymous

Hi,

 

try

acText.TextStyleID = acTxtStylTbl(sStyleName)

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 4
Anonymous
in reply to: Anonymous

Please disregard the previous post. I forgot I was referencing AutoCAD 2009 dll files (the version the users of this current proeject are running) and was trying to test the project in AutoCAD 2012.

 

Sorry,

Scott

Message 4 of 4
Wolfsbane
in reply to: Anonymous

So I understand that this is a VERY old post; however I thought I would add an answer here for ACAD 2020 since this has gone unanswered.
First I would suggest making a check for the style; I did this in a separate function as so it could create the style with some default settings if the TextStyle does not exits

Shared Function GetTextSyleRecord(StyleName As String) As TextStyleTableRecord
	Dim SubStacktrace As StackTrace = New StackTrace(New StackFrame(True))
	UpdateTrans()
	Dim ts As TextStyleTableRecord
	Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
		Dim st As TextStyleTable = CType(acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForRead, False), TextStyleTable)
		If st.Has(StyleName) Then
			Debug_Msg(SubStacktrace, $"Textstyle '{StyleName}' exist; casting object to TextStyleTableRecord", Debug_Debug)
			Dim obj As ObjectId = st.Item(StyleName)
			ts = acTrans.GetObject(st.Item(StyleName), OpenMode.ForRead)
		Else
			Debug_Msg(SubStacktrace, $"Textstyle '{StyleName}' does not exist; creating style", Debug_Warning)
			Dim acTextStyleTblRec As TextStyleTableRecord = acTrans.GetObject(acCurDb.Textstyle, OpenMode.ForWrite)
			Dim acFont As Autodesk.AutoCAD.GraphicsInterface.FontDescriptor = acTextStyleTblRec.Font
			st.UpgradeOpen()
			ts = New TextStyleTableRecord With {
				.Name = StyleName,
				.TextSize = 0.25,
				.Font = New Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("Swis721 BT", False, False, Nothing, Nothing)
			}
			st.Add(ts)
			acTrans.AddNewlyCreatedDBObject(ts, True)
		End If
		acTrans.Commit()
	End Using
	Return ts
End Function


Next a function to insert new text at a given point (Note you need to set the text height to the styles text height, I assumed it would do this since it knows what style it is to follow but it does not)

Shared Function InsertText(ByVal InsertPoint As Point3d, ByVal Txt As String, ByVal TextStyle As String) As Boolean
	UpdateTrans()

	'lookup text style
	Using acDoc.LockDocument()
		Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
			Dim acBlkTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
			Dim acBlkTblRec As BlockTableRecord = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
			Dim TextStyleRec As TextStyleTableRecord = GetTextSyleRecord(TextStyle)
			Using acText As DBText = New DBText()
				acText.Position = InsertPoint
				acText.TextString = Txt
				acText.Height = TextStyleRec.TextSize
				acText.TextStyleId = TextStyleRec.ObjectId
				acBlkTblRec.AppendEntity(acText)
				acTrans.AddNewlyCreatedDBObject(acText, True)
			End Using
			acTrans.Commit()
		End Using
	End Using

	Debug_MarkTimingFunction(SrcStack, New StackTrace(New StackFrame(True)), stopWatch, "finished proccess")
	Return True
End Function


my 'UpdateTrans' function just makes sure my module variables for the document are point correct so I dont have to keep redefining them over and over for every possibly new transaction for newly opened document

 Shared Function UpdateTrans(Optional ByVal Doc As Document = Nothing) As Boolean
	Try
		If IsNothing(Doc) Then
			acDoc = Application.DocumentManager.MdiActiveDocument
		Else
			acDoc = Doc
		End If
		acCurDb = acDoc.Database
		acEditor = acDoc.Editor
	Catch ex As Exception
		Return False
	End Try
	Return True
End Function

Hope this helps the next person to stumble across this post.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost