Message 1 of 12
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi All,
I know I am missing something very simple here (and i am sure I have done this before, but cannot find the code), but I basically need to get all blocks (dynamic in this case) with a particular name.
I can do it easily by searching through all blocks in the drawing, but it's a bit slow:
Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
'Declare document lock
Dim acDocLock As DocumentLock = acDoc.LockDocument
Dim acCurDb As Database = acDoc.Database
'Start a transaction to open the recently copied title-block file and edit the attributes
Using acTrans As Transaction = acDoc.TransactionManager.StartTransaction
Try
'Get the blocktable from the current open drawing
Dim acBT As BlockTable = acCurDb.BlockTableId.GetObject(OpenMode.ForRead)
'Iterrate through block table to determine if they are layouts or not
For Each btrObjId As ObjectId In acBT
Dim btr As BlockTableRecord = btrObjId.GetObject(OpenMode.ForRead)
If btr.IsLayout Then
Continue For
End If
Dim objIdColl As ObjectIdCollection = btr.GetBlockReferenceIds(True, True)
For Each objId In objIdColl
Dim acBref As BlockReference = objId.GetObject(OpenMode.ForWrite)
Dim myAttValsTB As New Dictionary(Of String, String)
If acBref.Name = "ECDT_IssueStatus" Then
'Do stuff to attributes here...
End If
If acBref.IsDynamicBlock = True Then
Dim acBlkDef As BlockTableRecord = acBref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)
Dim acDynObjId As ObjectId = acBref.DynamicBlockTableRecord
Dim dynBtr As BlockTableRecord = CType(acTrans.GetObject(acDynObjId, OpenMode.ForRead, False), BlockTableRecord)
If dynBtr.Name = "ECDT_IssueStatus" Then
'Do stuff to dynamic properties here...
Dim strDynParameterName As String = "Visibility1"
Dim strDynValue As String = strSubState
Dim acDynBref As BlockReference = objId.GetObject(OpenMode.ForRead)
For Each myDynamProp As DynamicBlockReferenceProperty In _
acDynBref.DynamicBlockReferencePropertyCollection()
If myDynamProp.PropertyName.Equals( _
strDynParameterName, StringComparison.OrdinalIgnoreCase) = True Then
myDynamProp.Value = strDynValue
End If
Next
End If
End If
Next
Next
Finally
End Try
actrans.commit()
acDocLock.Dispose()
End Using
Again, the above works with no problems other than being slow.
So I figured since I already know the block name, I can just use it to get all of the blocks in the drawing. But for some reason, with the code below, the collection (btrids) is never populated. What am I doing wrong?
Dim Blockname As String = "ECDT_IssueStatus"
Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
'Declare document lock
Dim acDocLock As DocumentLock = acDoc.LockDocument
Dim acCurDb As Database = acDoc.Database
Using acTrans As Transaction = acDoc.TransactionManager.StartTransaction
Try
Dim BT As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
If BT.Has(Blockname) Then
Dim btr As BlockTableRecord = acTrans.GetObject(bt(Blockname), OpenMode.ForRead)
Dim btrIDs As ObjectIdCollection = btr.GetBlockReferenceIds(True, False)
For Each id As ObjectId In btrIDs
If Not id.IsEffectivelyErased Then
Dim acBref As BlockReference = id.GetObject(OpenMode.ForWrite)
'Do stuff to block attributes here...
If acBref.IsDynamicBlock = True Then
Dim acBlkDef As BlockTableRecord = acBref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)
Dim acDynObjId As ObjectId = acBref.DynamicBlockTableRecord
Dim dynBtr As BlockTableRecord = CType(acTrans.GetObject(acDynObjId, OpenMode.ForRead, False), BlockTableRecord)
If dynBtr.Name = "ECDT_IssueStatus" Then
'Do stuff to dynamic properties here...
End If
End If
End If
Next
End If
acTrans.Commit()
acDocLock.Dispose()
Finally
End Try
End Using
Solved! Go to Solution.