| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Jürgen Riegel <[email protected]> * |
| 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 <cstring> |
| 25 |
#include <Inventor/nodes/SoSeparator.h> |
| 26 |
#include <Inventor/nodes/SoSwitch.h> |
| 27 |
|
| 28 |
|
| 29 |
#include <Base/Exception.h> |
| 30 |
#include <Base/Stream.h> |
| 31 |
|
| 32 |
#include "ViewProviderExtern.h" |
| 33 |
#include "SoFCSelection.h" |
| 34 |
|
| 35 |
|
| 36 |
using std::string; |
| 37 |
using std::vector; |
| 38 |
|
| 39 |
|
| 40 |
using namespace Gui; |
| 41 |
|
| 42 |
PROPERTY_SOURCE(Gui::ViewProviderExtern, Gui::ViewProvider) |
| 43 |
|
| 44 |
|
| 45 |
ViewProviderExtern::ViewProviderExtern() = default; |
| 46 |
|
| 47 |
ViewProviderExtern::~ViewProviderExtern() = default; |
| 48 |
|
| 49 |
void ViewProviderExtern::setModeByString(const char* name, const char* ivFragment) |
| 50 |
{ |
| 51 |
SoInput in; |
| 52 |
in.setBuffer((void*)ivFragment, std::strlen(ivFragment)); |
| 53 |
setModeBySoInput(name, in); |
| 54 |
} |
| 55 |
|
| 56 |
void ViewProviderExtern::setModeByFile(const char* name, const char* ivFileName) |
| 57 |
{ |
| 58 |
SoInput in; |
| 59 |
Base::ifstream file(Base::FileInfo(ivFileName), std::ios::in | std::ios::binary); |
| 60 |
if (file) { |
| 61 |
std::streamoff size = 0; |
| 62 |
std::streambuf* buf = file.rdbuf(); |
| 63 |
if (buf) { |
| 64 |
std::streamoff curr; |
| 65 |
curr = buf->pubseekoff(0, std::ios::cur, std::ios::in); |
| 66 |
size = buf->pubseekoff(0, std::ios::end, std::ios::in); |
| 67 |
buf->pubseekoff(curr, std::ios::beg, std::ios::in); |
| 68 |
} |
| 69 |
|
| 70 |
// read in the file |
| 71 |
std::vector<unsigned char> content; |
| 72 |
content.reserve(size); |
| 73 |
unsigned char c; |
| 74 |
while (file.get((char&)c)) { |
| 75 |
content.push_back(c); |
| 76 |
} |
| 77 |
|
| 78 |
file.close(); |
| 79 |
in.setBuffer(&(content[0]), content.size()); |
| 80 |
setModeBySoInput(name, in); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void ViewProviderExtern::setModeBySoInput(const char* name, SoInput& ivFileInput) |
| 85 |
{ |
| 86 |
SoSeparator* root = SoDB::readAll(&ivFileInput); |
| 87 |
if (root) { |
| 88 |
std::vector<std::string>::iterator pos = std::find<std::vector<std::string>::iterator, string>( |
| 89 |
modes.begin(), |
| 90 |
modes.end(), |
| 91 |
string(name) |
| 92 |
); |
| 93 |
if (pos == modes.end()) { |
| 94 |
// new mode |
| 95 |
modes.emplace_back(name); |
| 96 |
addDisplayMaskMode(root, name); |
| 97 |
setDisplayMaskMode(name); |
| 98 |
} |
| 99 |
else { |
| 100 |
// existing mode |
| 101 |
// not implemented yet |
| 102 |
assert(0); |
| 103 |
root->unref(); |
| 104 |
} |
| 105 |
} |
| 106 |
else { |
| 107 |
throw Base::RuntimeError("No valid Inventor input"); |
| 108 |
} |
| 109 |
|
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
void ViewProviderExtern::adjustDocumentName(const char* docname) |
| 114 |
{ |
| 115 |
for (int i = 0; i < this->pcModeSwitch->getNumChildren(); i++) { |
| 116 |
SoNode* child = this->pcModeSwitch->getChild(i); |
| 117 |
adjustRecursiveDocumentName(child, docname); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void ViewProviderExtern::adjustRecursiveDocumentName(SoNode* child, const char* docname) |
| 122 |
{ |
| 123 |
if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) { |
| 124 |
static_cast<SoFCSelection*>(child)->documentName = docname; |
| 125 |
} |
| 126 |
else if (child->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) { |
| 127 |
SoGroup* group = static_cast<SoGroup*>(child); |
| 128 |
for (int i = 0; i < group->getNumChildren(); i++) { |
| 129 |
SoNode* subchild = group->getChild(i); |
| 130 |
adjustRecursiveDocumentName(subchild, docname); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
const char* ViewProviderExtern::getDefaultDisplayMode() const |
| 136 |
{ |
| 137 |
// returns the first item of the available modes |
| 138 |
return (modes.empty() ? "" : modes.front().c_str()); |
| 139 |
} |
| 140 |
|
| 141 |
std::vector<std::string> ViewProviderExtern::getDisplayModes() const |
| 142 |
{ |
| 143 |
return modes; |
| 144 |
} |