+ fixes #0002029: VRML inline ignored

This commit is contained in:
wmayer
2015-03-28 14:51:03 +01:00
parent 80956aef8d
commit 9751d36fae
4 changed files with 250 additions and 4 deletions

View File

@@ -26,12 +26,24 @@
#ifndef _PreComp_
# include <Inventor/SoDB.h>
# include <Inventor/SoInput.h>
# include <Inventor/SoFullPath.h>
# include <Inventor/actions/SoSearchAction.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoTransform.h>
# include <QFile>
# include <QFileInfo>
#endif
#include <Inventor/nodes/SoFile.h>
#include <Inventor/VRMLnodes/SoVRMLInline.h>
#include <Inventor/VRMLnodes/SoVRMLImageTexture.h>
#include <Inventor/VRMLnodes/SoVRMLMovieTexture.h>
#include <Inventor/VRMLnodes/SoVRMLScript.h>
#include <Inventor/VRMLnodes/SoVRMLBackground.h>
#include <Inventor/VRMLnodes/SoVRMLAudioClip.h>
#include <Inventor/VRMLnodes/SoVRMLAnchor.h>
#include <Inventor/lists/SbStringList.h>
#include "ViewProviderVRMLObject.h"
#include "SoFCSelection.h"
#include <App/VRMLObject.h>
@@ -81,6 +93,116 @@ std::vector<std::string> ViewProviderVRMLObject::getDisplayModes(void) const
return StrList;
}
template<typename T>
void ViewProviderVRMLObject::getResourceFile(SoNode* node, std::list<std::string>& resources)
{
SoSearchAction sa;
sa.setType(T::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoFullPath * path = static_cast<SoFullPath *>(pathlist[i]);
if (path->getTail()->isOfType(T::getClassTypeId())) {
T * tex = static_cast<T*>(path->getTail());
for (int j = 0; j < tex->url.getNum(); j++) {
this->addResource(tex->url[j], resources);
}
}
}
}
// Special handling for SoVRMLBackground
template<>
void ViewProviderVRMLObject::getResourceFile<SoVRMLBackground>(SoNode* node, std::list<std::string>& resources)
{
SoSearchAction sa;
sa.setType(SoVRMLBackground::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoFullPath * path = static_cast<SoFullPath *>(pathlist[i]);
if (path->getTail()->isOfType(SoVRMLBackground::getClassTypeId())) {
SoVRMLBackground * vrml = static_cast<SoVRMLBackground*>(path->getTail());
// backUrl
for (int j = 0; j < vrml->backUrl.getNum(); j++) {
addResource(vrml->backUrl[j], resources);
}
// bottomUrl
for (int j = 0; j < vrml->bottomUrl.getNum(); j++) {
addResource(vrml->bottomUrl[j], resources);
}
// frontUrl
for (int j = 0; j < vrml->frontUrl.getNum(); j++) {
addResource(vrml->frontUrl[j], resources);
}
// leftUrl
for (int j = 0; j < vrml->leftUrl.getNum(); j++) {
addResource(vrml->leftUrl[j], resources);
}
// rightUrl
for (int j = 0; j < vrml->rightUrl.getNum(); j++) {
addResource(vrml->rightUrl[j], resources);
}
// topUrl
for (int j = 0; j < vrml->topUrl.getNum(); j++) {
addResource(vrml->topUrl[j], resources);
}
}
}
}
void ViewProviderVRMLObject::addResource(const SbString& url, std::list<std::string>& resources)
{
SbString found = SoInput::searchForFile(url, SoInput::getDirectories(), SbStringList());
Base::FileInfo fi(found.getString());
if (fi.exists()) {
// add the resource file if not yet listed
if (std::find(resources.begin(), resources.end(), found.getString()) == resources.end()) {
resources.push_back(found.getString());
}
}
}
void ViewProviderVRMLObject::getLocalResources(SoNode* node, std::list<std::string>& resources)
{
// search for SoVRMLInline files
SoSearchAction sa;
sa.setType(SoVRMLInline::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoPath * path = pathlist[i];
SoVRMLInline * vrml = static_cast<SoVRMLInline*>(path->getTail());
const SbString& url = vrml->getFullURLName();
if (url.getLength() > 0) {
// add the resource file if not yet listed
if (std::find(resources.begin(), resources.end(), url.getString()) == resources.end()) {
resources.push_back(url.getString());
}
// if the resource file could be loaded check if it references further resources
if (vrml->getChildData()) {
getLocalResources(vrml->getChildData(), resources);
}
}
}
// search for SoVRMLImageTexture, ... files
getResourceFile<SoVRMLImageTexture >(node, resources);
getResourceFile<SoVRMLMovieTexture >(node, resources);
getResourceFile<SoVRMLScript >(node, resources);
getResourceFile<SoVRMLBackground >(node, resources);
getResourceFile<SoVRMLAudioClip >(node, resources);
getResourceFile<SoVRMLAnchor >(node, resources);
}
void ViewProviderVRMLObject::updateData(const App::Property* prop)
{
App::VRMLObject* ivObj = static_cast<App::VRMLObject*>(pcObject);
@@ -93,14 +215,31 @@ void ViewProviderVRMLObject::updateData(const App::Property* prop)
pcVRML->removeAllChildren();
if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
QFileInfo fi(fn);
QByteArray path = fi.absolutePath().toUtf8();
QByteArray filepath = fi.absolutePath().toUtf8();
QByteArray subpath = filepath + "/" + ivObj->getNameInDocument();
// Add this to the search path in order to read inline files
SoInput::addDirectoryFirst(path.constData());
SoInput::addDirectoryFirst(filepath.constData());
SoInput::addDirectoryFirst(subpath.constData());
// Read in the file
QByteArray buffer = file.readAll();
in.setBuffer((void *)buffer.constData(), buffer.length());
SoSeparator * node = SoDB::readAll(&in);
if (node) pcVRML->addChild(node);
SoInput::removeDirectory(path.constData());
if (node) {
pcVRML->addChild(node);
std::list<std::string> urls;
getLocalResources(node, urls);
if (!urls.empty() && ivObj->Urls.getSize() == 0) {
std::vector<std::string> res;
res.insert(res.end(), urls.begin(), urls.end());
ivObj->Urls.setValues(res);
}
}
SoInput::removeDirectory(filepath.constData());
SoInput::removeDirectory(subpath.constData());
}
}
else if (prop->isDerivedFrom(App::PropertyPlacement::getClassTypeId()) &&