Code:
Public Function CreateMText(ByVal InsertPoint As Point3d, ByVal Text2Write As String, ByVal Orientation As Double, _
Optional TextWindowWidth As Double = 0.0, Optional TextHeight As Double = 1.0, Optional TextStyleName As String = "", _
Optional ByVal FreistellungAnwenden As Boolean = False, _
Optional ByVal UseFuellfarbeHintergrund As Boolean = True, _
Optional ByVal FreistellungUmlaufenderAbstand As Double = 1.0, _
Optional ByVal FreistellungFarbe As Integer = 255, _
Optional ByVal ColorIndex As Integer = 256, _
Optional ByVal MTextAlign As AttachmentPoint = AttachmentPoint.TopLeft) As Integer ' wenn einmal doch etwas ungültiges kommt, dann auf Standard setzen.
If CInt(MTextAlign) > 9 Then MTextAlign = AttachmentPoint.TopLeft
FreistellungFarbe = SetObjectColor(FreistellungFarbe)
ColorIndex = SetObjectColor(ColorIndex)
'' Get the current document and database
Dim acDoc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim ErrDetail As String = ""
Try
ErrDetail = "Start a transaction"
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
ErrDetail = "Open the Block table for read"
Dim acBlkTbl As BlockTable
ErrDetail = "lock document"
Using acDoc.LockDocument
acBlkTbl = CType(acTrans.GetObject(acCurDb.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead), BlockTable)
ErrDetail = "Open the Block table record Model space for write"
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = CType(acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite), BlockTableRecord)
ErrDetail = "Create a multiline text object"
Using acMText As MText = New MText()
acMText.Location = InsertPoint
acMText.Width = TextWindowWidth
acMText.TextHeight = TextHeight
acMText.Attachment = MTextAlign
acMText.BackgroundFill = UseFuellfarbeHintergrund 'Füllfarbe des Zeichnungshintergrundes verwenden
acMText.UseBackgroundColor = FreistellungAnwenden
acMText.BackgroundScaleFactor = FreistellungUmlaufenderAbstand
acMText.BackgroundFillColor = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Colors.ColorMethod.None, FreistellungFarbe) ' Farbe für den Hintergrund
If TextStyleName.Length > 0 Then
Dim acTextStyleTable As Autodesk.AutoCAD.DatabaseServices.TextStyleTable = Nothing
Dim acTextStyleID As Autodesk.AutoCAD.DatabaseServices.ObjectId
' Transaction starten
Using acTransaction As Autodesk.AutoCAD.DatabaseServices.Transaction = _Database.TransactionManager.StartTransaction()
acTextStyleTable = acTransaction.GetObject(_Database.TextStyleTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
If Not acTextStyleTable.Has(TextStyleName) Then
acTextStyleID = _Database.Textstyle
Else
acTextStyleID = acTextStyleTable(TextStyleName)
End If
End Using
acMText.TextStyleId = acTextStyleID
End If
acMText.ColorIndex = ColorIndex
acMText.Contents = Text2Write
acMText.Rotation = Orientation
acBlkTblRec.AppendEntity(acMText)
acTrans.AddNewlyCreatedDBObject(acMText, True)
End Using 'acMText
End Using 'acDoc.LockDocument
ErrDetail = "Save the changes and dispose of the transaction"
acTrans.Commit()
End Using 'acTrans
Catch ex As Exception
_TryReport.Show("unerwarteter Fehler in EBL.Service > cls_Acad > CreateMText", "ErrDetail: " & ErrDetail & vbCrLf & vbCrLf & "Text2Write: " & Text2Write & vbCrLf & _
"Orientation: " & Orientation.ToString & vbCrLf & _
"TextWindowWidth: " & TextWindowWidth.ToString & vbCrLf & _
"TextHeight: " & TextHeight.ToString & vbCrLf & _
"TextStyleName: " & TextStyleName & vbCrLf & _
"MTextAlign: " & MTextAlign.ToString & " (" & CInt(MTextAlign).ToString & ") nur 1-9 zulässig !" & vbCrLf & ex.ToString)
Return 1
End Try
Return 0
End Function