ViewProviderExtern.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #ifndef _PreComp_
00027 # include <Inventor/nodes/SoMaterial.h>
00028 # include <Inventor/nodes/SoSeparator.h>
00029 # include <Inventor/nodes/SoSwitch.h>
00030 #endif
00031
00033 #include <Base/Exception.h>
00034 #include <Base/FileInfo.h>
00035 #include <Base/Stream.h>
00036 #include "SoFCSelection.h"
00037 #include "ViewProviderExtern.h"
00038
00039 #include <cstring>
00040 #include <algorithm>
00041
00042 using std::vector;
00043 using std::string;
00044
00045
00046 using namespace Gui;
00047
00048 PROPERTY_SOURCE(Gui::ViewProviderExtern, Gui::ViewProvider)
00049
00050
00051 ViewProviderExtern::ViewProviderExtern()
00052 {
00053
00054 }
00055
00056 ViewProviderExtern::~ViewProviderExtern()
00057 {
00058
00059 }
00060
00061 void ViewProviderExtern::setModeByString(const char* name, const char* ivFragment)
00062 {
00063 SoInput in;
00064 in.setBuffer((void*)ivFragment,std::strlen(ivFragment));
00065 setModeBySoInput(name,in);
00066 }
00067
00068 void ViewProviderExtern::setModeByFile(const char* name, const char* ivFileName)
00069 {
00070 SoInput in;
00071 Base::ifstream file(ivFileName, std::ios::in | std::ios::binary);
00072 if (file){
00073 std::streamoff size = 0;
00074 std::streambuf* buf = file.rdbuf();
00075 if (buf) {
00076 std::streamoff curr;
00077 curr = buf->pubseekoff(0, std::ios::cur, std::ios::in);
00078 size = buf->pubseekoff(0, std::ios::end, std::ios::in);
00079 buf->pubseekoff(curr, std::ios::beg, std::ios::in);
00080 }
00081
00082
00083 std::vector<unsigned char> content;
00084 content.reserve(size);
00085 unsigned char c;
00086 while (file.get((char&)c)) {
00087 content.push_back(c);
00088 }
00089
00090 file.close();
00091 in.setBuffer(&(content[0]),content.size());
00092 setModeBySoInput(name,in);
00093 }
00094 }
00095
00096 void ViewProviderExtern::setModeBySoInput(const char* name, SoInput &ivFileInput)
00097 {
00098 SoSeparator * root = SoDB::readAll(&ivFileInput);
00099 if (root) {
00100 std::vector<std::string>::iterator pos = std::find<std::vector<std::string>
00101 ::iterator,string>(modes.begin(),modes.end(),string(name));
00102 if (pos == modes.end()) {
00103
00104 modes.push_back(name);
00105 addDisplayMaskMode(root, name);
00106 setDisplayMaskMode(name);
00107 }
00108 else {
00109
00110
00111 assert(0);
00112 root->unref();
00113 }
00114 }
00115 else {
00116 throw Base::Exception("No valid Inventor input");
00117 }
00118
00119 return;
00120 }
00121
00122 void ViewProviderExtern::adjustDocumentName(const char* docname)
00123 {
00124 for (int i=0; i<this->pcModeSwitch->getNumChildren(); i++) {
00125 SoNode* child = this->pcModeSwitch->getChild(i);
00126 adjustRecursiveDocumentName(child, docname);
00127 }
00128 }
00129
00130 void ViewProviderExtern::adjustRecursiveDocumentName(SoNode* child, const char* docname)
00131 {
00132 if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) {
00133 static_cast<SoFCSelection*>(child)->documentName = docname;
00134 }
00135 else if (child->getTypeId().isDerivedFrom( SoGroup::getClassTypeId())) {
00136 SoGroup* group = (SoGroup*)child;
00137 for (int i=0; i<group->getNumChildren(); i++) {
00138 SoNode* subchild = group->getChild(i);
00139 adjustRecursiveDocumentName(subchild, docname);
00140 }
00141 }
00142 }
00143
00144 const char* ViewProviderExtern::getDefaultDisplayMode() const
00145 {
00146
00147 return (modes.empty() ? "" : modes.front().c_str());
00148 }
00149
00150 std::vector<std::string> ViewProviderExtern::getDisplayModes(void) const
00151 {
00152 return modes;
00153 }