1
/***************************************************************************
2
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
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>
36
#include "MergeDocuments.h"
40
namespace sp = std::placeholders;
44
class XMLMergeReader : public Base::XMLReader
47
XMLMergeReader(std::map<std::string, std::string>& name, const char* FileName, std::istream& str)
48
: Base::XMLReader(FileName, str), nameMap(name)
51
void addName(const char* s1, const char* s2) override
55
const char* getName(const char* name) const override
57
std::map<std::string, std::string>::const_iterator it = nameMap.find(name);
58
if (it != nameMap.end())
59
return it->second.c_str();
63
bool doNameMapping() const override
71
std::map<std::string, std::string>& nameMap;
72
using PropertyTag = std::pair<std::string, std::string>;
73
std::stack<PropertyTag> propertyStack;
77
MergeDocuments::MergeDocuments(App::Document* doc) : appdoc(doc)
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);
88
MergeDocuments::~MergeDocuments()
90
connectExport.disconnect();
91
connectImport.disconnect();
94
unsigned int MergeDocuments::getMemSize () const
99
std::vector<App::DocumentObject*>
100
MergeDocuments::importObjects(std::istream& input)
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);
108
this->stream = nullptr;
113
void MergeDocuments::importObject(const std::vector<App::DocumentObject*>& o, Base::XMLReader & r)
116
for (auto it : objects) {
117
Gui::ViewProvider* vp = document->getViewProvider(it);
122
r.readFiles(*this->stream);
125
void MergeDocuments::exportObject(const std::vector<App::DocumentObject*>& o, Base::Writer & w)
131
void MergeDocuments::Save (Base::Writer & w) const
133
w.addFile("GuiDocument.xml", this);
136
void MergeDocuments::Restore(Base::XMLReader &r)
138
r.addFile("GuiDocument.xml", this);
141
void MergeDocuments::SaveDocFile (Base::Writer & w) const
143
document->exportObjects(objects, w);
146
void MergeDocuments::RestoreDocFile(Base::Reader & r)
148
document->importObjects(objects, r, nameMap);