How to get the mesh details of a block instance object in Rhino?


(Alex) #1

I am using C++ API, and would like to know how would I properly get the details regarding the mesh of a block instance in Rhino?


(Dale Fugier) #2

Hi @alexian007,

I am not I understand what you want. Are you looking for the render meshes attached to instance definition geometry?

Any additional details you can provide might help.

– Dale


Exporting Block instances as meshes
(Alex) #3

Hi @dale

Yes, I am looking for the render meshes attached to the instance definition geometry.

Right now, I am following the similar approach to this extremely helpful sample mesh export code that has been provided to us:

So, the approach I have taken is as follows:

  1. Used RhinoMeshObjects() to get all the meshes into a single array of meshes i.e ON_ClassArray<CRhinoObjectMesh> meshes(objects.Count());.

  2. Now that I have the meshes, I have to associate it with the objects on the document. I do so by, using the object iterator as:
    CRhinoObjectIterator objectIterator(CRhinoObjectIterator::undeleted_objects, CRhinoObjectIterator::active_objects);

  3. Then as I iterate through the obtained objects, I check if it has a mesh or not, by traversing through the meshes array, which I had created before and compare the mesh’s parent object’s UUID to that of the current object’s UUID. If they match, I have written a code that stores the reference of the mesh from the meshes array into the object definition. (What I am doing is storing the rhino object information into another custom data structure, which will be saved into a JSON file and then be readable by another 3D viewer software.)

I do so by:

for (int k = 0; k < allMeshes.Count(); k++)
{
if (allMeshes[k].m_parent_object->Attributes().m_uuid == currentObjID)
{
// code to store the mesh reference to the object
}
}

  1. This works fine and well for normal objects, but in the case of block instances, as the block object itself doesn’t have a mesh, I had problem getting mesh reference for them. So, I used GetSubObjects() to store the subobjects of a block into an array.

  2. So, now I wrote a special case for an object having sub-object (i.e a block instance) as:

int subCount = rhino_object->GetSubObjects(allSubObjects, NULL);

		if (subCount > 0)
		{
			hasSubObj = true;

			for (int i = 0; i < subCount; i++)
			{
				CRhinoObject* currentSubObject = allSubObjects[i];

				ON_UUID currentSubObjUUID = currentSubObject->Attributes().m_uuid;

				for (int k = 0; k < allMeshes.Count(); k++)
				{
					if (allMeshes[k].m_parent_object->Attributes().m_uuid == currentSubObjUUID)
					{
						// Code to handle reference to the mesh
					}
				}

			}
		}
  1. And that should have done it. But, even doing this gives me some weird results. I found out that, if a block object was duplicated i.e if there is a block object “block1”, and then duplicate it in the document as “block2”, the code above will get the meshes for the block2 too, but the meshes will be in the same position as the original object.

And, when I debugged the sub-object’s UUID in case of both the objects’ sub-objects, they were the same.
So, the meshes that were referred by both the object’s sub-objects were the same. Ideally, the references should’ve been different. I just dont get it why this is happening. How do I handle this case?

@dale, this was all that’s happening. Please give me some feedback upon how do I improve this situation. I apologize if the details I have provided were ambiguous or not clear. Let me know if I can do a better job at explaining.

Thank you Dale, and hope to get your helpful reply.


(Dale Fugier) #4

Hi @alexian007,

The CRhinoObjectMesh objects, returned by RhinoMeshObjects contains the mesh and a pointer to the object the mesh came from. Is this all you need?

– Dale


(Alex) #5

Hey @dale, yes that is helpful and I can get the object the mesh came from through the m_parent_object, but that is not kinda helpful to me as I need to get the block instance from the mesh.
I need it because I would like to apply the block transform to the mesh to correctly set the mesh’s position.

So, if given a mesh from RhinoMeshObjects how would I set its position (vertices) correctly with regards to the block instance it belongs? I need help on that.


(Dale Fugier) #6

Just use RhinoGetRenderMeshes. See rhinoSdkMeshObject.h for details.

https://github.com/mcneel/rhino-developer-samples/blob/6/cpp/SampleCommands/cmdSampleExtractRenderMesh.cpp

– Dale