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

GlobalSectionWriter/GlobalSectionWriter.cpp

This sample shows how to add a global section (DWFToolkit::DWFEPlotGlobalSection) to a DWF (DWFToolkit::DWFPackageWriter) including: adding global resources (DWFToolkit::DWFResource) and objects (DWFToolkit::DWFObjectDefinitionResource), bookmarks (DWFToolkit::DWFBookmark) and custom sections (DWFToolkit::DWFCustomSection).

// SimpleEnumReader.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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


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

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

    try
    {
        //
        // simple test case for adding a global section to a dwf
        //

        //
        // add an eplot section
        //
        DWFToolkit::DWFEPlotSection* pPage = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFEPlotSection(L"", L"", 0, DWFSource(L"",L"",L""), 0x00ffffff, NULL) );

        if (pPage == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate section" );
        }

        //
        // we could add a source & title to this section if necessary
        //
        DWFToolkit::DWFEPlotGlobalSection* pGlobal = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFEPlotGlobalSection );

        if (pGlobal == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate section" );
        }

        //
        // add a property to the global section
        // this should appear in the section descriptor of the global section
        //
        DWFToolkit::DWFProperty oProp( L"pi_short", L"3.14159" );
        pGlobal->addProperty( &oProp, false );

        //
        // eplot object definition
        //
        DWFToolkit::DWFObjectDefinitionResource* pEPlotObjectDef = 
            DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFObjectDefinitionResource(DWFXML::kzElement_PageObjectDefinition, DWFXML::kzRole_ObjectDefinition) );

        //
        // create a set of properties
        //
        DWFToolkit::DWFPropertyContainer* pSet = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFPropertyContainer );
        
        oProp.setName( L"Dad" );
        oProp.setValue( L"Homer" );
        oProp.setCategory( L"Simpsons" );
        pSet->addProperty( &oProp, false );

        oProp.setName( L"Son" );
        oProp.setValue( L"Bart" );
        oProp.setCategory( L"Simpsons" );
        pSet->addProperty( &oProp, false );

        oProp.setName( L"Daughter" );
        oProp.setValue( L"Lisa" );
        oProp.setCategory( L"Simpsons" );
        pSet->addProperty( &oProp, false );

        //
        // add this property set to the eplot object definition
        // this property set will appear in that document (not a section descriptor)
        //
        pEPlotObjectDef->addPropertyContainer( pSet );

        //
        // create another set
        //
        DWFToolkit::DWFPropertyContainer* pSet2 = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFPropertyContainer );
        
        oProp.setName( L"Name" );
        oProp.setValue( L"Value" );
        oProp.setCategory( L"" );
        pSet2->addProperty( &oProp, false );

        //
        // add these properties to the global section
        // again, this should appear in the section descriptor of the global section
        //
        pGlobal->addPropertyContainer( pSet2 );


        //
        // create two objects
        //
        DWFToolkit::DWFDefinedObject* pEPlotObject = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFDefinedObject(L"") );
        DWFToolkit::DWFDefinedObject* pGlobalObject = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFDefinedObject(L"Custom ID") );

        //
        // add some properties to the eplot object
        // this will create a reference to the property set in the same document
        //
        pEPlotObject->referencePropertyContainer( *pSet );

        //
        // add some properties to the global object
        // this will create a reference to the property set in the global section descriptor
        // and appear in the global section object definition document
        //
        pGlobalObject->referencePropertyContainer( *pSet2 );

        //
        // add some global properties to the eplot object
        // this will create a reference to the property set in the global section descriptor
        // and appear in the eplot object definition document
        //
        pEPlotObject->referencePropertyContainer( *pSet2 );

        //
        // override an eplot property set property
        //
        oProp.setName( L"Daughter" );
        oProp.setValue( L"Maggie" );
        oProp.setCategory( L"Simpsons" );

        //
        // add the property to the eplot object
        // this should appear in the eplot object definition document
        //
        pEPlotObject->addProperty( &oProp, false );

        //
        // override the global property set property
        //
        oProp.setName( L"Name" );
        oProp.setValue( L"Value Override" );
        oProp.setCategory( L"" );

        //
        // add the property to the eplot object
        // this should appear in the eplot object definition document
        //
        pEPlotObject->addProperty( &oProp, false );

        //
        // add the object to the object definition
        //
        pEPlotObjectDef->addObject( pEPlotObject );

        //
        // add the definition to the page
        //
        pPage->addResource( pEPlotObjectDef, true, true, true, NULL );

        //
        // global object definition
        //
        DWFToolkit::DWFObjectDefinitionResource* pGlobalObjectDef = 
            DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFObjectDefinitionResource(DWFXML::kzElement_GlobalObjectDefinition, DWFXML::kzRole_ObjectDefinition) );

        //
        // add the global object to the global object definition document
        //
        pGlobalObjectDef->addObject( pGlobalObject );

        //
        // add to the global section
        //
        pGlobal->addResource( pGlobalObjectDef, true, true, true, NULL );

        //
        // add a simple bookmark tree
        //
        DWFToolkit::DWFBookmark* pRoot = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"autodesk",L"http://www.autodesk.com") );
        DWFToolkit::DWFBookmark* pMark = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"dwf",L"http://www.autodesk.com/dwf") );
        pRoot->addChildBookmark( pMark );

        DWFToolkit::DWFBookmark* pMark2 = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"viewers",L"http://www.autodesk.com/viewers") );
        pMark->addChildBookmark( pMark2 );

        pMark = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"adv",L"http://www.autodesk.com/dwfviewer") );
        pMark2->addChildBookmark( pMark );

        pMark = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"composer",L"http://www.autodesk.com/dwfcomposer") );
        pMark2->addChildBookmark( pMark );

        pGlobal->provideBookmark( pRoot );

        //
        // add a global image resource
        //
        DWFToolkit::DWFImageResource* pImage = 
            DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFImageResource( L"Oceanarium",
                                                    L"Image",
                                                     DWFMIME::kzMIMEType_PNG) );

        if (pImage == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
        }

        //
        // configure the resource
        //
        double anTransform2[4][4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
        double anExtents2[4] = { 0, 0, 220, 170 };

        pImage->configureGraphic( (const double*)anTransform2,
                                  (const double*)anExtents2 );

        //
        // extras for the image
        // here we note the image has 24 bpp (color depth)
        //
        pImage->configureImage( 24 );

        DWFCore::DWFFile oImageFilename( L"ocean_thumbnail.png" );
        DWFCore::DWFStreamFileDescriptor* pImageFile = DWFCORE_ALLOC_OBJECT( DWFCore::DWFStreamFileDescriptor(oImageFilename, L"rb") );

        if (pImageFile == NULL)
        {
            DWFCORE_FREE_OBJECT( pImage );

            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
        }

        DWFCore::DWFFileInputStream* pImageFilestream = DWFCORE_ALLOC_OBJECT( DWFCore::DWFFileInputStream );

        if (pImageFilestream == NULL)
        {
            DWFCORE_FREE_OBJECT( pImage );
            DWFCORE_FREE_OBJECT( pImageFile );

            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
        }

        pImageFile->open();
        pImageFilestream->attach( pImageFile, true );

        pImage->setInputStream( pImageFilestream );

        //
        // drop the resource into the global section
        //
        pGlobal->addResource( pImage, true, true, true, NULL );


        //
        // let's also add a custom private section to this dwf
        //

        DWFToolkit::DWFCustomSection* pCustomSection = 
            DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFCustomSection(L"Custom.Private.Type", L"Custom Private Title", L"BOND-007", 0.0, 1, DWFSource(L"", L"", L"")) );

        if (pCustomSection == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate section" );
        }

        //
        // add a private resource to the section
        //
        DWFToolkit::DWFResource* pCustomResource = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFResource(L"GlobalSectionWriter.cpp", L"Source File", DWFMIME::kzMIMEType_TXT, L"GlobalSectionWriter.cpp") );

        if (pCustomResource == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
        }

        DWFCore::DWFFile oCustomFilename( L"GlobalSectionWriter.cpp" );
        DWFCore::DWFStreamFileDescriptor* pCustomFile = DWFCORE_ALLOC_OBJECT( DWFCore::DWFStreamFileDescriptor(oCustomFilename, L"rb") );
        if (pCustomFile == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
        }

        DWFCore::DWFFileInputStream* pCustomFilestream = DWFCORE_ALLOC_OBJECT( DWFCore::DWFFileInputStream );
        if (pCustomFilestream == NULL)
        {
            _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
        }

        pCustomFile->open();
        pCustomFilestream->attach( pCustomFile, true );
        pCustomResource->setInputStream( pCustomFilestream );

        //
        // drop the resource into the global section
        //
        pCustomSection->addResource( pCustomResource, true, true, true, NULL );


        DWFCore::DWFFile oDWF( argv[1] );
        DWFToolkit::DWFPackageWriter oWriter( oDWF );

        oWriter.addSection( pPage );
        oWriter.addSection( pCustomSection );
        oWriter.addGlobalSection( pGlobal );
        oWriter.write( L"Autodesk", L"GlobalSectionWriter.cpp", L"", L"Autodesk", _DWFTK_VERSION_STRING );

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

        return 0;
}



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