Layer control in paperspace viewports with VB.net

Layer control in paperspace viewports with VB.net

Anonymous
Not applicable
2,398 Views
2 Replies
Message 1 of 3

Layer control in paperspace viewports with VB.net

Anonymous
Not applicable

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.

0 Likes
2,399 Views
2 Replies
Replies (2)
Message 2 of 3

Hallex
Advisor
Advisor

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

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

                            Else

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


                                Return

                            End If

                            Dim idCol As ObjectIdCollection = New ObjectIdCollection

                            idCol.Add(idLayTblRcd)

                            ' Check that we are in paper space 

                            Dim vpid As ObjectId = ed.CurrentViewportObjectId

                            If vpid.IsNull() Then

                                ed.WriteMessage("No Viewport current.")

                                Return

                            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

                                oViewport.FreezeLayersInViewport(idCol.GetEnumerator())

                            Else

                                oViewport.ThawLayersInViewport(idCol.GetEnumerator())

                            End If
                        Next
                    End If
                Next

                ta.Commit()

            Finally

                ta.Dispose()

            End Try

        End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 3

Anonymous
Not applicable

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.

0 Likes