Code Red > .NET

Adding and removing Layers from layerstates

(1/1)

Jeff H:
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#: ---        private LayerStateMasks lsMasks = LayerStateMasks.Color | LayerStateMasks.Frozen | LayerStateMasks.LineWeight | LayerStateMasks.LineType | LayerStateMasks.On | LayerStateMasks.Plot | LayerStateMasks.Locked;         [CommandMethod("XrefLayersOnlyLayerState")]        public void XrefLayersOnlyLayerState()        {            var lman = Db.LayerStateManager;            string name = "xref";            lman.SaveLayerState(name, lsMasks, ObjectId.Null);             using (Transaction trx = Db.TransactionManager.StartTransaction())            {                var dic = lman.LayerStatesDictionaryId(false).GetDBObject<DBDictionary>();                 if (!dic.Contains(name))                {                    Ed.WriteLine("The pooch is now screwed");                    return;                }                 Xrecord xrec = dic.GetAt(name).GetDBObject<Xrecord>(OpenMode.ForWrite);                 XrecordEnumerator enumer = xrec.GetEnumerator();                while (enumer.MoveNext())                {                    if (enumer.Current.TypeCode == (int)DxfCode.SoftPointerId && enumer.Current.Value != null)                    {                        try                        {                            ObjectId id = (ObjectId)enumer.Current.Value;                            if (!id.IsNull && id.IsValid)                            {                                LayerTableRecord ltr = id.GetDBObject<LayerTableRecord>();                                if (!ltr.IsDependent)                                {                                    enumer.RemoveCurrent();                                    while (enumer.Current.TypeCode != (int)DxfCode.Alpha)                                    {                                        enumer.RemoveCurrent();                                    }                                }                            }                        }                        catch (Autodesk.AutoCAD.Runtime.Exception ex)                        {                            Ed.WriteLine(ex.Message);                        }                    }                }                 trx.Commit();            }        } 

kdub_nz:
elegantly regal !!


--- Code - C#: ---Ed.WriteLine("The pooch is now screwed");

retsameht:

--- Quote from: Jeff H on April 12, 2024, 09:40:20 PM ---
--- Code - C#: ---                 XrecordEnumerator enumer = xrec.GetEnumerator(); 
--- End quote ---

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

Jeff H:

--- Quote from: retsameht on April 13, 2024, 04:19:17 AM ---
--- Quote from: Jeff H on April 12, 2024, 09:40:20 PM ---
--- Code - C#: ---                 XrecordEnumerator enumer = xrec.GetEnumerator(); 
--- End quote ---

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

--- End quote ---
Didn’t think about it.
Thanks !

Navigation

[0] Message Index

Go to full version