// // Copyright (c) 2007 by Autodesk, Inc. // // Read2DDWFxFile.cpp // The information contained herein is confidential, proprietary // to Autodesk, Inc., and considered a trade secret as defined in // section 499C of the penal code of the State of California. // Use of this information by anyone other than authorized employees // of Autodesk, Inc. is granted only under a written non-disclosure // agreement, expressly prescribing the scope and manner of such use. #include #include using namespace std; #include "dwfcore/File.h" #include "dwfcore/String.h" using namespace DWFCore; #include "dwf/whiptk/whipcore.h" #include "dwf/whiptk/whiperrs.h" #include "dwf/package/Constants.h" #include "dwf/package/Manifest.h" #include "dwf/package/GlobalSection.h" #include "dwf/package/ContentManager.h" #include "dwf/package/reader/PackageReader.h" #include "dwf/dwfx/Constants.h" using namespace DWFToolkit; #include "dwf/xaml/XamlFile.h" #include "dwf/xaml/XamlObjectFactory.h" //const wchar_t* const DWFXXML::kzRelationship_Graphics2dExtensionResource = L"http://schemas.autodesk.com/dwfx/2007/relationships/graphics2dextensionresource"; #ifdef _DWFCORE_WIN32_SYSTEM #include #endif class Access : public WT_OpcResourceMaterializer , public WT_OpcResourceSerializer { private: // // The default initial buffer size for buffer output streams wrapping // file output streams. // static const size_t _DEF_BUFFER_SIZE; DWFToolkit::DWFResourceContainer& _rResources; DWFToolkit::DWFResource& _rFixedPage; public: Access( DWFToolkit::DWFResource& rRes, DWFToolkit::DWFResourceContainer& rCont) : _rResources( rCont ) , _rFixedPage( rRes ) {;} virtual ~Access() {;} // // WT_OpcResourceMaterializer // WT_Result getPartInputStream( const DWFCore::DWFString& rzPath, DWFCore::DWFInputStream** ppStream); // // WT_OpcResourceSerializer // WT_Result getPartOutputStream( const DWFCore::DWFString& rzMimeType, DWFCore::DWFString& rzPath, DWFCore::DWFOutputStream** ppStream); private: Access& operator =(Access&){}; }; ///////////////////////////////////////////////////////////////////////// // // main // int main(int argc, char* argv[]) { #ifdef _DWFCORE_WIN32_SYSTEM // // Enable memory leak reporting in Debug mode under Win32. // int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // Turn on leak-checking bit tmpFlag |= _CRTDBG_LEAK_CHECK_DF; // Turn off CRT block checking bit tmpFlag &= ~_CRTDBG_CHECK_CRT_DF; // Set flag to the new value _CrtSetDbgFlag( tmpFlag ); // For mem leak debugging... /* long foo = 8217; _CrtSetBreakAlloc(foo); */ #endif if (argc < 2) { wcout << L"Usage:" << argv[0] << L" file.dwfx [-q]" << endl; wcout << L"-q is used to get into interactive mode to query specific objects" << endl; return ( 0 ); } try { wcout << L"Reading package: " << argv[1] << endl; DWFFile oDWF( argv[1] ); // argv[1] records a dwfx file DWFPackageReader oReader( oDWF ); DWFPackageReader::tPackageInfo tInfo; oReader.getPackageInfo( tInfo ); /////////////////// // // Read manifest - this will create/read the manifest, and also create the content manager // DWFManifest& rManifest = oReader.getManifest(); // DWFContentManager* pContentManager = rManifest.getContentManager(); // DWFContent* pContent = pContentManager->getContent(); WT_XAML_Class_Factory XAMLFactory; DWFSection* pSection = NULL; DWFManifest::SectionIterator* piSections = rManifest.getSections(); if (piSections) { for (; piSections->valid(); piSections->next()) { pSection = piSections->get(); pSection->readDescriptor(); // // This is NOT creating NEW contents - it loading one that was created during the manifest // loading process. Do not delete the content pointer here. In most cases there will be // only one content. // DWFResourceContainer::ResourceKVIterator* piResource = pSection->getResourcesByRole(); if (piResource) { for (; piResource->valid(); piResource->next()) { DWFResource* pSource = piResource->value(); if (pSource->role() == L"2d streaming graphics") { WT_File* pFile = XAMLFactory.Create_File(); WT_XAML_File* pXAMLFile = static_cast(pFile); DWFToolkit::DWFResource* pExtRes( NULL ); DWFCore::DWFPointer< DWFToolkit::DWFResourceRelationship::tList::ConstIterator > pRelIt( (*pSource).getRelationships(), false ); for ( ; !pRelIt.isNull() && pRelIt->valid(); pRelIt->next() ) { if ( pRelIt->get()->type() == DWFToolkit::DWFXXML::kzRelationship_Graphics2dExtensionResource ) { pExtRes = pSection->findResourceByObjectID( pRelIt->get()->resourceID() ); break; } } if ( pExtRes ) { pXAMLFile->w2xStreamIn() = pExtRes->getInputStream(); } DWFInputStream* pTempInputStream = pSource->getInputStream(); DWFBufferInputStream* pInputStream = DWFCORE_ALLOC_OBJECT( DWFCore::DWFBufferInputStream( pTempInputStream, false ) ); pXAMLFile->xamlStreamIn() = pInputStream; Access *pAccess = DWFCORE_ALLOC_OBJECT( Access(*pSource, *pSection) ); pXAMLFile->opcResourceMaterializer() = pAccess; pXAMLFile->set_file_mode( WT_File::File_Read ); pXAMLFile->open(); FILE* fp = fopen("./coordinates.txt","a"); WT_Result result; while((result = pXAMLFile->process_next_object()) == WT_Result::Success) { const WT_Object * obj = pXAMLFile->current_object(); obj; // here we can get each objects in the dwfx if(obj->object_id() == WT_Object::Text_ID) { const WT_Text * text_obj = dynamic_cast(obj); const WT_Logical_Point & pt = text_obj->position(); char buff[256]; sprintf(buff, "Text: start point %d, %d\n", pt.m_x, pt.m_y); ::fwrite( buff, sizeof(char), strlen(buff), fp ); } else if (obj->object_id() == WT_Object::Polyline_ID) { const WT_Polyline * poly_obj = dynamic_cast(obj); WT_Logical_Point* pts = poly_obj->points(); char buff[256]; sprintf(buff, "Polyline: the first point %d, %d\n", pts[0].m_x, pts[0].m_y); ::fwrite( buff, sizeof(char), strlen(buff), fp ); } } if(fp) { fclose(fp); } DWFCORE_FREE_OBJECT(pAccess); pXAMLFile->close(); XAMLFactory.Destroy(pXAMLFile); DWFCORE_FREE_OBJECT(pTempInputStream); } } } DWFCORE_FREE_OBJECT(piResource); } DWFCORE_FREE_OBJECT( piSections ); } //// wcout << L"\nDONE\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; } // // WT_OpcResourceMaterializer // WT_Result Access::getPartInputStream( const DWFCore::DWFString& rzPath, DWFCore::DWFInputStream** ppStream) { if ( ppStream == NULL ) return WT_Result::Toolkit_Usage_Error; if ( (rzPath.substring(0, 2) != /* NOXLATE */ L"./") && (rzPath.findFirst(/* NOXLATE */L'/') != -1) ) { // Can't handle non-relative or non-local paths. // i.e.) path must be relative and local // Is this the right error? return WT_Result::Unsupported_DWF_Extension_Error; } DWFCore::DWFString zSearch( (rzPath.substring(0, 2) == /* NOXLATE */ "./") ? rzPath.substring(2) : rzPath ); DWFToolkit::DWFResource *pRes( _rResources.findResourceByHREF( zSearch ) ); WT_Result res( WT_Result::File_Inconsistency_Warning ); if (!pRes ) { // // Try again. // DWFCore::DWFPointer< DWFToolkit::DWFResourceContainer::ResourceKVIterator > pIt( _rResources.getResourcesByHREF(), false ); if ( !pIt.isNull() && pIt->valid() ) { // // Use the key from the iterator to infer the full-path href. // DWFCore::DWFString zKey( pIt->key() ); off_t pos( zKey.findLast( /*NOXLATE*/ L'/' ) ); if ( pos >= 0 ) { DWFCore::DWFString zFullPath( zKey.substring( 0, pos + 1 ) ); // // Append the local part taking in to consideration that it might // start with the "current directory" specifier. // zFullPath.append( zSearch ); pRes = _rResources.findResourceByHREF( zFullPath ); } } } if ( pRes ) { try { *ppStream = pRes->getInputStream(); res = WT_Result::Success; } catch ( DWFException& ) { res = WT_Result::Internal_Error; } } return res; } WT_Result Access::getPartOutputStream( const DWFCore::DWFString& rzMimeType, DWFCore::DWFString& rzPath, DWFCore::DWFOutputStream** ppStream) { rzMimeType; rzPath; ppStream; WT_Result res( WT_Result::Success ); return res; }