Identify if a block is nested?

Identify if a block is nested?

js75CAD
Enthusiast Enthusiast
243 Views
1 Reply
Message 1 of 2

Identify if a block is nested?

js75CAD
Enthusiast
Enthusiast

I am trying to iterate the block table records of a particular block, but I put this block in my current drawing and both xreferences and nested xreferences contain it.

 

My code is this, but I am struggling to identify only those blocks directly inserted into model space. I know why it is doing what it is doing, because they all belong in the block table, but how do I stop it from accessing anything that is nested?

 

    <CommandMethod("UDC_PhaseConnectionReport")> Public Sub UDC_PhaseConnectionReport()
        doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        db = HostApplicationServices.WorkingDatabase
        ed = doc.Editor

        Dim phList As New List(Of Phasedata)

        Try
            Using tr As Transaction = db.TransactionManager.StartTransaction
                Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)

                If Not bt.Has("phase") Then
                    Return
                Else
                    Dim btr As BlockTableRecord = Nothing
                    Dim btrIDs As ObjectIdCollection = Nothing

                    btr = tr.GetObject(bt("phase"), OpenMode.ForRead)
                    btrIDs = Nothing
                    btrIDs = btr.GetBlockReferenceIds(True, False)

                    ' Go through each phase block and check ju=st like the other one..
                    For Each id As ObjectId In btrIDs
                        If Not id.IsEffectivelyErased Then

                            Dim oBr As BlockReference = TryCast(tr.GetObject(id, OpenMode.ForWrite, False, True), BlockReference)

                            ' Check if it is nested (dependent?)
                            Dim chkbtr As BlockTableRecord = CType(tr.GetObject(oBr.OwnerId, OpenMode.ForWrite), BlockTableRecord)

                            If Not chkbtr.IsDependent Then
                                ' This isn't stopping it...
                                Dim datts As New Dictionary(Of String, String)
                                datts = oBr.GetAttributesValues
                                iPt = oBr.Position

                                Dim strStn As String = "NaN"
                                Dim Sid As String = "NaN"
                                Dim strLot As String = "NaN"
                                Dim strTx As String = "NaN"
                                Dim strPh As String = "NaN"

                                If datts.ContainsKey("STN") Then If Not String.IsNullOrEmpty(datts.Item("STN")) Then strStn = datts.Item("STN")

                                If datts.ContainsKey("ID") Then If Not String.IsNullOrEmpty(datts.Item("ID")) Then Sid = datts.Item("ID")

                                If datts.ContainsKey("LOT") Then If Not String.IsNullOrEmpty(datts.Item("LOT")) Then strLot = datts.Item("LOT")

                                If datts.ContainsKey("TX") Then If Not String.IsNullOrEmpty(datts.Item("TX")) Then strTx = datts.Item("TX")

                                If datts.ContainsKey("P1") Then If Not String.IsNullOrEmpty(datts.Item("P1")) Then strPh = datts.Item("P1")

                                Dim phd As New Phasedata(strLot, strPh, Sid, strStn, strTx)
                                phList.Add(phd)
                            End If
                        End If
                    Next
                End If

                '' Display the data..
                phList = phList.OrderBy(Function(m) m.lot).ToList

                For Each l In phList
                    ed.WriteMessage(vbLf & "{0} {1}, {2}", l.lot.PadRight(10), l.ph.PadRight(10), l.sID)
                Next

                tr.Commit()
            End Using

        Catch ex As Exception
            MsgBoxWithLineNumber(ex.Message)
        End Try

    End Sub

 

Is anyone able to please help? I know I could just get all the ones in model space with a editor selection, but I am really trying to learn how to navigate the AutoCAD database and understand the relationship between the block table and its records.

0 Likes
244 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant

Hi,

If I do not misundertand the request, this should work:

        static ObjectIdCollection GetModelSpaceInsertIds(string blockName)
        {
            var db = HostApplicationServices.WorkingDatabase;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (blockTable.Has(blockName))
                {
                    var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                    var btr = (BlockTableRecord)tr.GetObject(blockTable[blockName], OpenMode.ForRead);
                    foreach (ObjectId id in btr.GetBlockReferenceIds(true, true))
                    {
                        var br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                        if (br.OwnerId == modelSpaceId)
                            ids.Add(id);
                    }
                }
                tr.Commit();
            }
            return ids;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes