Community
I am trying to access the entties in the Dynamic Blocks of a drawing. My code crashes.....
<CommandMethod("DTest2")> _ Public Sub DynamicBlockgetValuesofVisibleEntites() Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Dim i As Integer = 0 Using trx As Transaction = db.TransactionManager.StartTransaction() Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable) For Each objId As ObjectId In bt Dim btr As BlockTableRecord = DirectCast(objId.GetObject(OpenMode.ForRead), BlockTableRecord) If btr.IsDynamicBlock Then Dim brefObjidColl As ObjectIdCollection = btr.GetBlockReferenceIds(True, True) Dim anonObjIdsColl As ObjectIdCollection = btr.GetAnonymousBlockIds() For Each anonObjId As ObjectId In anonObjIdsColl Dim btrAnon As BlockTableRecord = DirectCast(anonObjId.GetObject(OpenMode.ForRead), BlockTableRecord) Dim ObjidColl As ObjectIdCollection = btrAnon.GetBlockReferenceIds(True, True) For Each Id As ObjectId In ObjidColl brefObjidColl.Add(Id) Next Next For Each brefObjId As ObjectId In brefObjidColl Dim bref As Autodesk.AutoCAD.DatabaseServices.BlockReference = TryCast(brefObjId.GetObject(OpenMode.ForWrite), Autodesk.AutoCAD.DatabaseServices.BlockReference) Dim objs As DBObjectCollection = Nothing bref.UpgradeOpen() 'bref.ConvertToStaticBlock() ' Converts block reference to a static anonymous block bref.Explode(objs) ' This is being exploded in memory only - there is no Commit line For Each obj As Autodesk.AutoCAD.DatabaseServices.DBObject In objs Dim ent As Autodesk.AutoCAD.DatabaseServices.Entity = CType(obj, Autodesk.AutoCAD.DatabaseServices.Entity) If ent.Visible Then ' this is prob not necessary since the block has been exploded but it doesn't do any harm i += 1 End If Next obj Next End If Next ed.WriteMessage("There are " & i & " entities in the dynamic blocks") trx.Commit() End Using End Sub
Hi
You need to dispose those exploded DBObjects, they will leak otherwise...
Take a look at the following blog post, you may find it useful:
http://adndevblog.typepad.com/autocad/2012/05/accessing-visible-entities-in-a-dynamic-block.html
Philippe.
Dim ids_diff As New ObjectIdCollection <CommandMethod("DBINFO", CommandFlags.Modal)> _ Public Sub Apply_Information_To_Dynamic_Blocks() Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Using tr As Transaction = db.TransactionManager.StartTransaction Dim ms As BlockTableRecord = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) Dim intCount As Integer = 0 For Each id As ObjectId In ms Dim br As Autodesk.AutoCAD.DatabaseServices.BlockReference = TryCast(tr.GetObject(id, OpenMode.ForWrite), Autodesk.AutoCAD.DatabaseServices.BlockReference) If br Is Nothing Then Continue For If Not br.IsDynamicBlock Then Continue For ids_diff.Clear() AddHandler db.ObjectAppended, AddressOf db_ObjectAppended br.ExplodeToOwnerSpace() RemoveHandler db.ObjectAppended, AddressOf db_ObjectAppended For Each id2 As ObjectId In ids_diff 'ed.WriteMessage(vbNewLine & "Object Exploded to Modelspace: " & id2.ObjectClass.Name) If id2.ObjectClass.Name = "AcDb3dSolid" Then ' This is the 3D solid that was displayed for the dynamic block ‘ Do something here to the 3D Solids End If ' Now dispose of the entities Try Dim ent As Autodesk.AutoCAD.DatabaseServices.Entity = tr.GetObject(id2, OpenMode.ForWrite, False, False) ent.Erase() ent.Dispose() Catch ' not able to cast ObjectID as an entity End Try Next ' Now do something here to the dynamic block if necessary intCount += 1 Next ed.WriteMessage(vbNewLine & intCount.ToString & " Dynamic Blocks have had information added successfully from the visible 3DSolid within the block, apart from 'Notes'") tr.Commit() tr.Dispose() End Using End Sub Public Sub db_ObjectAppended(s As Object, e As ObjectEventArgs) ids_diff.Add(e.DBObject.ObjectId) End Sub
Thanks for the link Philippe, I think I have it sorted now (see code above). I believe that I have disposed of the DBObjects......
Great! Please mark as solved if the answer helped.
Thank you,
Philippe.