Convert a Body to a SubDMesh in Facet Modeler

Taras Khalimanenko

March 12, 2021

Sometimes you might need to convert a FacetModeler::Body to an OdDbSubDMesh object to continue working with it. First you convert the body to a triangle mesh and then follow a few more steps.

First, use the generateMesh() method to generate a triangle mesh that represents a body.

void generateMesh(GeMesh::OdGeTrMesh& mesh) const;

Here, the mesh is an output parameter.

Since it's a member function, it can simply be called for the object:

FacetModeler::Body body;
/*...*/
GeMesh::OdGeTrMesh mesh;
body.generateMesh(mesh);

Now, you need to convert that OdGeTrMesh to a format suitable for OdDbSubDMesh creation. Here's how:

OdInt32Array faceArray;
faceArray.reserve(mesh.m_aTr.logicalLength() * 4);
for (unsigned int j = 0; j < mesh.m_aTr.logicalLength(); ++j)
{
  const GeMesh::OdGeTr& tr = mesh.m_aTr[j];
  faceArray.push_back(3);
  faceArray.push_back(tr.tr[0]);
  faceArray.push_back(tr.tr[1]);
  faceArray.push_back(tr.tr[2]);
}

Here, faceArray contains data in the following pattern:

"n P1 P2 ... Pn",

where n represents the number of points that form a face, followed by the indices of the required vertices from the array of 3D points.

For example, in this particular array, data is stored like this:

... 3, 10, 11, 12, 3, 13, 14, 15 ...

Finally, to complete the conversion, create a new OdDbSubDMesh object or apply the collected data (mesh.m_aVx, faceArray) to an existing object. To do that, use the setSubDMesh() method.

OdDbSubDMeshPtr pMesh = OdDbSubDMesh::createObject();
/*...*/
OdResult res = pMesh->setSubDMesh(mesh.m_aVx, faceArray, 0); // 0 - no smoothing

Now that it's done, it may be necessary to transfer additional information received from GeMesh::OdGeTrMesh or FacetModeler::Body through the Tags mechanism.

In most cases, the next step is to put the result in a block, for example ModelSpace:

OdDbDatabase *pDb;
/*...*/
OdDbBlockTableRecordPtr pMS = pDb->getModelSpaceId().openObject(OdDb::kForWrite);
pMesh->setDatabaseDefaults(pDb);
pMS->appendOdDbEntity(pMesh);

To see working code and try it yourself, see CommonApplications/Drawing/Examples/ExFMBody/ExFMBodyCommands.cpp and the FMBodyConvert2SubDMesh_func function. It's a part of the ExFMBody example, which you can load as a module. To test the function, call the FMBodyConvert2SubDMesh command.