ViewProviderExtern.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de>              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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         // read in the file
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             // new mode
00104             modes.push_back(name);
00105             addDisplayMaskMode(root, name);
00106             setDisplayMaskMode(name);
00107         }
00108         else {
00109             // existing mode
00110             // not implemented yet
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     // returns the first item of the available modes
00147     return (modes.empty() ? "" : modes.front().c_str());
00148 }
00149 
00150 std::vector<std::string> ViewProviderExtern::getDisplayModes(void) const
00151 {
00152     return modes;
00153 }

Generated on Wed Nov 23 19:00:59 2011 for FreeCAD by  doxygen 1.6.1