AcGe Persistency Examples

The following is an example of reading and writing of an AcGeExternalSurface with a concrete subclass of AcGeFiler. For the purpose of this example, the serializing filer is a DWG filer. Consequently, it provides persistency by reading and writing DWG format:

#include "gefiler.h"
class AcDbDwgFiler;
class AcGeDwgFiler : public AcGeFiler
{
public:
// Construct the filer with DWG filer. 
// All read and write methods are implemented
 
// by delegating to this filer.
 
    AcGeDwgFiler (AcDbDwgFiler* = NULL);
    // Read/write methods.
    //
    Acad::ErrorStatus      readBoolean(Adesk::Boolean*); 
    Acad::ErrorStatus      writeBoolean(Adesk::Boolean);
    Acad::ErrorStatus      readBool(bool*);
    Acad::ErrorStatus      writeBool(bool);
    Acad::ErrorStatus      readChar(char*);
    Acad::ErrorStatus      writeChar(char);
    Acad::ErrorStatus      readShort(short*);
    Acad::ErrorStatus      writeShort(short);
    Acad::ErrorStatus      readLong(long*);
    Acad::ErrorStatus      writeLong(long);
    Acad::ErrorStatus      readUChar(unsigned char*);
    Acad::ErrorStatus      writeUChar(unsigned char);
    Acad::ErrorStatus      readUShort(unsigned short*);
    Acad::ErrorStatus      writeUShort(unsigned short);
    Acad::ErrorStatus      readULong(unsigned long*);
    Acad::ErrorStatus      writeULong(unsigned long);
    Acad::ErrorStatus      readDouble(double*);
    Acad::ErrorStatus      writeDouble(double);
    Acad::ErrorStatus      readPoint2d(AcGePoint2d*);
    Acad::ErrorStatus      writePoint2d(const AcGePoint2d&);
    Acad::ErrorStatus      readPoint3d(AcGePoint3d*);
    Acad::ErrorStatus      writePoint3d(const AcGePoint3d&);
    Acad::ErrorStatus      readVector2d(AcGeVector2d*);
    Acad::ErrorStatus      writeVector2d(const AcGeVector2d&);
    Acad::ErrorStatus      readVector3d(AcGeVector3d*);
    Acad::ErrorStatus      writeVector3d(const AcGeVector3d&);
    // Set/Get methods
    //
    AcGeDwgFiler&    setDwgFiler     (AcDbDwgFiler*);
    AcDbDwgFiler*    dwgFiler        ();
protected:
    AcDbDwgFiler*    mpFiler;
};
// Inline methods.
//
inline
AcGeDwgFiler::AcGeDwgFiler(AcDbDwgFiler* filer) : mpFiler(filer)
{}
inline AcGeDwgFiler&
AcGeDwgFiler::setDwgFiler(AcDbDwgFiler* filer)
{
    mpFiler = filer;
    return *this;
}
inline AcDbDwgFiler*
AcGeDwgFiler::dwgFiler()
{
    return mpFiler;
}

The next code fragment illustrates the implementation of a few functions. Other functions are implemented in the same manner:

Acad::ErrorStatus AcGeDwgFiler::readBoolean(Adesk::Boolean* data)
{
    return mpFiler ? mpFiler->readBoolean(data) : Acad::eNoDatabase;
}
Acad::ErrorStatus AcGeDwgFiler::writeBoolean(Adesk::Boolean data)
{
    return mpFiler ? 
	mpFiler->writeBoolean(data) : Acad::eNoDatabase;
}

The next example illustrates a persistent class that uses AcGeExternalSurface. The code after the class declaration illustrates reading and writing of the external surface class. In particular, note that

AcGe::gLibVersion is written out first and subsequently read first prior to writing or reading of the external surface class:

class AcGeExternalCurve2d;
class AcGeExternalCurve3d;
class AcGeExternalBoundedSurface;
class AcGeExternalSurface;    
class  AcGePersistentXEnt : public AcDbObject  
{ 
public:
    ACRX_DECLARE_MEMBERS (AcGePersistentXEnt);
    AcGePersistentXEnt ();	
    ~AcGePersistentXEnt ();
    Acad::ErrorStatus dwgOutFields (AcDbDwgFiler*) const; 
    Acad::ErrorStatus dwgInFields  (AcDbDwgFiler*);
    AcGeExternalSurface*           mpXSrf;
};

The implementation of this class is as follows:

ACRX_DXF_DEFINE_MEMBERS(
    AcGePersistentXEnt, AcDbObject, AcDb::kDHL_CURRENT,
    AcDb::kMReleaseCurrent, 0, GEPERSISTENT, "GeometryLib");
Acad::ErrorStatus 
AcGePersistentXEnt::dwgOutFields(AcDbDwgFiler* filer) const
{
    assertReadEnabled();
    Acad::ErrorStatus stat = AcDbObject::dwgOutFields(filer);
    if (stat != Acad::eOk)
    {
        ADS_ASSERT(0);
        return stat;
    }
    // Only interested in a file filer.
    //
    if (filer->filerType() != AcDb::kFileFiler)
        return stat;
    AcGeDwgFiler   geDwgFiler(filer);
    stat = AcGeFileIO::outFields(
        &geDwgFiler, 
        AcGe::gLibVersion);
    if ((stat = AcGeFileIO::outFields(&geDwgFiler,
        *mpXSrf)) != Acad::eOk)
        return stat;
    return stat;
}
Acad::ErrorStatus 
AcGePersistentXEnt::dwgInFields(AcDbDwgFiler* filer)
{
    assertWriteEnabled();
    Acad::ErrorStatus stat = AcDbObject::dwgInFields(filer);
    if (stat != Acad::eOk)
    {
        ADS_ASSERT(0);
        return stat;
    }
    // Only interested in a file filer.
    //
    if (filer->filerType() != AcDb::kFileFiler)
        return stat;
    AcGeDwgFiler    geDwgFiler(filer);
    AcGeLibVersion  gelibVersion;
    if ((stat = AcGeFileIO::inFields(&geDwgFiler,
        gelibVersion)) != Acad::eOk)
        return stat;
    acutPrintf("\n... Reading External Surface\n");
    mpXSrf = new AcGeExternalSurface;
    ADS_ASSERT(mpXSrf);
    if ((stat = AcGeFileIO::inFields(&geDwgFiler,
        *mpXSrf, gelibVersion))  != Acad::eOk)
        return stat;
    return stat;
}