Mesh: add exporter class for 3MF format

This commit is contained in:
wmayer
2022-08-31 17:11:15 +02:00
parent 580c124b22
commit 16c73780b9
4 changed files with 89 additions and 23 deletions

View File

@@ -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: '");

View File

@@ -22,28 +22,29 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <algorithm>
#include <vector>
#include <boost/algorithm/string/replace.hpp>
#endif // #ifndef _PreComp_
# include <algorithm>
# include <vector>
# include <boost/algorithm/string/replace.hpp>
# include <boost/core/ignore_unused.hpp>
#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 <Base/Console.h>
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Interpreter.h>
#include "Base/Sequencer.h"
#include "Base/Stream.h"
#include "Base/Tools.h"
#include <Base/Sequencer.h>
#include <Base/Tools.h>
#include "App/Application.h"
#include "App/ComplexGeoData.h"
#include "App/ComplexGeoDataPy.h"
#include "App/DocumentObject.h"
#include <App/Application.h>
#include <App/ComplexGeoData.h>
#include <App/ComplexGeoDataPy.h>
#include <App/DocumentObject.h>
#include <zipios++/zipoutputstream.h>
@@ -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<std::string, std::string> &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()) );

View File

@@ -27,9 +27,10 @@
#include <vector>
#include <ostream>
#include "Base/Type.h"
#include <Base/Stream.h>
#include <Base/Type.h>
#include "App/Property.h"
#include <App/Property.h>
#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<const App::DocumentObject *, std::vector<std::string> > subObjectNameCache;
std::map<const App::DocumentObject *, MeshObject> 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<Private> d;
};
/// Used for exporting to Additive Manufacturing File (AMF) format
/*!
* The constructor and destructor write the beginning and end of the AMF,

View File

@@ -67,6 +67,7 @@
#endif
#include <boost/algorithm/string/replace.hpp>
#include <boost/core/ignore_unused.hpp>
#endif //_PreComp_