Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

ObjectDefinitionReader/ObjectDefinitionReader.cpp

This sample shows how to read a DWF package (DWFToolkit::DWFPackageReader) and read the package manifest (DWFToolkit::DWFManifest) and process its object definitions (DWFToolkit::DWFObjectDefinition, DWFToolkit::DWFDefinedObjectInstance) and properties (DWFToolkit::DWFProperty, DWFToolkit::DWFPropertyContainer).

#include "stdafx.h"

using namespace std;
using namespace DWFCore;
using namespace DWFToolkit;


DWFString do_children( DWFToolkit::DWFObjectDefinition* pDef, DWFToolkit::DWFDefinedObjectInstance* pInst )
{
    DWFString zOut;

                    zOut.append( L"Object [" );
                    zOut.append( pInst->object() );
                    zOut.append( L"] Node [" );
                    zOut.append( pInst->node() );
                    zOut.append( L"]\n" );

                    DWFToolkit::DWFProperty* pProp = NULL;
                    DWFToolkit::DWFPropertyContainer* pInstProps = pDef->getInstanceProperties( *pInst );
                                
                    DWFProperty::tMap::Iterator* piProp = pInstProps->getProperties();

                    if (piProp)
                    {
                        for (;piProp->valid(); piProp->next())
                        {
                            pProp = piProp->value();

                            zOut.append( pProp->name() );
                            zOut.append( L", " );
                            zOut.append( pProp->value() );
                            zOut.append( L"    [" );
                            zOut.append( pProp->category() );
                            zOut.append( L"]\n" );

                        }

                        DWFCORE_FREE_OBJECT( piProp );
                    }

                    DWFDefinedObjectInstance::tMap::Iterator* piChildren = pInst->resolvedChildren();
                    
                    if (piChildren)
                    {
                        for (;piChildren->valid(); piChildren->next())
                        {
                            zOut.append( do_children( pDef, piChildren->value() ) );
                        }
                        DWFCORE_FREE_OBJECT( piChildren );
                    }

    return zOut;
}

int main(int argc, char* argv[])
{

    if (argc < 2)
    {
        wcout << L"Usage:" << argv[0] << L" file.dwf" << endl;
        return ( 0 );
    }

    try
    {
        DWFCore::DWFFile oDWF( argv[1] );
        DWFToolkit::DWFPackageReader oReader( oDWF );

        DWFToolkit::DWFPackageReader::tPackageInfo tInfo;
        oReader.getPackageInfo( tInfo );

        char zBuffer[4000] = {0};

        if (tInfo.eType != DWFToolkit::DWFPackageReader::eDWFPackage)
        {
            ::sprintf( zBuffer, "File is not a DWF package [%s]", 
                        (tInfo.eType == DWFPackageReader::eW2DStream) ? "W2D Stream" :
                        (tInfo.eType == DWFPackageReader::eDWFStream) ? "DWF Stream (<6.0)" :
                        (tInfo.eType == DWFPackageReader::eZIPFile) ? "ZIP Archive" : "Unknown" );

            cout << zBuffer << endl;
            exit( 0 );
        }

        ::sprintf( zBuffer, "DWF Package version [%0.2f]", (float)(tInfo.nVersion)/100.0f );
        cout << zBuffer << endl;

        DWFToolkit::DWFManifest& rManifest = oReader.getManifest();

        //
        // create a text file
        //
        DWFCore::DWFFile oTextFilename( L"ObjectDefinitionDump.txt" );
        DWFCore::DWFStreamFileDescriptor oTextFile( oTextFilename, L"w+" );
        oTextFile.open();

        //
        // create a stream to write to the files
        //
        DWFCore::DWFFileOutputStream oFilestream;

        //
        // first, attach the descriptor for the text file to the stream
        // and write the text buffer to the file
        //
        oFilestream.attach( &oTextFile, false );


        DWFToolkit::DWFSection* pSection = NULL;
        DWFToolkit::DWFManifest::SectionIterator* piSections = rManifest.getSections();
        
        if (piSections)
        {
            for (; piSections->valid(); piSections->next())
            {
                pSection = piSections->get();

                DWFToolkit::DWFResourceContainer::ResourceIterator* piObjDefs = pSection->findResourcesByRole( DWFXML::kzRole_ObjectDefinition );
                if (piObjDefs && piObjDefs->valid())
                {
                    DWFToolkit::DWFObjectDefinition* pDef = pSection->getObjectDefinition();
                    if (pDef)
                    {
                        DWFCore::DWFString zOut;

                        DWFToolkit::DWFDefinedObjectInstance::tList oRootInstances;
                        pDef->getRootInstances( oRootInstances );

                        DWFToolkit::DWFDefinedObjectInstance* pInst = NULL;
                        DWFToolkit::DWFDefinedObjectInstance::tList::const_iterator iInst = oRootInstances.begin();
                        for (; iInst != oRootInstances.end(); iInst++)
                        {
                            pInst = *iInst;
                            zOut = do_children( pDef, pInst );

                            wcout << (const wchar_t*)zOut;

                            oFilestream.write( (const wchar_t*)zOut, zOut.bytes() );
                        }
                        
                        DWFCORE_FREE_OBJECT( pDef );
                    }

                    DWFCORE_FREE_OBJECT( piObjDefs );
                }
            }
            DWFCORE_FREE_OBJECT( piSections );
        }

        oFilestream.detach();

        wcout << L"OK\n";
    }
    catch (DWFException& ex)
    {
        wcout << ex.type() << endl;
        wcout << ex.message() << endl;
        wcout << ex.function() << endl;
        wcout << ex.file() << endl;
        wcout << ex.line() << endl;
    }

        return 0;
}



Generated on Tue May 17 12:38:49 2005 for Autodesk DWF Toolkit by  doxygen 1.4.1