Working with layers in C# Xamarin

I am new to this and going through the layer class in the SDK,

I want to delete all the default layers and set up my own, I can do this one by one but still working on adding child layers.

Code so far

namespace RGSetLayers
{
public class RGSetLayersCommand : Rhino.Commands.Command
{
	public override string EnglishName {
		get { return "RGSetLayersCommand"; }
	}

	protected override Result RunCommand (Rhino.RhinoDoc doc, RunMode mode)
	{
		// TODO: start here modifying the behaviour of your command.
		// ---

		// Add a new layers to the document
		doc.Layers.Add("dimensions", System.Drawing.Color.FromArgb(71,28,28));
		doc.Layers.Add("information", System.Drawing.Color.FromArgb(0,10,86));
		doc.Layers.Add("images", System.Drawing.Color.FromArgb(0,10,86));


		doc.Layers.Delete(01, true);

		// Get an existing layer
		string default_name = doc.Layers.CurrentLayer.Name;

		int index = doc.Layers.Find("images", true);
		if (index<0)
			return Rhino.Commands.Result.Cancel;

		Rhino.DocObjects.Layer parent_layer = doc.Layers[index];

		return Result.Success;
	    }
    }
}

Is there a simple way to set up a list of layers to add and delete? (Side note, I am going through a course I just bought on C# with VisualStudio on Windows).

Question 2; is there a way to save this out to a command to use all the time, not just from the one instance of Rhino that opens from the run command in this code.

Thanks in advance

I’m not sure I understand the question. What are you looking to do?

Well, plug-in commands do work all the time, with every running instance of Rhino. Is something not working for you?

Hi Dale, here is my code, since I am new to C#, I am wondering if there is a simpler way to do what I have done here … like maybe adding the layers I want to delete to a list / array / dictionary looking for the right term.

   // TODO: start here modifying the behaviour of your command.
		// ---

		// Add a new layers to the document
		doc.Layers.Add("dimensions", System.Drawing.Color.FromArgb(71,28,28));
		doc.Layers.Add("information", System.Drawing.Color.FromArgb(0,10,86));
		doc.Layers.Add("images", System.Drawing.Color.FromArgb(0,10,86));

		doc.Layers.SetCurrentLayerIndex(06,true);

		// Delete default layers
		doc.Layers.Delete(00, true);
		doc.Layers.Delete(01, true);
		doc.Layers.Delete(02, true);
		doc.Layers.Delete(03, true);
		doc.Layers.Delete(04, true);
		doc.Layers.Delete(05, true);


		// Get an existing layer
		int index = doc.Layers.Find("images",true);
		if (index<0)
			return Rhino.Commands.Result.Cancel;

		Rhino.DocObjects.Layer parent_layer = doc.Layers[index];

		// Create a child layer
		string child_name = "Top Image";
		Rhino.DocObjects.Layer childlayer = new Rhino.DocObjects.Layer();
		childlayer.ParentLayerId = parent_layer.Id;
		childlayer.Name = child_name;
		childlayer.Color = System.Drawing.Color.Red;

		index = doc.Layers.Add(childlayer);
		if (index < 0) 
		{
			Rhino.RhinoApp.WriteLine ("Unable to add {0} layer.", child_name);
			return Rhino.Commands.Result.Failure;
		}

		// Create a child layer
		string child_name2 = "Front Image";
		Rhino.DocObjects.Layer childlayer2 = new Rhino.DocObjects.Layer();
		childlayer2.ParentLayerId = parent_layer.Id;
		childlayer2.Name = child_name2;
		childlayer2.Color = System.Drawing.Color.Black;

		index = doc.Layers.Add(childlayer2);
		if (index < 0) 
		{
			Rhino.RhinoApp.WriteLine ("Unable to add {0} layer.", child_name2);
			return Rhino.Commands.Result.Failure;
		}
		// Create a child layer
		string child_name3 = "Side Image";
		Rhino.DocObjects.Layer childlayer3 = new Rhino.DocObjects.Layer();
		childlayer3.ParentLayerId = parent_layer.Id;
		childlayer3.Name = child_name3;
		childlayer3.Color = System.Drawing.Color.Blue;

		index = doc.Layers.Add(childlayer3);
		if (index < 0) 
		{
			Rhino.RhinoApp.WriteLine ("Unable to add {0} layer.", child_name3);
			return Rhino.Commands.Result.Failure;
		}

		return Result.Success;

This code runs fine I am just wondering if there is a simpler way to do this?

My second question was wondering how to save this command out so I can add it to my Rhino at work / office, for example. If I run the command from Tamarin, it opens a new instance of Rhino and I can type RgSetLayers and it works. How do I save it to run on my other copy of Rhino? I probably haven’t got to that part of the devDocs yet, eh?

I will admit I am going through the new Developer docs in a bit of a random order, so bare with me. Your samples are helpful.

I don’t know, maybe. But typing your own code and understanding how it works sometimes trumps “simpler.”

One think you could do is store the return value of doc.Layers.Add, which is the newly added layer index if successful. This way, you wouldn’t have to find the layer you just added.

Unlike scripts, you don’t save out commands, as your command has been compiled into a plugin (RHP, DLL). So if you want this command on another computer, you have to copy and install this plug-in on another computer.

Of course, it’s a learning curve. And it works, that is the main thing for now. One step at a time.

Thanks, I will get it. That makes sense!