Update Dynamic block definition from other file's block definition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I use the the below to update block definitions from other drawings. It works on both regular and dynamic blocks. For some reason when the block is dynamic I get errors when auditing along with the "Program Errors were detected. Recommend that you save your work and restart the program." Everything else works as expected. Audit error example directly below. Does anyone have any insight on why I'm getting audit errors when updating dynamic block definitions?
Pass 1 200 objects auditedAcDbSortentsTable(1886)
AcDbSortEntsTable Block Id not valid fixed.
Auditing Entities Pass 2
Pass 2 100 objects auditedAcDbBlockBasepointParameter(1849)
could not be repaired. It will be Erased.
AcDbBlockLinearParameter(184A) could not be repaired. It will be Erased.
AcDbBlockLinearGrip(184B) could not be repaired. It will be Erased.
AcDbBlockGripExpr(184C) could not be repaired. It will be Erased.
AcDbBlockGripExpr(184D) could not be repaired. It will be Erased.
AcDbBlockArrayAction(184E) could not be repaired. It will be Erased.
Pass 2 200 objects audited
Auditing Blocks
3 Blocks audited
Total errors found 1 fixed 1
Erased 6 objects
Sub UpdateBlockDefinitions(myTargetDB As Database, DrawingFilePath As String, BlockName As String)
Dim myTargetDoc As Document = DocumentManager.MdiActiveDocument
Dim myObjIDColl As New ObjectIdCollection
myObjIDColl.Clear()
'Read the drawing into a side database to update block definition
Dim mySourceDB As New Database(False, True)
mySourceDB.ReadDwgFile(DrawingFilePath, FileOpenMode.OpenForReadAndAllShare, True, "")
Using mySourceTrans As Transaction = mySourceDB.TransactionManager.StartTransaction
' Open the Block table for read
Dim mySourceBT As BlockTable = CType(mySourceTrans.GetObject(mySourceDB.BlockTableId, OpenMode.ForRead), BlockTable)
For Each myID As ObjectId In mySourceBT
Dim mySourceBTR As BlockTableRecord = CType(mySourceTrans.GetObject(myID, OpenMode.ForRead), BlockTableRecord)
'Filter out modelspace and paperspaces
If Not mySourceBTR.Name.StartsWith("*") And Not mySourceBTR.Name.StartsWith("_") Then
If mySourceBTR.Name = BlockName Then
If mySourceBT.Has(mySourceBTR.Name) Then
'Add blocktablerecord to collection for cloning
myObjIDColl.Add(myID)
End If
End If
End If
Next
mySourceTrans.Commit()
End Using
If myObjIDColl.Count > 0 Then
'lock target document
Using myTargetLock As DocumentLock = myTargetDoc.LockDocument
Using myTargetTrans As Transaction = myTargetDB.TransactionManager.StartTransaction
' Open the Block table for read
Dim myTargetBT As BlockTable = CType(myTargetTrans.GetObject(myTargetDB.BlockTableId, OpenMode.ForRead), BlockTable)
' Open the Block table record Model space for read
Dim myTargetBTR As BlockTableRecord = CType(myTargetTrans.GetObject(myTargetBT(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)
Dim BRobjectid As List(Of ObjectId) = CType(GetBlockReference(myTargetDB, BlockName), Global.System.Collections.Generic.List(Of Global.Autodesk.AutoCAD.DatabaseServices.ObjectId))
Dim myMap As New IdMapping
'Clone the collection from the tempDB blocktable to the current drawing's blocktable
mySourceDB.WblockCloneObjects(myObjIDColl, myTargetDB.CurrentSpaceId, myMap, DuplicateRecordCloning.Replace, False)
'Update the anonymous blocks (dynamic blocks)
Dim targetBlockDef As BlockTableRecord = CType(myTargetBT(BlockName).GetObject(OpenMode.ForRead), BlockTableRecord)
targetBlockDef.UpdateAnonymousBlocks()
myTargetTrans.Commit()
End Using
End Using
mySourceDB.Dispose()
End If