FreeCAD

Форк
0
/
SoQtOffscreenRendererPy.cpp 
198 строк · 7.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2022 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
#include "PreCompiled.h"
24
#include <Base/Interpreter.h>
25

26
#include "SoQtOffscreenRendererPy.h"
27

28

29
using namespace Gui;
30

31
SoQtOffscreenRendererPy::SoQtOffscreenRendererPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
32
    : Py::PythonClass<SoQtOffscreenRendererPy>(self, args, kwds), renderer(SbViewportRegion())
33
{
34
    this->setViewportRegion(args);
35
}
36

37
SoQtOffscreenRendererPy::~SoQtOffscreenRendererPy() = default;
38

39
Py::Object SoQtOffscreenRendererPy::repr()
40
{
41
    std::stringstream s;
42
    s << "<SoQtOffscreenRenderer at " << this << ">";
43
    return Py::String(s.str());
44
}
45

46
Py::Object SoQtOffscreenRendererPy::setViewportRegion(const Py::Tuple& args)
47
{
48
    short w, h;
49
    if (!PyArg_ParseTuple(args.ptr(), "hh", &w, &h)) {
50
        throw Py::Exception();
51
    }
52

53
    renderer.setViewportRegion(SbViewportRegion(w, h));
54
    return Py::None();
55
}
56
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setViewportRegion)
57

58
Py::Object SoQtOffscreenRendererPy::getViewportRegion()
59
{
60
    const SbViewportRegion& vpr = renderer.getViewportRegion();
61
    SbVec2s size = vpr.getWindowSize();
62

63
    return Py::TupleN(Py::Long(size[0]), Py::Long(size[1]));
64
}
65
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getViewportRegion)
66

67
Py::Object SoQtOffscreenRendererPy::setBackgroundColor(const Py::Tuple& args)
68
{
69
    float r, g, b, a = 1.0f;
70
    if (!PyArg_ParseTuple(args.ptr(), "fff|f", &r, &g, &b, &a)) {
71
        throw Py::Exception();
72
    }
73

74
    renderer.setBackgroundColor(SbColor4f(r, g, b, a));
75

76
    return Py::None();
77
}
78
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setBackgroundColor)
79

80
Py::Object SoQtOffscreenRendererPy::getBackgroundColor()
81
{
82
    SbColor4f color = renderer.getBackgroundColor();
83
    return Py::TupleN(Py::Float(color[0]), Py::Float(color[1]), Py::Float(color[2]), Py::Float(color[3]));
84
}
85
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getBackgroundColor)
86

87
Py::Object SoQtOffscreenRendererPy::setNumPasses(const Py::Tuple& args)
88
{
89
    int num;
90
    if (!PyArg_ParseTuple(args.ptr(), "i", &num)) {
91
        throw Py::Exception();
92
    }
93

94
    renderer.setNumPasses(num);
95

96
    return Py::None();
97
}
98
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setNumPasses)
99

100
Py::Object SoQtOffscreenRendererPy::getNumPasses()
101
{
102
    return Py::Long(renderer.getNumPasses());
103
}
104
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getNumPasses)
105

106
Py::Object SoQtOffscreenRendererPy::setInternalTextureFormat(const Py::Tuple& args)
107
{
108
    unsigned int format;
109
    if (!PyArg_ParseTuple(args.ptr(), "I", &format)) {
110
        throw Py::Exception();
111
    }
112

113
    renderer.setInternalTextureFormat(format);
114

115
    return Py::None();
116
}
117
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setInternalTextureFormat)
118

119
Py::Object SoQtOffscreenRendererPy::getInternalTextureFormat()
120
{
121
    return Py::Long(static_cast<unsigned long>(renderer.internalTextureFormat()));
122
}
123
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getInternalTextureFormat)
124

125
Py::Object SoQtOffscreenRendererPy::render(const Py::Tuple& args)
126
{
127
    PyObject* proxy;
128
    if (!PyArg_ParseTuple(args.ptr(), "O", &proxy)) {
129
        throw Py::Exception();
130
    }
131

132
    try {
133
        void* ptr = nullptr;
134
        Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0);
135
        auto node = static_cast<SoNode*>(ptr);
136
        bool ok = false;
137
        if (node) {
138
            ok = renderer.render(node);
139
        }
140
        return Py::Boolean(ok);
141
    }
142
    catch (const Base::Exception& e) {
143
        e.setPyException();
144
        throw Py::Exception();
145
    }
146
}
147
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, render)
148

149
Py::Object SoQtOffscreenRendererPy::writeToImage(const Py::Tuple& args)
150
{
151
    const char* filename;
152
    if (!PyArg_ParseTuple(args.ptr(), "s", &filename)) {
153
        throw Py::Exception();
154
    }
155

156
    QImage img;
157
    renderer.writeToImage(img);
158
    img.save(QString::fromUtf8(filename));
159

160
    return Py::None();
161
}
162
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, writeToImage)
163

164
Py::Object SoQtOffscreenRendererPy::getWriteImageFiletypeInfo()
165
{
166
    QStringList list = renderer.getWriteImageFiletypeInfo();
167
    Py::Tuple tuple(list.size());
168
    int index = 0;
169
    for (const auto& item : list) {
170
        tuple[index++] = Py::String(item.toStdString());
171
    }
172

173
    return tuple;
174
}
175
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getWriteImageFiletypeInfo)
176

177
void SoQtOffscreenRendererPy::init_type()
178
{
179
    behaviors().name("Gui.SoQtOffscreenRenderer");
180
    behaviors().doc("Python interface for SoQtOffscreenRenderer");
181

182
    // you must have overwritten the virtual functions
183
    behaviors().supportRepr();
184

185
    PYCXX_ADD_VARARGS_METHOD(setViewportRegion, setViewportRegion, "setViewportRegion(int, int)");
186
    PYCXX_ADD_NOARGS_METHOD(getViewportRegion, getViewportRegion, "getViewportRegion() -> tuple");
187
    PYCXX_ADD_VARARGS_METHOD(setBackgroundColor, setBackgroundColor, "setBackgroundColor(float, float, float, [float])");
188
    PYCXX_ADD_NOARGS_METHOD(getBackgroundColor, getBackgroundColor, "getBackgroundColor() -> tuple");
189
    PYCXX_ADD_VARARGS_METHOD(setNumPasses, setNumPasses, "setNumPasses(int)");
190
    PYCXX_ADD_NOARGS_METHOD(getNumPasses, getNumPasses, "getNumPasses() -> int");
191
    PYCXX_ADD_VARARGS_METHOD(setInternalTextureFormat, setInternalTextureFormat, "setInternalTextureFormat(int)");
192
    PYCXX_ADD_NOARGS_METHOD(getInternalTextureFormat, getInternalTextureFormat, "getInternalTextureFormat() -> int");
193
    PYCXX_ADD_VARARGS_METHOD(render, render, "render(node)");
194
    PYCXX_ADD_VARARGS_METHOD(writeToImage, writeToImage, "writeToImage(string)");
195
    PYCXX_ADD_NOARGS_METHOD(getWriteImageFiletypeInfo, getWriteImageFiletypeInfo, "getWriteImageFiletypeInfo() -> tuple");
196

197
    behaviors().readyType();
198
}
199

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

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

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

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