[SOLVED] Layer.IsVisible doesn't work


I am having issues with turning the visibility of all my layers off (except the default one).

So in my main function I do a lot of things including adding new layers, childlayers, adding objects to these layers and changing properties as well. Then after everything, but before doc.Views.Redraw(); I do the following, which sadly doesn’t work:

for (int i = 1; i < doc.Layers.Count; i++)
    doc.Layers[i].IsVisible = false;

Not one layers has been turned invisible. What is wrong with that code? Can someone help how to do it correctly? (I program a C# Rhino Plug-In for Windows.)

Do you call for a redraw after the code you’ve shown?

You must call doc.Layers[i].CommitChanges() to, well, commit your changes :slight_smile:

Makes sense, I added that now, but still no layer is turned invisible. Weird right? Does it have to do with childlayers? I turn IsVisible = false for every layer though so that shouldn’t be a reason.

The problem remains. :confused:

Oh I did something wrong in my example earlier, sorry! You need to use the same instance of the layer that you get from the Layer table. Every time you get a layer using doc.Layers[i] a new instance of a Layer object is created for you. This is highly counter-intuitive, and a result of interfacing a C++ SDK from .NET. So, having said that, I wonder if this works better:

for(int i = 1; i < doc.Layers.Count; ++i)
 Layer l = doc.Layers[i];
 l.IsVisible = false;
 l.CommitChanges(); // commit changes ON THE SAME INSTANCE

Test for instance equality:

Layer lay_zero = doc.Layers[0];
Layer lay_zero_too = doc.Layers[0];
Debug.Assert(ReferenceEquals(lay_zero, lay_zero_too)); // this will fail.
1 Like

Yes, this works now! Thank you very much.

This is highly counter-intuitive, and a result of interfacing a C++ SDK from .NET.

I guess you learn something new every day right? :wink:

This was a bug we never got around to fixing in V5. I fixed this for V6. You shouldn’t have to call commitchanges on layers in V6

1 Like