.NET

Reply
Distinguished Contributor
GilesPhillips
Posts: 487
Registered: ‎12-03-2003
Message 1 of 3 (338 Views)

Accessing entities on paperspace Layouts

338 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
Next

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

Next

 

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??

 

G

 

 

ACad, MEP, 3DS Max
Windows 7x64
X5482 @3.2Ghz 8Gb Ram
Quadro FX1700
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 3 (320 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)
            Else
                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())

            Try

                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

                            Next

                        End If
                    Next

                    tr.Commit()

                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))
                Next

            Catch ex As System.Exception

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

            End Try

        End Sub

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Distinguished Contributor
GilesPhillips
Posts: 487
Registered: ‎12-03-2003
Message 3 of 3 (303 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:

 

G

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community