From 0bd3e5d4abce1203c1f327e99120eca141ebc1ce Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Sat, 19 Dec 2015 23:58:17 +1300 Subject: [PATCH] Added AMF compression. Works with Cura, not Slic3r --- src/Mod/Mesh/App/Exporter.cpp | 23 ++++++++++++++++++++--- src/Mod/Mesh/App/Exporter.h | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Mod/Mesh/App/Exporter.cpp b/src/Mod/Mesh/App/Exporter.cpp index 7be80fc60..c715498fc 100644 --- a/src/Mod/Mesh/App/Exporter.cpp +++ b/src/Mod/Mesh/App/Exporter.cpp @@ -37,6 +37,8 @@ #include "Base/Stream.h" #include "Base/Tools.h" +#include + using namespace Mesh; using namespace MeshCore; @@ -143,16 +145,31 @@ bool MergeExporter::addShape(App::Property *shape, float tol) return false; } -AmfExporter::AmfExporter(std::string fileName) : +AmfExporter::AmfExporter(std::string fileName, bool compress) : outputStreamPtr(nullptr), nextObjectIndex(0) { // ask for write permission Base::FileInfo fi(fileName.c_str()); Base::FileInfo di(fi.dirPath().c_str()); - if ((fi.exists() && !fi.isWritable()) || !di.exists() || !di.isWritable()) + if ((fi.exists() && !fi.isWritable()) || !di.exists() || !di.isWritable()) { throw Base::FileException("No write permission for file", fileName); + } + + if (compress) { + auto *zipStreamPtr( new zipios::ZipOutputStream(fi.filePath()) ); + + // ISO 52915 specifies that compressed AMF files are zip-compressed and + // must contain the AMF XML in an entry with the same name as the + // compressed file. It's OK to have other files in the zip too. + zipStreamPtr->putNextEntry( zipios::ZipCDirEntry(fi.fileName()) ); + + // Default compression seems to work fine. + outputStreamPtr = zipStreamPtr; + + } else { + outputStreamPtr = new Base::ofstream(fi, std::ios::out | std::ios::binary); + } - outputStreamPtr = new Base::ofstream(fi, std::ios::out | std::ios::binary); if (outputStreamPtr) { *outputStreamPtr << "\n" << "\n"; diff --git a/src/Mod/Mesh/App/Exporter.h b/src/Mod/Mesh/App/Exporter.h index 2d7eef843..d1c7a2ca6 100644 --- a/src/Mod/Mesh/App/Exporter.h +++ b/src/Mod/Mesh/App/Exporter.h @@ -80,7 +80,7 @@ class AmfExporter : public Exporter { public: /// Writes AMF header - AmfExporter(std::string fileName); + AmfExporter(std::string fileName, bool compress = true); /// Writes AMF footer ~AmfExporter();