Restore Layer States (.NET)

The RestoreLayerState method resets the layer settings in a layer state and requires four values. The first value is the name of the layer state to restore, and the second value is the object id of the viewport whose layer settings you want to restore. The third value is an integer that defines how layers not in the layer state are handled. The fourth value determines which layer settings are restored.

The following values determine how layers not in a layer state are handled:

Note: You can use the sum of multiple values previous listed to define the restore behavior of layers not in a layer state. For example, you can turn off and freeze the layers that are not saved with a layer state.

For example, if you save the color and linetype settings under the name “ColorLinetype” and subsequently change those settings, restoring “ColorLinetype” resets the layers to the colors and linetypes they had when “ColorLinetype” was saved. If you add new layers to the drawing after saving “ColorLinetype,” those new layers are not affected when you restore “ColorLinetype.”

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 restores the color and linetype settings of each layer in the drawing to the value they had when “ColorLinetype” was saved.

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("RestoreLayerState")> _
Public Sub RestoreLayerState()
    '' Get the current document
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
 
    Dim acLyrStMan As LayerStateManager
    acLyrStMan = acDoc.Database.LayerStateManager
 
    Dim sLyrStName As String = "ColorLinetype"
 
    If acLyrStMan.HasLayerState(sLyrStName) = True Then
        acLyrStMan.RestoreLayerState(sLyrStName, _
                                     ObjectId.Null, _
                                     1, _
                                     LayerStateMasks.Color + _
                                     LayerStateMasks.LineType)
    End If
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("RestoreLayerState")]
public static void RestoreLayerState()
{
    // Get the current document
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
 
    LayerStateManager acLyrStMan;
    acLyrStMan = acDoc.Database.LayerStateManager;
 
    string sLyrStName = "ColorLinetype";
 
    if (acLyrStMan.HasLayerState(sLyrStName) == true)
    {
        acLyrStMan.RestoreLayerState(sLyrStName,
                                     ObjectId.Null,
                                     1,
                                     LayerStateMasks.Color |
                                     LayerStateMasks.LineType);
    }
}

VBA/ActiveX Code Reference

Sub RestoreLayerState()
    Dim oLSM As AcadLayerStateManager
    Set oLSM = ThisDrawing.Application. _
                   GetInterfaceObject("AutoCAD.AcadLayerStateManager.20")
 
    oLSM.SetDatabase ThisDrawing.Database
    oLSM.Restore "ColorLinetype"
End Sub