Dim TargetDocument As Document = Application.DocumentManager.MDIActiveDocument
Dim TargetDocumentDB As Database = TargetDocument.Database
Using dockLock As DocumentLock = TargetDocument.LockDocument()
Using transOuterMost As Transaction = TargetDocumentDB.TransactionManager.StartTransaction()
...
' INSIDE BlockDef Class
'**The purpose of the BlockDef class is so that other classes that work with BlockReferences based on each block can do quick lookups against the relevant BlockDef's dictionaries to access/modify the AttributeReferences or DynamicBlockReferenceProperties w/o enumerating each time.
' At this Point, the block has been imported into the TargetDocument.
'newBTR: BlockTableRecord of Block Recently Added to TargetDocument
'idBlockTableRecord: ObjectId of BlockTableRecord recently added to BlockTable of TargetDocument's Database
'AttIdsByTag: Dictionary with Non-Constant Attribute Reference Tag as key, AttributeDefinition ObjectId as value
'AttIdxByTag: Dictionary with Non-Constant Attribute Reference Tag as key, AttributeDefinition 0-based Index (not counting constant) as value
'DynamicPropertyIndexByName: Dictionary with DynamicBlockReferenceProperty PropertyName as Key (skipping "ORIGIN"), and 0-based Index as value)
Using oTrans As Transaction = idBlockTableRecord.Database.TransactionManager.StartTransaction()
Dim eBRE As IEnumerator = newBTR.GetEnumerator
Dim iIndex As Integer = 0
Dim oEnt As Entity
Dim oAD As AttributeDefinition
While eBRE.MoveNext
oEnt = DirectCast(oTrans.GetObject(eBRE.Current, OpenMode.ForRead), Entity)
If TypeOf oEnt Is AttributeDefinition Then
oAD = DirectCast(oEnt, AttributeDefinition)
If Not oAD.Constant Then
doc.WriteLine("Adding NonConstant Attribute:{Tag:'" + oAD.Tag + "', Index:" + iIndex.ToString + ", ObjectId:" + HexIdVal(oAD.ObjectId) + "} to Dictionaries")
AttIdsByTag.Add(oAD.Tag, oAD.ObjectId)
AttIdxByTag.Add(oAD.Tag, iIndex )
iIndex += 1
End If
End If
End While
If newBTR.IsDynamicBlock Then
iIndex = 0
'Cause of Failures since AutoCAD 2020. If This Section (lines 35 through 47) is Commented out, no issues.
Using tempBR As New BlockReference(Point3d.Origin, idBlockTableRecord)
For Each cDBRP As DynamicBlockReferenceProperty In tempBR.DynamicBlockReferencePropertyCollection
iIndex += 1
If cDBRP.PropertyName.ToUpper = "ORIGIN" Then
Debug.WriteLine("Skipping Index " + iIndex.ToString + ", <ORIGIN>") ' < Updated per n.yaun's post immediately below
Continue For
End If
DynamicPropertyIndexByName.Add(cDBRP.PropertyName.ToUpper, iIndex) ' < Updated per n.yaun's post immediately below
doc.WriteLine("Adding DynamicBlockReferenceProperty:{PropertyName.ToUpper():'" + cDBRP.PropertyName.ToUpper + "', Index:" + iIndex.ToString + "} to Dictionaries")
Next
tempBR.Dispose()
End Using
'End Cause of Failures.
End If
End Using
...
Try
transOuterMost.Commit()
Catch ex As System.Exception
End Try
End Using
End Using