1
/***************************************************************************
2
* Copyright (c) 2009 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"
27
# include <Inventor/SoDB.h>
28
# include <Inventor/SoFullPath.h>
29
# include <Inventor/SoInput.h>
30
# include <Inventor/actions/SoSearchAction.h>
31
# include <Inventor/nodes/SoSeparator.h>
32
# include <Inventor/nodes/SoTransform.h>
37
#include <Inventor/lists/SbStringList.h>
38
#include <Inventor/VRMLnodes/SoVRMLAnchor.h>
39
#include <Inventor/VRMLnodes/SoVRMLAudioClip.h>
40
#include <Inventor/VRMLnodes/SoVRMLBackground.h>
41
#include <Inventor/VRMLnodes/SoVRMLImageTexture.h>
42
#include <Inventor/VRMLnodes/SoVRMLInline.h>
43
#include <Inventor/VRMLnodes/SoVRMLMovieTexture.h>
44
#include <Inventor/VRMLnodes/SoVRMLScript.h>
46
#include <App/Document.h>
47
#include <App/VRMLObject.h>
48
#include <Base/Console.h>
49
#include <Base/FileInfo.h>
51
#include "ViewProviderVRMLObject.h"
52
#include "SoFCSelection.h"
57
PROPERTY_SOURCE(Gui::ViewProviderVRMLObject, Gui::ViewProviderDocumentObject)
59
ViewProviderVRMLObject::ViewProviderVRMLObject()
61
pcVRML = new SoFCSelection();
62
pcVRML->highlightMode = Gui::SoFCSelection::OFF;
63
pcVRML->selectionMode = Gui::SoFCSelection::SEL_OFF;
64
//pcVRML->style = Gui::SoFCSelection::BOX;
68
ViewProviderVRMLObject::~ViewProviderVRMLObject()
73
void ViewProviderVRMLObject::attach(App::DocumentObject *pcObj)
75
ViewProviderDocumentObject::attach(pcObj);
76
addDisplayMaskMode(pcVRML, "VRML");
77
pcVRML->objectName = pcObj->getNameInDocument();
78
pcVRML->documentName = pcObj->getDocument()->getName();
79
pcVRML->subElementName = "Main";
82
void ViewProviderVRMLObject::setDisplayMode(const char* ModeName)
84
if ( strcmp("VRML",ModeName)==0 )
85
setDisplayMaskMode("VRML");
86
ViewProviderDocumentObject::setDisplayMode( ModeName );
89
std::vector<std::string> ViewProviderVRMLObject::getDisplayModes() const
91
std::vector<std::string> StrList;
92
StrList.emplace_back("VRML");
97
void ViewProviderVRMLObject::getResourceFile(SoNode* node, std::list<std::string>& resources)
100
sa.setType(T::getClassTypeId());
101
sa.setInterest(SoSearchAction::ALL);
102
sa.setSearchingAll(true);
104
const SoPathList & pathlist = sa.getPaths();
105
for (int i = 0; i < pathlist.getLength(); i++ ) {
106
auto path = static_cast<SoFullPath *>(pathlist[i]);
107
if (path->getTail()->isOfType(T::getClassTypeId())) {
108
T * tex = static_cast<T*>(path->getTail());
109
for (int j = 0; j < tex->url.getNum(); j++) {
110
this->addResource(tex->url[j], resources);
117
// Special handling for SoVRMLBackground
119
void ViewProviderVRMLObject::getResourceFile<SoVRMLBackground>(SoNode* node, std::list<std::string>& resources)
122
sa.setType(SoVRMLBackground::getClassTypeId());
123
sa.setInterest(SoSearchAction::ALL);
124
sa.setSearchingAll(true);
126
const SoPathList & pathlist = sa.getPaths();
127
for (int i = 0; i < pathlist.getLength(); i++ ) {
128
auto path = static_cast<SoFullPath *>(pathlist[i]);
129
if (path->getTail()->isOfType(SoVRMLBackground::getClassTypeId())) {
130
auto vrml = static_cast<SoVRMLBackground*>(path->getTail());
132
for (int j = 0; j < vrml->backUrl.getNum(); j++) {
133
addResource(vrml->backUrl[j], resources);
136
for (int j = 0; j < vrml->bottomUrl.getNum(); j++) {
137
addResource(vrml->bottomUrl[j], resources);
140
for (int j = 0; j < vrml->frontUrl.getNum(); j++) {
141
addResource(vrml->frontUrl[j], resources);
144
for (int j = 0; j < vrml->leftUrl.getNum(); j++) {
145
addResource(vrml->leftUrl[j], resources);
148
for (int j = 0; j < vrml->rightUrl.getNum(); j++) {
149
addResource(vrml->rightUrl[j], resources);
152
for (int j = 0; j < vrml->topUrl.getNum(); j++) {
153
addResource(vrml->topUrl[j], resources);
161
void ViewProviderVRMLObject::addResource(const SbString& url, std::list<std::string>& resources)
163
SbString found = SoInput::searchForFile(url, SoInput::getDirectories(), SbStringList());
164
Base::FileInfo fi(found.getString());
166
// add the resource file if not yet listed
167
if (std::find(resources.begin(), resources.end(), found.getString()) == resources.end()) {
168
resources.emplace_back(found.getString());
173
void ViewProviderVRMLObject::getLocalResources(SoNode* node, std::list<std::string>& resources)
175
// search for SoVRMLInline files
177
sa.setType(SoVRMLInline::getClassTypeId());
178
sa.setInterest(SoSearchAction::ALL);
179
sa.setSearchingAll(true);
182
const SoPathList & pathlist = sa.getPaths();
183
for (int i = 0; i < pathlist.getLength(); i++ ) {
184
SoPath * path = pathlist[i];
185
auto vrml = static_cast<SoVRMLInline*>(path->getTail());
186
const SbString& url = vrml->getFullURLName();
187
if (url.getLength() > 0) {
188
// add the resource file if not yet listed
189
if (std::find(resources.begin(), resources.end(), url.getString()) == resources.end()) {
190
resources.emplace_back(url.getString());
193
// if the resource file could be loaded check if it references further resources
194
if (vrml->getChildData()) {
195
getLocalResources(vrml->getChildData(), resources);
200
// search for SoVRMLImageTexture, ... files
201
getResourceFile<SoVRMLImageTexture >(node, resources);
202
getResourceFile<SoVRMLMovieTexture >(node, resources);
203
getResourceFile<SoVRMLScript >(node, resources);
204
getResourceFile<SoVRMLBackground >(node, resources);
205
getResourceFile<SoVRMLAudioClip >(node, resources);
206
getResourceFile<SoVRMLAnchor >(node, resources);
209
void ViewProviderVRMLObject::updateData(const App::Property* prop)
211
auto ivObj = static_cast<App::VRMLObject*>(pcObject);
212
if (prop == &ivObj->VrmlFile) {
213
// read also from file
214
const char* filename = ivObj->VrmlFile.getValue();
215
QString fn = QString::fromUtf8(filename);
218
coinRemoveAllChildren(pcVRML);
219
if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
221
QByteArray filepath = fi.absolutePath().toUtf8();
222
QByteArray subpath = filepath + "/" + ivObj->getNameInDocument();
224
// Add this to the search path in order to read inline files
225
SoInput::addDirectoryFirst(filepath.constData());
226
SoInput::addDirectoryFirst(subpath.constData());
229
QByteArray buffer = file.readAll();
230
in.setBuffer((void *)buffer.constData(), buffer.length());
231
SoSeparator * node = SoDB::readAll(&in);
234
if (!checkRecursion(node)) {
235
Base::Console().Error("The VRML file causes an infinite recursion!\n");
238
pcVRML->addChild(node);
240
std::list<std::string> urls;
241
getLocalResources(node, urls);
242
if (!urls.empty() && ivObj->Urls.getSize() == 0) {
243
std::vector<std::string> res;
244
res.insert(res.end(), urls.begin(), urls.end());
245
ivObj->Urls.setValues(res);
248
SoInput::removeDirectory(filepath.constData());
249
SoInput::removeDirectory(subpath.constData());
252
else if (prop->isDerivedFrom(App::PropertyPlacement::getClassTypeId()) &&
253
strcmp(prop->getName(), "Placement") == 0) {
254
// Note: If R is the rotation, c the rotation center and t the translation
255
// vector then Inventor applies the following transformation: R*(x-c)+c+t
256
// In FreeCAD a placement only has a rotation and a translation part but
257
// no rotation center. This means that the following equation must be ful-
258
// filled: R * (x-c) + c + t = R * x + t
259
// <==> R * x + t - R * c + c = R * x + t
260
// <==> (I-R) * c = 0 ==> c = 0
261
// This means that the center point must be the origin!
262
Base::Placement p = static_cast<const App::PropertyPlacement*>(prop)->getValue();
263
auto q0 = (float)p.getRotation().getValue()[0];
264
auto q1 = (float)p.getRotation().getValue()[1];
265
auto q2 = (float)p.getRotation().getValue()[2];
266
auto q3 = (float)p.getRotation().getValue()[3];
267
auto px = (float)p.getPosition().x;
268
auto py = (float)p.getPosition().y;
269
auto pz = (float)p.getPosition().z;
270
pcTransform->rotation.setValue(q0,q1,q2,q3);
271
pcTransform->translation.setValue(px,py,pz);
272
pcTransform->center.setValue(0.0f,0.0f,0.0f);
273
pcTransform->scaleFactor.setValue(1.0f,1.0f,1.0f);