Active Contributor
40 Posts
0 Kudos
Registered: ‎05-05-2010
Post 1 of 3

Layer control in paperspace viewports with VB.net

1167 Views, 2 Replies
12-17-2012 06:04 AM

Hi to all, any one have suggestions to, how to control existing layers (Freeze On/Off), in an existing viewports  (5 of them in Paperspace) in AutoCad Existing drawing with VB.net?  I just want to trigger On or Off, different layers in each different viewports.

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 3

Re: Layer control in paperspace viewports with VB.net

12-17-2012 12:41 PM in reply to: Alainmorin

Found this one in my codes, author unknown,

try it for current viewport in Paper space

    <CommandMethod("VpLayersMan")> _
        Public Sub TestLayerFreeze()
           '' add layer names to freeze/ thaw in paperspace viewports separated by commas

            Dim layers As List(Of String) = {"Wall", "ANNO-TEXT"}.ToList()''<-- layers for test

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim ed As Editor = doc.Editor

            Dim db As Database = doc.Database

            Dim idLay As ObjectId

            Dim idLayTblRcd As ObjectId

            Dim lt As LayerTableRecord

            Dim layOut As Layout

            Dim tm As Autodesk.AutoCAD.ApplicationServices.TransactionManager = db.TransactionManager

            Dim ta As Transaction = tm.StartTransaction()

                Dim acLayoutMgr As LayoutManager

                acLayoutMgr = LayoutManager.Current

                Dim layDict As DBDictionary = DirectCast(ta.GetObject(db.LayoutDictionaryId, OpenMode.ForRead, False), DBDictionary)

                For Each itmdict As DBDictionaryEntry In layDict

                    layOut = DirectCast(ta.GetObject(itmdict.Value, OpenMode.ForRead, False), Layout)

                    ed.WriteMessage(vbLf + "Layout: {0}" + vbLf, layOut.LayoutName)

                    If layOut.LayoutName <> "Model" Then

                        acLayoutMgr.CurrentLayout = layOut.LayoutName

                        Dim ltt As LayerTable = DirectCast(ta.GetObject(db.LayerTableId, OpenMode.ForRead, False), LayerTable)

                        For Each lname As String In layers

                            idLay = ltt(lname)

                            lt = ta.GetObject(idLay, OpenMode.ForRead)

                            If ltt.Has(lname) Then

                                idLayTblRcd = ltt.Item(lname)


                                ed.WriteMessage("Layer: """ + lname + """ not available")


                            End If

                            Dim idCol As ObjectIdCollection = New ObjectIdCollection


                            ' Check that we are in paper space 

                            Dim vpid As ObjectId = ed.CurrentViewportObjectId

                            If vpid.IsNull() Then

                                ed.WriteMessage("No Viewport current.")


                            End If

                            'VP need to be open for write 

                            Dim oViewport As Viewport = DirectCast(tm.GetObject(vpid, OpenMode.ForWrite, False), Viewport)

                            If Not oViewport.IsLayerFrozenInViewport(idLayTblRcd) Then




                            End If
                    End If




            End Try

        End Sub


Active Contributor
40 Posts
0 Kudos
Registered: ‎05-05-2010
Post 3 of 3

Re: Layer control in paperspace viewports with VB.net

12-18-2012 04:52 AM in reply to: Alainmorin

Thank's Hallex, the problem I have is with this, is the viewport must be active to be able to freeze layers.  The drawings I have, have already viewport created (6 of them), and each one must have different frozen layer assign to it.  I must find a way to call viewport individually, and then change the required layer properties.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!