Two Ways to Work with Teigha BIM B-Reps

Ivan Serbinovsky

July 27, 2017

Many elements in Teigha BIM contain B-Rep geometries in GElement, which is a geometry cache of an element. However, B-Rep is not a universal format. Each product has a specific representation of B-Reps.

An overview of B-Reps and BIM specifics can be found in the documentation (login required).

Outputting B-Rep data using OdaBimApp

_BrBrepDump_func in the \Examples\TB_DevGuideCommands folder is a sample from the documentation and can be used to output B-Rep data.

  1. Run OdaBimApp
  2. Load DevGuideCommands.tx
  3. Run the BrBrepDump command (Edit->Registered Commands->TBDevGuideCommands->BrBrepDump)

B-Rep data outputs in the console.

This is a good way to parse data of a BIM B-Rep, but BIM data structure must be considered.

Using BrBrep classes

BrBrep classes provide a general interface for different B-Rep representations.

This interface is used for Teigha BIM, .dgn files (Siemens Parasolid®), and .dwg files (Spatial ACIS®). It is a general interface for customers, but each product has an implementation which works with product-specific data. The interface converts internal data to the interface needed, but you can use the same code for different B-Rep data in different Teigha products.

_BmBrepDump_func in the \Examples\TB_DevGuideCommands folder is a sample of using BrBrep classes.

void _BmBrepDump_func(OdEdCommandContext* pCmdCtx)
{
  OdBmCommandContextPtr pDbCmdCtx(pCmdCtx);
  OdBmDatabasePtr pDb = pDbCmdCtx->database();
  OdSmartPtr<OdEdBaseUserIO> pIO = pDbCmdCtx->userIO();
  OdInt32 iHandle = pIO->getInt("Enter id of an element for dumping:", OdEd::kInpDefault, -1);

  if (iHandle > -1) 
  {
    // Get object
    OdBmElementPtr pElem = pDb->getObjectId(OdDbHandle(OdUInt64(iHandle))).safeOpenObject();
    // Get object's geometry
    OdBmObjectPtr pGeom = pElem->getGeometry();
    if (pGeom->isA() == OdBmGElement::desc())
    {
      OdBmGElement* pGElem = dynamic_cast<OdBmGElement*>(pGeom.get());
      //Get nodes
      OdBmGNodePtrArray nodes;
      pGElem->getAllSubNodes(nodes);

      //Test all nodes until OdBmGeometry found or all nodes verified
      for (OdUInt32 iIdx = 0; iIdx < nodes.size(); ++iIdx)
      {
        if (nodes[iIdx]->isA() == OdBmGeometry::desc())
        {
          const OdBmGeometry* pGeometry = dynamic_cast<const OdBmGeometry*>(nodes[iIdx].get());

          OdString message;
          // Get faces
          OdBmFacePtrArray faceNodes;
          pGeometry->getFaces(faceNodes);
          message.format(L"Number of faces is %x", faceNodes.size());
          pIO->putString(message);

          // Use Face Iterator to iterate all faces of the object
          int faceNum = 0;
          for (OdBmFacePtrArray::iterator faceIt = faceNodes.begin(); faceIt != faceNodes.end(); ++faceIt)
          {
            message.format(L"Face %x", faceNum);
            pIO->putString(message);

            faceNum++;
            // Get first EdgeLoop of a face
            OdBmFacePtr pFace = *faceIt;

            OdBmGEdgeLoopPtr pFirstEdgeLoop = pFace->getFirstLoop();
            if (pFirstEdgeLoop.isNull()) //it is possible
              continue;

            int loopNum = 0;
            OdBmGEdgeLoopPtr pNextEdgeLoop = pFirstEdgeLoop;
            while (!pNextEdgeLoop.isNull())
            {
              message.format(L"  Loop %x", loopNum);
              pIO->putString(message);

              // Get first edge in loop
              OdBmGEdgeBase* pNext = pNextEdgeLoop->getNext();
              int edgeNum = 0;
              while (!pNext->isLoop())
              {
                OdBmGEdge* pGEdge = dynamic_cast<OdBmGEdge*>(pNext);

                bool bForvardDir = true;
                int iEdgeForFace = 0;
                {
                  OdBmFace* pFaceInt = pGEdge->getFacesItem(iEdgeForFace);
                  if (pFaceInt != pFace.get())
                  {
                    pFaceInt = pGEdge->getFacesItem(1);
                    if (pFaceInt == pFace.get())
                    {
                      bForvardDir = false;
                      iEdgeForFace = 1;
                    }
                  }
                }

                if (pGEdge->isFlipped())
                  bForvardDir = !bForvardDir;

                // Get start and end points of the Edge
                OdGePoint3dArray egepnts;
                pGEdge->getFirstAndLastEdgeGePnts(egepnts);
                if (bForvardDir)
                  message.format(L"    Edge %i  points: [%f , %f , %f], [%f , %f , %f]", edgeNum++, egepnts[0].x, egepnts[0].y, egepnts[0].z, egepnts[1].x, egepnts[1].y, egepnts[1].z);
                else
                  message.format(L"    Edge %i  points: [%f , %f , %f], [%f , %f , %f]", edgeNum++, egepnts[1].x, egepnts[1].y, egepnts[1].z, egepnts[0].x, egepnts[0].y, egepnts[0].z);

                pIO->putString(message);
                // Go to next edge in loop
                try
                {
                  pNext = pGEdge->getNextItem(iEdgeForFace);
                }
                catch (const OdError& err)
                {
                  message.format(L"%ls", err.description().c_str());
                  pIO->putString(message);
                  break;
                }
              }//while (!pNext->isLoop())
              loopNum++;
              pNextEdgeLoop = pNextEdgeLoop->getNextLoop();
            }//while (!pNextEdgeLoop.isNull())
          }//for (OdBmFacePtrArray::iterator faceIt = faceNodes.begin(); faceIt != faceNodes.end(); ++faceIt)
        }//if (nodes[0]->isA() == OdBmGeometry::desc())

      }//for (OdBmGNodePtrArray::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
      
    }//if (pGeom->isA() == OdBmGElement::desc())    
  }//if (iHandle > -1) 
}