Get and set layerstate to viewport

Get and set layerstate to viewport

stefanveurink68AXD
Advocate Advocate
1,280 Views
2 Replies
Message 1 of 3

Get and set layerstate to viewport

stefanveurink68AXD
Advocate
Advocate

Dear everybody. 

 

What I want is user to select a viewport, then get that viewports layer-settings (vpfreeze, vpcolor, vplineweight), and set it to all viewports in the drawing with width smaller 250. 

 

this is the code i got: 

 

ThisDrawing.Utility.GetEntity VP, a, "Select source-viewport:"

''''NEED TO GET LAYERSTATE HERE

For Each aLayout In ThisDrawing.Layouts
    If aLayout.Name <> "Model" Then
        For Each aEntity In aLayout.Block
            If TypeOf aEntity Is AcadPViewport Then
                aEntity.GetBoundingBox minpunt, maxpunt
                If maxpunt(0) - minpunt(0) < 250 Then
            
                ''''NEED TO SET LAYERSTATE HERE
                
                End If
            enidf
        Next
    End If
Next

 

Anyone who can get me going with getting and setting the layerstate? Cause I can't figure out how to do this or where to start. 

 

Any help would be appreciated. 

0 Likes
1,281 Views
2 Replies
Replies (2)
Message 2 of 3

grobnik
Collaborator
Collaborator

@stefanveurink68AXD 

Hi,

I'm not so expert with Layerstate but by the way I found something that seems could solve your issue:

See link and code below

https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-52B191C9-B183-4B20-96FD-AE3F44FA9AFA

 

Sub Ch4_ListStates()
  On Error Resume Next
  
  Dim oLSMDict As AcadDictionary
  Dim XRec As Object
  Dim layerstateNames As String
  layerstateNames = ""
  
  ' Get the ACAD_LAYERSTATES dictionary, which is in the
  ' extension dictionary in the Layers object.
 Dim oLSM As AcadLayerStateManager

Set oLSM = ThisDrawing.Application.GetInterfaceObject("AutoCAD.AcadLayerStateManager." & Left(AcadApplication.Version, 2))

Set oLSMDict = ThisDrawing.Layers.GetExtensionDictionary.Item("ACAD_LAYERSTATES")

  ' List the name of each saved layer setting. Settings are
  ' stored as XRecords in the dictionary.
  For Each XRec In oLSMDict
   layerstateNames = layerstateNames + XRec.Name + vbCrLf
  Next XRec
  MsgBox "The saved layer settings in this drawing are: " + vbCrLf + layerstateNames
  oLSM.SetDatabase ThisDrawing.Database
End Sub

' Restore the color and linetype settings of a drawing's layers
'Assuming that the color and linetype settings of the layers in the current drawing were previously saved under the name “ColorLinetype”, the following code resets the color and linetype settings of each layer in the drawing to the value they had when “ColorLinetype” was saved.

Sub Ch4_RestoreLayerSettings()
  Dim oLSM As AcadLayerStateManager
  Set oLSM = ThisDrawing.Application.GetInterfaceObject("AutoCAD.AcadLayerStateManager." & Left(AcadApplication.Version, 2))
  
  oLSM.SetDatabase ThisDrawing.Database
  oLSM.Restore "ColorLinetype"
End Sub
Message 3 of 3

stefanveurink68AXD
Advocate
Advocate

Thanks, this is helpful, but what i am missing (and am struggling with since the start, also when trying to make a code in .NET) is linking the layerstate to a viewport. 

 

Your code just sets the layers in the DRAWING itself back to a previously saved layerstate. 

 

What i need is set the layers in a certain VIEWPORT to a (saved) layerstate. 

 

Wondering if this is even possible in VBA or C#, because i don't see the terms VPcolor, VPfreeze, etcetera don't show up anywhere. Still would surprise me if really not possible, cause in Autocad this is possible since as long as I use the program. So, still got the question: anyone knows how to link this the dictonairy to a viewport?

 

0 Likes