.NET

Reply
Valued Contributor
andred
Posts: 89
Registered: ‎10-17-2003
Message 1 of 4 (439 Views)

Change layer viewport properties

439 Views, 3 Replies
03-06-2013 11:37 AM

Hi everybody,

 

I'm looking to find a way to change layer properties by viewport. I know how to freeze/thaw layer by viewport but not changing their properties. I'm using VB .Net.

 

Anybody have an idea?

 

Regards

 

André

 

Distinguished Mentor
BlackBox_
Posts: 785
Registered: ‎02-25-2013
Message 2 of 4 (402 Views)

Re: Change layer viewport properties

03-09-2013 06:05 PM in reply to: andred

Have you considered LayerViewportProperties?

 

This thread may also be of use.



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security


AutoCAD® 2014, and Civil 3D® 2014 Certified Professional ~ Autodesk® Authorized Developer

Valued Contributor
andred
Posts: 89
Registered: ‎10-17-2003
Message 3 of 4 (365 Views)

Re: Change layer viewport properties

03-12-2013 11:07 AM in reply to: andred

Thank you very much for your hint and link to the thread.

 

For your information, here the sub I created:

This sub overides layer settings in all viewport except the current one. The overrides properties came from a layer "template". (A layer with the properties to apply as overrides).

 

Public Sub SetLayerOverridesInOtherViewPort(ByVal LayerName As String, ByVal LayerNameTemplate As String)

        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        Using doclock As DocumentLock = acDoc.LockDocument

            '' Start a transaction
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                'Define variables
                Dim acLyrTbl As LayerTable
                Dim CVPort As Viewport
                Dim LVPO As LayerViewportProperties

                'Get current viewport
                CVPort = DirectCast(acTrans.GetObject(acDoc.Editor.CurrentViewportObjectId, OpenMode.ForRead), Viewport)

                'Get current layout name
                Dim layoutDict As DBDictionary = CType(acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
                Dim strCurrentLayoutName As String = LayoutManager.Current.CurrentLayout

                'Get layer table object
                acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
                If acLyrTbl.Has(LayerNameTemplate) = True Then
                    'Get layer template object
                    Dim acLyrTemplateTblRec As LayerTableRecord
                    acLyrTemplateTblRec = acTrans.GetObject(acLyrTbl(LayerNameTemplate), OpenMode.ForRead)
                    'Scan all viewport
                    For Each entry As DBDictionaryEntry In layoutDict
                        If entry.Key <> "Model" Then
                            Dim lay As Layout = CType(acTrans.GetObject(entry.Value, OpenMode.ForRead), Layout)
                            If lay.LayoutName.ToUpper <> strCurrentLayoutName.ToUpper Then
                                SetCurrentLayoutTab(lay.LayoutName)
                                Dim nCount As Integer = 0
                                For Each vpid As ObjectId In lay.GetViewports
                                    nCount = nCount + 1
                                    Dim vp As Viewport = CType(acTrans.GetObject(vpid, OpenMode.ForWrite), Viewport)
                                    If vp.Handle <> CVPort.Handle Then
                                        If nCount > 1 Then
                                            'acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
                                            For Each acObjId As ObjectId In acLyrTbl
                                                Dim acLyrTblRec As LayerTableRecord
                                                acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead)
                                                If acLyrTblRec.Name.ToUpper = LayerName.ToUpper Then
                                                    Dim LaagTabelRec As LayerTableRecord = acTrans.GetObject(acLyrTblRec.ObjectId, OpenMode.ForRead)
                                                    LVPO = LaagTabelRec.GetViewportOverrides(vp.Id)
                                                    LVPO.Color = acLyrTemplateTblRec.Color
                                                    LVPO.LinetypeObjectId = acLyrTemplateTblRec.LinetypeObjectId
                                                    LVPO.LineWeight = acLyrTemplateTblRec.LineWeight
                                                    Exit For
                                                End If
                                            Next
                                        End If
                                    End If
                                Next
                            End If
                        End If
                    Next
                    acTrans.Commit()
                    SetCurrentLayoutTab(strCurrentLayoutName)
                    acDoc.Editor.Regen()
                End If

            End Using
        End Using


    End Sub

 

I also include a sub to set the current layout tab:

 

Public Sub SetCurrentLayoutTab(ByVal LayoutName As String)
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        Using doclock As DocumentLock = acDoc.LockDocument
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                Try
                    Dim acLayoutMgr As LayoutManager
                    acLayoutMgr = LayoutManager.Current
                    acLayoutMgr.CurrentLayout = LayoutName
                    acTrans.Commit()
                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message & vbCr & ex.StackTrace)
                    acTrans.Abort()
                End Try
            End Using
        End Using

    End Sub

 

 

Regards

 

André

 

Distinguished Mentor
BlackBox_
Posts: 785
Registered: ‎02-25-2013
Message 4 of 4 (356 Views)

Re: Change layer viewport properties

03-12-2013 02:24 PM in reply to: andred

You're welcome; I'm happy to help.

 

As one of many things... If you implement the appropriate Imports directive (using in C#), you'll enable Type references without Namespace qualification... As one example, consider:

 

Imports acApp = Autodesk.AutoCAD.ApplicationServices.Application

Public Sub GetCTab()
	acApp.GetSystemVariable("CTAB")
End Sub

Public Sub SetCTab(ByVal layoutName As String)
	acApp.SetSystemVariable("CTAB", layoutName)
End Sub

 



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security


AutoCAD® 2014, and Civil 3D® 2014 Certified Professional ~ Autodesk® Authorized Developer

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
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.