
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I need to Redefine Dynamic blocks and update all existing instances of the original block within a drawing to conform to the new definition. The new block definition will be read from a different dwg file/database.
To accomplish this in ACAD, I run Insert, select the file containing the updated block definition (with block name matching that of existing blocks to be redefined). When blocks with the same name are present in the active drawing, I am prompted to redefine and all existing blocks are updated.
I found code that seemes to meant to accomplish this exact bit of work on the AutoCAD DevBlock (http://adndevblog.typepad.com/autocad/2012/05/redefining-a-block.html). I then rewrote this code which I will provide.
When I run the method, whether it is the original code provided on the blog or my own, the new block definition is written to the database successfully. However, no existing block references are found after, so the code meant to update the geometry of existing references is never executed. The exising block instances do not reflect the changes made to the block definition after this code runs.
Oddly, opening an existing block instance in the block definition editor after running this code makes it clear that the new block defintion is in place.
Here is my code:
Public Shared Function TryRedifineBlock(BlockName As String, BlockFilePath As String) As Boolean Dim result As Boolean = False Dim doc As Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument Using lock As DocumentLock = doc.LockDocument() Using tran As Transaction = doc.TransactionManager.StartTransaction() Try Dim blockDB As Database = New Database(False, True) blockDB.ReadDwgFile(BlockFilePath, FileOpenMode.OpenForReadAndReadShare, True, "") Dim blockTableRecordID As ObjectId = doc.Database.Insert(BlockName, blockDB, True) If Not blockTableRecordID.IsNull Then Dim btr As BlockTableRecord = tran.GetObject(blockTableRecordID, OpenMode.ForRead, False, True) For Each bRefID As ObjectId In btr.GetBlockReferenceIds(False, True) Dim bRef As BlockReference = tran.GetObject(bRefID, OpenMode.ForWrite, False, True) bRef.RecordGraphicsModified(True) Next End If tran.Commit() blockDB.Dispose() result = True Catch ex As System.Exception tran.Abort() result = False Try EventLog.WriteEntry("DynamicBlockHelper.TryRedifineBlock", ex.GetType().Name + ": " + ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error) Catch eex As Exception End Try End Try End Using End Using Return result End Function
I have also attached the definition of the dynamic block I am using for testing. I apologize in advance if my ACAD termonology is off. I am fairly new to ACAD and its APIs.
Best Regards,
Kevin
Solved! Go to Solution.