WriteContent/WriteContent.cpp

This sample shows how to create meta data in the content definition scheme (DWFToolkit::DWFContentManager) for an ePlot section (DWFToolkit::DWFEPlotSection).

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 // WriteContent.cpp : Defines the entry point for the console application.
00023 //
00024 //  $Header: //DWF/Development/Components/Internal/DWF Toolkit/v7.6/samples/DWFToolkit/WriteContent/WriteContent.cpp#1 $
00025 //  $DateTime: 2008/02/20 08:38:28 $
00026 //  $Author: appacsviewers $
00027 //  $Change: 84992 $
00028 //  $Revision: #1 $
00029 //
00030 
00031 #include "dwfcore/STL.h"
00032 #include <iostream>
00033 
00034 #include "dwfcore/MIME.h"
00035 #include "dwfcore/FileInputStream.h"
00036 #include "dwfcore/StreamFileDescriptor.h"
00037 
00038 #include "dwf/package/EPlotSection.h"
00039 #include "dwf/package/ContentManager.h"
00040 #include "dwf/package/SectionContentResource.h"
00041 #include "dwf/package/writer/DWF6PackageWriter.h"
00042 
00043 using namespace std;
00044 using namespace DWFCore;
00045 using namespace DWFToolkit;
00046 
00047 //
00048 // declarations
00049 //
00050 DWFGraphicResource* CreateGraphicsResource()
00051     throw( DWFException );
00052 
00053 DWFImageResource* CreateThumbnailResource()
00054     throw( DWFException );
00055 
00056 DWFEPlotSection* CreateSimpleEPlotSection( DWFContentManager* pContentManager )
00057     throw( DWFException );
00058 
00059 
00061 
00062 int main( int /*argc*/, char* /*argv[]*/ )
00063 {
00064     try
00065     {
00066         //
00067         // Take ownership of the content manager
00068         //
00069         DWFContentManager* pContentMgr = DWFCORE_ALLOC_OBJECT( DWFContentManager() );
00070 
00071         DWFEPlotSection* pEPlotSection = CreateSimpleEPlotSection( pContentMgr );
00072 
00073         //
00074         // Write out the dwf with the created sections and associated content.
00075         //
00076         DWFFile oDWF( "content.dwf" );
00077         DWF6PackageWriter oWriter( oDWF );
00078         oWriter.attachContentManager( pContentMgr );
00079         oWriter.addSection( pEPlotSection );
00080         oWriter.write();
00081     }
00082     catch (DWFException& ex)
00083     {
00084         wcout << ex.type() << L": " << ex.message() << endl;
00085         wcout << L"(function) " << ex.function() << endl;
00086         wcout << L"(file) " << ex.file() << endl;
00087         wcout << L"(line) " << ex.line() << endl;
00088     }
00089 
00090         return 0;
00091 }
00092 
00094 
00095 DWFEPlotSection* CreateSimpleEPlotSection( DWFContentManager* pContentManager )
00096 throw( DWFException )
00097 {
00098     //
00099     //  This will be used to define where this DWF originated
00100     //
00101     DWFSource oSource( L"WriteContent.cpp", "DWFToolkit Samples", L"" );
00102 
00103     //
00104     //  We will be adding a graphic resource to this page (namely the W2D)
00105     //  this dictates that we define paper for this section
00106     //
00107     double anPaperClip[4] = { 0, 0, 11, 8.5 };
00108     DWFPaper oPaper( 11, 8.5, DWFPaper::eInches, 0x00ffffff, (const double*)anPaperClip );
00109 
00110     // 
00111     //  Create the EPlot section with the paper and source information.
00112     //
00113     DWFEPlotSection* pPage = DWFCORE_ALLOC_OBJECT( DWFEPlotSection(L"Page Title", L"123", 1, oSource, 0x00ff00ff, &oPaper) );
00114 
00115     //
00116     //  Create a 2D graphics resource.
00117     //  Add it to the section. The section will take ownership.
00118     //
00119     DWFGraphicResource* p2Dgfx = CreateGraphicsResource();
00120     pPage->addResource( p2Dgfx, true );
00121 
00122     //
00123     //  Create the thumbnail/image resource.
00124     //  Add it to the page and be sure to mention that it "belongs" to the W2D 
00125     //  (and replace and delete)
00126     //
00127     DWFImageResource* pThumbnail = CreateThumbnailResource();
00128     pPage->addResource( pThumbnail, true, true, true, p2Dgfx );
00129 
00130 
00131     if (pContentManager)
00132     {
00133         //
00134         //  This will create a content library if it doesn't exist - this new content
00135         //  will also become the primary content since it is the first one created.
00136         //
00137         DWFContent* pContent = pContentManager->getContent();
00138         DWFString zSchema1 = pContent->getIDProvider()->next(true);
00139         DWFString zSchema2 = pContent->getIDProvider()->next(true);
00140         DWFString zSetID1 = pContent->getIDProvider()->next(true);
00141 
00142         //
00143         // Lets add a couple of property sets with properties to be shared.
00144         //
00145         DWFPropertySet* pPSet1 = pContent->addSharedPropertySet( L"PSet1" );
00146         pPSet1->addProperty( L"Geo", L"Ithaca" );
00147         pPSet1->addProperty( L"State", L"NY" );
00148         {
00149             DWFPropertySet* pPSubSet1 = pPSet1->addPropertySet( L"PSet subset1" );
00150             pPSubSet1->addProperty( L"SubGeo", L"North East Ithaca" );
00151 
00152             pPSubSet1->addProperty( L"Geo", L"Greater Ithaca" );
00153             pPSubSet1->setSchemaID( zSchema1 );
00154         }
00155 
00156         DWFPropertySet* pPSet2 = pContent->addSharedPropertySet( L"PSet2" );
00157         pPSet2->setClosed( zSetID1 );
00158         pPSet2->addProperty( L"Geo", L"SF" );
00159         pPSet2->addProperty( L"State", L"CA" );
00160         {
00161             DWFPropertySet* pSubSet2 = pPSet2->addPropertySet( zSchema1 );
00162             pSubSet2->addProperty( L"Geo", L"Southern SF" );
00163         }
00164 
00165         //
00166         // Add a couple of classes
00167         //
00168         DWFClass* pClass1 = pContent->addClass();
00169 
00170         DWFClass::tList oBaseClass;
00171         oBaseClass.push_back( pClass1 );
00172         DWFClass* pClass2 = pContent->addClass( oBaseClass );
00173         pClass2->addProperty( L"Class#", L"#2" );
00174 
00175         DWFClass* pClass3 = pContent->addClass();
00176         pClass3->addProperty( L"Class#", L"#3" );
00177         pClass3->addProperty( L"Color", L"Purple" );
00178 
00179         //
00180         // Add a couple of entities
00181         //
00182         oBaseClass.clear();
00183         oBaseClass.push_back( pClass2 );
00184         DWFEntity* pEntityParent = pContent->addEntity( oBaseClass );
00185         DWFEntity* pEntity1 = pContent->addEntity( pEntityParent );
00186         pContent->addClassToEntity( pEntity1, pClass3 );
00187         {
00188             pEntity1->addProperty( "EntityName", "Entity #1" );
00189             pEntity1->addProperty( "ObjectName", "ObjectEntity #1" );
00190         }
00191         
00192         //
00193         // Add a feature
00194         //
00195         DWFFeature* pFeature1 = pContent->addFeature();
00196         pFeature1->addProperty( L"EdgeType", L"Bevelled" );
00197 
00198 
00199         //
00200         // Add a couple of objects
00201         //
00202         DWFObject* pObject1 = pContent->addObject( pEntity1 );
00203         pObject1->setLabel( "Object #1" );
00204         {
00205             pContent->addSharedPropertySetToElement( pObject1, pPSet1 );
00206             pObject1->addProperty( L"ObjectName", L"Object1" );
00207             pObject1->addProperty( L"Location", L"Geo1" );
00208         }
00209 
00210         DWFObject* pObject2 = pContent->addObject( pEntity1, pObject1 );
00211         pContent->addSharedPropertySetToElement( pObject2, pPSet2 );
00212 
00213 
00214         //
00215         // Add a group with properties
00216         //
00217         DWFContentElement::tList oGroupItems;
00218         oGroupItems.push_back( pClass1 );
00219         oGroupItems.push_back( pClass2 );
00220         oGroupItems.push_back( pFeature1 );
00221         DWFGroup* pGroup1 = pContent->addGroup( oGroupItems );
00222         pGroup1->addProperty( L"Type", L"Assortment", L"None", L"string", L"wide" );
00223         pGroup1->addProperty( L"Object", L"A", L"None", L"string", L"wide" );
00224         {
00225             DWFPropertySet* pSetGroup1 = pGroup1->addPropertySet( L"GroupSet" );
00226             pSetGroup1->setClosed( zSetID1 );
00227             pSetGroup1->setSchemaID( pContent->getIDProvider()->next(true) );
00228             pSetGroup1->addProperty( L"GName1", L"GVal1" );
00229             pSetGroup1->addProperty( L"GName2", L"GVal2" );
00230             {
00231                 DWFPropertySet* pSetGroup11 = pSetGroup1->addPropertySet( pContent->getIDProvider()->next(true) );
00232                 pSetGroup11->addProperty( L"GName11", L"GVal11" );
00233                 pSetGroup11->referencePropertyContainer( *pPSet1 );
00234             }
00235         }
00236 
00237         //
00238         //  Add a section content resource to tie the instance information to the graphics stream. 
00239         //  Assume we have access to graphics nodes 10 and 20 for the instances (below).
00240         //
00241         DWFSectionContentResource* pSCR = DWFCORE_ALLOC_OBJECT( DWFSectionContentResource(pContent) );
00242         pPage->addResource( pSCR, true, true, true, p2Dgfx );
00243         DWFString zResourceID = pSCR->objectID();
00244         //
00245         // Add a couple of instances based on the objects
00246         //
00247         DWFInstance* pInstance1 = pContent->addInstance( zResourceID, pObject1, 10 );
00248         pInstance1->setVisibility( false );
00249         pInstance1->setGeometricVariationIndex( 2 );
00250         /*DWFInstance* pInstance2 = */pContent->addInstance( zResourceID, pFeature1, 20 );
00251     }
00252 
00253     return pPage;
00254 }
00255 
00256 DWFGraphicResource* CreateGraphicsResource()
00257 throw( DWFException )
00258 {
00259     //
00260     // Create a graphics resource to bind to an existing w2d stream on disk.
00261     //
00262     DWFGraphicResource* p2Dgfx = 
00263         DWFCORE_ALLOC_OBJECT( DWFGraphicResource(L"Oceanarium",             // title
00264                                                  DWFXML::kzRole_Graphics2d, // role
00265                                                  DWFMIME::kzMIMEType_W2D,   // MIME type
00266                                                  L"Autodesk, Inc.",         // author
00267                                                  L"Oceanarium Sample",      // description
00268                                                  L"",                       // creation time
00269                                                  L"") );                    // modification time
00270     if (p2Dgfx == NULL)
00271     {
00272         _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
00273     }
00274 
00275     //
00276     // Configure the resource.
00277     //
00278     double anTransform[4][4] = { 0.00015625, 0, 0, 0, 0, 0.00015625, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
00279     double anClip[4] = { 0, 0, 11, 8.5 };
00280     p2Dgfx->configureGraphic( (const double*)anTransform, NULL, (const double*)anClip );
00281 
00282     //
00283     // Bind a stream to the resource. We have a file on disk so we open the file with a 
00284     // streaming descriptor. Everything is created on the heap since the package writer 
00285     // will not use the stream immediately. It will need to own these resources.
00286     //
00287     DWFFile oW2DFilename( L"ocean_90.w2d" );
00288     DWFStreamFileDescriptor* pW2DFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oW2DFilename, L"rb") );
00289 
00290     if (pW2DFile == NULL)
00291     {
00292         DWFCORE_FREE_OBJECT( p2Dgfx );
00293         _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
00294     }
00295 
00296     DWFFileInputStream* pW2DFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
00297 
00298     if (pW2DFilestream == NULL)
00299     {
00300         DWFCORE_FREE_OBJECT( p2Dgfx );
00301         DWFCORE_FREE_OBJECT( pW2DFile );
00302         _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
00303     }
00304 
00305     //
00306     // Open the file and bind it to the stream
00307     //
00308     pW2DFile->open();
00309     pW2DFilestream->attach( pW2DFile, true );
00310 
00311     //
00312     // hand the stream off to the resource
00313     // NOTE: since we don't already know the filesize (in the application space - of course we can look on disk...)
00314     // leave the second parameter (nBytes) default as zero, this will tell the package writer
00315     // to use the number of bytes it processed through the stream as the size attribute in the descriptor.
00316     //
00317     p2Dgfx->setInputStream( pW2DFilestream );
00318 
00319     return p2Dgfx;
00320 }
00321 
00322 DWFImageResource* CreateThumbnailResource()
00323 throw( DWFException )
00324 {
00325     DWFImageResource* pThumbnail = 
00326         DWFCORE_ALLOC_OBJECT( DWFImageResource( L"Oceanarium Thumbnail",
00327                                                  DWFXML::kzRole_Thumbnail,
00328                                                  DWFMIME::kzMIMEType_PNG) );
00329     if (pThumbnail == NULL)
00330     {
00331         _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
00332     }
00333 
00334     //
00335     // Configure the resource.
00336     //
00337     double anTransform2[4][4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
00338     double anExtents2[4] = { 0, 0, 220, 170 };
00339 
00340     pThumbnail->configureGraphic( (const double*)anTransform2,
00341                                   (const double*)anExtents2 );
00342 
00343     //
00344     // Extras for the image. Here we note the image has 24 bpp (color depth).
00345     //
00346     pThumbnail->configureImage( 24 );
00347 
00348     DWFFile oThumbnailFilename( L"ocean_thumbnail.png" );
00349     DWFStreamFileDescriptor* pThumbnailFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oThumbnailFilename, L"rb") );
00350 
00351     if (pThumbnailFile == NULL)
00352     {
00353         DWFCORE_FREE_OBJECT( pThumbnail );
00354         _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
00355     }
00356 
00357     DWFFileInputStream* pThumbnailFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
00358 
00359     if (pThumbnailFilestream == NULL)
00360     {
00361         DWFCORE_FREE_OBJECT( pThumbnail );
00362         DWFCORE_FREE_OBJECT( pThumbnailFile );
00363         _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
00364     }
00365 
00366     pThumbnailFile->open();
00367     pThumbnailFilestream->attach( pThumbnailFile, true );
00368 
00369     pThumbnail->setInputStream( pThumbnailFilestream );
00370 
00371     return pThumbnail;
00372 }
00373 

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