C# : How to attribute a new layer to an existing object?

I’m having troubles to move an object to a new layer in C#. After searching a while, I noticed the following behavior : If I assign a new LayerIndex value to a BrepObject.Attributes, this value is not stored. See the following code :

_doc.Objects.Find(<A GUID>).Attributes.LayerIndex = <AN INTEGER, 21 for example>;
RhinoApp.WriteLine("Object on layer n°" + _doc.Objects.Find(<THE SAME GUID>).Attributes.LayerIndex.ToString());

So we set the LayerIndex to a fix value. But when asking back for it, the LayerIndex didn’t change. I precise that the layer with the tested index exists because the following code works for creating a brep on a specified layer :

_doc.Objects.AddBrep(myBrep, new ObjectAttributes() { LayerIndex = <AN INTEGER, 21 for example> };)

Any idea ?

Hi @em.rudelle,
You need to run the RhinoObject.CommitChanges() method after you change layer index. For example,

RhinoObject myObject = _doc.Objects.Find(< A GUID >);
myObject.Attributes.LayerIndex = 21;
1 Like

Hi @Darryl_Menezes,
This works perfectly fine. It would have take me ages to figure that out. Thanks a lot.