Grasshopper

algorithmic modeling for Rhino

Hey all,

  I'm trying to bake objects on to some layers that don't exist yet and I'd like to create some 3 and 4 level deep sublayers.

  I have strings made with the format of "Layer::sub1::sub2::sub3"

  I am trying:

Rhino.DocObjects.Tables.LayerTable.Add("Layer::sub1::sub2::sub3", System.Drawing.Color.Blue);

and I'm getting an error that says:

1. Error (CS0120): An object reference is required for the non-static field, method, or property 'Rhino.DocObjects.Tables.LayerTable.Add(string, System.Drawing.Color)'

I'm sure its a syntax thing but I'm totally stumped

Views: 4004

Replies to This Discussion

Update:

 I fixed the layer making part with the oh-so-simple doc.Layers.Add() but I have not figured out how to do sublayers yet. Hopefully you all can help me out

Hi Andy,

I do not know how this could be done in C#, but python. Sorry for that.

You need to set your child layer's ParentLayerId property (it will be equal to Id of its parent layer).

That's the trick. I'm not a big fan of this workaround but you need to add the parent layer to the document, "findbyfullpath" it and then the child assignment works. I also don't like how rhino doesn't let you set the layer ID before you add it to the document.

List<String> sL = new List<String>();

Random r = new Random(2);

for(int i = 0;i < A.Count;i++)
{
Rhino.DocObjects.Layer l1 = new Rhino.DocObjects.Layer();
l1.Name = A[i].ToString();
l1.Color = Color.FromArgb(255, r.Next(255), r.Next(255), r.Next(255));
l1.LayerIndex = 456;
doc.Layers.Add(l1);
l1.LayerIndex = doc.Layers.FindByFullPath(l1.Name, true);

for(int j = 0;j < B.Count;j++)
{
Rhino.DocObjects.Layer l2 = new Rhino.DocObjects.Layer();
l2.Name = B[j].ToString();
l2.Color = Color.FromArgb(255, r.Next(255), r.Next(255), r.Next(255));
l2.ParentLayerId = doc.Layers[l1.LayerIndex].Id;
doc.Layers.Add(l2);
Print(doc.Layers[doc.Layers.Find(l2.Name, true)].FullPath.ToString());
Print(l1.Name + "::" + l2.Name);
String p = l1.Name + "::" + l2.Name;
l2.LayerIndex = Rhino.RhinoDoc.ActiveDoc.Layers.FindByFullPath(p, true);

for(int k = 0;k < C.Count;k++)
{
Rhino.DocObjects.Layer l3 = new Rhino.DocObjects.Layer();
l3.Name = C[k].ToString();
l3.Color = Color.FromArgb(255, r.Next(255), r.Next(255), r.Next(255));
l3.ParentLayerId = doc.Layers[l2.LayerIndex].Id;
doc.Layers.Add(l3);
l3.LayerIndex = doc.Layers.FindByFullPath(l1.Name + "::" + l2.Name + "::" + l3.Name, true);

for(int l = 0;l < D.Count;l++)
{
Rhino.DocObjects.Layer l4 = new Rhino.DocObjects.Layer();
l4.Name = D[l].ToString();
l4.Color = Color.FromArgb(255, r.Next(255), r.Next(255), r.Next(255));
l4.ParentLayerId = doc.Layers[l3.LayerIndex].Id;
doc.Layers.Add(l4);

sL.Add(A[i].ToString() + "::" + B[j].ToString() + "::" + C[k].ToString() + "::" + D[l].ToString());
}
}
}
}

layer = sL;

The LayerTable.Add method returns an index of the layer it has just added to the layertable (that's your doc.Layers.Add(l1);). So you don't have to use the FindByFullPath to obtain parent's layer index.
Not only layers, but all other geometry does not have an Id, until it has been added to the rhino/grasshopper documents.

Nope. It's not so trivial. I've created these tools lately to handle the recursion of layers with a bit more grace.

See functions here:  http://pastebin.com/xYpE0xNi

Here's the snippet that should do the trick (you'll need to add the methods from pastebin as additional code)

  private void RunScript(string layerPath, Color color, bool bake, ref object A)
  {
    if (!bake) {
      return;
    }
    Layer target = EnsureFullPath(layerPath);
    target.Color = color;
    target.CommitChanges();
  }

It also makes sure not to try to create it when it already exists.

Attached is an example.

Attachments:

Based on your work I was able to implement the creation of sublayers in Giulio Piancentino's "Bake with attributes" component. It's not very elegant - mostly copy-paste, but it now accepts  sublayers and creates them if not present in Rhino doc.

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service