Hi, So I am exploring C++ by getting started in Rhino Plugin development. I have successfully configured visual studio and have made a basic “hello-world” plugin in Rhino. What I would like to know is what is the context variable all about and what is a Rhino Document, how do I get access to it? Can I use it to get information about the objects, materials, etc?
Hi Alex,
A context, or CRhinoCommandContext
provides information on how the command was run by the user (either interactive or scripted). It also contains a reference to the active document, or CRhinoDoc
object.
For example:
CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
if (context.IsInteractive())
{
// Show some dialog interface
}
else
{
// Show a command line interface
}
// Access the active document (for example)
context.m_doc.Redraw();
The document, or CRhinoDoc
, holds everything that is saved to 3dm files.
– Dale
Hi @dale,
Thank you so much for your helpful replies on both my posts
This was really helpful. Though, I do have further questions, which I do hope you could give me some clarification about?
- When/Why exactly do I need to call the Redraw() method if I am just getting the attributes of the objects present in the document?
- Also, where can I find the all the objects present in the document from the CRhinoDoc. I am aware that I can get objects using the objectIterator and using Layers. But, is it possible to get them through the CRhinoDoc and get its geometry and attributes data?
You don’t. It’s just an example of access the document from a command.
An object iterator walks the object list in the document for you.
For example:
CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
// Create an object iterator to process non-deleted objects that are stored in the active document.
CRhinoObjectIterator it(CRhinoObjectIterator::undeleted_objects, CRhinoObjectIterator::active_objects);
it.IncludeLights();
const CRhinoObject* rhino_object = nullptr;
for (rhino_object = it.First(); nullptr != rhino_object; rhino_object = it.Next())
{
const ON_Geometry* geometry = rhino_object->Geometry();
if (nullptr != geometry)
{
const CRhinoObjectAttributes& attributes = rhino_object->Attributes();
// TODO...
}
}
return CRhinoCommand::success;
}
– Dale
Hello @dale, thank you for the prompt reply.
Oh okay, I was somehow under the impression that the document had some sort of data structure to hold all the objects. I now get that I should be using the object iterator.
Also, can I use the object Iterator to get the NamedViews in the document too right? I did not find anything in the docs regarding that, so am I on the right path?
Finally, I have a doubt regarding the materials. Should I get the materials of the current document by using
context.m_doc.m_material_table
and store all the material data as I require? Is this the correct way to get material data correctly when exporting 3dm to another custom file format? And also, how do I associate which object is using which material?
Should I be using the m_material_index and searching it on the material table to find out? :
const CRhinoObjectAttributes& attributes = currentRhinoObject->Attributes();
attributes.m_material_index;
Named views are stored in the document’s properties, or CRhinoDocProperties
, member.
int i = 0;
const ON_3dmView* view_3dm = nullptr;
while (nullptr != (view_3dm = context.m_doc.Properties().NamedView(i)))
{
RhinoApp().Print(L"%s\n", static_cast<const wchar_t*>(view_3dm->m_name));
i++;
}
Yes. You can also just call CRhinoObject::ObjectMaterial()
.
– Dale