FreeCAD

Форк
0
/
MaterialPyImp.cpp 
308 строк · 10.1 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2010 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

24
#include "PreCompiled.h"
25

26
// inclusion of the generated files (generated out of MaterialPy.xml)
27
#include "MaterialPy.h"
28

29
#include "MaterialPy.cpp"
30

31
#include <Base/PyWrapParseTupleAndKeywords.h>
32

33
using namespace App;
34

35
Color MaterialPy::toColor(PyObject* value)
36
{
37
    Color cCol;
38
    if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) {
39
        PyObject* item {};
40
        item = PyTuple_GetItem(value, 0);
41
        if (PyFloat_Check(item)) {
42
            cCol.r = (float)PyFloat_AsDouble(item);
43
            item = PyTuple_GetItem(value, 1);
44
            if (PyFloat_Check(item)) {
45
                cCol.g = (float)PyFloat_AsDouble(item);
46
            }
47
            else {
48
                throw Base::TypeError("Type in tuple must be consistent (float)");
49
            }
50
            item = PyTuple_GetItem(value, 2);
51
            if (PyFloat_Check(item)) {
52
                cCol.b = (float)PyFloat_AsDouble(item);
53
            }
54
            else {
55
                throw Base::TypeError("Type in tuple must be consistent (float)");
56
            }
57
            if (PyTuple_Size(value) == 4) {
58
                item = PyTuple_GetItem(value, 3);
59
                if (PyFloat_Check(item)) {
60
                    cCol.a = (float)PyFloat_AsDouble(item);
61
                }
62
                else {
63
                    throw Base::TypeError("Type in tuple must be consistent (float)");
64
                }
65
            }
66
        }
67
        else if (PyLong_Check(item)) {
68
            cCol.r = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
69
            item = PyTuple_GetItem(value, 1);
70
            if (PyLong_Check(item)) {
71
                cCol.g = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
72
            }
73
            else {
74
                throw Base::TypeError("Type in tuple must be consistent (integer)");
75
            }
76
            item = PyTuple_GetItem(value, 2);
77
            if (PyLong_Check(item)) {
78
                cCol.b = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
79
            }
80
            else {
81
                throw Base::TypeError("Type in tuple must be consistent (integer)");
82
            }
83
            if (PyTuple_Size(value) == 4) {
84
                item = PyTuple_GetItem(value, 3);
85
                if (PyLong_Check(item)) {
86
                    cCol.a = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
87
                }
88
                else {
89
                    throw Base::TypeError("Type in tuple must be consistent (integer)");
90
                }
91
            }
92
        }
93
        else {
94
            throw Base::TypeError("Type in tuple must be float or integer");
95
        }
96
    }
97
    else if (PyLong_Check(value)) {
98
        cCol.setPackedValue(PyLong_AsUnsignedLong(value));
99
    }
100
    else {
101
        std::string error =
102
            std::string("type must be integer or tuple of float or tuple integer, not ");
103
        error += value->ob_type->tp_name;
104
        throw Base::TypeError(error);
105
    }
106

107
    return cCol;
108
}
109

110
PyObject* MaterialPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
111
{
112
    // create a new instance of MaterialPy and the Twin object
113
    return new MaterialPy(new Material);
114
}
115

116
// constructor method
117
int MaterialPy::PyInit(PyObject* args, PyObject* kwds)
118
{
119
    PyObject* diffuse = nullptr;
120
    PyObject* ambient = nullptr;
121
    PyObject* specular = nullptr;
122
    PyObject* emissive = nullptr;
123
    PyObject* shininess = nullptr;
124
    PyObject* transparency = nullptr;
125
    static const std::array<const char*, 7> kwds_colors {"DiffuseColor",
126
                                                         "AmbientColor",
127
                                                         "SpecularColor",
128
                                                         "EmissiveColor",
129
                                                         "Shininess",
130
                                                         "Transparency",
131
                                                         nullptr};
132

133
    if (!Base::Wrapped_ParseTupleAndKeywords(args,
134
                                             kwds,
135
                                             "|OOOOOO",
136
                                             kwds_colors,
137
                                             &diffuse,
138
                                             &ambient,
139
                                             &specular,
140
                                             &emissive,
141
                                             &shininess,
142
                                             &transparency)) {
143
        return -1;
144
    }
145

146
    try {
147
        if (diffuse) {
148
            setDiffuseColor(Py::Object(diffuse));
149
        }
150

151
        if (ambient) {
152
            setAmbientColor(Py::Object(ambient));
153
        }
154

155
        if (specular) {
156
            setSpecularColor(Py::Object(specular));
157
        }
158

159
        if (emissive) {
160
            setEmissiveColor(Py::Object(emissive));
161
        }
162

163
        if (shininess) {
164
            setShininess(Py::Float(shininess));
165
        }
166

167
        if (transparency) {
168
            setTransparency(Py::Float(transparency));
169
        }
170

171
        return 0;
172
    }
173
    catch (const Py::Exception&) {
174
        return -1;
175
    }
176
}
177

178
// returns a string which represents the object e.g. when printed in python
179
std::string MaterialPy::representation() const
180
{
181
    return {"<Material object>"};
182
}
183

184
PyObject* MaterialPy::set(PyObject* args)
185
{
186
    char* pstr {};
187
    if (!PyArg_ParseTuple(args, "s", &pstr)) {
188
        return nullptr;
189
    }
190

191
    getMaterialPtr()->set(pstr);
192

193
    Py_Return;
194
}
195

196
Py::Object MaterialPy::getAmbientColor() const
197
{
198
    Py::Tuple tuple(4);
199
    tuple.setItem(0, Py::Float(getMaterialPtr()->ambientColor.r));
200
    tuple.setItem(1, Py::Float(getMaterialPtr()->ambientColor.g));
201
    tuple.setItem(2, Py::Float(getMaterialPtr()->ambientColor.b));
202
    tuple.setItem(3, Py::Float(getMaterialPtr()->ambientColor.a));
203
    return tuple;
204
}
205

206
void MaterialPy::setAmbientColor(Py::Object arg)
207
{
208
    try {
209
        getMaterialPtr()->ambientColor = toColor(*arg);
210
    }
211
    catch (const Base::Exception& e) {
212
        e.setPyException();
213
        throw Py::Exception();
214
    }
215
}
216

217
Py::Object MaterialPy::getDiffuseColor() const
218
{
219
    Py::Tuple tuple(4);
220
    tuple.setItem(0, Py::Float(getMaterialPtr()->diffuseColor.r));
221
    tuple.setItem(1, Py::Float(getMaterialPtr()->diffuseColor.g));
222
    tuple.setItem(2, Py::Float(getMaterialPtr()->diffuseColor.b));
223
    tuple.setItem(3, Py::Float(getMaterialPtr()->diffuseColor.a));
224
    return tuple;
225
}
226

227
void MaterialPy::setDiffuseColor(Py::Object arg)
228
{
229
    try {
230
        getMaterialPtr()->diffuseColor = toColor(*arg);
231
    }
232
    catch (const Base::Exception& e) {
233
        e.setPyException();
234
        throw Py::Exception();
235
    }
236
}
237

238
Py::Object MaterialPy::getEmissiveColor() const
239
{
240
    Py::Tuple tuple(4);
241
    tuple.setItem(0, Py::Float(getMaterialPtr()->emissiveColor.r));
242
    tuple.setItem(1, Py::Float(getMaterialPtr()->emissiveColor.g));
243
    tuple.setItem(2, Py::Float(getMaterialPtr()->emissiveColor.b));
244
    tuple.setItem(3, Py::Float(getMaterialPtr()->emissiveColor.a));
245
    return tuple;
246
}
247

248
void MaterialPy::setEmissiveColor(Py::Object arg)
249
{
250
    try {
251
        getMaterialPtr()->emissiveColor = toColor(*arg);
252
    }
253
    catch (const Base::Exception& e) {
254
        e.setPyException();
255
        throw Py::Exception();
256
    }
257
}
258

259
Py::Object MaterialPy::getSpecularColor() const
260
{
261
    Py::Tuple tuple(4);
262
    tuple.setItem(0, Py::Float(getMaterialPtr()->specularColor.r));
263
    tuple.setItem(1, Py::Float(getMaterialPtr()->specularColor.g));
264
    tuple.setItem(2, Py::Float(getMaterialPtr()->specularColor.b));
265
    tuple.setItem(3, Py::Float(getMaterialPtr()->specularColor.a));
266
    return tuple;
267
}
268

269
void MaterialPy::setSpecularColor(Py::Object arg)
270
{
271
    try {
272
        getMaterialPtr()->specularColor = toColor(*arg);
273
    }
274
    catch (const Base::Exception& e) {
275
        e.setPyException();
276
        throw Py::Exception();
277
    }
278
}
279

280
Py::Float MaterialPy::getShininess() const
281
{
282
    return Py::Float(getMaterialPtr()->shininess);
283
}
284

285
void MaterialPy::setShininess(Py::Float arg)
286
{
287
    getMaterialPtr()->shininess = arg;
288
}
289

290
Py::Float MaterialPy::getTransparency() const
291
{
292
    return Py::Float(getMaterialPtr()->transparency);
293
}
294

295
void MaterialPy::setTransparency(Py::Float arg)
296
{
297
    getMaterialPtr()->transparency = arg;
298
}
299

300
PyObject* MaterialPy::getCustomAttributes(const char* /*attr*/) const
301
{
302
    return nullptr;
303
}
304

305
int MaterialPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
306
{
307
    return 0;
308
}
309

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

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

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

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