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

C# Example

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);
    }
}