Finding all block references of a dynamic block


https://adndevblog.typepad.com/autocad/2012/06/fin ... rences-of-a-dynamic-block.html
程序代码:

<CommandMethod("selb")> _
    Public Sub selectDynamicBlockReferences()
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            'get the blockTable and iterate through all blockDef
            Dim bt As BlockTable = CType(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
            For Each btrId As ObjectId In bt
                'get the blockDef and check if is anonymous
                Dim btr As BlockTableRecord = CType(trans.GetObject(btrId, OpenMode.ForRead), BlockTableRecord)
                If btr.IsDynamicBlock Then
                    'get all anonymous blocks from this dynamic block
                    Dim anonymousIds As ObjectIdCollection = btr.GetAnonymousBlockIds()
                    Dim dynBlockRefs As New ObjectIdCollection()
                    For Each anonymousBtrId As ObjectId In anonymousIds
                        'get the anonymous block
                        Dim anonymousBtr As BlockTableRecord = CType(trans.GetObject(anonymousBtrId, OpenMode.ForRead), BlockTableRecord)
                        'and all references to this block
                        Dim blockRefIds As ObjectIdCollection = anonymousBtr.GetBlockReferenceIds(True, True)
                        For Each id As ObjectId In blockRefIds
                            dynBlockRefs.Add(id)
                        Next
                    Next
                    'Do something with the collection we created
                    ed.WriteMessage([String].Format("Dynamic block ""{0}"" found with {1} anonymous block and {2} block references" & vbLf, btr.Name, anonymousIds.Count, dynBlockRefs.Count))
                End If
            Next
        End Using
    End Sub




Vb.net acdbEntGet GetEntityDxf



欢迎关注微信公众账号ByCAD