Delete or Purge Reference Layer in C#

I have created a Reference layer to show debug values for a command I am writing.
I use reference layers because they are not saved with the document.

I am trying to write another command to clear these layers.

success = true;
foreach (var reference in doc.Layers.Where(l => l.IsReference).ToList()) {
success = success && doc.Layers.Purge(reference.Index, true);
}

if (!success) {
Log.Warning(“Failed to clear managed Layers”);
}

However this code never succeeds - any clues why?

Hi @david.birch.uk

It is possible to delete reference layers. Just make sure the layer is empty before trying to delete.

Here is a simple Python script that demonstrates this:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

def print_layers(msg):
    print(msg)
    for lay in sc.doc.Layers:
        print('{}, index = {}, reference = {}'.format(lay.Name, lay.Index, lay.IsReference))

print_layers('Before LayerTable.AddReferenceLayer')
idx = sc.doc.Layers.AddReferenceLayer()
print_layers('After LayerTable.AddReferenceLayer')
rc, str = Rhino.Input.RhinoGet.GetString('Press <Enter> to continue', True, '')
rc = sc.doc.Layers.Delete(idx, False)
print_layers('After LayerTable.Delete')

Let me know if you have any questions.

– Dale

Thanks Dale,

I have added code to delete objects (from the documentation I understood this is not necessary with purge)

However the code still fails to delete the objects and layers.

I forgot to mention that I also cannot manually delete the layers or the objects through the Rhino GUI

  success = true;
            foreach (var layer in doc.Layers.Where(l => l.IsReference).ToList()) {
                foreach (var obj in doc.Objects.FindByLayer(layer).ToList()) {
                         success = success && doc.Objects.Delete(obj,true,true);
                         success = success && obj.IsDeleted; 
                }

                success = success && doc.Layers.Purge(layer.Index, true);
                success = success && doc.Layers.Delete(layer.Index, true);
            }

            if (!success) {
                Log.Warning("Failed to clear managed Layers");
            }

Hi @david.birch.uk,

Can you provide me a way to reproduce this here - creating a reference layer that you cannot delete using Rhino?

Thanks,

– Dale

Sure, please find below

private void AddReferenceLayerTest(object sender, EventArgs e) {
    var doc = RhinoDoc.ActiveDoc;

    doc.Layers.AddReferenceLayer( // todo change to Add and everything works
        new Layer {
            Name = "test",
            Color = Color.Aqua,
            PlotColor = Color.Aqua,
            ParentLayerId = Guid.Empty
        });

    var layer = doc.Layers.FindName("test");

    TextDot dot = new TextDot("help", Point3d.Origin);
    doc.Objects.Add(dot, new ObjectAttributes {LayerIndex = layer.Index});

    var line = new LineCurve(Point3d.Origin, new Point3d(100, 100, 0));
    doc.Objects.Add(line, new ObjectAttributes {LayerIndex = layer.Index});
}

private void Clear(object sender, EventArgs e) {
    var doc = RhinoDoc.ActiveDoc;
    var layer = doc.Layers.FindName("test");

    foreach (var obj in doc.Objects.FindByLayer(layer).ToList()) {
        doc.Objects.Delete(obj, true, true);
    }

    doc.Layers.Purge(layer.Index, true);
    doc.Layers.Delete(layer.Index, true);

    doc.Views.Redraw();
}

Hi @david.birch.uk,

See this work any better:

import Rhino
import scriptcontext as sc

layer = sc.doc.Layers.FindName('test')
if layer:
    for rh_obj in sc.doc.Objects.FindByLayer(layer):
        sc.doc.Objects.Purge(rh_obj)
    sc.doc.Layers.Purge(layer.Index, False)
    sc.doc.Views.Redraw()

– Dale

ah ha! that’s got it - the Purge method does indeed delete the objects in the reference layer

Any idea why Delete doesn’t?