Set Multi-UserString on the same CRhinoObjRef

Command1:

CRhinoMeshObject* obj=m_Doc->AddMeshObject(mesh);
CRhinoObjRef ObjRef(obj);

ON_UserString us[2];
us[0].m_key = _T("test1");
us[0].m_string_value = _T("1");
us[1].m_key = _T("test2");
us[1].m_string_value = _T("2");
CRhinoObject* pObj = (CRhinoObject*)ObjRef.Object();
CRhinoObjectAttributes Attrib( pObj->Attributes() );
Attrib.SetUserStrings( 2, us, true );
pObj->ModifyAttributes(Attrib);

Command2: modify same CRhinoObjRef ObjRef

ON_UserString us[2];
us[0].m_key = _T("test1");
us[0].m_string_value = _T("1");
us[1].m_key = _T("test2");
us[1].m_string_value = _T("4");
CRhinoObject* pObj = (CRhinoObject*)ObjRef.Object();
CRhinoObjectAttributes Attrib( pObj->Attributes() );
Attrib.SetUserStrings( 2, us, true );
pObj->ModifyAttributes(Attrib);

go through all UserString:

ON_ClassArray<ON_UserString> NewUserStringArray;
int iTCount = pObj->Attributes().GetUserStrings( NewUserStringArray );
for ( int iKeys=0; iKeys<iTCount; iKeys++ )
{
	ON_UserString* us = NewUserStringArray.At(iKeys);
	ON_wString key = us->m_key;
	ON_wString value = us->m_string_value;
};// iKeys loop

The value of UserString key"test2" still “2”.
The value do not update.
Please help me.

Best Regard,
Vaker

Hi Vaker,

There is a know issue with attribute user strings and ModifyAttributes.

http://mcneel.myjetbrains.com/youtrack/issue/RH-29393

Here is a method that works.

https://github.com/mcneel/Rhino5Samples_CPP/blob/master/SampleCommands/cmdSampleUserStrings.cpp

Hi Dale,

It works.
Thank for your help.

Best Regard,
Vaker

I have another question:
I created one Rhino object with UserString data.
I save the object to 3dm file.
I open the file again, then UserString data of object is missing.
If I want to save the UserString data, how can I do?
coding write UserString data on CRhinoUtilityPlugIn::WriteDocument()?

Best regards,
Vaker

The GitHub code sample seems to work (serialize). How does your code differ?

Sorry, my mistake.
It works fine.
Thank you.

Hi Dale,

I don’t seem to be able to access the issue on myjetbrains, and I can’t seem to spot what the solution is in your sample c++ code.

Can you please advise further on how to address this issue using c# and rhinocommon?
I’ve seen also that user strings aren’t being set when using modify user attributes method.

Thanks,

Jon

I searched harder through my emails (as I did recall encountering this in the past).
Adding a line such as atts.LayerIndex = atts.LayerIndex; ensures Rhino recognizes that the attributes have been modified.

You should now be able to view the issue.