How to use SetUserString(opennurbs) and GetUserString(c++ sdk) properly

try to save curves with opennurbs and SetUserString:

ON_NurbsCurve* rhinoCurve = new ON_NurbsCurve(...);
rhinoCurve->SetCV();
rhinoCurve->SetKnot();
// Option 1: SetUserString directly
rhinoCurve->SetUserString(L"MyKey", L"MyValue");
// Option 2: SetUserString through attributes
ON_3dmObjectAttributes* attributes = new ON_3dmObjectAttributes();
attributes->SetUserString(L"MyKey", L"MyValue");
// save model
model.AddManagedModelGeometryComponent(rhinoCurve, attributes);
model.Write(path.c_str(), 0, &error_log);

then read with c++ plugin(command):

CRhinoObjectIterator it(CRhinoObjectIterator::normal_objects, CRhinoObjectIterator::active_objects);
for (object = it.First(); 0 != object; object = it.Next())
{
    // Try casting as an instance object
    const CRhinoInstanceObject* iref_object = CRhinoInstanceObject::Cast(object);
    if (nullptr != iref_object)
    {
        // Try getting the instance object's definition
        const CRhinoInstanceDefinition* idef = iref_object->InstanceDefinition();
        if (nullptr != idef)
        {
            ON_wString idef_name = idef->Name();
            dump.Print(L"Instance Model object %d: %ls (%ls)\n", object_count++, iref_object->ShortDescription(false), static_cast<const wchar_t*>(idef_name));
            IterateInstanceDefinition(iref_object->InstanceDefinition(), dump);
        }
    }
    else
    {
        // Just a regular model object
        CRhinoObjectAttributes attrib = object->Attributes();
        ON_wString userStr;
        bool hasKey = attrib.GetUserString(L"MyKey", userStr);
        // not work : <
        dump.Print(L"Regular Model object %d: %ls\n", object_count++,  userStr);
    }
}

both attrib.GetUserString() and object->GetUserString() return false, any idea?

it’s my fault, after correct the key name i get the value with attrib.GetUserString.
but why does ON_Object::SetUserString not work here and what is it designed for?

Hi @tracyma,

Objects that are derived from CRhinoObject do not have user data, as they are created at runtime. The user text attached to geometry or attributes in openNURBS will be available via CRhinoObject::Geometry() or RhinoObject::Attributes().

– Dale

1 Like