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
Solved! Go to Solution.
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)
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