Identify if a block is nested?

Identify if a block is nested?

js75CAD
Enthusiast Enthusiast
501 Views
2 Replies
Message 1 of 3

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.

 

Thanks

0 Likes
502 Views
2 Replies
Replies (2)
Message 2 of 3

cadffm
Consultant
Consultant

Hi,

 

a Block is never nested, just Blockreference can be nested in a Blockefinition.

 

You posted in AutoCAD board, this isn't the best place for your question -

You should ask in a board for programmong and the API you use (.net?)

 

https://forums.autodesk.com/t5/autocad/ct-p/8

 

 

Sebastian

0 Likes
Message 3 of 3

Kent1Cooper
Consultant
Consultant

@js75CAD wrote:

.... I am struggling to identify only those blocks directly inserted ....


If it's an option for you, AutoLisp can do that, with Blocks that are not dynamic.  This will "see" those Inserted directly, but not those that are nested in other Blocks/Xrefs.

 

(ssget "_X" '((2 . "YourBlockName")))

Kent Cooper, AIA
0 Likes