// // Copyright (c) 2006 by Autodesk, Inc. // // By using this code, you are agreeing to the terms and conditions of // the License Agreement included in the documentation for this code. // // AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED, // AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE // WORKS WHICH INCORPORATE IT. // // AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS // AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING // CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS, // OMISSIONS, AND OTHER PROBLEMS IN THE CODE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer Software // Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical // Data and Computer Software), as applicable. // #include "stdafx.h" //#include #include #include "dwf/package/EModelSection.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" using namespace std; using namespace DWFCore; using namespace DWFToolkit; #ifdef _DWFCORE_WIN32_SYSTEM /* #if _USE_VLD #define _USE_VLD_FOR_MEMORY_LEAK_TEST #include #else #define _USE_CRTDBG_FOR_MEMORY_LEAK_TEST #include #endif*/ #endif std::wostream& operator<<( std::wostream& wos, const DWFString& zString ) { if (zString.chars()) { wos << (const wchar_t*)zString; } return wos; } class COpenSgmtHndlr : public TK_Open_Segment { public: COpenSgmtHndlr( ) : TK_Open_Segment(){;} virtual ~COpenSgmtHndlr(){;} TK_Status Execute( BStreamFileToolkit& pTk ) { TK_Status status = TK_Open_Segment::Execute(pTk); if(status == TK_Normal ) { char* segString = this->GetSegment(); //fprintf(pFile,"Got Segment: %s", segString); if(segString != NULL && segString[0] != '\0') cout<<"OpSeg: " << segString << endl; } return status; } }; class CORefSgmtHndlr : public TK_Referenced_Segment { public: CORefSgmtHndlr(unsigned char opcode) : TK_Referenced_Segment(opcode){;} virtual ~CORefSgmtHndlr(){;} TK_Status Execute( BStreamFileToolkit& pTk ) { TK_Status status = TK_Referenced_Segment::Execute(pTk); if(status == TK_Normal ) { char* segString = this->GetSegment(); //fprintf(pFile,"Got Segment: %s", segString); if(segString != NULL && segString[0] != '\0') { cout<<"RefSeg: " << segString << endl; } } return status; } }; class COSellSgmtHndlr : public TK_Shell { public: COSellSgmtHndlr() : TK_Shell(){;} virtual ~COSellSgmtHndlr(){;} TK_Status Execute( BStreamFileToolkit& pTk ) { TK_Status status = TK_Shell::Execute(pTk); if(status == TK_Normal ) { // cout<<"TK_Shell \t" ; } return status; } }; void getObjDefInstances( DWFObjectDefinition* pDef, DWFDefinedObjectInstance* pInst ) { wcout << L"Object [" << pInst->object() << L"] Node [" << pInst->node()<< L"]\n" ; DWFProperty* pProp = NULL; DWFPropertyContainer* pInstProps = pDef->getInstanceProperties( *pInst ); DWFProperty::tMap::Iterator* piProp = pInstProps->getProperties(); if (piProp) { for (;piProp->valid(); piProp->next()) { pProp = piProp->value(); wcout<< pProp->name() << L", " << pProp->value() << L" [" << pProp->category() << L"]\n" ; } DWFCORE_FREE_OBJECT( piProp ); } DWFCORE_FREE_OBJECT( pInstProps ); DWFDefinedObjectInstance::tMap::Iterator* piChildren = pInst->resolvedChildren(); if (piChildren) { for (;piChildren->valid(); piChildren->next()) { //zOut.append( do_children( pDef, piChildren->value() ) ); getObjDefInstances( pDef, piChildren->value()); } DWFCORE_FREE_OBJECT( piChildren ); } } int main(int argc, char* argv[]) { #if 0 //defined(_DEBUG) && defined(_USE_CRTDBG_FOR_MEMORY_LEAK_TEST) // // 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... Please do not delete. long foo = 0; _CrtSetBreakAlloc(foo); #endif if (argc < 2) { wcout << L"Usage:" << argv[0] << L" file.dwf" << endl; return ( 0 ); } try { DWFFile oDWF( argv[1] ); DWFPackageReader oReader( oDWF ); DWFPackageReader::tPackageInfo tInfo; oReader.getPackageInfo( tInfo ); if (tInfo.eType != DWFPackageReader::eDWFPackage) { cout << "File is not a DWF package "; if (tInfo.eType == DWFPackageReader::eW2DStream) { cout << "[W2D Stream]"; } else if (tInfo.eType == DWFPackageReader::eDWFStream) { cout << "[DWF Stream (<6.0)]"; } else if (tInfo.eType == DWFPackageReader::eZIPFile) { cout << "[ZIP Archive]"; } else { cout << "[Unknown]"; } cout << endl; exit( 0 ); } cout << "DWF Package version [" << (float)(tInfo.nVersion)/100.0f << "]" << endl; DWFManifest& rManifest = oReader.getManifest(); // // create a text file // BStreamFileToolkit oW3DStreamParser; oW3DStreamParser.SetOpcodeHandler( TKE_Open_Segment, new COpenSgmtHndlr); oW3DStreamParser.SetOpcodeHandler( TKE_Include_Segment, new CORefSgmtHndlr(TKE_Include_Segment)); oW3DStreamParser.SetOpcodeHandler( TKE_Shell, new COSellSgmtHndlr); // // create a stream to write to the files // // // first, attach the descriptor for the text file to the stream // and write the text buffer to the file // /* DWFContentManager* pContentManager = rManifest.getContentManager(); DWFContent* pContent = pContentManager->getContent(); if(pContent == NULL) { cout << failed to get content } pContent->load();*/ /*DWFString zOut; getGlobalContentInfo( pContent, zOut ); wcout << (const wchar_t*) zOut << endl;*/ DWFSection* pSection = NULL; DWFManifest::SectionIterator* piGlobSections = rManifest.getGlobalSections(); if (piGlobSections) { for (; piGlobSections->valid(); piGlobSections->next()) { pSection = piGlobSections->get(); pSection->readDescriptor(); DWFResourceContainer::ResourceIterator* piObjDefs = pSection->findResourcesByRole( DWFXML::kzRole_ObjectDefinition ); //pSection->getObjectDefinition(); if ((piObjDefs != NULL) && piObjDefs->valid()) { DWFObjectDefinition* pDef = pSection->getObjectDefinition(); if (pDef) { DWFDefinedObjectInstance::tList oRootInstances; pDef->getRootInstances( oRootInstances ); DWFDefinedObjectInstance* pInst = NULL; DWFDefinedObjectInstance::tList::const_iterator iInst = oRootInstances.begin(); for (; iInst != oRootInstances.end(); iInst++) { pInst = *iInst; getObjDefInstances( pDef, pInst ); } } } DWFCORE_FREE_OBJECT( piObjDefs ); } } DWFCORE_FREE_OBJECT( piGlobSections ); pSection = NULL; DWFManifest::SectionIterator* piSections = rManifest.getSections(); if (piSections) { for (; piSections->valid(); piSections->next()) { pSection = piSections->get(); pSection->readDescriptor(); DWFResourceContainer::ResourceIterator* piObjDefs = pSection->findResourcesByRole( DWFXML::kzRole_MarkupObjectDefinition ); //DWFResourceContainer::ResourceIterator* piObjDefs = pSection->findResourcesByRole(kzRole_ObjectDefinition); if ((piObjDefs != NULL) && piObjDefs->valid()) { DWFObjectDefinition* pDef = pSection->getObjectDefinition(); if (pDef) { DWFString zOut; for(piObjDefs->reset(); piObjDefs->valid(); piObjDefs->next()) { //DWFObjectDefinition* pDef = dynamic_cast(piObjDefs->get()) ; DWFDefinedObjectInstance::tList oRootInstances; pDef->getRootInstances( oRootInstances ); DWFDefinedObjectInstance* pInst = NULL; DWFDefinedObjectInstance::tList::const_iterator iInst = oRootInstances.begin(); for (; iInst != oRootInstances.end(); iInst++) { pInst = *iInst; getObjDefInstances( pDef, pInst ); } DWFCORE_FREE_OBJECT( pDef ); } } DWFCORE_FREE_OBJECT( piObjDefs ); } piObjDefs = pSection->findResourcesByRole(DWFXML::kzRole_ObjectDefinition); if ((piObjDefs != NULL) && piObjDefs->valid()) { DWFObjectDefinition* pDef = pSection->getObjectDefinition(); if (pDef) { DWFString zOut; for(piObjDefs->reset(); piObjDefs->valid(); piObjDefs->next()) { //DWFObjectDefinition* pDef = dynamic_cast(piObjDefs->get()) ; DWFDefinedObjectInstance::tList oRootInstances; pDef->getRootInstances( oRootInstances ); DWFDefinedObjectInstance* pInst = NULL; DWFDefinedObjectInstance::tList::const_iterator iInst = oRootInstances.begin(); for (; iInst != oRootInstances.end(); iInst++) { pInst = *iInst; getObjDefInstances( pDef, pInst ); } DWFCORE_FREE_OBJECT( pDef ); } } DWFCORE_FREE_OBJECT( piObjDefs ); } } DWFCORE_FREE_OBJECT( piSections ); } DWFToolkit::DWFManifest::SectionIterator* selectItr = rManifest.findSectionsByType(_DWF_FORMAT_EMODEL_TYPE_WIDE_STRING); if ((selectItr == NULL) || (selectItr->valid() == false)) { if (selectItr) { DWFCORE_FREE_OBJECT( selectItr ); } cout<<"Malformed or unexpected 3D DWF, cannot continue without an EModel section\n"; return -1; } for(selectItr->reset();selectItr->valid(); selectItr->next()) { DWFToolkit::DWFEModelSection* pSection = dynamic_cast(selectItr->get()); if(pSection == NULL) { cout<<"type miss match, not a EMODEL Section type\n"; DWFCORE_FREE_OBJECT( selectItr ); exit(0); } // pSection->readDescriptor(); DWFToolkit::DWFResourceContainer::ResourceIterator* piResource = pSection->findResourcesByRole(DWFXML::kzRole_Graphics3d ); //DWFToolkit::DWFResourceContainer::ResourceIterator* piResource = pSection->findResourcesByRole( DWFXML::kzRole_ObjectDefinition ); if(piResource != NULL && piResource->valid()) { DWFToolkit::DWFGraphicResource* pW3D = static_cast(piResource->get()); if(pW3D != NULL) { DWFCore::DWFInputStream* pW3DStream = pW3D->getInputStream(); oW3DStreamParser.OpenStream( *pW3DStream ); size_t nBytesRead = 0; char aBuffer[16384] = {0}; while (pW3DStream->available() > 0) { nBytesRead = pW3DStream->read( aBuffer, 16384 ); if (oW3DStreamParser.ParseBuffer(aBuffer, (int)nBytesRead, TK_Normal) == TK_Error) { cout<<"Error occured parsing buffer\n"; break; } } oW3DStreamParser.CloseStream(); DWFCORE_FREE_OBJECT( pW3DStream ); } DWFCORE_FREE_OBJECT(piResource); } } /* char dummy[10]={0}; wcout << L"Press any key to purge string table...\n"; ::scanf( dummy ); DWFStringTable::Purge(); wcout << L"Press any key to continue...\n"; ::scanf( dummy ); wcout << L"OK\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; }