Author Topic: Adding and removing Layers from layerstates  (Read 284 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Adding and removing Layers from layerstates
« on: April 12, 2024, 09:40:20 PM »
Looks like they forgot to wrap some methods of the LayerStateManager  AcDbLayerStateManager::addLayerStateLayers to add layers to a layerstate and AcDbLayerStateManager::removeLayerStateLayers for removing layers

So far this has been working with minimal testing, but not sure the best way to do this or if this gonna blow up on me later

I have the following xrefs
Host
-XrefA
--XrefB

I am copying XreB layers settings from XreA into the Host.
Seems like the easiest so for is using LayerStateManager.ImportLayerStateFromDb will handle cloning linetypes etc...

So open XrefA in a side databse use its LayerStateManager to create a LayerState with only XrefB layers then use Host LayerStateManger to import it.
The problem is I cannot choose which layers with the LayerStateManger and having to do it directly in the extension directory.

So I use the XrecordEnumerator to enumerate through the result buffer until I find a 330 for Layer ObjectId then I remove it and continue removing lines until I remove 440 for its transparency which I am assuming is the last code for a layer.
Shit! I just realized I need to set transparency on layer to see if adds more codes following it.
If you look at pic below I am assuming the format starts with its pointer and ends with the alpha value.

This example creates a layerstate called xref then removes all the layers from it that not xref layers.
Code - C#: [Select]
  1.         private LayerStateMasks lsMasks = LayerStateMasks.Color | LayerStateMasks.Frozen | LayerStateMasks.LineWeight | LayerStateMasks.LineType | LayerStateMasks.On | LayerStateMasks.Plot | LayerStateMasks.Locked;
  2.  
  3.         [CommandMethod("XrefLayersOnlyLayerState")]
  4.         public void XrefLayersOnlyLayerState()
  5.         {
  6.             var lman = Db.LayerStateManager;
  7.             string name = "xref";
  8.             lman.SaveLayerState(name, lsMasks, ObjectId.Null);
  9.  
  10.             using (Transaction trx = Db.TransactionManager.StartTransaction())
  11.             {
  12.                 var dic = lman.LayerStatesDictionaryId(false).GetDBObject<DBDictionary>();
  13.  
  14.                 if (!dic.Contains(name))
  15.                 {
  16.                     Ed.WriteLine("The pooch is now screwed");
  17.                     return;
  18.                 }
  19.  
  20.                 Xrecord xrec = dic.GetAt(name).GetDBObject<Xrecord>(OpenMode.ForWrite);
  21.  
  22.                 XrecordEnumerator enumer = xrec.GetEnumerator();
  23.                 while (enumer.MoveNext())
  24.                 {
  25.                     if (enumer.Current.TypeCode == (int)DxfCode.SoftPointerId && enumer.Current.Value != null)
  26.                     {
  27.                         try
  28.                         {
  29.                             ObjectId id = (ObjectId)enumer.Current.Value;
  30.                             if (!id.IsNull && id.IsValid)
  31.                             {
  32.                                 LayerTableRecord ltr = id.GetDBObject<LayerTableRecord>();
  33.                                 if (!ltr.IsDependent)
  34.                                 {
  35.                                     enumer.RemoveCurrent();
  36.                                     while (enumer.Current.TypeCode != (int)DxfCode.Alpha)
  37.                                     {
  38.                                         enumer.RemoveCurrent();
  39.                                     }
  40.                                 }
  41.                             }
  42.                         }
  43.                         catch (Autodesk.AutoCAD.Runtime.Exception ex)
  44.                         {
  45.                             Ed.WriteLine(ex.Message);
  46.                         }
  47.                     }
  48.                 }
  49.  
  50.                 trx.Commit();
  51.             }
  52.         }
  53.  
« Last Edit: April 12, 2024, 10:23:24 PM by Jeff H »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: Adding and removing Layers from layerstates
« Reply #1 on: April 13, 2024, 01:31:03 AM »
elegantly regal !!

Code - C#: [Select]
  1. Ed.WriteLine("The pooch is now screwed");
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

retsameht

  • Mosquito
  • Posts: 15
Re: Adding and removing Layers from layerstates
« Reply #2 on: April 13, 2024, 04:19:17 AM »
Code - C#: [Select]
  1.  
  2.                 XrecordEnumerator enumer = xrec.GetEnumerator();
  3.  

Hey Jeff. Nice exploitation of XrecordEnumerator, but I think you also need to dispose that.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Adding and removing Layers from layerstates
« Reply #3 on: April 13, 2024, 05:46:28 PM »
Code - C#: [Select]
  1.  
  2.                 XrecordEnumerator enumer = xrec.GetEnumerator();
  3.  

Hey Jeff. Nice exploitation of XrecordEnumerator, but I think you also need to dispose that.
Didn’t think about it.
Thanks !