.NET

.NET

Reply
Active Contributor
jdsmit
Posts: 26
Registered: ‎03-25-2011
Message 1 of 3 (258 Views)
Accepted Solution

AttributeCollection from a non-placed block

258 Views, 2 Replies
04-19-2011 05:08 AM

I need to get the AttributeCollection of a named block wich is not been placed in the drawing, but it does exist

        Dim paaltype As String

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

        Dim acCurDb As Database = acDoc.Database

        Dim acDocEd As Editor = acDoc.Editor

 

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                paaltype = "paal01"

                Dim acBlkTbl As BlockTable

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

                If acBlkTbl.Has(paaltype) Then

                    ‘block exist, how to get the attributecollection

                End If

            acTrans.Dispose()

        End Using

Thanks a lot.

With some modifications it work perfect.

Here is my working code:

 

       Dim paaltype As String

        lvPalen.Items.Clear()

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

        Dim acCurDb As Database = acDoc.Database

        Dim acDocEd As Editor = acDoc.Editor

 

 

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

            For i = 1 To 36

                paaltype = "paal" + Format(i, "00")

                Dim acBlkTbl As BlockTable

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

                If acBlkTbl.Has(paaltype) Then

                    Dim blk As BlockTableRecord = acBlkTbl(paaltype).GetObject(OpenMode.ForRead)

                    If blk.HasAttributeDefinitions Then

                        For Each id2 As ObjectId In blk

                            Dim att As AttributeDefinition = TryCast(acTrans.GetObject(id2, OpenMode.ForRead), AttributeDefinition)

                            If att IsNot Nothing Then

                                If att.Tag.ToUpper = "AFMETING" Then lvPalen.Items.Add(att.TextString, i - 1)

                            End If

                        Next

                    End If

                End If

            Next

            acTrans.Dispose()

        End Using

 

*Expert Elite*
norman.yuan
Posts: 1,064
Registered: ‎04-27-2009
Message 2 of 3 (256 Views)

Re: AttributeCollection from a non-placed block

04-19-2011 06:46 AM in reply to: jdsmit

If by "AttributeCollection", you mean the class Autodesk.AutoCAD.DatabaseServices.AttribueCollection, then, you cannot: non-placed block is a block definition, a BlockTableRecord, it DOES not hold an AttributeCollection - a collection of AttributeReference. BlockTableRecord itself is a collection of entities, including possible 0 or more AttributeDefinitions. To see if a block has attrbutions and collect a list of them, you do:

 

...

Dim attrs As New List(Of AttributeDefinition)()

 

If acBlkTbl.Has(paaltype) Then

    Dim blk As BlockTableRecord=acBlkTbl(paaltype)

    If blk.HasAttributeDefinitions Then

        For Each id As ObjectId in blk

            Dim att As AttributeDefinition=TryCast(acTrans.GetObject(id,OpenMode.ForRead),AttributeDefinition)

            If att IsNot Nothing Then

                attrs.Add(att)

            End

        Next

    End If

End If

 

MessageBox.Show(attr.Count & " attributes found in block " & paaltype)

 

Active Contributor
jdsmit
Posts: 26
Registered: ‎03-25-2011
Message 3 of 3 (231 Views)

Re: AttributeCollection from a non-placed block

04-21-2011 01:29 AM in reply to: norman.yuan

Thanks a lot.

With some modifications it work perfect.

Here is my working code:

 

       Dim paaltype As String

        lvPalen.Items.Clear()

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

        Dim acCurDb As Database = acDoc.Database

        Dim acDocEd As Editor = acDoc.Editor

 

 

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

            For i = 1 To 36

                paaltype = "paal" + Format(i, "00")

                Dim acBlkTbl As BlockTable

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

                If acBlkTbl.Has(paaltype) Then

                    Dim blk As BlockTableRecord = acBlkTbl(paaltype).GetObject(OpenMode.ForRead)

                    If blk.HasAttributeDefinitions Then

                        For Each id2 As ObjectId In blk

                            Dim att As AttributeDefinition = TryCast(acTrans.GetObject(id2, OpenMode.ForRead), AttributeDefinition)

                            If att IsNot Nothing Then

                                If att.Tag.ToUpper = "AFMETING" Then lvPalen.Items.Add(att.TextString, i - 1)

                            End If

                        Next

                    End If

                End If

            Next

            acTrans.Dispose()

        End Using

 

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.