Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This section describes advanced functionality and techniques for extracting data from a DWG/DXF file using DWGdirectTeigha.

Dispatching on Entity Type

Applications that process entity data from a database require a means to identify each entity by its type, and perform type-specific processing on the entity. DWGdirect Teigha uses a mechanism called called protocal extension to  to provide this functionality. Protocol extension allows a client application to create a new class and associate an instance of this class with a particular class of database objects. Then this new class instance may be retrieved from a generalization of the class with which it was registered, providing an effective means of dispatching by type.

...

The following is the parent class for our entity dumpers:

Code Block


class OdDbEntity_Dumper : public OdRxObject
{
public:
  ODRX_DECLARE_MEMBERS(OdDbEntity_Dumper);

  virtual void dump(OdDbEntity* pEnt, STD(ostream) &os) const;
}; // end class OdDbEntity_Dumper

...

The following defines a class for dumping OdDb2dPolyline entities. It overrides the OdDbEntity_Dumper::dump() function in order to dump the data specific to this entity.

Code Block


class OdDb2dPolyline_Dumper : public OdDbEntity_Dumper
{
public:

  void dump(OdDbEntity* pEnt, STD(ostream) & os) const
  {
    dumpCommonData(pEnt, os);

    OdDb2dPolylinePtr pPoly = pEnt;
    OdDbObjectIteratorPtr pIter = pPoly->vertexIterator();
    for (; !pIter->done(); pIter->step())
    {
      OdDb2dVertexPtr pVertex = pIter->entity();
      if (pVertex.get())
      {
        os << "      " << pVertex->isA()->name() << STD(endl);
      }
    }
  }
}; // end class OdDb2dPolyline_Dumper

...

The following function registers custom dumper classes for a number of entity types:

Code Block

void ExProtocolExtension::initialize()
{
  // Register OdDbEntity_Dumper with DWGdirect
  OdDbEntity_Dumper::rxInit();
  m_pDumpers = new Dumpers;
  m_pDumpers->addXs();
  
}//  end ExProtocolExtension::initialize()

And the Dumpers::addXs() functions does the following:

Code Block
   void addXs()
  {
    OdDbEntity::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_entityDumper);
    OdDbRegion::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_regionDumper);
    OdDbPolyline::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_polylineDumper);
    OdDb2dPolyline::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_2dPolylineDumper);
    OdDb3dPolyline::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_3dPolylineDumper);
    OdDbPolyFaceMesh::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_polyFaceMeshDumper);
    OdDbPolygonMesh::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_polygonMesh);
    OdDbBlockReference::desc()->addX(
    OdDbEntity_Dumper::desc(), &m_blockReference);
    OdDbMInsertBlock::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_mInsertBlock);
    OdDbSpline::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_splineDumper);
    OdDbEllipse::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_ellipseDumper);
    OdDbSolid::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_solidDumper);
    OdDbTrace::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_traceDumper);
    OdDb3dSolid::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_3DSolidDumper);
    OdDbProxyEntity::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_proxyEntityDumper);
    OdDbHatch::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_hatchDumper);
    OdDbCircle::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_circleDumper);
    OdDbMText::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_mTextDumper);
    OdDbText::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_textDumper);
    OdDbMline::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_mlineDumper);
    OdDbRasterImage::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_imageDumper);
    OdDbArcAlignedText::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_arcAlignedTextDumper);
    OdDbOle2Frame::desc()->addX(
      OdDbEntity_Dumper::desc(), &m_ole2FrameDumper);
  } // end addXs

...

The following function retrieves the custom dispatching class associated with a particular OdDbEntity instance, and calls the virtual dump() method to display the contents of this entity.

Code Block

void DbDumper::dumpEntity(OdDbObjectId id, STD(ostream) & os)
{
  // Open the entity
  OdDbEntityPtr pEnt = id.safeOpenObject();

  // Retrieve the registered protocol extension object registered 
  // for this object type.
  OdSmartPtr<OdDbEntity_Dumper> pEntDumper = pEnt;

  pEntDumper->dump(pEnt, os);
  
  dumpGroupCodes(pEnt->xData(), os, "      ");    

  if (!pEnt->extensionDictionary().isNull())
  {
    dumpObject(pEnt->extensionDictionary(), 
      "ACAD_XDICTIONARY", os, "      ");
  }
} // end DbDumper::dumpEntity

...

The following is a protocol extension class for OdDbEntity, that uses the sample OdGiWorldGeometryDumper class to print the vector information contained in an OdEntity:

Code Block


void OdDbEntity_Dumper::dump(OdDbEntity* pEnt, STD(ostream) & os) const
{
  dumpCommonData(pEnt, os);
  
  os << "      " << "Entity graphics data:" << STD(endl);
  
  // Dump the graphics data of "unknown" entity
  // graphics for proxy entity are retrieved by the same way
  OdGiContextDumper ctx(pEnt->database());
  OdGiWorldDrawDumper wd(os);
  wd.setContext(&ctx);
  pEnt->worldDraw(&wd);
} // end OdDbEntity_Dumper::dump

...