SimpleEPlotWriter/SimpleEPlotWriter_UTF8.cpp

This sample shows how to create a 2D DWF (DWFToolkit::DWFPackageWriter, DWFToolkit::DWFEPlotSection), place W2D vector data (DWFToolkit::DWFGraphicResource) on a sheet of virtual paper (DWFToolkit::DWFPaper), associate objects with it (DWFToolkit::DWFObjectDefinitionResource) add a thumbnail (DWFToolkit::DWFImageResource) and more.

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         // create a simple page (eplot)
00045         //
00046 
00047         //
00048         // where did this dwf page originate?
00049         //
00050         DWFSource oSource( L"SimpleEPlotWriter.cpp", "DWFToolkit Samples", L"" );
00051 
00052         //
00053         // we will be adding a graphic resource to this page (namely the W2D)
00054         // this dictates that we define paper for this section
00055         //
00056         double anPaperClip[4] = { 0, 0, 11, 8.5 };
00057         DWFPaper oPaper( 11, 8.5, DWFPaper::eInches, 0x00ffffff, (const double*)anPaperClip );
00058 
00059         DWFEPlotSection* pPage = DWFCORE_ALLOC_OBJECT( DWFEPlotSection(L"Page Title", L"123", 1, oSource, 0x00ff00ff, &oPaper) );
00060 
00061         //
00062         // add some page properties
00063         //
00064 
00065         //
00066         // (1)
00067         //
00068         pPage->addProperty( DWFCORE_ALLOC_OBJECT(DWFProperty(L"creator", L"SimpleEPlotWriter.cpp", L"", L"", L"")), true );
00069 
00070         //
00071         // (2)
00072         //
00073 //        DWFProperty oProperty( L"file version", L"1", L"Source Code", L"", L"" );
00074         //pPage->addProperty( &oProperty, false );
00075 
00076         //oProperty.setName( L"binary" );
00077         //oProperty.setValue( L"debug executable" );
00078         //pPage->addProperty( &oProperty, false );
00079 
00080         /*
00081         DWFProperty oProperty1( L"いくらですか?", L"さんぜん円", L"Source Code", L"", L"" );
00082         DWFProperty oProperty2( L"いくらですか?", L"さんぜん円", L"プロプルチズ", L"", L"" );
00083         DWFProperty oProperty3( L"FOO", L"さんぜん円", L"FOOCAT", L"", L"" );
00084         
00085         pPage->addProperty( &oProperty1, false );
00086         pPage->addProperty( &oProperty2, false );
00087         pPage->addProperty( &oProperty3, false );
00088         */
00089 
00090         pPage->addProperty( DWFCORE_ALLOC_OBJECT(DWFProperty(L"Some guys", L"Dun & Broadstreet", L"", L"", L"")), true );
00091         pPage->addProperty( DWFCORE_ALLOC_OBJECT(DWFProperty(L"Some units", L"3'", L"", L"", L"")), true );
00092 
00093 //                oProperty.setName( L"Vendor" );
00094   //              oProperty.setValue( L"Dun & Broadstreet" );
00095     //            pPage->addProperty( oProperty );
00096 
00097         //
00098         // for this example, let's add a pre-existing w2d
00099         //
00100 
00101         //
00102         // define the resource - this must be allocated on the heap
00103         //
00104         DWFGraphicResource* p2Dgfx = 
00105             DWFCORE_ALLOC_OBJECT( DWFGraphicResource(L"Oceanarium",                       // title
00106                                                      DWFXML::kzRole_Graphics2d,           // role
00107                                                      DWFMIME::kzMIMEType_W2D,             // MIME type
00108                                                      L"Autodesk, Inc.",                   // author
00109                                                      L"Oceanarium Sample",                // description
00110                                                      L"",                                 // creation time
00111                                                      L"") );                              // modification time
00112 
00113         if (p2Dgfx == NULL)
00114         {
00115             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
00116         }
00117 
00118         //
00119         // configure the resource
00120         //
00121         double anTransform[4][4] = { 0.00015625, 0, 0, 0, 0, 0.00015625, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
00122         double anClip[4] = { 0, 0, 11, 8.5 };
00123 
00124         p2Dgfx->configureGraphic( (const double*)anTransform,
00125                                    NULL,
00126                                   (const double*)anClip );
00127 
00128         //
00129         // most importantly - bind a stream to the resource
00130         // in this case, we have a file on disk so we open the
00131         // file with a streaming descriptor.  we will also
00132         // create everything on the heap since the package writer
00133         // will not use the stream immediately, he will need to own
00134         // these resources.
00135         //
00136         DWFFile oW2DFilename( L"ocean_90.w2d" );
00137         DWFStreamFileDescriptor* pW2DFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oW2DFilename, L"rb") );
00138 
00139         if (pW2DFile == NULL)
00140         {
00141             DWFCORE_FREE_OBJECT( p2Dgfx );
00142 
00143             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
00144         }
00145 
00146         DWFFileInputStream* pW2DFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
00147 
00148         if (pW2DFilestream == NULL)
00149         {
00150             DWFCORE_FREE_OBJECT( p2Dgfx );
00151             DWFCORE_FREE_OBJECT( pW2DFile );
00152 
00153             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
00154         }
00155 
00156         //
00157         // open the file and bind it to the stream
00158         //
00159         pW2DFile->open();
00160         pW2DFilestream->attach( pW2DFile, true );
00161 
00162         //
00163         // hand the stream off to the resource
00164         // NOTE: since we don't already know the filesize (in the application space - of course we can look on disk...)
00165         // leave the second parameter (nBytes) default as zero, this will tell the package writer
00166         // to use the number of bytes it processed through the stream as the size attribute in the descriptor.
00167         //
00168         p2Dgfx->setInputStream( pW2DFilestream );
00169 
00170         //
00171         // finally, drop the resource into the page
00172         //
00173         pPage->addResource( p2Dgfx, true );
00174 
00175         //
00176         // now let's add the thumbnail
00177         //
00178 
00179         DWFImageResource* pThumbnail = 
00180             DWFCORE_ALLOC_OBJECT( DWFImageResource( L"Oceanarium Thumbnail",
00181                                                      DWFXML::kzRole_Thumbnail,
00182                                                      DWFMIME::kzMIMEType_PNG) );
00183 
00184         if (pThumbnail == NULL)
00185         {
00186             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
00187         }
00188 
00189         //
00190         // configure the resource
00191         //
00192         double anTransform2[4][4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
00193         double anExtents2[4] = { 0, 0, 220, 170 };
00194 
00195         pThumbnail->configureGraphic( (const double*)anTransform2,
00196                                       (const double*)anExtents2 );
00197 
00198         //
00199         // extras for the image
00200         // here we note the image has 24 bpp (color depth)
00201         //
00202         pThumbnail->configureImage( 24 );
00203 
00204         DWFFile oThumbnailFilename( L"ocean_thumbnail.png" );
00205         DWFStreamFileDescriptor* pThumbnailFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oThumbnailFilename, L"rb") );
00206 
00207         if (pThumbnailFile == NULL)
00208         {
00209             DWFCORE_FREE_OBJECT( pThumbnail );
00210 
00211             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
00212         }
00213 
00214         DWFFileInputStream* pThumbnailFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
00215 
00216         if (pThumbnailFilestream == NULL)
00217         {
00218             DWFCORE_FREE_OBJECT( pThumbnail );
00219             DWFCORE_FREE_OBJECT( pThumbnailFile );
00220 
00221             _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
00222         }
00223 
00224         pThumbnailFile->open();
00225         pThumbnailFilestream->attach( pThumbnailFile, true );
00226 
00227         pThumbnail->setInputStream( pThumbnailFilestream );
00228 
00229         //
00230         // drop the resource into the page
00231         // and be sure to mention that it "belongs" to the W2D 
00232         // (and replace and delete)
00233         //
00234         pPage->addResource( pThumbnail, true, true, true, p2Dgfx );
00235 
00236         //
00237         // object definitions: in this small example,
00238         // let's assume we have a known object node in the w2d
00239         // we will create an object and instance for this node
00240         //
00241         DWFDefinedObject* pObject = DWFCORE_ALLOC_OBJECT( DWFDefinedObject );
00242         DWFDefinedObjectInstance* pInstance = pObject->instance( L"1" );
00243 
00244         //
00245         // add a property directly on the instance
00246         //
00247         pInstance->addProperty( DWFCORE_ALLOC_OBJECT(DWFProperty(L"vendor", L"abc manu", L"vendors")), true );
00248 
00249         //
00250         // create the object definition resource
00251         //
00252         DWFObjectDefinitionResource* pObjDefRes = 
00253             DWFCORE_ALLOC_OBJECT( DWFObjectDefinitionResource(DWFXML::kzElement_PageObjectDefinition,DWFXML::kzRole_ObjectDefinition) );
00254 
00255         //
00256         // add the object
00257         //
00258         pObjDefRes->addObject( pObject );
00259 
00260         //
00261         // add the instance
00262         //
00263         pObjDefRes->addInstance( pInstance );
00264 
00265         //
00266         // add the resource (as a child of the w2d)
00267         //
00268         pPage->addResource( pObjDefRes, true, true, true, p2Dgfx );
00269 
00272 
00273         DWFFile oDWF( argv[1] );
00274         DWF6PackageWriter oWriter( oDWF );
00275 
00276         //
00277         // add some package-wide properties
00278         //
00279         DWFPropertyContainer& rManifestProperties = oWriter.getManifestProperties();
00280         rManifestProperties.addProperty( DWFCORE_ALLOC_OBJECT(DWFProperty(L"Package Property Name", L"Package Property Value", L"Package Property Category", L"", L"")), true );
00281 
00282         oWriter.addSection( pPage );
00283         oWriter.write( L"Autodesk", L"SimpleEPlotWriter.cpp", L"", L"Autodesk", _DWFTK_VERSION_STRING );
00284 
00285         wcout << L"OK\n";
00286     }
00287     catch (DWFException& ex)
00288     {
00289         wcout << ex.type() << L": " << ex.message() << endl;
00290         wcout << L"(function) " << ex.function() << endl;
00291         wcout << L"(file) " << ex.file() << endl;
00292         wcout << L"(line) " << ex.line() << endl;
00293     }
00294 
00295         return 0;
00296 }
00297 
00298 

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