Deleting hidden objects

Im working on a plugin that will iterate through hidden objects and delete them if they are mesh and meet certain criteria. I wrote some simple code to see if this will work but its not deleting any of the hidden objects. I know im hitting the if statement and im getting the WriteLine that says its deleting each object, but when I check after the command is run, the hidden objects are still there. Am I missing something? New to RhinoCommon

// Start of the class
public Result deletehiddenface(RhinoDoc doc)
{
var settings = new ObjectEnumeratorSettings();
settings.HiddenObjects = true; // This includes objects that are explicitly hidden or on hidden layers.
settings.VisibleFilter = false;
settings.LockedObjects = true;
settings.NormalObjects = false;
settings.ReferenceObjects = false;

// Get the hidden objects.
RhinoObject[] hiddenObjects = doc.Objects.FindByFilter(settings);

RhinoApp.WriteLine($"Found {hiddenObjects.Length} hidden objects in enumerator.");

var go = new GetOption();
go.SetCommandPrompt("You have dense meshes that are hidden. This may cause submission errors. Do you want to delete them?");
int y = go.AddOption("Yes");
int n = go.AddOption("No");
GetResult res = go.Get();

if (res == GetResult.Option)
{
    int picked = go.OptionIndex();
    if (picked == y)
    {
        RhinoApp.WriteLine("The user selected yes");
        foreach (var cadobj in doc.Objects)
        {
            if (cadobj.Geometry is Mesh hmesh)
            {
                doc.Objects.Delete(cadobj.Id, true);
                RhinoApp.WriteLine("If deleted");
            }
            else
            {
                doc.Objects.Delete(cadobj);
                RhinoApp.WriteLine("ELse deleted");
            }
        }
    }
    if (picked == n)
    {

    }
}
else
{
    // Do nothing
}
    return Result.Success;

}

Hi,
The main issue is that you’re gathering hiddenObjects but then iterating over doc.Objects instead. If you want to delete only the hidden objects, you have to loop through hiddenObjects. Also, make sure you call doc.Views.Redraw() at the end so the view updates. Here’s a quick example:

foreach (var obj in hiddenObjects)
{
    if (obj.Geometry is Mesh)
        doc.Objects.Delete(obj.Id, true);
    else
        doc.Objects.Delete(obj.Id, true);
}
doc.Views.Redraw();
1 Like

Thanks, I was iterating through hiddenobjects at first. I had switched to doc.objects when i was attempting to look through .isVisible on the layers, i must have forgotten to change it back. But when i was iterating through hiddenobjects i was getting the same result (not deleting hidden objects).

Edit: i just found this (see image) maybe i need to commit changes first?