Understand How AutoCAD Saves Layer States
 
 
 

AutoCAD saves layer setting information in an extension dictionary of the Layers table 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 code lists the names of all saved layer states:

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("ListLayerStates")> _
Public Sub ListLayerStates()
  '' Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  '' Start a transaction
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
 
      Dim acLyrStMan As LayerStateManager
      acLyrStMan = acCurDb.LayerStateManager
 
      Dim acDbDict As DBDictionary
      acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(True), _
                                   OpenMode.ForRead)
 
      Dim sLayerStateNames As String = ""
 
      For Each acDbDictEnt As DBDictionaryEntry In acDbDict
          sLayerStateNames = sLayerStateNames & vbLf & acDbDictEnt.Key
      Next
 
      Application.ShowAlertDialog("The saved layer settings in this drawing are:" & _
                                  sLayerStateNames)
 
      '' Dispose of the transaction
  End Using
End Sub

C#

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
  }
}
VBA/ActiveX Code Reference