Issue when adding PolylineCurve with custom UserData

Hi all,

In C# rhinocommon, I’m creating a polyline curve with some custom user data and adding it to Rhino. Let’s say I have a PolylineCurve myCurve and I want to attach my custom user data myUserData, then in my RunCommand I have

myCurve.UserData.Add (myUserData);
doc.Objects.AddCurve(myCurve);

but this doesn’t work, as myUserData seems to be present but not properly initialized.
I have this workaround

doc.Objects.AddCurve(myCurve);
Rhino.DocObjects.RhinoObject objref = doc.Objects.MostRecentObject();
objref.Geometry.UserData.Add(myUserData);

This one works, but I’d rather prefer the first one, as the latter may not be suitable when multiple objects are being added. Any idea to fix the first one?

Many thanks!
Pablo

Without a full code sample to run, its difficult to tell what is going on. But here is a sample that you might find beneficial.

https://github.com/dalefugier/SampleCsUserData

– Dale

Many thanks Dale! I think I had to go through the Attributes property of my object.

By the way, do you have a similar sample, but using UserDictionary? Is it more or less the same as UserData? That would be great!

Pablo

I don’t think I do. But its pretty darn easy. To set a value, you basically do this:

var dict = obj.Attributes.UserDictionary;
dict.Set("MyKey", value);

To retrieve the value, you do this:

var dict = obj.Attributes.UserDictionary;
var value = dict.Get("MyKey");

– Dale

I see, it’s similar to a C++ map. Very useful!

Pablo