.NET

.NET

Reply
Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 1 of 3 (693 Views)

Explode a BlockReference and convert it's AttributeReferences to DBText or MText

693 Views, 2 Replies
09-17-2010 11:49 AM

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.

Distinguished Contributor
mohnston
Posts: 184
Registered: ‎09-22-2006
Message 2 of 3 (668 Views)

Re: Explode a BlockReference and convert it's AttributeReferences to DBText or M

09-18-2010 11:03 AM in reply to: skeletank

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.

CAD Programming Solutions
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (666 Views)

Re: Explode a BlockReference and convert it's AttributeReferences to DBText or M

09-18-2010 12:03 PM in reply to: skeletank

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.