Author Topic: Layerstate layer status off/on/frozen  (Read 164 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 816
  • .net stuff
Layerstate layer status off/on/frozen
« on: March 21, 2020, 01:47:11 AM »
Anyone know if there's a way to check the on/off/frozen value of a layerstate layer?

Code: [Select]
   DBDictionary layerstatemanagerdatabase = acTrans.GetObject(layerstatemanagerobject.LayerStatesDictionaryId(true), OpenMode.ForRead) as DBDictionary;

                string sLayerStateNames = "";

                foreach (DBDictionaryEntry layerstatedictionary in layerstatemanagerdatabase)
                {
                    string layerstatename = layerstatedictionary.Key;

                    if (layerstatename == pickedlayerstatename)
                    {
                        using (Xrecord layerstatexrecord = layerstatedictionary.Value.Open(OpenMode.ForRead) as Xrecord)
                        {

????

gile

  • Water Moccasin
  • Posts: 2251
  • Marseille, France
Re: Layerstate layer status off/on/frozen
« Reply #1 on: March 21, 2020, 02:12:08 PM »
Hi,

You can get the ACAD_LAYERSTATES dictionary which is an entry of the LayerTable extension dictionary. It contains an Xrecord for each layer state. In the Data of the Xrecord there's a typed value (code 90) which value is an integer which is a bitcode using the sum of the following values:
1 = IsOff
2 = IsFrozen
4 = IsLocked
8 = IsPrintable.

Code - C#: [Select]
  1.         static Dictionary<string, LayerInfo> TryGetLayerStateInfo(Database db, string name)
  2.         {
  3.             using (var tr = new OpenCloseTransaction())
  4.             {
  5.                 var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  6.                 var xDictId = layerTable.ExtensionDictionary;
  7.                 if (xDictId.IsNull)
  8.                     return null;
  9.                 var xDict = (DBDictionary)tr.GetObject(xDictId, OpenMode.ForRead);
  10.                 if (!xDict.Contains("ACAD_LAYERSTATES"))
  11.                     return null;
  12.                 var dict = (DBDictionary)tr.GetObject(xDict.GetAt("ACAD_LAYERSTATES"), OpenMode.ForRead);
  13.                 if (!dict.Contains(name))
  14.                     return null;
  15.                 var xrec = (Xrecord)tr.GetObject(dict.GetAt(name), OpenMode.ForRead);
  16.                 var data = xrec.Data;
  17.                 if (data is null)
  18.                     return null;
  19.                 var values = data.AsArray();
  20.                 var result = new Dictionary<string, LayerInfo>();
  21.                 for (int i = 0; i < values.Length; i++)
  22.                 {
  23.                     var val = values[i];
  24.                     if (val.TypeCode == 330)
  25.                     {
  26.                         var layer = (LayerTableRecord)tr.GetObject((ObjectId)val.Value, OpenMode.ForRead);
  27.                         result[layer.Name] = new LayerInfo((int)values[i + 1].Value);
  28.                     }
  29.                 }
  30.                 return result;
  31.             }
  32.         }
  33.  
  34.         struct LayerInfo
  35.         {
  36.             public bool IsOff { get; }
  37.             public bool IsFrozen { get; }
  38.             public bool IsLocked { get; }
  39.             public bool IsPrintable { get; }
  40.             public LayerInfo(int flags)
  41.             {
  42.                 IsOff = (flags & 1) == 1;
  43.                 IsFrozen = (flags & 2) == 2;
  44.                 IsLocked = (flags & 4) == 4;
  45.                 IsPrintable = (flags & 8) == 8;
  46.             }
  47.         }

Using example:
Code - C#: [Select]
  1.         [CommandMethod("TEST")]
  2.         public static void Test()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var pr = ed.GetString("\nEnter the layer state name: ");
  8.             if (pr.Status != PromptStatus.OK)
  9.                 return;
  10.             string name = pr.StringResult;
  11.             var layerStateInfo = TryGetLayerStateInfo(db, name);
  12.             if (layerStateInfo == null)
  13.             {
  14.                 ed.WriteMessage($"\nLayer state '{name}' not found");
  15.                 return;
  16.             }
  17.             {
  18.                 foreach (var entry in layerStateInfo)
  19.                 {
  20.                     var info = entry.Value;
  21.                     ed.WriteMessage($"\n{entry.Key} IsOff: {info.IsOff} IsFrozen: {info.IsFrozen} " +
  22.                         $"IsLocked: {info.IsLocked} IsPrintable: {info.IsPrintable}");
  23.                 }
  24.             }
  25.         }
« Last Edit: March 23, 2020, 03:04:17 AM by gile »
Speaking English as a French Frog

nobody

  • Swamp Rat
  • Posts: 816
  • .net stuff
Re: Layerstate layer status off/on/frozen
« Reply #2 on: March 25, 2020, 02:31:36 AM »
Thank you Gilles