FreeCAD

Форк
0
/
ViewProviderVRMLObject.cpp 
275 строк · 11.0 Кб
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 "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <sstream>
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>
33
# include <QFile>
34
# include <QFileInfo>
35
#endif
36

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>
45

46
#include <App/Document.h>
47
#include <App/VRMLObject.h>
48
#include <Base/Console.h>
49
#include <Base/FileInfo.h>
50

51
#include "ViewProviderVRMLObject.h"
52
#include "SoFCSelection.h"
53

54

55
using namespace Gui;
56

57
PROPERTY_SOURCE(Gui::ViewProviderVRMLObject, Gui::ViewProviderDocumentObject)
58

59
ViewProviderVRMLObject::ViewProviderVRMLObject()
60
{
61
    pcVRML = new SoFCSelection();
62
    pcVRML->highlightMode = Gui::SoFCSelection::OFF;
63
    pcVRML->selectionMode = Gui::SoFCSelection::SEL_OFF;
64
    //pcVRML->style = Gui::SoFCSelection::BOX;
65
    pcVRML->ref();
66
}
67

68
ViewProviderVRMLObject::~ViewProviderVRMLObject()
69
{
70
    pcVRML->unref();
71
}
72

73
void ViewProviderVRMLObject::attach(App::DocumentObject *pcObj)
74
{
75
    ViewProviderDocumentObject::attach(pcObj);
76
    addDisplayMaskMode(pcVRML, "VRML");
77
    pcVRML->objectName = pcObj->getNameInDocument();
78
    pcVRML->documentName = pcObj->getDocument()->getName();
79
    pcVRML->subElementName = "Main";
80
}
81

82
void ViewProviderVRMLObject::setDisplayMode(const char* ModeName)
83
{
84
    if ( strcmp("VRML",ModeName)==0 )
85
        setDisplayMaskMode("VRML");
86
    ViewProviderDocumentObject::setDisplayMode( ModeName );
87
}
88

89
std::vector<std::string> ViewProviderVRMLObject::getDisplayModes() const
90
{
91
    std::vector<std::string> StrList;
92
    StrList.emplace_back("VRML");
93
    return StrList;
94
}
95

96
template<typename T>
97
void ViewProviderVRMLObject::getResourceFile(SoNode* node, std::list<std::string>& resources)
98
{
99
    SoSearchAction sa;
100
    sa.setType(T::getClassTypeId());
101
    sa.setInterest(SoSearchAction::ALL);
102
    sa.setSearchingAll(true);
103
    sa.apply(node);
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);
111
            }
112
        }
113
    }
114
}
115

116
namespace Gui {
117
// Special handling for SoVRMLBackground
118
template<>
119
void ViewProviderVRMLObject::getResourceFile<SoVRMLBackground>(SoNode* node, std::list<std::string>& resources)
120
{
121
    SoSearchAction sa;
122
    sa.setType(SoVRMLBackground::getClassTypeId());
123
    sa.setInterest(SoSearchAction::ALL);
124
    sa.setSearchingAll(true);
125
    sa.apply(node);
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());
131
            // backUrl
132
            for (int j = 0; j < vrml->backUrl.getNum(); j++) {
133
                addResource(vrml->backUrl[j], resources);
134
            }
135
            // bottomUrl
136
            for (int j = 0; j < vrml->bottomUrl.getNum(); j++) {
137
                addResource(vrml->bottomUrl[j], resources);
138
            }
139
            // frontUrl
140
            for (int j = 0; j < vrml->frontUrl.getNum(); j++) {
141
                addResource(vrml->frontUrl[j], resources);
142
            }
143
            // leftUrl
144
            for (int j = 0; j < vrml->leftUrl.getNum(); j++) {
145
                addResource(vrml->leftUrl[j], resources);
146
            }
147
            // rightUrl
148
            for (int j = 0; j < vrml->rightUrl.getNum(); j++) {
149
                addResource(vrml->rightUrl[j], resources);
150
            }
151
            // topUrl
152
            for (int j = 0; j < vrml->topUrl.getNum(); j++) {
153
                addResource(vrml->topUrl[j], resources);
154
            }
155
        }
156
    }
157
}
158

159
}
160

161
void ViewProviderVRMLObject::addResource(const SbString& url, std::list<std::string>& resources)
162
{
163
    SbString found = SoInput::searchForFile(url, SoInput::getDirectories(), SbStringList());
164
    Base::FileInfo fi(found.getString());
165
    if (fi.exists()) {
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());
169
        }
170
    }
171
}
172

173
void ViewProviderVRMLObject::getLocalResources(SoNode* node, std::list<std::string>& resources)
174
{
175
    // search for SoVRMLInline files
176
    SoSearchAction sa;
177
    sa.setType(SoVRMLInline::getClassTypeId());
178
    sa.setInterest(SoSearchAction::ALL);
179
    sa.setSearchingAll(true);
180
    sa.apply(node);
181

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());
191
            }
192

193
            // if the resource file could be loaded check if it references further resources
194
            if (vrml->getChildData()) {
195
                getLocalResources(vrml->getChildData(), resources);
196
            }
197
        }
198
    }
199

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);
207
}
208

209
void ViewProviderVRMLObject::updateData(const App::Property* prop)
210
{
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);
216
        QFile file(fn);
217
        SoInput in;
218
        coinRemoveAllChildren(pcVRML);
219
        if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
220
            QFileInfo fi(fn);
221
            QByteArray filepath = fi.absolutePath().toUtf8();
222
            QByteArray subpath = filepath + "/" + ivObj->getNameInDocument();
223

224
            // Add this to the search path in order to read inline files
225
            SoInput::addDirectoryFirst(filepath.constData());
226
            SoInput::addDirectoryFirst(subpath.constData());
227

228
            // Read in the file
229
            QByteArray buffer = file.readAll();
230
            in.setBuffer((void *)buffer.constData(), buffer.length());
231
            SoSeparator * node = SoDB::readAll(&in);
232

233
            if (node) {
234
                if (!checkRecursion(node)) {
235
                    Base::Console().Error("The VRML file causes an infinite recursion!\n");
236
                    return;
237
                }
238
                pcVRML->addChild(node);
239

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);
246
                }
247
            }
248
            SoInput::removeDirectory(filepath.constData());
249
            SoInput::removeDirectory(subpath.constData());
250
        }
251
    }
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);
274
    }
275
}
276

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

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

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

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