| 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 |
// inclusion of the generated files (generated out of MaterialPy.xml) |
| 25 |
#include "MaterialPy.h" |
| 26 |
|
| 27 |
#include "MaterialPy.cpp" |
| 28 |
|
| 29 |
#include <Base/PyWrapParseTupleAndKeywords.h> |
| 30 |
|
| 31 |
using namespace App; |
| 32 |
|
| 33 |
Base::Color MaterialPy::toColor(PyObject* value) |
| 34 |
{ |
| 35 |
Base::Color cCol; |
| 36 |
if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) { |
| 37 |
PyObject* item {}; |
| 38 |
item = PyTuple_GetItem(value, 0); |
| 39 |
if (PyFloat_Check(item)) { |
| 40 |
cCol.r = (float)PyFloat_AsDouble(item); |
| 41 |
item = PyTuple_GetItem(value, 1); |
| 42 |
if (PyFloat_Check(item)) { |
| 43 |
cCol.g = (float)PyFloat_AsDouble(item); |
| 44 |
} |
| 45 |
else { |
| 46 |
throw Base::TypeError("Type in tuple must be consistent (float)"); |
| 47 |
} |
| 48 |
item = PyTuple_GetItem(value, 2); |
| 49 |
if (PyFloat_Check(item)) { |
| 50 |
cCol.b = (float)PyFloat_AsDouble(item); |
| 51 |
} |
| 52 |
else { |
| 53 |
throw Base::TypeError("Type in tuple must be consistent (float)"); |
| 54 |
} |
| 55 |
if (PyTuple_Size(value) == 4) { |
| 56 |
item = PyTuple_GetItem(value, 3); |
| 57 |
if (PyFloat_Check(item)) { |
| 58 |
cCol.a = (float)PyFloat_AsDouble(item); |
| 59 |
} |
| 60 |
else { |
| 61 |
throw Base::TypeError("Type in tuple must be consistent (float)"); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
else if (PyLong_Check(item)) { |
| 66 |
cCol.r = static_cast<float>(PyLong_AsLong(item)) / 255.0F; |
| 67 |
item = PyTuple_GetItem(value, 1); |
| 68 |
if (PyLong_Check(item)) { |
| 69 |
cCol.g = static_cast<float>(PyLong_AsLong(item)) / 255.0F; |
| 70 |
} |
| 71 |
else { |
| 72 |
throw Base::TypeError("Type in tuple must be consistent (integer)"); |
| 73 |
} |
| 74 |
item = PyTuple_GetItem(value, 2); |
| 75 |
if (PyLong_Check(item)) { |
| 76 |
cCol.b = static_cast<float>(PyLong_AsLong(item)) / 255.0F; |
| 77 |
} |
| 78 |
else { |
| 79 |
throw Base::TypeError("Type in tuple must be consistent (integer)"); |
| 80 |
} |
| 81 |
if (PyTuple_Size(value) == 4) { |
| 82 |
item = PyTuple_GetItem(value, 3); |
| 83 |
if (PyLong_Check(item)) { |
| 84 |
cCol.a = static_cast<float>(PyLong_AsLong(item)) / 255.0F; |
| 85 |
} |
| 86 |
else { |
| 87 |
throw Base::TypeError("Type in tuple must be consistent (integer)"); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
else { |
| 92 |
throw Base::TypeError("Type in tuple must be float or integer"); |
| 93 |
} |
| 94 |
} |
| 95 |
else if (PyLong_Check(value)) { |
| 96 |
cCol.setPackedValue(PyLong_AsUnsignedLong(value)); |
| 97 |
} |
| 98 |
else { |
| 99 |
std::string error = |
| 100 |
std::string("type must be integer or tuple of float or tuple integer, not "); |
| 101 |
error += value->ob_type->tp_name; |
| 102 |
throw Base::TypeError(error); |
| 103 |
} |
| 104 |
|
| 105 |
return cCol; |
| 106 |
} |
| 107 |
|
| 108 |
PyObject* MaterialPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper |
| 109 |
{ |
| 110 |
// create a new instance of MaterialPy and the Twin object |
| 111 |
return new MaterialPy(new Material); |
| 112 |
} |
| 113 |
|
| 114 |
// constructor method |
| 115 |
int MaterialPy::PyInit(PyObject* args, PyObject* kwds) |
| 116 |
{ |
| 117 |
PyObject* diffuse = nullptr; |
| 118 |
PyObject* ambient = nullptr; |
| 119 |
PyObject* specular = nullptr; |
| 120 |
PyObject* emissive = nullptr; |
| 121 |
PyObject* shininess = nullptr; |
| 122 |
PyObject* transparency = nullptr; |
| 123 |
static const std::array<const char*, 7> kwds_colors {"DiffuseColor", |
| 124 |
"AmbientColor", |
| 125 |
"SpecularColor", |
| 126 |
"EmissiveColor", |
| 127 |
"Shininess", |
| 128 |
"Transparency", |
| 129 |
nullptr}; |
| 130 |
|
| 131 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, |
| 132 |
kwds, |
| 133 |
"|OOOOOO", |
| 134 |
kwds_colors, |
| 135 |
&diffuse, |
| 136 |
&ambient, |
| 137 |
&specular, |
| 138 |
&emissive, |
| 139 |
&shininess, |
| 140 |
&transparency)) { |
| 141 |
return -1; |
| 142 |
} |
| 143 |
|
| 144 |
try { |
| 145 |
if (diffuse) { |
| 146 |
setDiffuseColor(Py::Object(diffuse)); |
| 147 |
} |
| 148 |
|
| 149 |
if (ambient) { |
| 150 |
setAmbientColor(Py::Object(ambient)); |
| 151 |
} |
| 152 |
|
| 153 |
if (specular) { |
| 154 |
setSpecularColor(Py::Object(specular)); |
| 155 |
} |
| 156 |
|
| 157 |
if (emissive) { |
| 158 |
setEmissiveColor(Py::Object(emissive)); |
| 159 |
} |
| 160 |
|
| 161 |
if (shininess) { |
| 162 |
setShininess(Py::Float(shininess)); |
| 163 |
} |
| 164 |
|
| 165 |
if (transparency) { |
| 166 |
setTransparency(Py::Float(transparency)); |
| 167 |
} |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |
| 171 |
catch (const Py::Exception&) { |
| 172 |
return -1; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// returns a string which represents the object e.g. when printed in python |
| 177 |
std::string MaterialPy::representation() const |
| 178 |
{ |
| 179 |
return {"<Material object>"}; |
| 180 |
} |
| 181 |
|
| 182 |
PyObject* MaterialPy::set(PyObject* args) |
| 183 |
{ |
| 184 |
char* pstr {}; |
| 185 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 186 |
return nullptr; |
| 187 |
} |
| 188 |
|
| 189 |
getMaterialPtr()->set(pstr); |
| 190 |
|
| 191 |
Py_Return; |
| 192 |
} |
| 193 |
|
| 194 |
Py::Object MaterialPy::getAmbientColor() const |
| 195 |
{ |
| 196 |
Py::Tuple tuple(4); |
| 197 |
tuple.setItem(0, Py::Float(getMaterialPtr()->ambientColor.r)); |
| 198 |
tuple.setItem(1, Py::Float(getMaterialPtr()->ambientColor.g)); |
| 199 |
tuple.setItem(2, Py::Float(getMaterialPtr()->ambientColor.b)); |
| 200 |
tuple.setItem(3, Py::Float(getMaterialPtr()->ambientColor.a)); |
| 201 |
return tuple; |
| 202 |
} |
| 203 |
|
| 204 |
void MaterialPy::setAmbientColor(Py::Object arg) |
| 205 |
{ |
| 206 |
try { |
| 207 |
getMaterialPtr()->ambientColor = toColor(*arg); |
| 208 |
} |
| 209 |
catch (const Base::Exception& e) { |
| 210 |
e.setPyException(); |
| 211 |
throw Py::Exception(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
Py::Object MaterialPy::getDiffuseColor() const |
| 216 |
{ |
| 217 |
Py::Tuple tuple(4); |
| 218 |
tuple.setItem(0, Py::Float(getMaterialPtr()->diffuseColor.r)); |
| 219 |
tuple.setItem(1, Py::Float(getMaterialPtr()->diffuseColor.g)); |
| 220 |
tuple.setItem(2, Py::Float(getMaterialPtr()->diffuseColor.b)); |
| 221 |
tuple.setItem(3, Py::Float(getMaterialPtr()->diffuseColor.a)); |
| 222 |
return tuple; |
| 223 |
} |
| 224 |
|
| 225 |
void MaterialPy::setDiffuseColor(Py::Object arg) |
| 226 |
{ |
| 227 |
try { |
| 228 |
getMaterialPtr()->diffuseColor = toColor(*arg); |
| 229 |
} |
| 230 |
catch (const Base::Exception& e) { |
| 231 |
e.setPyException(); |
| 232 |
throw Py::Exception(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
Py::Object MaterialPy::getEmissiveColor() const |
| 237 |
{ |
| 238 |
Py::Tuple tuple(4); |
| 239 |
tuple.setItem(0, Py::Float(getMaterialPtr()->emissiveColor.r)); |
| 240 |
tuple.setItem(1, Py::Float(getMaterialPtr()->emissiveColor.g)); |
| 241 |
tuple.setItem(2, Py::Float(getMaterialPtr()->emissiveColor.b)); |
| 242 |
tuple.setItem(3, Py::Float(getMaterialPtr()->emissiveColor.a)); |
| 243 |
return tuple; |
| 244 |
} |
| 245 |
|
| 246 |
void MaterialPy::setEmissiveColor(Py::Object arg) |
| 247 |
{ |
| 248 |
try { |
| 249 |
getMaterialPtr()->emissiveColor = toColor(*arg); |
| 250 |
} |
| 251 |
catch (const Base::Exception& e) { |
| 252 |
e.setPyException(); |
| 253 |
throw Py::Exception(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
Py::Object MaterialPy::getSpecularColor() const |
| 258 |
{ |
| 259 |
Py::Tuple tuple(4); |
| 260 |
tuple.setItem(0, Py::Float(getMaterialPtr()->specularColor.r)); |
| 261 |
tuple.setItem(1, Py::Float(getMaterialPtr()->specularColor.g)); |
| 262 |
tuple.setItem(2, Py::Float(getMaterialPtr()->specularColor.b)); |
| 263 |
tuple.setItem(3, Py::Float(getMaterialPtr()->specularColor.a)); |
| 264 |
return tuple; |
| 265 |
} |
| 266 |
|
| 267 |
void MaterialPy::setSpecularColor(Py::Object arg) |
| 268 |
{ |
| 269 |
try { |
| 270 |
getMaterialPtr()->specularColor = toColor(*arg); |
| 271 |
} |
| 272 |
catch (const Base::Exception& e) { |
| 273 |
e.setPyException(); |
| 274 |
throw Py::Exception(); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
Py::Float MaterialPy::getShininess() const |
| 279 |
{ |
| 280 |
return Py::Float(getMaterialPtr()->shininess); |
| 281 |
} |
| 282 |
|
| 283 |
void MaterialPy::setShininess(Py::Float arg) |
| 284 |
{ |
| 285 |
getMaterialPtr()->shininess = arg; |
| 286 |
} |
| 287 |
|
| 288 |
Py::Float MaterialPy::getTransparency() const |
| 289 |
{ |
| 290 |
return Py::Float(getMaterialPtr()->transparency); |
| 291 |
} |
| 292 |
|
| 293 |
void MaterialPy::setTransparency(Py::Float arg) |
| 294 |
{ |
| 295 |
getMaterialPtr()->transparency = arg; |
| 296 |
} |
| 297 |
|
| 298 |
PyObject* MaterialPy::getCustomAttributes(const char* /*attr*/) const |
| 299 |
{ |
| 300 |
return nullptr; |
| 301 |
} |
| 302 |
|
| 303 |
int MaterialPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) |
| 304 |
{ |
| 305 |
return 0; |
| 306 |
} |