Understand How AutoCAD Saves Layer States (.NET)

AutoCAD saves layer setting information in an extension dictionary of the LayerTable object. When you first save a layer state, AutoCAD does the following:

Each time you save another layer setting in the drawing, AutoCAD creates another XRecord object describing the saved settings and stores the XRecord in the ACAD_LAYERSTATE dictionary. The following diagram illustrates the process.

You do not need (and should not try) to directly manipulate the entries when working with layer states. Use the functions of the LayerStateManager object to access the dictionary. Once you have a reference to the dictionary, you can step through each of the entries which are represented as DBDictionaryEntry objects.

List the saved layer states in a drawing

If layer states have been saved in the current drawing, the following example lists the names of all saved layer states:

C# Example

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("ListLayerStates")]
public static void ListLayerStates()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        LayerStateManager acLyrStMan;
        acLyrStMan = acCurDb.LayerStateManager;

        DBDictionary acDbDict;
        acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(true),
                                        OpenMode.ForRead) as DBDictionary;

        string sLayerStateNames = "";

        foreach (DBDictionaryEntry acDbDictEnt in acDbDict)
        {
            sLayerStateNames = sLayerStateNames + "\n" + acDbDictEnt.Key;
        }

        Application.ShowAlertDialog("The saved layer settings in this drawing are:" +
                                    sLayerStateNames);

        // Dispose of the transaction
    }
}