I was wondering how to set object attributes (properties) in rhino grasshopper c# script.
Elefront plugin would do the job, unfortunately it does not support Mac OS.
I wish to set key and value string on curves to use object information for rhino inside Revit.
here… where you can check properties/attributes in rhino
private void RunScript(GeometryBase geo, object y, ref object A)
{
var oa = new Rhino.DocObjects.ObjectAttributes();
oa.SetUserString("test", "value");
Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;
ot.Add(geo, oa);
}
when I bake the result, it creates duplicated line (one with attributes, the other without attributes) and I assume it is because I created new Rhino.DocObjects.Tables.ObjectTable class.
Is there any way to cull duplicated line (the line without attributes)?
What is very strange is that you have 420 lines going into your C# script and 420 lines coming out. Yet baking the output Line gives you 840 lines while baking the input Line gives only 420, as expected.
P.S. Oh, I see what’s happening by toggling the ‘run’ input. The lines are already “baked” by just running the script so there is no need to bake the output?
“Just in case”? It’s always valuable to post code!
It works the same way as what @Jane.Cho already had. The ‘AddLine()’ method “Adds a line object to Rhino”, effectively “baking it”, so baking the output is what causes the duplication.
In elefront component, if user update/change attribute value (key or value string) and click ‘bake’ button it automatically update attributes without baking new object.
However in my script, if I change the attribute value, the component bake new object with updated input value. Thus I get two objects with different attributes in Rhino.
Anyway thanks a lot for the comments!
To be a little more precise about this, your C# code “bakes” your lines with assigned attribute (key, value) due to ‘ot.AddLine()’ but the attribute is not added to the ‘A’ output.
And adding a ‘nl’ array doesn’t help (I just checked):
private void RunScript(bool run, DataTree<Line> baselines, string key, string value, ref object A)
{
if(run){
ObjectAttributes attr = new ObjectAttributes();
attr.SetUserString(key, value);
Line[] nl = new Line[baselines.DataCount];
int i = 0;
foreach(GH_Path path in baselines.Paths){
foreach(Line ln in baselines.Branch(path)){
Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;
ot.AddLine(ln, attr);
nl[i] = ln;
i = i + 1;
}
}
A = nl;
}
}