FreeCAD

Форк
0
/
MergeDocuments.cpp 
149 строк · 4.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <stack>
26
#endif
27

28
#include <App/Document.h>
29
#include <App/DocumentObject.h>
30
#include <Base/Reader.h>
31
#include <Base/Writer.h>
32
#include <Gui/Application.h>
33
#include <Gui/Document.h>
34
#include <Gui/ViewProvider.h>
35

36
#include "MergeDocuments.h"
37

38

39
using namespace Gui;
40
namespace sp = std::placeholders;
41

42
namespace Gui {
43

44
class XMLMergeReader : public Base::XMLReader
45
{
46
public:
47
    XMLMergeReader(std::map<std::string, std::string>& name, const char* FileName, std::istream& str)
48
      : Base::XMLReader(FileName, str), nameMap(name)
49
    {}
50

51
    void addName(const char* s1, const char* s2) override
52
    {
53
        nameMap[s1] = s2;
54
    }
55
    const char* getName(const char* name) const override
56
    {
57
        std::map<std::string, std::string>::const_iterator it = nameMap.find(name);
58
        if (it != nameMap.end())
59
            return it->second.c_str();
60
        else
61
            return name;
62
    }
63
    bool doNameMapping() const override
64
    {
65
        return true;
66
    }
67
protected:
68

69

70
private:
71
    std::map<std::string, std::string>& nameMap;
72
    using PropertyTag = std::pair<std::string, std::string>;
73
    std::stack<PropertyTag> propertyStack;
74
};
75
}
76

77
MergeDocuments::MergeDocuments(App::Document* doc) : appdoc(doc)
78
{
79
    //NOLINTBEGIN
80
    connectExport = doc->signalExportObjects.connect
81
        (std::bind(&MergeDocuments::exportObject, this, sp::_1, sp::_2));
82
    connectImport = doc->signalImportObjects.connect
83
        (std::bind(&MergeDocuments::importObject, this, sp::_1, sp::_2));
84
    document = Gui::Application::Instance->getDocument(doc);
85
    //NOLINTEND
86
}
87

88
MergeDocuments::~MergeDocuments()
89
{
90
    connectExport.disconnect();
91
    connectImport.disconnect();
92
}
93

94
unsigned int MergeDocuments::getMemSize () const
95
{
96
    return 0;
97
}
98

99
std::vector<App::DocumentObject*>
100
MergeDocuments::importObjects(std::istream& input)
101
{
102
    this->nameMap.clear();
103
    this->stream = new zipios::ZipInputStream(input);
104
    XMLMergeReader reader(this->nameMap,"<memory>", *stream);
105
    std::vector<App::DocumentObject*> objs = appdoc->importObjects(reader);
106

107
    delete this->stream;
108
    this->stream = nullptr;
109

110
    return objs;
111
}
112

113
void MergeDocuments::importObject(const std::vector<App::DocumentObject*>& o, Base::XMLReader & r)
114
{
115
    objects = o;
116
    for (auto it : objects) {
117
        Gui::ViewProvider* vp = document->getViewProvider(it);
118
        if (vp) vp->hide();
119
    }
120
    Restore(r);
121

122
    r.readFiles(*this->stream);
123
}
124

125
void MergeDocuments::exportObject(const std::vector<App::DocumentObject*>& o, Base::Writer & w)
126
{
127
    objects = o;
128
    Save(w);
129
}
130

131
void MergeDocuments::Save (Base::Writer & w) const
132
{
133
    w.addFile("GuiDocument.xml", this);
134
}
135

136
void MergeDocuments::Restore(Base::XMLReader &r)
137
{
138
    r.addFile("GuiDocument.xml", this);
139
}
140

141
void MergeDocuments::SaveDocFile (Base::Writer & w) const
142
{
143
    document->exportObjects(objects, w);
144
}
145

146
void MergeDocuments::RestoreDocFile(Base::Reader & r)
147
{
148
    document->importObjects(objects, r, nameMap);
149
}
150

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.