Distinguished Contributor
Posts: 500
Registered: ‎12-03-2003
Message 1 of 3 (425 Views)

Accessing entities on paperspace Layouts

425 Views, 2 Replies
12-13-2012 06:36 AM

I'm trying to rewrite some old VBA code into VB.net (which I'm not even Novice level yet), and I'm stumped on what would be a fairly simple principle (in VBA)..


For Each BlockDef in ThisDrawing.blocks
   If Left(BlockDef.name, 12) = "*Paper_Space" then
      Select Case True
         Case BlockDef.Layout.Name Like "*Test*" 
            blah blah
      End Select
   End If

Essentially conditional manipulation of paperspace elements depending on the layout name.


 The following fragment allows me to recurse through entities in paperspace, but I've no idea which layout tab..


Dim BlkPaperSpc As BlockTableRecord = ScanTrans.GetObject(BlkTab _
(BlockTableRecord.PaperSpace), OpenMode.ForWrite)

For Each AcObjId As ObjectId In BlkPaperSpc
   Dim PspaceObj As String = AcObjId.ObjectClass.Name



What I need to do is to be able to identify a type of object (a table), confirm it's location, delete it and replace it with another one, depending on which layout tab it's on.


Any pointers or tips for the complete novice??





ACad, MEP, 3DS Max
Windows 7x64
X5482 @3.2Ghz 8Gb Ram
Quadro FX1700
*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 3 (407 Views)

Re: Accessing entities on paperspace Layouts

12-13-2012 02:11 PM in reply to: GilesPhillips

This is not the best solution but I hope

it will to get you started

       ''      get the real block name.        ''
        Public Shared Function EffectiveName(tr As Transaction, bref As BlockReference) As String
            Dim btr As BlockTableRecord = Nothing

            If (bref.IsDynamicBlock) Or (bref.Name.StartsWith("`*U", StringComparison.InvariantCultureIgnoreCase)) Then
                btr = TryCast(tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                btr = TryCast(tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
            End If
            Return btr.Name
        End Function
        <CommandMethod("cblk")> _
        Public Sub iterateThroughLayouts()

            Dim layts As New List(Of String)

            Dim doc As Document = acApp.DocumentManager.MdiActiveDocument

            Dim db As Database = doc.Database

            Dim ed As Editor = doc.Editor

            Dim blkdict As New Dictionary(Of ObjectId, String())


                Dim tr As Transaction = db.TransactionManager.StartTransaction

                Using tr

                    Dim ldict As DBDictionary = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead)

                    For Each dicent As DBDictionaryEntry In ldict

                        Dim lay As Layout = CType(dicent.Value.GetObject(OpenMode.ForRead), Layout)

                        Dim layname As String = lay.LayoutName

                        ''do your stuffs in the every layout
                        If layname <> "Model" Then
                            Dim btr As BlockTableRecord = tr.GetObject(lay.BlockTableRecordId, OpenMode.ForRead)

                            For Each id As ObjectId In btr

                                Dim ent As Entity = tr.GetObject(id, OpenMode.ForRead, False)

                                If TypeOf (ent) Is BlockReference Then
                                    'get block instance
                                    Dim bref As BlockReference = DirectCast(ent, BlockReference)
                                    'get block name
                                    Dim blkname = EffectiveName(tr, bref)
                                    ' get block instance owner

                                    blkdict.Add(bref.ObjectId, New String() {blkname, layname})
                                    'we have to gather all desired values to Dictionary (of string, string()) 

                                End If


                        End If


                End Using
                'display info in the command line after
                For Each kvp As KeyValuePair(Of ObjectId, String()) In blkdict
                    ed.WriteMessage(vbLf + "{0}   {1}   {2}" + vbLf, kvp.Key, kvp.Value(0), kvp.Value(1))

            Catch ex As System.Exception

                acApp.ShowAlertDialog(Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace)

            End Try

        End Sub



Distinguished Contributor
Posts: 500
Registered: ‎12-03-2003
Message 3 of 3 (390 Views)

Re: Accessing entities on paperspace Layouts

12-14-2012 09:19 AM in reply to: Hallex

Hi, Thanks for the code fragment - unfortunately I've not had time today to read through it and understand it completely, but I didn't want your effort to go without response.


I'm sure I'll be back with more questions, hopefully on other topics :smileyhappy:



ACad, MEP, 3DS Max
Windows 7x64
X5482 @3.2Ghz 8Gb Ram
Quadro FX1700
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.