File size: 9,065 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: LGPL-2.1-or-later

/****************************************************************************
 *   Copyright (c) 2018 Zheng, Lei (realthunder) <realthunder.dev@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 IMPORT_IMPORTOCAF2_H
#define IMPORT_IMPORTOCAF2_H

#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <TDocStd_Document.hxx>
#include <TopoDS_Shape.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <XCAFDoc_ShapeTool.hxx>

#include <Base/Sequencer.h>
#include <Mod/Part/App/TopoShape.h>

#include "ExportOCAF.h"
#include "ImportOCAF.h"
#include "Tools.h"


class TDF_Label;
class TopLoc_Location;

namespace App
{
class Document;
class DocumentObject;
}  // namespace App
namespace Part
{
class Feature;
}

namespace Import
{

struct ImportExport ImportOCAFOptions
{
    ImportOCAFOptions();
    Base::Color defaultFaceColor;
    Base::Color defaultEdgeColor;
    bool merge = false;
    bool useLinkGroup = false;
    bool useBaseName = true;
    bool importHidden = true;
    bool reduceObjects = false;
    bool showProgress = false;
    bool expandCompound = false;
    int mode = 0;
};

class ImportExport ImportOCAF2
{
public:
    ImportOCAF2(Handle(TDocStd_Document) h, App::Document* d, const std::string& name);
    virtual ~ImportOCAF2();
    App::DocumentObject* loadShapes();

    static ImportOCAFOptions customImportOptions();
    void setImportOptions(ImportOCAFOptions opts);
    void setMerge(bool enable)
    {
        options.merge = enable;
    }
    void setUseLinkGroup(bool enable);
    void setBaseName(bool enable)
    {
        options.useBaseName = enable;
    }
    void setImportHiddenObject(bool enable)
    {
        options.importHidden = enable;
    }
    void setReduceObjects(bool enable)
    {
        options.reduceObjects = enable;
    }
    void setShowProgress(bool enable)
    {
        options.showProgress = enable;
    }
    void setExpandCompound(bool enable)
    {
        options.expandCompound = enable;
    }

    enum ImportMode
    {
        SingleDoc = 0,
        GroupPerDoc = 1,
        GroupPerDir = 2,
        ObjectPerDoc = 3,
        ObjectPerDir = 4,
        ModeMax,
    };
    void setMode(int m);
    int getMode() const
    {
        return options.mode;
    }

private:
    struct Info
    {
        std::string baseName;
        App::DocumentObject* obj = nullptr;
        App::PropertyPlacement* propPlacement = nullptr;
        Base::Color faceColor;
        Base::Color edgeColor;
        bool hasFaceColor = false;
        bool hasEdgeColor = false;
        int free = true;
    };

    App::DocumentObject* loadShape(
        App::Document* doc,
        TDF_Label label,
        const TopoDS_Shape& shape,
        bool baseOnly = false,
        bool newDoc = true
    );
    App::Document* getDocument(App::Document* doc, TDF_Label label);
    bool createAssembly(
        App::Document* doc,
        TDF_Label label,
        const TopoDS_Shape& shape,
        Info& info,
        bool newDoc
    );
    bool createObject(
        App::Document* doc,
        TDF_Label label,
        const TopoDS_Shape& shape,
        Info& info,
        bool newDoc
    );
    bool createGroup(
        App::Document* doc,
        Info& info,
        const TopoDS_Shape& shape,
        std::vector<App::DocumentObject*>& children,
        const boost::dynamic_bitset<>& visibilities,
        bool canReduce = false
    );
    bool getColor(const TopoDS_Shape& shape, Info& info, bool check = false, bool noDefault = false);
    void getSHUOColors(TDF_Label label, std::map<std::string, Base::Color>& colors, bool appendFirst);
    void setObjectName(Info& info, TDF_Label label);
    std::string getLabelName(TDF_Label label);
    App::DocumentObject* expandShape(App::Document* doc, TDF_Label label, const TopoDS_Shape& shape);

    virtual void applyEdgeColors(Part::Feature*, const std::vector<Base::Color>&)
    {}
    virtual void applyFaceColors(Part::Feature*, const std::vector<Base::Color>&)
    {}
    virtual void applyElementColors(App::DocumentObject*, const std::map<std::string, Base::Color>&)
    {}
    virtual void applyLinkColor(App::DocumentObject*, int /*index*/, Base::Color)
    {}

private:
    class ImportLegacy: public ImportOCAF
    {
    public:
        explicit ImportLegacy(ImportOCAF2& parent)
            : ImportOCAF(parent.pDoc, parent.pDocument, parent.default_name)
            , myParent(parent)
        {}

    private:
        void applyColors(Part::Feature* part, const std::vector<Base::Color>& colors) override
        {
            myParent.applyFaceColors(part, colors);
        }

        ImportOCAF2& myParent;
    };
    friend class ImportLegacy;

    Handle(TDocStd_Document) pDoc;
    App::Document* pDocument;
    Handle(XCAFDoc_ShapeTool) aShapeTool;
    Handle(XCAFDoc_ColorTool) aColorTool;
    std::string default_name;

    ImportOCAFOptions options;
    std::string filePath;

    std::unordered_map<TopoDS_Shape, Info, ShapeHasher> myShapes;
    std::unordered_map<TDF_Label, std::string, LabelHasher> myNames;
    std::unordered_map<App::DocumentObject*, App::PropertyPlacement*> myCollapsedObjects;

    Base::SequencerLauncher* sequencer {nullptr};
};

class ImportExport ImportOCAFExt: public ImportOCAF2
{
public:
    ImportOCAFExt(Handle(TDocStd_Document) hStdDoc, App::Document* doc, const std::string& name);

    std::map<Part::Feature*, std::vector<Base::Color>> partColors;

private:
    void applyFaceColors(Part::Feature* part, const std::vector<Base::Color>& colors) override;
};

struct ImportExport ExportOCAFOptions
{
    ExportOCAFOptions();
    Base::Color defaultColor;
    bool exportHidden = true;
    bool keepPlacement = false;
};

class ImportExport ExportOCAF2
{
public:
    using GetShapeColorsFunc
        = std::function<std::map<std::string, Base::Color>(App::DocumentObject*, const char*)>;
    explicit ExportOCAF2(Handle(TDocStd_Document) h, GetShapeColorsFunc func = GetShapeColorsFunc());

    static ExportOCAFOptions customExportOptions();
    void setExportOptions(ExportOCAFOptions opts)
    {
        options = opts;
    }
    void setExportHiddenObject(bool enable)
    {
        options.exportHidden = enable;
    }
    void setKeepPlacement(bool enable)
    {
        options.keepPlacement = enable;
    }
    void exportObjects(std::vector<App::DocumentObject*>& objs, const char* name = nullptr);
    bool canFallback(std::vector<App::DocumentObject*> objs);

private:
    TDF_Label exportObject(
        App::DocumentObject* obj,
        const char* sub,
        TDF_Label parent,
        const char* name = nullptr
    );
    void setupObject(
        TDF_Label label,
        App::DocumentObject* obj,
        const Part::TopoShape& shape,
        const std::string& prefix,
        const char* name = nullptr,
        bool force = false
    );
    void setName(TDF_Label label, App::DocumentObject* obj, const char* name = nullptr);
    TDF_Label findComponent(const char* subname, TDF_Label label, TDF_LabelSequence& labels);

private:
    Handle(TDocStd_Document) pDoc;
    Handle(XCAFDoc_ShapeTool) aShapeTool;
    Handle(XCAFDoc_ColorTool) aColorTool;

    std::unordered_map<App::DocumentObject*, TDF_Label> myObjects;

    std::unordered_map<TDF_Label, std::vector<std::string>, LabelHasher> myNames;

    std::set<std::pair<App::DocumentObject*, std::string>> mySetups;

    std::vector<App::DocumentObject*> groupLinks;

    GetShapeColorsFunc getShapeColors;

    ExportOCAFOptions options;
};

}  // namespace Import

#endif  // IMPORT_IMPORTOCAF2_H