| McNeel Wiki | |||||
| edit · print · help · all topics | |||||
Main Pages
Languages
| OverviewUser Data is a power SDK feature that allows 3rd party developers to attach custom data of any kind to any object derived from ON_Object. In order to take advantage of User Data, you are required to implement your own user data object by deriving a class from ON_UserData and overriding the required virtual functions. In Rhino 4.0, the SDK adds a new standardized approach for adding User Data to objects called User Strings. The Rhino 4.0 SDK allows you to quickly attaches User Data in the form of a key-value string pair to any object derived from ON_Object. This feature is exposed to the SDK as new member functions on ON_Object:
There are a number of advantages to User Strings:
ExampleThe following example demonstrates how to add User Strings to a selected object.
CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
bool bAttribute = true;
CRhinoGetObject go;
go.SetCommandPrompt( L"Select object to attach user text" );
go.AddCommandOptionToggle(
RHCMDOPTNAME(L"Location"),
RHCMDOPTVALUE(L"Object"),
RHCMDOPTVALUE(L"Attribute"),
bAttribute,
&bAttribute
);
for(;;)
{
CRhinoGet::result res = go.GetObjects( 1, 1 );
if( res == CRhinoGet::option )
continue;
if( res != CRhinoGet::object )
return cancel;
break;
}
const CRhinoObjRef& ref = go.Object(0);
const CRhinoObject* obj = ref.Object();
if( !obj )
return failure;
ON_wString key = L"test";
ON_wString text = L"sample text";
if( bAttribute )
{
// Attach user string to object's attributes
CRhinoObjectAttributes attribs = obj->Attributes();
attribs.SetUserString( key, text );
context.m_doc.ModifyObjectAttributes( ref, attribs );
}
else
{
// Attach user string to object's geometry
CRhinoObject* dupe = obj->DuplicateRhinoObject();
if( dupe )
{
ON_Geometry* geom = const_cast<ON_Geometry*>( dupe->Geometry() );
if( geom )
{
geom->SetUserString( key, text );
context.m_doc.ReplaceObject( ref, dupe );
}
}
}
return success;
}
| ||||
| rename · changes · history · subscriptions · lost and found · references · file upload | |||||