Updating Block Attribute Text via C#

Hey everyone,

How can I set Block Attribute Text via C# / Rhinocommon?

I have multiple layouts with the same Block that represents the layout sheet/label and some text in the label should change according to the layout. I use DocumentUserText and Text Fields for data that is document related, but couldn’t figure out how to make it work for the individual layout data.

I currently get the Block Instance Definition by its name to insert it:

var block = doc.InstanceDefinitions.Find("Block Name");
doc.Objects.AddInstanceObject(block.Index, xform);

I found this topic that uses RhinoApp.RunScript with -Insert — there’s no way of doing it directly with rhinocommon?

Hey @pegiachini,

It’s pretty straightforward to set the Attribute text of any RhinoObject, including the InstanceObject. Below is my test using a Grasshopper C# component in Rhino 8. Although this should work for Rhino 7 also.

var doc = RhinoDoc.ActiveDoc;
var selected = doc.Objects.GetSelectedObjects(false, false).FirstOrDefault();
selected.Attributes.SetUserString("Key", "Value");

1 Like