.NET

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

AttributeCollection from a non-placed block

234 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

*Expert Elite*
norman.yuan
Posts: 944
Registered: ‎04-27-2009
Message 2 of 3 (232 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)

 

Contributor
jdsmit
Posts: 22
Registered: ‎03-25-2011
Message 3 of 3 (207 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

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community