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).

00001 //
00002 //  Copyright (c) 2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 // SimpleEnumReader.cpp : Defines the entry point for the console application.
00023 //
00024 
00025 #include "stdafx.h"
00026 
00027 using namespace std;
00028 using namespace DWFCore;
00029 using namespace DWFToolkit;
00030 
00031 
00032 int main(int argc, char* argv[])
00033 {
00034 
00035     if (argc < 2)
00036     {
00037         wcout << L"Usage:" << argv[0] << L" file.dwf" << endl;
00038         return ( 0 );
00039     }
00040 
00041     try
00042     {
00043         //
00044         // simple test case for adding a global section to a dwf
00045         //
00046 
00047         //
00048         // add a phony eplot section
00049         //
00050         DWFEPlotSection* pPage = DWFCORE_ALLOC_OBJECT( DWFEPlotSection(L"", L"", 0, DWFSource(L"",L"",L""), 0x00ffffff, NULL) );
00051 
00052         if (pPage == NULL)
00053         {
00054             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate section" );
00055         }
00056 
00057         //
00058         // we could add a source & title to this section if necessary
00059         //
00060         DWFEPlotGlobalSection* pGlobal = DWFCORE_ALLOC_OBJECT( DWFEPlotGlobalSection );
00061 
00062         if (pGlobal == NULL)
00063         {
00064             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate section" );
00065         }
00066 
00067         //
00068         // add a property
00069         //
00070         DWFProperty oProp( L"pi_short", L"3.14159" );
00071         pGlobal->addProperty( &oProp, false );
00072 
00073         //
00074         // add a set of properties
00075         //
00076         DWFPropertyContainer* pSet = DWFCORE_ALLOC_OBJECT( DWFPropertyContainer );
00077         
00078         oProp.setName( L"Dad" );
00079         oProp.setValue( L"Homer" );
00080         oProp.setCategory( L"Simpsons" );
00081         pSet->addProperty( &oProp, false );
00082 
00083         oProp.setName( L"Son" );
00084         oProp.setValue( L"Bart" );
00085         oProp.setCategory( L"Simpsons" );
00086         pSet->addProperty( &oProp, false );
00087 
00088         oProp.setName( L"Daughter" );
00089         oProp.setValue( L"Lisa" );
00090         oProp.setCategory( L"Simpsons" );
00091         pSet->addProperty( &oProp, false );
00092 
00093         pGlobal->addPropertyContainer( pSet );
00094 
00095         //
00096         // create another set
00097         //
00098         DWFPropertyContainer* pSet2 = DWFCORE_ALLOC_OBJECT( DWFPropertyContainer );
00099         
00100         oProp.setName( L"Name" );
00101         oProp.setValue( L"Value" );
00102         oProp.setCategory( L"" );
00103         pSet2->addProperty( &oProp, false );
00104 
00105         pGlobal->addPropertyContainer( pSet2 );
00106 
00107 
00108         //
00109         // create two objects
00110         //
00111         DWFDefinedObject* pGlobalObject = DWFCORE_ALLOC_OBJECT( DWFDefinedObject(L"") );
00112         DWFDefinedObject* pGlobalObject2 = DWFCORE_ALLOC_OBJECT( DWFDefinedObject(L"Custom ID") );
00113 
00114         //
00115         // add some properties
00116         //
00117         pGlobalObject->referencePropertyContainer( *pSet );
00118         pGlobalObject->referencePropertyContainer( *pSet2 );
00119 
00120         //
00121         // override set prop
00122         //
00123         oProp.setName( L"Daughter" );
00124         oProp.setValue( L"Maggie" );
00125         oProp.setCategory( L"Simpsons" );
00126 
00127         pGlobalObject->addProperty( &oProp, false );
00128 
00129         //
00130         // object defintion
00131         //
00132         DWFObjectDefinitionResource* pGlobalObjectDef = 
00133             DWFCORE_ALLOC_OBJECT( DWFObjectDefinitionResource(DWFXML::kzElement_GlobalObjectDefinition, DWFXML::kzRole_ObjectDefinition) );
00134 
00135         pGlobalObjectDef->addObject( pGlobalObject );
00136         pGlobalObjectDef->addObject( pGlobalObject2 );
00137 
00138         //
00139         // add to section
00140         //
00141         pGlobal->addResource( pGlobalObjectDef, true, true, true, NULL );
00142 
00143         //
00144         // add a simple bookmark tree
00145         //
00146         DWFBookmark* pRoot = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"autodesk",L"http://www.autodesk.com") );
00147         DWFBookmark* pMark = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"dwf",L"http://www.autodesk.com/dwf") );
00148         pRoot->addChildBookmark( pMark );
00149 
00150         DWFBookmark* pMark2 = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"viewers",L"http://www.autodesk.com/viewers") );
00151         pMark->addChildBookmark( pMark2 );
00152 
00153         pMark = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"adv",L"http://www.autodesk.com/dwfviewer") );
00154         pMark2->addChildBookmark( pMark );
00155 
00156         pMark = DWFCORE_ALLOC_OBJECT( DWFBookmark(L"composer",L"http://www.autodesk.com/dwfcomposer") );
00157         pMark2->addChildBookmark( pMark );
00158 
00159         pGlobal->provideBookmark( pRoot );
00160 
00161         //
00162         // add a global image resource
00163         //
00164         DWFImageResource* pImage = 
00165             DWFCORE_ALLOC_OBJECT( DWFImageResource( L"Oceanarium",
00166                                                     L"Image",
00167                                                      DWFMIME::kzMIMEType_PNG) );
00168 
00169         if (pImage == NULL)
00170         {
00171             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
00172         }
00173 
00174         //
00175         // configure the resource
00176         //
00177         double anTransform2[4][4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
00178         double anExtents2[4] = { 0, 0, 220, 170 };
00179 
00180         pImage->configureGraphic( (const double*)anTransform2,
00181                                   (const double*)anExtents2 );
00182 
00183         //
00184         // extras for the image
00185         // here we note the image has 24 bpp (color depth)
00186         //
00187         pImage->configureImage( 24 );
00188 
00189         DWFFile oImageFilename( L"ocean_thumbnail.png" );
00190         DWFStreamFileDescriptor* pImageFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oImageFilename, L"rb") );
00191 
00192         if (pImageFile == NULL)
00193         {
00194             DWFCORE_FREE_OBJECT( pImage );
00195 
00196             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
00197         }
00198 
00199         DWFFileInputStream* pImageFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
00200 
00201         if (pImageFilestream == NULL)
00202         {
00203             DWFCORE_FREE_OBJECT( pImage );
00204             DWFCORE_FREE_OBJECT( pImageFile );
00205 
00206             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
00207         }
00208 
00209         pImageFile->open();
00210         pImageFilestream->attach( pImageFile, true );
00211 
00212         pImage->setInputStream( pImageFilestream );
00213 
00214         //
00215         // drop the resource into the global section
00216         //
00217         pGlobal->addResource( pImage, true, true, true, NULL );
00218 
00219 
00220         //
00221         // let's also add a custom private section to this dwf
00222         //
00223 
00224         DWFCustomSection* pCustomSection = 
00225             DWFCORE_ALLOC_OBJECT( DWFCustomSection(L"Custom.Private.Type", L"Custom Private Title", L"BOND-007", 0.0, 1, DWFSource(L"", L"", L"")) );
00226 
00227         if (pCustomSection == NULL)
00228         {
00229             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate section" );
00230         }
00231 
00232         //
00233         // add a private resource to the section
00234         //
00235         DWFResource* pCustomResource = DWFCORE_ALLOC_OBJECT( DWFResource(L"GlobalSectionWriter.cpp", L"Source File", DWFMIME::kzMIMEType_TXT, L"GlobalSectionWriter.cpp") );
00236 
00237         if (pCustomResource == NULL)
00238         {
00239             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
00240         }
00241 
00242         DWFFile oCustomFilename( L"GlobalSectionWriter.cpp" );
00243         DWFStreamFileDescriptor* pCustomFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oCustomFilename, L"rb") );
00244         if (pCustomFile == NULL)
00245         {
00246             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
00247         }
00248 
00249         DWFFileInputStream* pCustomFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
00250         if (pCustomFilestream == NULL)
00251         {
00252             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
00253         }
00254 
00255         pCustomFile->open();
00256         pCustomFilestream->attach( pCustomFile, true );
00257         pCustomResource->setInputStream( pCustomFilestream );
00258 
00259         //
00260         // drop the resource into the global section
00261         //
00262         pCustomSection->addResource( pCustomResource, true, true, true, NULL );
00263 
00264 
00265         DWFFile oDWF( argv[1] );
00266         DWF6PackageWriter oWriter( oDWF );
00267 
00268         oWriter.addSection( pPage );
00269         oWriter.addSection( pCustomSection );
00270         oWriter.addGlobalSection( pGlobal );
00271         oWriter.write( L"Autodesk", L"GlobalSectionWriter.cpp", L"", L"Autodesk", _DWFTK_VERSION_STRING );
00272 
00273         wcout << L"OK\n";
00274     }
00275     catch (DWFException& ex)
00276     {
00277         wcout << ex.type() << L": " << ex.message() << endl;
00278         wcout << L"(function) " << ex.function() << endl;
00279         wcout << L"(file) " << ex.file() << endl;
00280         wcout << L"(line) " << ex.line() << endl;
00281     }
00282 
00283         return 0;
00284 }
00285 
00286 

Generated on Tue Jan 6 22:40:01 2009 for Autodesk DWF Toolkit by  doxygen 1.4.5