From 0bdb375804be69126522bf8e52763755a052b770 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 24 Nov 2017 18:59:37 +0100 Subject: [PATCH] Add missing file export into archive. --- code/3DSExporter.cpp | 1 - code/3MFXmlTags.h | 4 +- code/D3MFExporter.cpp | 94 ++++++++++++++++++++++++++++++++--------- code/D3MFExporter.h | 14 +++--- code/D3MFOpcPackage.cpp | 6 --- code/D3MFOpcPackage.h | 6 +++ 6 files changed, 92 insertions(+), 33 deletions(-) diff --git a/code/3DSExporter.cpp b/code/3DSExporter.cpp index 91c241ef7..2f561fcd6 100644 --- a/code/3DSExporter.cpp +++ b/code/3DSExporter.cpp @@ -39,7 +39,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ - #ifndef ASSIMP_BUILD_NO_EXPORT #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER diff --git a/code/3MFXmlTags.h b/code/3MFXmlTags.h index 9ee17c7e4..7e47422f1 100644 --- a/code/3MFXmlTags.h +++ b/code/3MFXmlTags.h @@ -85,5 +85,5 @@ namespace XmlTag { } -} -} +} // Namespace D3MF +} // Namespace Assimp diff --git a/code/D3MFExporter.cpp b/code/D3MFExporter.cpp index 4d3d45f8c..189985b90 100644 --- a/code/D3MFExporter.cpp +++ b/code/D3MFExporter.cpp @@ -39,25 +39,21 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ #include "D3MFExporter.h" + #include #include #include #include - #include #include "Exceptional.h" #include "3MFXmlTags.h" +#include "D3MFOpcPackage.h" namespace Assimp { void ExportScene3MF( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* /*pProperties*/ ) { - std::shared_ptr outfile( pIOSystem->Open( pFile, "wb" ) ); - if ( !outfile ) { - throw DeadlyExportError( "Could not open output .3ds file: " + std::string( pFile ) ); - } - - D3MF::D3MFExporter myExporter( outfile, pIOSystem, pScene ); + D3MF::D3MFExporter myExporter( pFile, pIOSystem, pScene ); if ( myExporter.validate() ) { bool ok = myExporter.exportArchive(pFile); } @@ -67,16 +63,20 @@ namespace D3MF { #ifndef ASSIMP_BUILD_NO3MF_EXPORTER -D3MFExporter::D3MFExporter( std::shared_ptr outfile, IOSystem* pIOSystem, const aiScene* pScene ) +D3MFExporter::D3MFExporter( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene ) : mIOSystem( pIOSystem ) -, mStream( outfile.get() ) +, mArchiveName( pFile ) , mScene( pScene ) -, mBuildItems() { +, mBuildItems() +, mRelations() { // empty } D3MFExporter::~D3MFExporter() { - // empty + for ( size_t i = 0; i < mRelations.size(); ++i ) { + delete mRelations[ i ]; + } + mRelations.clear(); } bool D3MFExporter::createFileStructure( const char *file ) { @@ -100,7 +100,7 @@ bool D3MFExporter::createFileStructure( const char *file ) { } bool D3MFExporter::validate() { - if ( nullptr == mStream ) { + if ( mArchiveName.empty() ) { return false; } @@ -117,6 +117,9 @@ bool D3MFExporter::exportArchive( const char *file ) { ok |= exportRelations(); ok |= export3DModel(); + if ( ok ) { + createZipArchiveFromeFileStructure(); + } return ok; } @@ -124,9 +127,16 @@ bool D3MFExporter::exportRelations() { mOutput.clear(); mOutput << "\n"; - //mOutput + mOutput << "\n"; - writeRelInfoToFile(); + for ( size_t i = 0; i < mRelations.size(); ++i ) { + mOutput << "target << " "; + mOutput << "id=\"" << mRelations[i]->id << " "; + mOutput << "Type=\"" << mRelations[ i ]->type << "/>\n"; + } + mOutput << "\n"; + + writeRelInfoToFile( "_rels", ".rels" ); return true; } @@ -142,14 +152,21 @@ bool D3MFExporter::export3DModel() { writeObjects(); - writeModelToArchive(); mOutput << "\n"; writeBuild(); mOutput << "\n"; - std::string exportedFile = mOutput.str(); + OpcPackageRelationship *info = new OpcPackageRelationship; + info->id = mArchiveName; + info->target = "rel0"; + info->type = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"; + mRelations.push_back( info ); + + writeModelToArchive( "3D", mArchiveName ); + + mOutput.clear(); return true; } @@ -229,10 +246,49 @@ void D3MFExporter::writeBuild() { mOutput << "\n"; } -bool writeModelToArchive(); -bool writeRelInfoToFile(); +void D3MFExporter::writeModelToArchive( const std::string &folder, const std::string &modelName ) { + const std::string &oldFolder( mIOSystem->CurrentDirectory() ); + if ( folder != oldFolder ) { + mIOSystem->PushDirectory( oldFolder ); + mIOSystem->ChangeDirectory( folder ); + + const std::string &exportTxt( mOutput.str() ); + std::shared_ptr outfile( mIOSystem->Open( modelName, "wb" ) ); + if ( !outfile ) { + throw DeadlyExportError( "Could not open output model file: " + std::string( modelName ) ); + } + + const size_t writtenBytes( outfile->Write( exportTxt.c_str(), sizeof( char ), exportTxt.size() ) ); + + mIOSystem->ChangeDirectory( ".." ); + mIOSystem->PopDirectory(); + } +} + +void D3MFExporter::writeRelInfoToFile( const std::string &folder, const std::string &relName ) { + const std::string &oldFolder( mIOSystem->CurrentDirectory() ); + if ( folder != oldFolder ) { + mIOSystem->PushDirectory( oldFolder ); + mIOSystem->ChangeDirectory( folder ); + + const std::string &exportTxt( mOutput.str() ); + std::shared_ptr outfile( mIOSystem->Open( relName, "wb" ) ); + if ( !outfile ) { + throw DeadlyExportError( "Could not open output model file: " + std::string( relName ) ); + } + + const size_t writtenBytes( outfile->Write( exportTxt.c_str(), sizeof( char ), exportTxt.size() ) ); + + mIOSystem->ChangeDirectory( ".." ); + mIOSystem->PopDirectory(); + } +} + +void D3MFExporter::createZipArchiveFromeFileStructure() { + +} #endif // ASSIMP_BUILD_NO3MF_EXPORTER -} +} // Namespace D3MF } // Namespace Assimp diff --git a/code/D3MFExporter.h b/code/D3MFExporter.h index eacf91069..2fd647a4c 100644 --- a/code/D3MFExporter.h +++ b/code/D3MFExporter.h @@ -59,9 +59,11 @@ namespace D3MF { #ifndef ASSIMP_BUILD_NO3MF_EXPORTER +struct OpcPackageRelationship; + class D3MFExporter { public: - D3MFExporter( std::shared_ptr outfile, IOSystem* pIOSystem, const aiScene* pScene ); + D3MFExporter( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene ); ~D3MFExporter(); bool validate(); bool createFileStructure( const char *file ); @@ -76,19 +78,21 @@ protected: void writeVertex( const aiVector3D &pos ); void writeFaces( aiMesh *mesh ); void writeBuild(); - bool writeModelToArchive(); - bool writeRelInfoToFile(); + void writeModelToArchive( const std::string &folder, const std::string &modelName ); + void writeRelInfoToFile( const std::string &folder, const std::string &relName ); + void createZipArchiveFromeFileStructure(); private: IOSystem *mIOSystem; - IOStream *mStream; + std::string mArchiveName; const aiScene *mScene; std::ostringstream mOutput; std::vector mBuildItems; + std::vector mRelations; }; #endif // ASSIMP_BUILD_NO3MF_EXPORTER -} +} // Namespace D3MF } // Namespace Assimp diff --git a/code/D3MFOpcPackage.cpp b/code/D3MFOpcPackage.cpp index ed792461b..1db8cab13 100644 --- a/code/D3MFOpcPackage.cpp +++ b/code/D3MFOpcPackage.cpp @@ -381,12 +381,6 @@ bool D3MFZipArchive::mapArchive() { // ------------------------------------------------------------------------------------------------ -struct OpcPackageRelationship { - std::string id; - std::string type; - std::string target; -}; - typedef std::shared_ptr OpcPackageRelationshipPtr; class OpcPackageRelationshipReader { diff --git a/code/D3MFOpcPackage.h b/code/D3MFOpcPackage.h index 40ab2e462..818415e9f 100644 --- a/code/D3MFOpcPackage.h +++ b/code/D3MFOpcPackage.h @@ -53,6 +53,12 @@ namespace D3MF { typedef irr::io::IrrXMLReader XmlReader; typedef std::shared_ptr XmlReaderPtr; +struct OpcPackageRelationship { + std::string id; + std::string type; + std::string target; +}; + class D3MFZipArchive; class D3MFOpcPackage {