FreeCAD

Форк
0
/
ViewProviderExtern.cpp 
143 строки · 5.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de>              *
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
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <cstring>
27
# include <Inventor/nodes/SoSeparator.h>
28
# include <Inventor/nodes/SoSwitch.h>
29
#endif
30

31
#include <Base/Exception.h>
32
#include <Base/Stream.h>
33

34
#include "ViewProviderExtern.h"
35
#include "SoFCSelection.h"
36

37

38
using std::vector;
39
using std::string;
40

41

42
using namespace Gui;
43

44
PROPERTY_SOURCE(Gui::ViewProviderExtern, Gui::ViewProvider)
45

46

47
ViewProviderExtern::ViewProviderExtern() = default;
48

49
ViewProviderExtern::~ViewProviderExtern() = default;
50

51
void ViewProviderExtern::setModeByString(const char* name, const char* ivFragment)
52
{
53
    SoInput in;
54
    in.setBuffer((void*)ivFragment,std::strlen(ivFragment));
55
    setModeBySoInput(name,in);
56
}
57

58
void ViewProviderExtern::setModeByFile(const char* name, const char* ivFileName)
59
{
60
    SoInput in;
61
    Base::ifstream file(Base::FileInfo(ivFileName), std::ios::in | std::ios::binary);
62
    if (file){
63
        std::streamoff size = 0;
64
        std::streambuf* buf = file.rdbuf();
65
        if (buf) {
66
            std::streamoff curr;
67
            curr = buf->pubseekoff(0, std::ios::cur, std::ios::in);
68
            size = buf->pubseekoff(0, std::ios::end, std::ios::in);
69
            buf->pubseekoff(curr, std::ios::beg, std::ios::in);
70
        }
71

72
        // read in the file
73
        std::vector<unsigned char> content;
74
        content.reserve(size);
75
        unsigned char c;
76
        while (file.get((char&)c)) {
77
            content.push_back(c);
78
        }
79

80
        file.close();
81
        in.setBuffer(&(content[0]),content.size());
82
        setModeBySoInput(name,in);
83
    }
84
}
85

86
void ViewProviderExtern::setModeBySoInput(const char* name, SoInput &ivFileInput)
87
{
88
    SoSeparator * root = SoDB::readAll(&ivFileInput);
89
    if (root) {
90
        std::vector<std::string>::iterator pos = std::find<std::vector<std::string>
91
           ::iterator,string>(modes.begin(),modes.end(),string(name));
92
        if (pos == modes.end()) {
93
            // new mode
94
            modes.emplace_back(name);
95
            addDisplayMaskMode(root, name);
96
            setDisplayMaskMode(name);
97
        }
98
        else {
99
            // existing mode
100
            // not implemented yet
101
            assert(0);
102
            root->unref();
103
        }
104
    }
105
    else {
106
        throw Base::RuntimeError("No valid Inventor input");
107
    }
108

109
    return;
110
}
111

112
void ViewProviderExtern::adjustDocumentName(const char* docname)
113
{
114
    for (int i=0; i<this->pcModeSwitch->getNumChildren(); i++) {
115
        SoNode* child = this->pcModeSwitch->getChild(i);
116
        adjustRecursiveDocumentName(child, docname);
117
    }
118
}
119

120
void ViewProviderExtern::adjustRecursiveDocumentName(SoNode* child, const char* docname)
121
{
122
    if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) {
123
        static_cast<SoFCSelection*>(child)->documentName = docname;
124
    }
125
    else if (child->getTypeId().isDerivedFrom( SoGroup::getClassTypeId())) {
126
        SoGroup* group = static_cast<SoGroup*>(child);
127
        for (int i=0; i<group->getNumChildren(); i++) {
128
            SoNode* subchild = group->getChild(i);
129
            adjustRecursiveDocumentName(subchild, docname);
130
        }
131
    }
132
}
133

134
const char* ViewProviderExtern::getDefaultDisplayMode() const
135
{
136
    // returns the first item of the available modes
137
    return (modes.empty() ? "" : modes.front().c_str());
138
}
139

140
std::vector<std::string> ViewProviderExtern::getDisplayModes() const
141
{
142
    return modes;
143
}
144

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.