C# sharp - How to Set Current Layer (Index)?

Hi all,

I’ve written a quite simple C# component for Grasshopper.

It creates a new layer in Rhino, and I now want to make that layer ‘Current’ (in order to then write certain geometry to that Layer) – however I cannot get this working.

I’m trying to use:

Rhino.DocObjects.Tables.LayerTable.SetCurrentLayerIndex(index, true);

where ‘index’ is an integer, and is the ‘LayerIndex’ of my newly created layer. (Checked this, and I’m very sure it’s the correct number).

The error I get is:

Error (CS0120): An object reference is required for the non-static field, method, or property 'Rhino.DocObjects.Tables.LayerTable.SetCurrentLayerIndex(int, bool)' (line 137)

I’ve read up on this problem over at StackOverflow (surprise!) and a few other places, and tried to implement some of their suggested solutions like putting this Method into a new class, defining it as ‘static’, etc, etc. No methods working yet.

Any ideas?

(and apologies if this is a really simple question – I’ve only been using C# for a few weeks.)

Thanks!
~Jeg

You should probably call:

RhinoDoc.ActiveDoc.Layers.SetCurrentLayerIndex(index, quiet)

Arendvw,

Thanks for the suggestion. C# doesn’t seem to like that idea though – if I use your method it returns:

Error (CS0103): The name 'quiet' does not exist in the current context (line 187)

Just to confirm with you – I am using this method in the main body of the code - i.e. in

private void RunScript(object x, object y, ref object A)
  {
// CODE IN HERE
}

Any other suggestions?

Thanks,
~Jeg

Replace the “quiet” with false or true.

Djordje,

Thanks for your email.

If I replace with true or false, the script runs and I do not get any errors - but it does not make my new layer the current one!

The lack of errors suggest that line of code is fine, so could it now be that, say, the Layer Table needs to be updated?

(I suggest this because I’ve used the CommitChanged method earlier on in the script to make my new layer appear in Rhino):

NewLayer.CommitChanges();

I’ve just tried using CommitChanges on the CurrentLayer:

Rhino.DocObjects.Tables.LayerTable.CurrentLayer.CommitChanges();

No solution there either! (give me the same ‘Non-Static field error’…)

The full code is:

    // Create a child layer -------------------------------------
    string child_name = "TempPatchLayer";
    Rhino.DocObjects.Layer JNewLayer = new Rhino.DocObjects.Layer();
    JNewLayer.Name = child_name;
    JNewLayer.Color = System.Drawing.Color.Red;
    int index = 100;
    JNewLayer.LayerIndex = index;

    // Bring New layer into Rhino ----------------------
    RhinoDoc.ActiveDoc.Layers.Add(JNewLayer);
    JNewLayer.CommitChanges();
    
   // Make New Layer 'current' ---------------------------------
   // option (1) not working ---- Rhino.DocObjects.Tables.LayerTable.SetCurrentLayerIndex(index, quiet);
        
   // option (2) not working
   RhinoDoc.ActiveDoc.Layers.SetCurrentLayerIndex(index, true);

Thanks,
~Jeg

Hi Jeg,
I am using python, so can’t help you out with your c# syntax.
I would advise you not to set the LayerIndex property, but instead to use the one that RhinoDoc.ActiveDoc.Layers.Add(JNewLayer); returns. Then supply that index to the SetCurrentLayerIndex method.

Djordje,

That worked! Fantastic – thank you so much for your help!

For anyone else, the working code is:

    string child_name = "TempPatchLayer";
    Rhino.DocObjects.Layer JNewLayer = new Rhino.DocObjects.Layer();
    JNewLayer.Name = child_name;
    JNewLayer.Color = System.Drawing.Color.Red;

    int index = RhinoDoc.ActiveDoc.Layers.Add(JNewLayer);
    RhinoDoc.ActiveDoc.Layers.SetCurrentLayerIndex(index, true);

~Jeg

4 Likes