| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 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 <Inventor/SoDB.h> |
| 25 |
#include <Inventor/SoInput.h> |
| 26 |
#include <Inventor/nodes/SoSeparator.h> |
| 27 |
#include <Inventor/nodes/SoTransform.h> |
| 28 |
#include <QFile> |
| 29 |
|
| 30 |
|
| 31 |
#include <App/Document.h> |
| 32 |
#include <App/InventorObject.h> |
| 33 |
|
| 34 |
#include "ViewProviderInventorObject.h" |
| 35 |
#include "SoFCSelection.h" |
| 36 |
|
| 37 |
|
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
PROPERTY_SOURCE(Gui::ViewProviderInventorObject, Gui::ViewProviderDocumentObject) |
| 41 |
|
| 42 |
ViewProviderInventorObject::ViewProviderInventorObject() |
| 43 |
{ |
| 44 |
pcBuffer = new SoSeparator(); |
| 45 |
pcBuffer->ref(); |
| 46 |
pcFile = new SoSeparator(); |
| 47 |
pcFile->ref(); |
| 48 |
} |
| 49 |
|
| 50 |
ViewProviderInventorObject::~ViewProviderInventorObject() |
| 51 |
{ |
| 52 |
pcBuffer->unref(); |
| 53 |
pcFile->unref(); |
| 54 |
} |
| 55 |
|
| 56 |
void ViewProviderInventorObject::attach(App::DocumentObject* pcObj) |
| 57 |
{ |
| 58 |
ViewProviderDocumentObject::attach(pcObj); |
| 59 |
auto pcFileBuf = new SoGroup(); |
| 60 |
pcFileBuf->addChild(pcBuffer); |
| 61 |
pcFileBuf->addChild(pcFile); |
| 62 |
addDisplayMaskMode(pcFileBuf, "FileBuffer"); |
| 63 |
addDisplayMaskMode(pcBuffer, "Buffer"); |
| 64 |
addDisplayMaskMode(pcFile, "File"); |
| 65 |
} |
| 66 |
|
| 67 |
void ViewProviderInventorObject::setDisplayMode(const char* ModeName) |
| 68 |
{ |
| 69 |
if (strcmp("File+Buffer", ModeName) == 0) { |
| 70 |
setDisplayMaskMode("FileBuffer"); |
| 71 |
} |
| 72 |
else if (strcmp("Buffer", ModeName) == 0) { |
| 73 |
setDisplayMaskMode("Buffer"); |
| 74 |
} |
| 75 |
else if (strcmp("File", ModeName) == 0) { |
| 76 |
setDisplayMaskMode("File"); |
| 77 |
} |
| 78 |
ViewProviderDocumentObject::setDisplayMode(ModeName); |
| 79 |
} |
| 80 |
|
| 81 |
std::vector<std::string> ViewProviderInventorObject::getDisplayModes() const |
| 82 |
{ |
| 83 |
std::vector<std::string> StrList; |
| 84 |
StrList.emplace_back("File+Buffer"); |
| 85 |
StrList.emplace_back("Buffer"); |
| 86 |
StrList.emplace_back("File"); |
| 87 |
return StrList; |
| 88 |
} |
| 89 |
|
| 90 |
void ViewProviderInventorObject::updateData(const App::Property* prop) |
| 91 |
{ |
| 92 |
auto ivObj = static_cast<App::InventorObject*>(pcObject); |
| 93 |
if (prop == &ivObj->Buffer) { |
| 94 |
// read from buffer |
| 95 |
SoInput in; |
| 96 |
std::string buffer = ivObj->Buffer.getValue(); |
| 97 |
coinRemoveAllChildren(pcBuffer); |
| 98 |
if (buffer.empty()) { |
| 99 |
return; |
| 100 |
} |
| 101 |
in.setBuffer((void*)buffer.c_str(), buffer.size()); |
| 102 |
SoSeparator* node = SoDB::readAll(&in); |
| 103 |
if (node) { |
| 104 |
const char* doc = this->pcObject->getDocument()->getName(); |
| 105 |
const char* obj = this->pcObject->getNameInDocument(); |
| 106 |
adjustSelectionNodes(node, doc, obj); |
| 107 |
pcBuffer->addChild(node); |
| 108 |
} |
| 109 |
} |
| 110 |
else if (prop == &ivObj->FileName) { |
| 111 |
// read also from file |
| 112 |
const char* filename = ivObj->FileName.getValue(); |
| 113 |
QString fn = QString::fromUtf8(filename); |
| 114 |
QFile file(fn); |
| 115 |
SoInput in; |
| 116 |
coinRemoveAllChildren(pcFile); |
| 117 |
if (!fn.isEmpty() && file.open(QFile::ReadOnly)) { |
| 118 |
QByteArray buffer = file.readAll(); |
| 119 |
in.setBuffer((void*)buffer.constData(), buffer.length()); |
| 120 |
SoSeparator* node = SoDB::readAll(&in); |
| 121 |
if (node) { |
| 122 |
const char* doc = this->pcObject->getDocument()->getName(); |
| 123 |
const char* obj = this->pcObject->getNameInDocument(); |
| 124 |
adjustSelectionNodes(node, doc, obj); |
| 125 |
pcFile->addChild(node); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
else if (prop->isDerivedFrom<App::PropertyPlacement>() |
| 130 |
&& strcmp(prop->getName(), "Placement") == 0) { |
| 131 |
// Note: If R is the rotation, c the rotation center and t the translation |
| 132 |
// vector then Inventor applies the following transformation: R*(x-c)+c+t |
| 133 |
// In FreeCAD a placement only has a rotation and a translation part but |
| 134 |
// no rotation center. This means that the following equation must be ful- |
| 135 |
// filled: R * (x-c) + c + t = R * x + t |
| 136 |
// <==> R * x + t - R * c + c = R * x + t |
| 137 |
// <==> (I-R) * c = 0 ==> c = 0 |
| 138 |
// This means that the center point must be the origin! |
| 139 |
Base::Placement p = static_cast<const App::PropertyPlacement*>(prop)->getValue(); |
| 140 |
auto q0 = (float)p.getRotation().getValue()[0]; |
| 141 |
auto q1 = (float)p.getRotation().getValue()[1]; |
| 142 |
auto q2 = (float)p.getRotation().getValue()[2]; |
| 143 |
auto q3 = (float)p.getRotation().getValue()[3]; |
| 144 |
auto px = (float)p.getPosition().x; |
| 145 |
auto py = (float)p.getPosition().y; |
| 146 |
auto pz = (float)p.getPosition().z; |
| 147 |
pcTransform->rotation.setValue(q0, q1, q2, q3); |
| 148 |
pcTransform->translation.setValue(px, py, pz); |
| 149 |
pcTransform->center.setValue(0.0f, 0.0f, 0.0f); |
| 150 |
pcTransform->scaleFactor.setValue(1.0f, 1.0f, 1.0f); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void ViewProviderInventorObject::adjustSelectionNodes(SoNode* child, const char* docname, const char* objname) |
| 155 |
{ |
| 156 |
if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) { |
| 157 |
static_cast<SoFCSelection*>(child)->documentName = docname; |
| 158 |
static_cast<SoFCSelection*>(child)->objectName = objname; |
| 159 |
} |
| 160 |
else if (child->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) { |
| 161 |
auto group = static_cast<SoGroup*>(child); |
| 162 |
for (int i = 0; i < group->getNumChildren(); i++) { |
| 163 |
SoNode* subchild = group->getChild(i); |
| 164 |
adjustSelectionNodes(subchild, docname, objname); |
| 165 |
} |
| 166 |
} |
| 167 |
} |