Debian Codegraph @

src/Gui/ViewProviderVRMLObject.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
24 #include <sstream>
25 #include <Inventor/SoDB.h>
26 #include <Inventor/SoFullPath.h>
27 #include <Inventor/SoInput.h>
28 #include <Inventor/actions/SoSearchAction.h>
29 #include <Inventor/nodes/SoSeparator.h>
30 #include <Inventor/nodes/SoTransform.h>
31 #include <QFile>
32 #include <QFileInfo>
33
34
35 #include <Inventor/lists/SbStringList.h>
36 #include <Inventor/VRMLnodes/SoVRMLAnchor.h>
37 #include <Inventor/VRMLnodes/SoVRMLAudioClip.h>
38 #include <Inventor/VRMLnodes/SoVRMLBackground.h>
39 #include <Inventor/VRMLnodes/SoVRMLImageTexture.h>
40 #include <Inventor/VRMLnodes/SoVRMLInline.h>
41 #include <Inventor/VRMLnodes/SoVRMLMovieTexture.h>
42 #include <Inventor/VRMLnodes/SoVRMLScript.h>
43
44 #include <App/Document.h>
45 #include <App/VRMLObject.h>
46 #include <Base/Console.h>
47 #include <Base/FileInfo.h>
48
49 #include "ViewProviderVRMLObject.h"
50 #include "SoFCSelection.h"
51
52
53 using namespace Gui;
54
55 PROPERTY_SOURCE(Gui::ViewProviderVRMLObject, Gui::ViewProviderDocumentObject)
56
57 ViewProviderVRMLObject::ViewProviderVRMLObject()
58 {
59 pcVRML = new SoFCSelection();
60 pcVRML->preselectionMode = Gui::SoFCSelection::OFF;
61 pcVRML->selectionMode = Gui::SoFCSelection::SEL_OFF;
62 // pcVRML->style = Gui::SoFCSelection::BOX;
63 pcVRML->ref();
64 }
65
66 ViewProviderVRMLObject::~ViewProviderVRMLObject()
67 {
68 pcVRML->unref();
69 }
70
71 void ViewProviderVRMLObject::attach(App::DocumentObject* pcObj)
72 {
73 ViewProviderDocumentObject::attach(pcObj);
74 addDisplayMaskMode(pcVRML, "VRML");
75 pcVRML->objectName = pcObj->getNameInDocument();
76 pcVRML->documentName = pcObj->getDocument()->getName();
77 pcVRML->subElementName = "Main";
78 }
79
80 void ViewProviderVRMLObject::setDisplayMode(const char* ModeName)
81 {
82 if (strcmp("VRML", ModeName) == 0) {
83 setDisplayMaskMode("VRML");
84 }
85 ViewProviderDocumentObject::setDisplayMode(ModeName);
86 }
87
88 std::vector<std::string> ViewProviderVRMLObject::getDisplayModes() const
89 {
90 std::vector<std::string> StrList;
91 StrList.emplace_back("VRML");
92 return StrList;
93 }
94
95 template<typename T>
96 void ViewProviderVRMLObject::getResourceFile(SoNode* node, std::list<std::string>& resources)
97 {
98 SoSearchAction sa;
99 sa.setType(T::getClassTypeId());
100 sa.setInterest(SoSearchAction::ALL);
101 sa.setSearchingAll(true);
102 sa.apply(node);
103 const SoPathList& pathlist = sa.getPaths();
104 for (int i = 0; i < pathlist.getLength(); i++) {
105 auto path = static_cast<SoFullPath*>(pathlist[i]);
106 if (path->getTail()->isOfType(T::getClassTypeId())) {
107 T* tex = static_cast<T*>(path->getTail());
108 for (int j = 0; j < tex->url.getNum(); j++) {
109 this->addResource(tex->url[j], resources);
110 }
111 }
112 }
113 }
114
115 namespace Gui
116 {
117 // Special handling for SoVRMLBackground
118 template<>
119 void ViewProviderVRMLObject::getResourceFile<SoVRMLBackground>(
120 SoNode* node,
121 std::list<std::string>& resources
122 )
123 {
124 SoSearchAction sa;
125 sa.setType(SoVRMLBackground::getClassTypeId());
126 sa.setInterest(SoSearchAction::ALL);
127 sa.setSearchingAll(true);
128 sa.apply(node);
129 const SoPathList& pathlist = sa.getPaths();
130 for (int i = 0; i < pathlist.getLength(); i++) {
131 auto path = static_cast<SoFullPath*>(pathlist[i]);
132 if (path->getTail()->isOfType(SoVRMLBackground::getClassTypeId())) {
133 auto vrml = static_cast<SoVRMLBackground*>(path->getTail());
134 // backUrl
135 for (int j = 0; j < vrml->backUrl.getNum(); j++) {
136 addResource(vrml->backUrl[j], resources);
137 }
138 // bottomUrl
139 for (int j = 0; j < vrml->bottomUrl.getNum(); j++) {
140 addResource(vrml->bottomUrl[j], resources);
141 }
142 // frontUrl
143 for (int j = 0; j < vrml->frontUrl.getNum(); j++) {
144 addResource(vrml->frontUrl[j], resources);
145 }
146 // leftUrl
147 for (int j = 0; j < vrml->leftUrl.getNum(); j++) {
148 addResource(vrml->leftUrl[j], resources);
149 }
150 // rightUrl
151 for (int j = 0; j < vrml->rightUrl.getNum(); j++) {
152 addResource(vrml->rightUrl[j], resources);
153 }
154 // topUrl
155 for (int j = 0; j < vrml->topUrl.getNum(); j++) {
156 addResource(vrml->topUrl[j], resources);
157 }
158 }
159 }
160 }
161
162 } // namespace Gui
163
164 void ViewProviderVRMLObject::addResource(const SbString& url, std::list<std::string>& resources)
165 {
166 SbString found = SoInput::searchForFile(url, SoInput::getDirectories(), SbStringList());
167 Base::FileInfo fi(found.getString());
168 if (fi.exists()) {
169 // add the resource file if not yet listed
170 if (std::ranges::find(resources, found.getString()) == resources.end()) {
171 resources.emplace_back(found.getString());
172 }
173 }
174 }
175
176 void ViewProviderVRMLObject::getLocalResources(SoNode* node, std::list<std::string>& resources)
177 {
178 // search for SoVRMLInline files
179 SoSearchAction sa;
180 sa.setType(SoVRMLInline::getClassTypeId());
181 sa.setInterest(SoSearchAction::ALL);
182 sa.setSearchingAll(true);
183 sa.apply(node);
184
185 const SoPathList& pathlist = sa.getPaths();
186 for (int i = 0; i < pathlist.getLength(); i++) {
187 SoPath* path = pathlist[i];
188 auto vrml = static_cast<SoVRMLInline*>(path->getTail());
189 const SbString& url = vrml->getFullURLName();
190 if (url.getLength() > 0) {
191 // add the resource file if not yet listed
192 if (std::ranges::find(resources, url.getString()) == resources.end()) {
193 resources.emplace_back(url.getString());
194 }
195
196 // if the resource file could be loaded check if it references further resources
197 if (vrml->getChildData()) {
198 getLocalResources(vrml->getChildData(), resources);
199 }
200 }
201 }
202
203 // search for SoVRMLImageTexture, ... files
204 getResourceFile<SoVRMLImageTexture>(node, resources);
205 getResourceFile<SoVRMLMovieTexture>(node, resources);
206 getResourceFile<SoVRMLScript>(node, resources);
207 getResourceFile<SoVRMLBackground>(node, resources);
208 getResourceFile<SoVRMLAudioClip>(node, resources);
209 getResourceFile<SoVRMLAnchor>(node, resources);
210 }
211
212 void ViewProviderVRMLObject::updateData(const App::Property* prop)
213 {
214 auto ivObj = static_cast<App::VRMLObject*>(pcObject);
215 if (prop == &ivObj->VrmlFile) {
216 // read also from file
217 const char* filename = ivObj->VrmlFile.getValue();
218 QString fn = QString::fromUtf8(filename);
219 QFile file(fn);
220 SoInput in;
221 coinRemoveAllChildren(pcVRML);
222 if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
223 QFileInfo fi(fn);
224 QByteArray filepath = fi.absolutePath().toUtf8();
225 QByteArray subpath = filepath + "/" + ivObj->getNameInDocument();
226
227 // Add this to the search path in order to read inline files
228 SoInput::addDirectoryFirst(filepath.constData());
229 SoInput::addDirectoryFirst(subpath.constData());
230
231 // Read in the file
232 QByteArray buffer = file.readAll();
233 in.setBuffer((void*)buffer.constData(), buffer.length());
234 SoSeparator* node = SoDB::readAll(&in);
235
236 if (node) {
237 if (!checkRecursion(node)) {
238 Base::Console().error("The VRML file causes an infinite recursion!\n");
239 return;
240 }
241 pcVRML->addChild(node);
242
243 std::list<std::string> urls;
244 getLocalResources(node, urls);
245 if (!urls.empty() && ivObj->Urls.getSize() == 0) {
246 std::vector<std::string> res;
247 res.insert(res.end(), urls.begin(), urls.end());
248 ivObj->Urls.setValues(res);
249 }
250 }
251 SoInput::removeDirectory(filepath.constData());
252 SoInput::removeDirectory(subpath.constData());
253 }
254 }
255 else if (prop->isDerivedFrom<App::PropertyPlacement>()
256 && strcmp(prop->getName(), "Placement") == 0) {
257 // Note: If R is the rotation, c the rotation center and t the translation
258 // vector then Inventor applies the following transformation: R*(x-c)+c+t
259 // In FreeCAD a placement only has a rotation and a translation part but
260 // no rotation center. This means that the following equation must be ful-
261 // filled: R * (x-c) + c + t = R * x + t
262 // <==> R * x + t - R * c + c = R * x + t
263 // <==> (I-R) * c = 0 ==> c = 0
264 // This means that the center point must be the origin!
265 Base::Placement p = static_cast<const App::PropertyPlacement*>(prop)->getValue();
266 auto q0 = (float)p.getRotation().getValue()[0];
267 auto q1 = (float)p.getRotation().getValue()[1];
268 auto q2 = (float)p.getRotation().getValue()[2];
269 auto q3 = (float)p.getRotation().getValue()[3];
270 auto px = (float)p.getPosition().x;
271 auto py = (float)p.getPosition().y;
272 auto pz = (float)p.getPosition().z;
273 pcTransform->rotation.setValue(q0, q1, q2, q3);
274 pcTransform->translation.setValue(px, py, pz);
275 pcTransform->center.setValue(0.0f, 0.0f, 0.0f);
276 pcTransform->scaleFactor.setValue(1.0f, 1.0f, 1.0f);
277 }
278 }