File size: 9,141 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2017 Ian Rees <ian.rees@gmail.com>                      *
 *                                                                         *
 *   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_EXPORTER_H
#define MESH_EXPORTER_H

#include <map>
#include <ostream>
#include <vector>

#include "MeshFeature.h"
#include "Core/MeshIO.h"
#include "Core/IO/Writer3MF.h"


namespace Mesh
{

/// Virtual base class for exporting meshes
/*!
 * Constructors of derived classes are expected to be required, for passing
 * in the name of output file.
 *
 * If objects are meant to be combined into a single file, then the file should
 * be saved from the derived class' destructor.
 */
class MeshExport Exporter
{
public:
    Exporter();
    virtual ~Exporter() = default;

    /// Add object and all subobjects and links etc. Returns the number of stuff added.
    /*!
     * @param obj The object to export. If this is a group like object, its
     *            sub-objects will be added.
     * @param tol The tolerance/accuracy with which to generate the triangle mesh
     * @return The number of objects/subobjects that was exported from the document.
               See the parameter `accuracy` of ComplexGeoData::getFaces
     */
    int addObject(App::DocumentObject* obj, float tol);

    virtual bool addMesh(const char* name, const MeshObject& mesh) = 0;

    Exporter(const Exporter&) = delete;
    Exporter(Exporter&&) = delete;
    Exporter& operator=(const Exporter&) = delete;
    Exporter& operator=(Exporter&&) = delete;

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;
};

/// Creates a single mesh, in a file, from one or more objects
class MeshExport MergeExporter: public Exporter
{
public:
    MergeExporter(std::string fileName, MeshCore::MeshIO::Format fmt);
    ~MergeExporter() override;

    MergeExporter(const MergeExporter&) = delete;
    MergeExporter(MergeExporter&&) = delete;
    MergeExporter& operator=(const MergeExporter&) = delete;
    MergeExporter& operator=(MergeExporter&&) = delete;

    bool addMesh(const char* name, const MeshObject& mesh) override;

private:
    /// Write the meshes of the added objects to the output file
    void write();

protected:
    // NOLINTBEGIN
    MeshObject mergingMesh;
    std::string fName;
    // NOLINTEND
};

// ------------------------------------------------------------------------------------------------

/*!
 * \brief The AbstractExtension class
 * Abstract base class for file format extensions
 */
class MeshExport AbstractFormatExtension
{
protected:
    AbstractFormatExtension() = default;

public:
    virtual ~AbstractFormatExtension() = default;

    AbstractFormatExtension(const AbstractFormatExtension&) = delete;
    AbstractFormatExtension(AbstractFormatExtension&&) = delete;
    AbstractFormatExtension& operator=(const AbstractFormatExtension&) = delete;
    AbstractFormatExtension& operator=(AbstractFormatExtension&&) = delete;
};

using AbstractFormatExtensionPtr = std::shared_ptr<AbstractFormatExtension>;

/*!
 * \brief The Extension3MF class
 * Abstract base class for 3MF extensions
 */
class MeshExport Extension3MF: public AbstractFormatExtension
{
public:
    using Resource = MeshCore::Resource3MF;
    Extension3MF() = default;
    virtual Resource addMesh(const MeshObject& mesh) = 0;
};

using Extension3MFPtr = std::shared_ptr<Extension3MF>;

/*!
 * \brief The AbstractExtensionProducer class
 * Abstract base class to create an instance of an AbstractFormatExtension.
 */
class MeshExport AbstractExtensionProducer
{
public:
    AbstractExtensionProducer() = default;
    virtual ~AbstractExtensionProducer() = default;
    virtual AbstractFormatExtensionPtr create() const = 0;

    AbstractExtensionProducer(const AbstractExtensionProducer&) = delete;
    AbstractExtensionProducer(AbstractExtensionProducer&&) = delete;
    AbstractExtensionProducer& operator=(const AbstractExtensionProducer&) = delete;
    AbstractExtensionProducer& operator=(AbstractExtensionProducer&&) = delete;
};

using AbstractExtensionProducerPtr = std::shared_ptr<AbstractExtensionProducer>;

/*!
 * \brief The Extension3MFProducer class
 * Abstract base class to create an instance of an Extension3MF.
 */
class MeshExport Extension3MFProducer: public AbstractExtensionProducer
{
public:
    Extension3MFProducer() = default;
    virtual void initialize() = 0;
};

using Extension3MFProducerPtr = std::shared_ptr<Extension3MFProducer>;

/*!
 * \brief The GuiExtension3MFProducer class
 * This class tries to load the MeshGui module to register further 3MF extensions.
 */
class MeshExport GuiExtension3MFProducer: public Extension3MFProducer
{
public:
    GuiExtension3MFProducer() = default;
    void initialize() override;
    AbstractFormatExtensionPtr create() const override;
};

/*!
 * \brief The Extension3MFFactory class
 * Factor class to manage the producers of Extension3MF
 */
class MeshExport Extension3MFFactory
{
public:
    static void addProducer(Extension3MFProducer* ext);
    static void initialize();
    static std::vector<Extension3MFPtr> createExtensions();

private:
    static std::vector<Extension3MFProducerPtr> producer;
};

// ------------------------------------------------------------------------------------------------

/// 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 MeshExport Exporter3MF: public Exporter
{
public:
    explicit Exporter3MF(std::string fileName, const std::vector<Extension3MFPtr>& = {});
    ~Exporter3MF() override;

    Exporter3MF(const Exporter3MF&) = delete;
    Exporter3MF(Exporter3MF&&) = delete;
    Exporter3MF& operator=(const Exporter3MF&) = delete;
    Exporter3MF& operator=(Exporter3MF&&) = delete;

    bool addMesh(const char* name, const MeshObject& mesh) override;
    /*!
     * \brief SetForceModel
     * Forcces to write the mesh as model even if itsn't a solid.
     * \param model
     */
    void setForceModel(bool model);

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,
 * addObject() is used to add geometry
 */
class MeshExport ExporterAMF: public Exporter
{
public:
    /// Writes AMF header
    /*!
     * meta information passed in is applied at the <amf> tag level
     */
    ExporterAMF(
        std::string fileName,
        const std::map<std::string, std::string>& meta,
        bool compress = true
    );

    /// Writes AMF footer
    ~ExporterAMF() override;

    ExporterAMF(const ExporterAMF&) = delete;
    ExporterAMF(ExporterAMF&&) = delete;
    ExporterAMF& operator=(const ExporterAMF&) = delete;
    ExporterAMF& operator=(ExporterAMF&&) = delete;

    bool addMesh(const char* name, const MeshObject& mesh) override;

private:
    /// Write the meshes of the added objects to the output file
    void write();

private:
    std::ostream* outputStreamPtr {nullptr};
    int nextObjectIndex {0};

    /// Helper for putting Base::Vector3f objects into a std::map in addMesh()
    class VertLess;
};  // class ExporterAMF

}  // namespace Mesh

#endif  // MESH_EXPORTER_H