diff --git a/src/Mod/Mesh/App/AppMeshPy.cpp b/src/Mod/Mesh/App/AppMeshPy.cpp index f2e272a6de..f4c0059e9a 100644 --- a/src/Mod/Mesh/App/AppMeshPy.cpp +++ b/src/Mod/Mesh/App/AppMeshPy.cpp @@ -267,11 +267,12 @@ private: App::Application::Config()["BuildRevisionHash"]; exporter.reset( new ExporterAMF(outputFileName, meta, exportAmfCompressed) ); - + } + else if (exportFormat == MeshIO::ThreeMF) { + exporter.reset( new Exporter3MF(outputFileName) ); } else if (exportFormat != MeshIO::Undefined) { exporter.reset( new MergeExporter(outputFileName, exportFormat) ); - } else { std::string exStr("Can't determine mesh format from file name: '"); diff --git a/src/Mod/Mesh/App/Exporter.cpp b/src/Mod/Mesh/App/Exporter.cpp index 986558b055..b557c8b948 100644 --- a/src/Mod/Mesh/App/Exporter.cpp +++ b/src/Mod/Mesh/App/Exporter.cpp @@ -22,28 +22,29 @@ #include "PreCompiled.h" #ifndef _PreComp_ - #include - #include - #include -#endif // #ifndef _PreComp_ +# include +# include +# include +# include +#endif #include "Exporter.h" #include "MeshFeature.h" #include "Core/Iterator.h" +#include "Core/IO/Writer3MF.h" -#include "Base/Console.h" -#include "Base/Exception.h" -#include "Base/FileInfo.h" +#include +#include +#include #include -#include "Base/Sequencer.h" -#include "Base/Stream.h" -#include "Base/Tools.h" +#include +#include -#include "App/Application.h" -#include "App/ComplexGeoData.h" -#include "App/ComplexGeoDataPy.h" -#include "App/DocumentObject.h" +#include +#include +#include +#include #include @@ -141,6 +142,16 @@ int Exporter::addObject(App::DocumentObject *obj, float tol) return count; } +void Exporter::throwIfNoPermission(const std::string& filename) +{ + // ask for write permission + Base::FileInfo fi(filename); + Base::FileInfo di(fi.dirPath()); + if ((fi.exists() && !fi.isWritable()) || !di.exists() || !di.isWritable()) { + throw Base::FileException("No write permission for file", filename); + } +} + // ---------------------------------------------------------------------------- MergeExporter::MergeExporter(std::string fileName, MeshIO::Format) @@ -217,18 +228,47 @@ bool MergeExporter::addMesh(const char *name, const MeshObject & mesh) // ---------------------------------------------------------------------------- +class Exporter3MF::Private { +public: + Private(const std::string& filename) + : writer3mf(filename) { + } + MeshCore::Writer3MF writer3mf; +}; + +Exporter3MF::Exporter3MF(std::string fileName) +{ + throwIfNoPermission(fileName); + d.reset(new Private(fileName)); +} + +Exporter3MF::~Exporter3MF() +{ + write(); +} + +bool Exporter3MF::addMesh(const char *name, const MeshObject & mesh) +{ + boost::ignore_unused(name); + return d->writer3mf.AddMesh(mesh.getKernel(), mesh.getTransform()); +} + +void Exporter3MF::write() +{ + d->writer3mf.Save(); +} + +// ---------------------------------------------------------------------------- + ExporterAMF::ExporterAMF( std::string fileName, const std::map &meta, 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()) { - throw Base::FileException("No write permission for file", fileName); - } + throwIfNoPermission(fileName); + Base::FileInfo fi(fileName); if (compress) { auto *zipStreamPtr( new zipios::ZipOutputStream(fi.filePath()) ); diff --git a/src/Mod/Mesh/App/Exporter.h b/src/Mod/Mesh/App/Exporter.h index 951c068095..9a55b39088 100644 --- a/src/Mod/Mesh/App/Exporter.h +++ b/src/Mod/Mesh/App/Exporter.h @@ -27,9 +27,10 @@ #include #include -#include "Base/Type.h" +#include +#include -#include "App/Property.h" +#include #include "MeshFeature.h" #include "Core/MeshIO.h" @@ -67,6 +68,7 @@ public: protected: /// Does some simple escaping of characters for XML-type exports static std::string xmlEscape(const std::string &input); + void throwIfNoPermission(const std::string&); std::map > subObjectNameCache; std::map meshCache; @@ -90,6 +92,28 @@ protected: std::string fName; }; +/// Used for exporting to 3D Manufacturing Format (3MF) +/*! + * The constructor and destructor write the beginning and end of the 3MF, + * addObject() is used to add geometry + */ +class Exporter3MF : public Exporter +{ +public: + Exporter3MF(std::string fileName); + ~Exporter3MF() override; + + bool addMesh(const char *name, const MeshObject & mesh) override; + +private: + /// Write the meshes of the added objects to the output file + void write(); + +private: + class Private; + std::unique_ptr d; +}; + /// Used for exporting to Additive Manufacturing File (AMF) format /*! * The constructor and destructor write the beginning and end of the AMF, diff --git a/src/Mod/Mesh/App/PreCompiled.h b/src/Mod/Mesh/App/PreCompiled.h index a1944774c3..d3bdb5f520 100644 --- a/src/Mod/Mesh/App/PreCompiled.h +++ b/src/Mod/Mesh/App/PreCompiled.h @@ -67,6 +67,7 @@ #endif #include +#include #endif //_PreComp_