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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// 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                                *
 *                                                                         *
 ***************************************************************************/

#include <algorithm>
#include <boost/algorithm/string/replace.hpp>
#include <boost/core/ignore_unused.hpp>
#include <vector>


#include <App/Application.h>
#include <App/ComplexGeoData.h>
#include <App/ComplexGeoDataPy.h>
#include <App/DocumentObject.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 "Core/Iterator.h"
#include "Core/IO/Writer3MF.h"
#include <zipios++/zipoutputstream.h>

#include "Exporter.h"


using namespace Mesh;
using namespace MeshCore;

static std::vector<std::string> expandSubObjectNames(
    const App::DocumentObject* obj,
    std::map<const App::DocumentObject*, std::vector<std::string>>& subObjectNameCache,
    int depth
)
{
    if (!App::GetApplication().checkLinkDepth(depth)) {
        return {};
    }

    auto subs = obj->getSubObjects();
    if (subs.empty()) {
        subs.emplace_back("");
        return subs;
    }

    std::vector<std::string> res;
    for (auto& sub : subs) {
        int vis = sub.empty() ? 1 : obj->isElementVisible(sub.c_str());
        if (vis == 0) {
            continue;
        }
        auto sobj = obj->getSubObject(sub.c_str());
        if (!sobj || (vis < 0 && !sobj->Visibility.getValue())) {
            continue;
        }
        auto linked = sobj->getLinkedObject(true);
        auto it = subObjectNameCache.find(linked);
        if (it == subObjectNameCache.end()) {
            it = subObjectNameCache
                     .emplace(linked, expandSubObjectNames(linked, subObjectNameCache, depth + 1))
                     .first;
        }
        for (auto& ssub : it->second) {
            res.push_back(sub + ssub);
        }
    }
    return res;
}

Exporter::Exporter() = default;

// static
std::string Exporter::xmlEscape(const std::string& input)
{
    std::string out(input);
    boost::replace_all(out, "&", "&amp;");
    boost::replace_all(out, "\"", "&quot;");
    boost::replace_all(out, "'", "&apos;");
    boost::replace_all(out, "<", "&lt;");
    boost::replace_all(out, ">", "&gt;");
    return out;
}

int Exporter::addObject(App::DocumentObject* obj, float tol)
{
    int count = 0;
    for (std::string& sub : expandSubObjectNames(obj, subObjectNameCache, 0)) {
        Base::Matrix4D matrix;
        auto sobj = obj->getSubObject(sub.c_str(), nullptr, &matrix);
        auto linked = sobj->getLinkedObject(true, &matrix, false);
        auto it = meshCache.find(linked);
        if (it == meshCache.end()) {
            if (linked->isDerivedFrom<Mesh::Feature>()) {
                it = meshCache.emplace(linked, static_cast<Mesh::Feature*>(linked)->Mesh.getValue()).first;
                it->second.setTransform(matrix);
            }
            else {
                Base::PyGILStateLocker lock;
                PyObject* pyobj = nullptr;
                linked->getSubObject("", &pyobj, nullptr, false);
                if (!pyobj) {
                    continue;
                }
                if (PyObject_TypeCheck(pyobj, &Data::ComplexGeoDataPy::Type)) {
                    std::vector<Base::Vector3d> aPoints;
                    std::vector<Data::ComplexGeoData::Facet> aTopo;
                    auto geoData = static_cast<Data::ComplexGeoDataPy*>(pyobj)->getComplexGeoDataPtr();
                    geoData->getFaces(aPoints, aTopo, tol);
                    it = meshCache.emplace(linked, MeshObject()).first;
                    it->second.setFacets(aTopo, aPoints);
                    it->second.setTransform(matrix);
                }
                Py_DECREF(pyobj);
            }
        }
        else if (it->second.getTransform() != matrix) {
            it->second.setTransform(matrix);
        }

        // Add a new mesh
        if (it != meshCache.end()) {
            if (addMesh(sobj->Label.getValue(), it->second)) {
                ++count;
            }
        }
    }
    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", fi);
    }
}

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

MergeExporter::MergeExporter(std::string fileName, MeshIO::Format)
    : fName(std::move(fileName))
{}

MergeExporter::~MergeExporter()
{
    write();
}

void MergeExporter::write()
{
    // if we have more than one segment set the 'save' flag
    if (mergingMesh.countSegments() > 1) {
        for (unsigned long i = 0; i < mergingMesh.countSegments(); ++i) {
            mergingMesh.getSegment(i).save(true);
        }
    }

    try {
        mergingMesh.save(fName.c_str());
    }
    catch (const Base::Exception& e) {
        std::cerr << "Saving mesh failed: " << e.what() << std::endl;
    }
}

bool MergeExporter::addMesh(const char* name, const MeshObject& mesh)
{
    auto kernel = mesh.getKernel();
    kernel.Transform(mesh.getTransform());
    auto countFacets(mergingMesh.countFacets());
    if (countFacets == 0) {
        mergingMesh.setKernel(kernel);
    }
    else {
        mergingMesh.addMesh(kernel);
    }

    // if the mesh already has persistent segments then use them instead
    unsigned long numSegm = mesh.countSegments();
    unsigned long canSave = 0;
    for (unsigned long i = 0; i < numSegm; i++) {
        if (mesh.getSegment(i).isSaved()) {
            canSave++;
        }
    }

    if (canSave > 0) {
        for (unsigned long i = 0; i < numSegm; i++) {
            const Segment& segm = mesh.getSegment(i);
            if (segm.isSaved()) {
                std::vector<FacetIndex> indices = segm.getIndices();
                std::for_each(indices.begin(), indices.end(), [countFacets](FacetIndex& v) {
                    v += countFacets;
                });
                Segment new_segm(&mergingMesh, indices, true);
                new_segm.setName(segm.getName());
                mergingMesh.addSegment(new_segm);
            }
        }
    }
    else {
        // now create a segment for the added mesh
        std::vector<FacetIndex> indices;
        indices.resize(mergingMesh.countFacets() - countFacets);
        std::generate(indices.begin(), indices.end(), Base::iotaGen<FacetIndex>(countFacets));
        Segment segm(&mergingMesh, indices, true);
        segm.setName(name);
        mergingMesh.addSegment(segm);
    }

    return true;
}

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

AbstractFormatExtensionPtr GuiExtension3MFProducer::create() const
{
    return nullptr;
}

void GuiExtension3MFProducer::initialize()
{
    Base::PyGILStateLocker lock;
    PyObject* module = PyImport_ImportModule("MeshGui");
    if (module) {
        Py_DECREF(module);
    }
    else {
        PyErr_Clear();
    }
}

void Extension3MFFactory::addProducer(Extension3MFProducer* ext)
{
    producer.emplace_back(ext);
}

void Extension3MFFactory::initialize()
{
    std::vector<Extension3MFProducerPtr> ext = producer;
    for (const auto& it : ext) {
        it->initialize();
    }
}

std::vector<Extension3MFPtr> Extension3MFFactory::createExtensions()
{
    std::vector<Extension3MFPtr> ext;
    for (const auto& it : producer) {
        Extension3MFPtr ptr = std::dynamic_pointer_cast<Extension3MF>(it->create());
        if (ptr) {
            ext.push_back(ptr);
        }
    }
    return ext;
}

std::vector<Extension3MFProducerPtr> Extension3MFFactory::producer;

class Exporter3MF::Private
{
public:
    explicit Private(const std::string& filename, std::vector<Extension3MFPtr> ext)
        : writer3mf(filename)
        , ext(std::move(ext))
    {}
    MeshCore::Writer3MF writer3mf;
    std::vector<Extension3MFPtr> ext;
};

Exporter3MF::Exporter3MF(std::string fileName, const std::vector<Extension3MFPtr>& ext)
{
    throwIfNoPermission(fileName);
    d = std::make_unique<Private>(fileName, ext);
}

Exporter3MF::~Exporter3MF()
{
    write();
}

bool Exporter3MF::addMesh(const char* name, const MeshObject& mesh)
{
    boost::ignore_unused(name);
    bool ok = d->writer3mf.AddMesh(mesh.getKernel(), mesh.getTransform());
    if (ok) {
        for (const auto& it : d->ext) {
            d->writer3mf.AddResource(it->addMesh(mesh));
        }
    }

    return ok;
}

void Exporter3MF::setForceModel(bool model)
{
    d->writer3mf.SetForceModel(model);
}

void Exporter3MF::write()
{
    d->writer3mf.Save();
}

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

ExporterAMF::ExporterAMF(std::string fileName, const std::map<std::string, std::string>& meta, bool compress)
{
    // ask for write permission
    throwIfNoPermission(fileName);

    Base::FileInfo fi(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);
    }

    if (outputStreamPtr) {
        *outputStreamPtr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                         << "<amf unit=\"millimeter\">\n";
        for (auto const& metaEntry : meta) {
            *outputStreamPtr << "\t<metadata type=\"" << metaEntry.first << "\">"
                             << metaEntry.second << "</metadata>\n";
        }
    }
}

ExporterAMF::~ExporterAMF()
{
    write();
}

void ExporterAMF::write()
{
    if (outputStreamPtr) {
        *outputStreamPtr << "\t<constellation id=\"0\">\n";
        for (auto objId(0); objId < nextObjectIndex; ++objId) {
            *outputStreamPtr << "\t\t<instance objectid=\"" << objId << "\">\n"
                             << "\t\t\t<deltax>0</deltax>\n"
                             << "\t\t\t<deltay>0</deltay>\n"
                             << "\t\t\t<rz>0</rz>\n"
                             << "\t\t</instance>\n";
        }
        *outputStreamPtr << "\t</constellation>\n"
                         << "</amf>\n";
        delete outputStreamPtr;
    }
}

class ExporterAMF::VertLess
{
public:
    bool operator()(const Base::Vector3f& a, const Base::Vector3f& b) const
    {
        if (a.x == b.x) {
            if (a.y == b.y) {
                if (a.z == b.z) {
                    return false;
                }

                return a.z < b.z;
            }

            return a.y < b.y;
        }

        return a.x < b.x;
    }
};

bool ExporterAMF::addMesh(const char* name, const MeshObject& mesh)
{
    auto kernel = mesh.getKernel();
    kernel.Transform(mesh.getTransform());

    if (!outputStreamPtr || outputStreamPtr->bad()) {
        return false;
    }

    auto numFacets(kernel.CountFacets());

    if (numFacets == 0) {
        return false;
    }

    MeshCore::MeshFacetIterator clIter(kernel), clEnd(kernel);

    Base::SequencerLauncher seq("Saving...", 2 * numFacets + 1);

    *outputStreamPtr << "\t<object id=\"" << nextObjectIndex << "\">\n";
    *outputStreamPtr << "\t\t<metadata type=\"name\">" << xmlEscape(name) << "</metadata>\n";
    *outputStreamPtr << "\t\t<mesh>\n"
                     << "\t\t\t<vertices>\n";

    const MeshCore::MeshGeomFacet* facet {};

    // Iterate through all facets of the mesh, and construct a:
    //   * Cache (map) of used vertices, outputting each new unique vertex to
    //     the output stream as we find it
    //   * Vector of the vertices, referred to by the indices from 1
    std::map<Base::Vector3f, unsigned long, ExporterAMF::VertLess> vertices;
    auto vertItr(vertices.begin());
    auto vertexCount(0UL);

    // {facet1A, facet1B, facet1C, facet2A, ..., facetNC}
    std::vector<unsigned long> facets;

    // For each facet in mesh
    for (clIter.Begin(), clEnd.End(); clIter < clEnd; ++clIter) {
        facet = &(*clIter);

        // For each vertex in facet
        for (auto pnt : facet->_aclPoints) {
            vertItr = vertices.find(pnt);

            if (vertItr == vertices.end()) {
                facets.push_back(vertexCount);

                vertices[pnt] = vertexCount++;

                // Output facet
                *outputStreamPtr << "\t\t\t\t<vertex>\n"
                                 << "\t\t\t\t\t<coordinates>\n";
                for (auto j(0); j < 3; ++j) {
                    char axis('x' + j);
                    *outputStreamPtr << "\t\t\t\t\t\t<" << axis << '>' << pnt[j] << "</" << axis
                                     << ">\n";
                }
                *outputStreamPtr << "\t\t\t\t\t</coordinates>\n"
                                 << "\t\t\t\t</vertex>\n";
            }
            else {
                facets.push_back(vertItr->second);
            }
        }

        seq.next(true);  // allow one to cancel
    }

    *outputStreamPtr << "\t\t\t</vertices>\n"
                     << "\t\t\t<volume>\n";

    // Now that we've output all the vertices, we can
    // output the facets that refer to them!
    for (auto triItr(facets.begin()); triItr != facets.end();) {
        *outputStreamPtr << "\t\t\t\t<triangle>\n";
        for (auto i(1); i < 4; ++i) {
            *outputStreamPtr << "\t\t\t\t\t<v" << i << '>' << *(triItr++) << "</v" << i << ">\n";
        }
        *outputStreamPtr << "\t\t\t\t</triangle>\n";
        seq.next(true);  // allow one to cancel
    }

    *outputStreamPtr << "\t\t\t</volume>\n"
                     << "\t\t</mesh>\n"
                     << "\t</object>\n";

    ++nextObjectIndex;
    return true;
}