3MF: add export to a given archive.

This commit is contained in:
Kim Kulling
2017-11-27 21:48:33 +01:00
parent 6c59c83e0f
commit 3dfca3bc84
15 changed files with 6152 additions and 60 deletions

View File

@@ -743,6 +743,14 @@ SET( unzip_SRCS
)
SOURCE_GROUP( unzip FILES ${unzip_SRCS})
SET( ziplib_SRCS
../contrib/zip/src/miniz.h
../contrib/zip/src/zip.c
../contrib/zip/src/zip.h
)
SOURCE_GROUP( ziplib FILES ${ziplib_SRCS} )
SET ( openddl_parser_SRCS
../contrib/openddlparser/code/OpenDDLParser.cpp
../contrib/openddlparser/code/DDLNode.cpp
@@ -854,6 +862,7 @@ SET( assimp_src
${Clipper_SRCS}
${openddl_parser_SRCS}
${open3dgc_SRCS}
${ziplib_SRCS}
# Necessary to show the headers in the project when using the VC++ generator:
${PUBLIC_HEADERS}

View File

@@ -50,12 +50,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "3MFXmlTags.h"
#include "D3MFOpcPackage.h"
#include <contrib/zip/src/zip.h>
namespace Assimp {
void ExportScene3MF( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* /*pProperties*/ ) {
D3MF::D3MFExporter myExporter( pFile, pIOSystem, pScene );
if ( myExporter.validate() ) {
bool ok = myExporter.exportArchive(pFile);
if ( !ok ) {
throw DeadlyExportError( "Could not export 3MP archive: " + std::string( pFile ) );
}
}
}
@@ -66,6 +71,7 @@ namespace D3MF {
D3MFExporter::D3MFExporter( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene )
: mIOSystem( pIOSystem )
, mArchiveName( pFile )
, m_zipArchive( nullptr )
, mScene( pScene )
, mBuildItems()
, mRelations() {
@@ -79,26 +85,6 @@ D3MFExporter::~D3MFExporter() {
mRelations.clear();
}
bool D3MFExporter::createFileStructure( const char *file ) {
if ( !mIOSystem->CreateDirectory( file ) ) {
return false;
}
if ( !mIOSystem->ChangeDirectory( file ) ) {
return false;
}
if ( !mIOSystem->CreateDirectory( "3D" ) ) {
return false;
}
if ( !mIOSystem->CreateDirectory( "_rels" ) ) {
return false;
}
return true;
}
bool D3MFExporter::validate() {
if ( mArchiveName.empty() ) {
return false;
@@ -113,13 +99,14 @@ bool D3MFExporter::validate() {
bool D3MFExporter::exportArchive( const char *file ) {
bool ok( true );
ok |= createFileStructure( file );
m_zipArchive = zip_open( file, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w' );
if ( nullptr == m_zipArchive ) {
return false;
}
ok |= exportRelations();
ok |= export3DModel();
if ( ok ) {
createZipArchiveFromeFileStructure();
}
return ok;
}
@@ -247,45 +234,21 @@ void D3MFExporter::writeBuild() {
}
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 entry = folder + "/" + mArchiveName;
zip_entry_open( m_zipArchive, entry.c_str() );
const std::string &exportTxt( mOutput.str() );
std::shared_ptr<IOStream> outfile( mIOSystem->Open( modelName, "wb" ) );
if ( !outfile ) {
throw DeadlyExportError( "Could not open output model file: " + std::string( modelName ) );
}
const std::string &exportTxt( mOutput.str() );
zip_entry_write( m_zipArchive, exportTxt.c_str(), exportTxt.size() );
const size_t writtenBytes( outfile->Write( exportTxt.c_str(), sizeof( char ), exportTxt.size() ) );
mIOSystem->ChangeDirectory( ".." );
mIOSystem->PopDirectory();
}
zip_entry_close( m_zipArchive );
}
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<IOStream> 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() {
const std::string entry = folder + "/" + "_rels";
zip_entry_open( m_zipArchive, entry.c_str() );
const std::string &exportTxt( mOutput.str() );
zip_entry_write( m_zipArchive, exportTxt.c_str(), exportTxt.size() );
zip_entry_close( m_zipArchive );
}
#endif // ASSIMP_BUILD_NO3MF_EXPORTER

View File

@@ -50,6 +50,8 @@ struct aiNode;
struct aiMaterial;
struct aiMesh;
struct zip_t;
namespace Assimp {
class IOStream;
@@ -66,7 +68,6 @@ public:
D3MFExporter( const char* pFile, IOSystem* pIOSystem, const aiScene* pScene );
~D3MFExporter();
bool validate();
bool createFileStructure( const char *file );
bool exportArchive( const char *file );
bool exportRelations();
bool export3DModel();
@@ -80,11 +81,12 @@ protected:
void writeBuild();
void writeModelToArchive( const std::string &folder, const std::string &modelName );
void writeRelInfoToFile( const std::string &folder, const std::string &relName );
void createZipArchiveFromeFileStructure();
void createZipArchiveFromeFileStructure( const char* pFile );
private:
IOSystem *mIOSystem;
std::string mArchiveName;
zip_t *m_zipArchive;
const aiScene *mScene;
std::ostringstream mOutput;
std::vector<unsigned int> mBuildItems;