How to traverse all entities in an OdDbBlockTableRecord?

  1. FAQ
  2. »
  3. .dwg support

To iterate over all of the entities in a block (for example, model space), you can use code like this:

OdDbObjectId blockId = pDb->getModelSpaceId();
OdDbBlockTableRecordPtr pBlockRecord = blockId.safeOpenObject(OdDb::kForWrite);

OdDbObjectIteratorPtr pIter = pBlockRecord->newIterator();
for (; !pIter->done(); pIter->step())
{
    OdDbObjectId objectId = pIter->objectId();
    OdDbEntityPtr pEntity = objectId.safeOpenObject(OdDb::kForWrite);
    //work with pEntity
}

If you only need to get information from entities without modifying them, you should use OdDb::kForRead when opening the block and its entities. 


You can learn more about structure of an OdDbDatabase and records in our documentation:

https://docs.opendesign.com/td/db_datasheet_collection.html 

https://docs.opendesign.com/td/db_datasheet_iterate.html