diff --git a/src/Mod/Mesh/App/CMakeLists.txt b/src/Mod/Mesh/App/CMakeLists.txt index 43351fdd9f..8ac48a221f 100644 --- a/src/Mod/Mesh/App/CMakeLists.txt +++ b/src/Mod/Mesh/App/CMakeLists.txt @@ -100,6 +100,8 @@ SET(Core_SRCS Core/CylinderFit.h Core/SphereFit.cpp Core/SphereFit.h + Core/IO/Writer3MF.cpp + Core/IO/Writer3MF.h ) SOURCE_GROUP("Core" FILES ${Core_SRCS}) diff --git a/src/Mod/Mesh/App/Core/IO/Writer3MF.cpp b/src/Mod/Mesh/App/Core/IO/Writer3MF.cpp new file mode 100644 index 0000000000..bfdd9f530f --- /dev/null +++ b/src/Mod/Mesh/App/Core/IO/Writer3MF.cpp @@ -0,0 +1,136 @@ +/*************************************************************************** + * Copyright (c) 2022 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" +#ifndef _PreComp_ +# include +#endif + + +#include "Writer3MF.h" +#include "Core/MeshKernel.h" +#include +#include +#include + +using namespace MeshCore; + + +void Writer3MF::SetTransform(const Base::Matrix4D& mat) +{ + transform = mat; + if (mat != Base::Matrix4D()) + applyTransform = true; +} + +bool Writer3MF::Save(std::ostream &str) const +{ + zipios::ZipOutputStream zip(str); + zip.putNextEntry("3D/3dmodel.model"); + if (!SaveModel(zip)) + return false; + zip.closeEntry(); + + zip.putNextEntry("_rels/.rels"); + if (!SaveRels(zip)) + return false; + zip.closeEntry(); + + zip.putNextEntry("[Content_Types].xml"); + if (!SaveContent(zip)) + return false; + zip.closeEntry(); + return true; +} + +bool Writer3MF::SaveModel(std::ostream &str) const +{ + const MeshPointArray& rPoints = kernel.GetPoints(); + const MeshFacetArray& rFacets = kernel.GetFacets(); + + if (!str || str.bad()) + return false; + + str << "\n" + << "\n" + << "FreeCAD\n"; + str << Base::blanks(2) << "\n"; + str << Base::blanks(4) << "\n"; + str << Base::blanks(6) << "\n"; + + // vertices + str << Base::blanks(8) << "\n"; + Base::Vector3f pt; + std::size_t index = 0; + for (MeshPointArray::_TConstIterator it = rPoints.begin(); it != rPoints.end(); ++it, ++index) { + pt.Set(it->x, it->y, it->z); + if (this->applyTransform) { + this->transform.multVec(pt, pt); + } + str << Base::blanks(10) << "\n"; + } + str << Base::blanks(8) << "\n"; + + // facet indices + str << Base::blanks(8) << "\n"; + for (MeshFacetArray::_TConstIterator it = rFacets.begin(); it != rFacets.end(); ++it) { + str << Base::blanks(10) << "_aulPoints[0] + << "\" v2=\"" << it->_aulPoints[1] + << "\" v3=\"" << it->_aulPoints[2] + << "\" />\n"; + } + str << Base::blanks(8) << "\n"; + + str << Base::blanks(6) << "\n"; + str << Base::blanks(4) << "\n"; + str << Base::blanks(2) << "\n"; + str << Base::blanks(2) << "\n"; + str << Base::blanks(4) << "\n"; + str << Base::blanks(2) << "\n"; + str << "\n"; + return true; +} + +bool Writer3MF::SaveRels(std::ostream &str) const +{ + str << "\n" + << "" + "" + ""; + return true; +} + +bool Writer3MF::SaveContent(std::ostream &str) const +{ + str << "\n" + << "" + "" + "" + ""; + return true; +} diff --git a/src/Mod/Mesh/App/Core/IO/Writer3MF.h b/src/Mod/Mesh/App/Core/IO/Writer3MF.h new file mode 100644 index 0000000000..4f7ffb8b75 --- /dev/null +++ b/src/Mod/Mesh/App/Core/IO/Writer3MF.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (c) 2022 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef MESH_IO_WRITER_3MF_H +#define MESH_IO_WRITER_3MF_H + +#include + +#include "Core/Elements.h" + +namespace MeshCore +{ +class MeshKernel; + +/** Saves the mesh object into 3MF format. */ +class MeshExport Writer3MF +{ +public: + Writer3MF(const MeshKernel &mesh): + kernel(mesh) {} + + void SetTransform(const Base::Matrix4D&); + bool Save(std::ostream &str) const; + +private: + bool SaveModel(std::ostream &str) const; + bool SaveRels(std::ostream &str) const; + bool SaveContent(std::ostream &str) const; + +private: + bool applyTransform = false; + Base::Matrix4D transform; + const MeshKernel &kernel; +}; + +} // namespace MeshCore + + +#endif // MESH_IO_WRITER_3MF_H diff --git a/src/Mod/Mesh/App/Core/MeshIO.cpp b/src/Mod/Mesh/App/Core/MeshIO.cpp index 549f919cad..b79c99cb30 100644 --- a/src/Mod/Mesh/App/Core/MeshIO.cpp +++ b/src/Mod/Mesh/App/Core/MeshIO.cpp @@ -30,6 +30,7 @@ #include "Algorithm.h" #include "Builder.h" #include "Degeneration.h" +#include "IO/Writer3MF.h" #include #include @@ -2882,94 +2883,9 @@ void MeshOutput::SaveXML (Base::Writer &writer) const /** Saves the mesh object into a 3MF file. */ bool MeshOutput::Save3MF(std::ostream &str) const { - zipios::ZipOutputStream zip(str); - zip.putNextEntry("3D/3dmodel.model"); - if (!Save3MFModel(zip)) - return false; - zip.closeEntry(); - - zip.putNextEntry("_rels/.rels"); - if (!Save3MFRels(zip)) - return false; - zip.closeEntry(); - - zip.putNextEntry("[Content_Types].xml"); - if (!Save3MFContent(zip)) - return false; - zip.closeEntry(); - return true; -} - -bool MeshOutput::Save3MFRels(std::ostream &str) const -{ - str << "\n" - << "" - "" - ""; - return true; -} - -bool MeshOutput::Save3MFContent(std::ostream &str) const -{ - str << "\n" - << "" - "" - "" - ""; - return true; -} - -bool MeshOutput::Save3MFModel (std::ostream &str) const -{ - const MeshPointArray& rPoints = _rclMesh.GetPoints(); - const MeshFacetArray& rFacets = _rclMesh.GetFacets(); - - if (!str || str.bad()) - return false; - - str << "\n" - << "\n" - << "FreeCAD\n"; - str << Base::blanks(2) << "\n"; - str << Base::blanks(4) << "\n"; - str << Base::blanks(6) << "\n"; - - // vertices - str << Base::blanks(8) << "\n"; - Base::Vector3f pt; - std::size_t index = 0; - for (MeshPointArray::_TConstIterator it = rPoints.begin(); it != rPoints.end(); ++it, ++index) { - pt.Set(it->x, it->y, it->z); - if (this->apply_transform) { - this->_transform.multVec(pt, pt); - } - str << Base::blanks(10) << "\n"; - } - str << Base::blanks(8) << "\n"; - - // facet indices - str << Base::blanks(8) << "\n"; - for (MeshFacetArray::_TConstIterator it = rFacets.begin(); it != rFacets.end(); ++it) { - str << Base::blanks(10) << "_aulPoints[0] - << "\" v2=\"" << it->_aulPoints[1] - << "\" v3=\"" << it->_aulPoints[2] - << "\" />\n"; - } - str << Base::blanks(8) << "\n"; - - str << Base::blanks(6) << "\n"; - str << Base::blanks(4) << "\n"; - str << Base::blanks(2) << "\n"; - str << Base::blanks(2) << "\n"; - str << Base::blanks(4) << "\n"; - str << Base::blanks(2) << "\n"; - str << "\n"; - return true; + Writer3MF writer(_rclMesh); + writer.SetTransform(_transform); + return writer.Save(str); } /** Writes an IDTF file. */ diff --git a/src/Mod/Mesh/App/Core/MeshIO.h b/src/Mod/Mesh/App/Core/MeshIO.h index 4dccd093ad..560aceeef8 100644 --- a/src/Mod/Mesh/App/Core/MeshIO.h +++ b/src/Mod/Mesh/App/Core/MeshIO.h @@ -228,9 +228,6 @@ public: protected: /** Writes an X3D file. */ bool SaveX3DContent (std::ostream &rstrOut, bool exportViewpoints) const; - bool Save3MFModel(std::ostream &str) const; - bool Save3MFRels(std::ostream &str) const; - bool Save3MFContent(std::ostream &str) const; protected: const MeshKernel &_rclMesh; /**< reference to mesh data structure */