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

AttributeCollection from a non-placed block

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
jdsmit
492 Views, 2 Replies

AttributeCollection from a non-placed block

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

2 REPLIES 2
Message 2 of 3
norman.yuan
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)

 

Message 3 of 3
jdsmit
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

 

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