TheSwamp

Code Red => .NET => Topic started by: Jeff H on April 12, 2024, 09:40:20 PM

Title: Adding and removing Layers from layerstates
Post by: Jeff H on April 12, 2024, 09:40:20 PM
Looks like they forgot to wrap some methods of the LayerStateManager  AcDbLayerStateManager::addLayerStateLayers (https://help.autodesk.com/view/OARX/2025/ENU/?guid=OARX-RefGuide-AcDbLayerStateManager__addLayerStateLayers_ACHAR___AcDbObjectIdArray_) to add layers to a layerstate and AcDbLayerStateManager::removeLayerStateLayers (https://help.autodesk.com/view/OARX/2025/ENU/?guid=OARX-RefGuide-AcDbLayerStateManager__removeLayerStateLayers_ACHAR___AcStringArray_) 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 (https://help.autodesk.com/view/OARX/2025/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_LayerStateManager_ImportLayerStateFromDb_string_Database) 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.  
Title: Re: Adding and removing Layers from layerstates
Post by: kdub_nz on April 13, 2024, 01:31:03 AM
elegantly regal !!

Code - C#: [Select]
  1. Ed.WriteLine("The pooch is now screwed");
Title: Re: Adding and removing Layers from layerstates
Post by: retsameht 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.
Title: Re: Adding and removing Layers from layerstates
Post by: Jeff H 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 !