Community
What would be the best way to create DBText or MText from the existing AttributeReferences of a BlockReference? I've seen it done in the burst.lsp files from express tools but I was wondering how I could do that part in .NET.
You will get some information from the attribute definition and some from the attribute reference.
From the definition you will get the text style, alignment etc.
From the reference you will get the textstring and location.
You should be able to build a matching mtext object from that.
Perhaps something like this
Not tested on dynamic blocks as well as
with different attribute alignments
Public Shared Sub AttDefToText() Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim ed As Editor = doc.Editor Dim db As Database = doc.Database Using docloc As DocumentLock = doc.LockDocument() Using tr As Transaction = db.TransactionManager.StartTransaction() Try Dim peo As PromptEntityOptions = New PromptEntityOptions(vbCr & "Select block") peo.SetRejectMessage(vbCr & "Selected object is not a block") peo.AddAllowedClass(GetType(BlockReference), True) Dim id As ObjectId = ed.GetEntity(peo).ObjectId Using blkref As BlockReference = CType(tr.GetObject(id, OpenMode.ForWrite), BlockReference) If blkref Is Nothing Then Return End If Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) Dim btr As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord) Dim ids As DBObjectCollection = New DBObjectCollection Dim nids As DBObjectCollection = New DBObjectCollection doc.TransactionManager.QueueForGraphicsFlush() blkref.Explode(ids) For Each xent As Entity In ids btr.AppendEntity(xent) tr.AddNewlyCreatedDBObject(xent, True) If TypeOf (xent) Is AttributeDefinition Then nids.Add(xent) End If Next For Each obj As DBObject In nids Dim att As AttributeDefinition = CType(tr.GetObject(obj.ObjectId, OpenMode.ForWrite), AttributeDefinition) Dim text As String = att.TextString Dim pos As Point3d = att.Position Dim alpt As Point3d = att.AlignmentPoint Dim vert As TextVerticalMode = att.VerticalMode Dim horz As TextHorizontalMode = att.HorizontalMode If att.IsMTextAttributeDefinition Then Dim mtxt As MText = New MText() mtxt.SetDatabaseDefaults() mtxt.SetPropertiesFrom(att) mtxt.Location = pos mtxt.TextHeight = att.Height mtxt.Width = 0.0 mtxt.TextStyle = att.TextStyle mtxt.Contents = att.TextString Dim attpt As AttachmentPoint = att.MTextAttributeDefinition.Attachment If attpt <> AttachmentPoint.BaseLeft Then mtxt.Attachment = attpt End If mtxt.Rotation = att.MTextAttributeDefinition.Rotation btr.AppendEntity(mtxt) tr.AddNewlyCreatedDBObject(mtxt, True) Else Dim txt As DBText = New DBText() txt.SetDatabaseDefaults() txt.SetPropertiesFrom(att) txt.Position = pos txt.Height = att.Height txt.TextStyle = att.TextStyle txt.TextString = att.TextString txt.VerticalMode = vert txt.HorizontalMode = horz txt.AdjustAlignment(db) If horz <> TextHorizontalMode.TextLeft Then txt.AlignmentPoint = att.AlignmentPoint txt.Position = txt.Position End If txt.Rotation = att.Rotation btr.AppendEntity(txt) tr.AddNewlyCreatedDBObject(txt, True) End If Next For Each obj As DBObject In nids obj.UpgradeOpen() obj.Erase() obj.Dispose() obj = Nothing Next blkref.Erase() End Using doc.TransactionManager.FlushGraphics() ed.UpdateScreen() tr.Commit() Catch ex As Autodesk.AutoCAD.Runtime.Exception MsgBox(ex.Message & vbCr & ex.StackTrace) End Try End Using End Using End Sub
~'J'~