Debian Codegraph @

src/App/VRMLObject.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2009 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 "VRMLObject.h"
24 #include "Document.h"
25 #include "DocumentObjectPy.h"
26 #include <Base/FileInfo.h>
27 #include <Base/Reader.h>
28 #include <Base/Stream.h>
29 #include <Base/Tools.h>
30 #include <Base/Writer.h>
31
32
33 using namespace App;
34
35 PROPERTY_SOURCE(App::VRMLObject, App::GeoFeature)
36
37
38 VRMLObject::VRMLObject()
39 {
40 ADD_PROPERTY_TYPE(VrmlFile, (nullptr), "", Prop_None, "Included file with the VRML definition");
41 ADD_PROPERTY_TYPE(Urls,
42 (""),
43 "",
44 static_cast<PropertyType>(Prop_ReadOnly | Prop_Output | Prop_Transient),
45 "Resource files loaded by the VRML file");
46 ADD_PROPERTY_TYPE(Resources,
47 (""),
48 "",
49 static_cast<PropertyType>(Prop_ReadOnly | Prop_Output),
50 "Resource files loaded by the VRML file");
51 Urls.setSize(0);
52 Resources.setSize(0);
53 }
54
55 short VRMLObject::mustExecute() const
56 {
57 return 0;
58 }
59
60 void VRMLObject::onChanged(const App::Property* prop)
61 {
62 if (restoreData) {
63 return;
64 }
65
66 if (prop == &VrmlFile) {
67 std::string orig = VrmlFile.getOriginalFileName();
68 if (!orig.empty()) {
69 // store the path name of the VRML file
70 Base::FileInfo fi(orig);
71 this->vrmlPath = fi.dirPath();
72 }
73 }
74 else if (prop == &Urls) {
75 // save the relative paths to the resource files in the project file
76 Resources.setSize(Urls.getSize());
77 const std::vector<std::string>& urls = Urls.getValues();
78 int index = 0;
79 for (const auto& it : urls) {
80 std::string output = getRelativePath(this->vrmlPath, it);
81 Resources.set1Value(index, output);
82 ++index;
83 }
84 }
85 GeoFeature::onChanged(prop);
86 }
87
88 PyObject* VRMLObject::getPyObject()
89 {
90 if (PythonObject.is(Py::_None())) {
91 // ref counter is set to 1
92 PythonObject = Py::Object(new DocumentObjectPy(this), true);
93 }
94 return Py::new_reference_to(PythonObject);
95 }
96
97 std::string VRMLObject::getRelativePath(const std::string& prefix,
98 const std::string& resource) const
99 {
100 std::string str;
101 std::string intname = this->getNameInDocument();
102 if (!prefix.empty()) {
103 if (resource.substr(0, prefix.size()) == prefix) {
104 std::string suffix = resource.substr(prefix.size());
105 str = intname + suffix;
106 }
107 }
108
109 if (str.empty()) {
110 Base::FileInfo fi(resource);
111 str = intname + "/" + fi.fileName();
112 }
113
114 return str;
115 }
116
117 std::string VRMLObject::fixRelativePath(const std::string& name, const std::string& resource)
118 {
119 // the part before the first '/' must match with object's internal name
120 std::string::size_type pos = resource.find('/');
121 if (pos != std::string::npos) {
122 std::string prefix = resource.substr(0, pos);
123 std::string suffix = resource.substr(pos);
124 if (prefix != name) {
125 return name + suffix;
126 }
127 }
128 return resource;
129 }
130
131 void VRMLObject::makeDirectories(const std::string& path, const std::string& subdir)
132 {
133 std::string::size_type pos = subdir.find('/');
134 while (pos != std::string::npos) {
135 std::string sub = subdir.substr(0, pos);
136 std::string dir = path;
137 dir += '/';
138 dir += sub;
139 Base::FileInfo fi(dir);
140 if (!fi.createDirectory()) {
141 break;
142 }
143 pos = subdir.find('/', pos + 1);
144 }
145 }
146
147 void VRMLObject::Save(Base::Writer& writer) const
148 {
149 App::GeoFeature::Save(writer);
150
151 // save also the inline files if there
152 const std::vector<std::string>& urls = Resources.getValues();
153 for (const auto& url : urls) {
154 writer.addFile(url.c_str(), this);
155 }
156
157 this->indexSave = 0;
158 }
159
160 void VRMLObject::Restore(Base::XMLReader& reader)
161 {
162 App::GeoFeature::Restore(reader);
163 Urls.setSize(Resources.getSize());
164
165 // restore also the inline files if there
166 const std::vector<std::string>& urls = Resources.getValues();
167 for (const auto& url : urls) {
168 reader.addFile(url.c_str(), this);
169 }
170
171 this->indexRestore = 0;
172 }
173
174 void VRMLObject::SaveDocFile(Base::Writer& writer) const
175 {
176 // store the inline files of the VRML file
177 if (this->indexSave < Urls.getSize()) {
178 std::string url = Urls[this->indexSave];
179
180 Base::FileInfo fi(url);
181 // it can happen that the transient directory has changed after
182 // saving the 'URLs' in RestoreDocFile() and then we have to
183 // try again with the new transient directory.
184 if (!fi.exists()) {
185 std::string path = getDocument()->TransientDir.getValue();
186 url = Resources[this->indexSave];
187 url = path + "/" + url;
188 fi.setFile(url);
189 }
190
191 this->indexSave++;
192 Base::ifstream file(fi, std::ios::in | std::ios::binary);
193 if (file) {
194 writer.Stream() << file.rdbuf();
195 }
196 }
197 }
198
199 bool VRMLObject::restoreTextureFinished(Base::Reader& reader)
200 {
201 Base::StateLocker locker(restoreData, true);
202 if (this->indexRestore < Resources.getSize()) {
203 std::string path = getDocument()->TransientDir.getValue();
204 std::string url = Resources[this->indexRestore];
205 std::string intname = this->getNameInDocument();
206 url = fixRelativePath(intname, url);
207 Resources.set1Value(this->indexRestore, url);
208 makeDirectories(path, url);
209
210 url = path + "/" + url;
211 Base::FileInfo fi(url);
212 Urls.set1Value(this->indexRestore, url);
213 this->indexRestore++;
214
215 Base::ofstream file(fi, std::ios::out | std::ios::binary);
216 if (file) {
217 reader >> file.rdbuf();
218 file.close();
219 }
220
221 return (this->indexRestore == Urls.getSize());
222 }
223
224 return false;
225 }
226
227 void VRMLObject::reloadFile()
228 {
229 // after restoring all inline files reload the VRML file
230 VrmlFile.touch();
231 Base::FileInfo fi(VrmlFile.getValue());
232 this->vrmlPath = fi.dirPath();
233 }
234
235 void VRMLObject::RestoreDocFile(Base::Reader& reader)
236 {
237 if (restoreTextureFinished(reader)) {
238 reloadFile();
239 }
240 }