From bb10ff72b53f291ecac387fda8303ff401dcaaf7 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 5 Apr 2024 13:48:54 +0200 Subject: [PATCH] App: refactor MaterialPy to avoid code duplication --- src/App/MaterialPy.xml | 3 ++ src/App/MaterialPyImp.cpp | 22 +++++------ src/App/PropertyStandard.cpp | 75 +----------------------------------- 3 files changed, 15 insertions(+), 85 deletions(-) diff --git a/src/App/MaterialPy.xml b/src/App/MaterialPy.xml index eff0db8c59..60b2567f9d 100644 --- a/src/App/MaterialPy.xml +++ b/src/App/MaterialPy.xml @@ -63,5 +63,8 @@ Satin, Metalized, Neon GNC, Chrome, Aluminium, Obsidian, Neon PHC, Jade, Ruby or + public: + static App::Color toColor(PyObject* value); + diff --git a/src/App/MaterialPyImp.cpp b/src/App/MaterialPyImp.cpp index dc0c5bbebf..bf7979106d 100644 --- a/src/App/MaterialPyImp.cpp +++ b/src/App/MaterialPyImp.cpp @@ -32,11 +32,11 @@ using namespace App; -Color parseColor(PyObject* value) +Color MaterialPy::toColor(PyObject* value) { Color cCol; if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) { - PyObject* item; + PyObject* item {}; item = PyTuple_GetItem(value, 0); if (PyFloat_Check(item)) { cCol.r = (float)PyFloat_AsDouble(item); @@ -65,17 +65,17 @@ Color parseColor(PyObject* value) } } else if (PyLong_Check(item)) { - cCol.r = PyLong_AsLong(item) / 255.0; + cCol.r = static_cast(PyLong_AsLong(item)) / 255.0F; item = PyTuple_GetItem(value, 1); if (PyLong_Check(item)) { - cCol.g = PyLong_AsLong(item) / 255.0; + cCol.g = static_cast(PyLong_AsLong(item)) / 255.0F; } else { throw Base::TypeError("Type in tuple must be consistent (integer)"); } item = PyTuple_GetItem(value, 2); if (PyLong_Check(item)) { - cCol.b = PyLong_AsLong(item) / 255.0; + cCol.b = static_cast(PyLong_AsLong(item)) / 255.0F; } else { throw Base::TypeError("Type in tuple must be consistent (integer)"); @@ -83,7 +83,7 @@ Color parseColor(PyObject* value) if (PyTuple_Size(value) == 4) { item = PyTuple_GetItem(value, 3); if (PyLong_Check(item)) { - cCol.a = PyLong_AsLong(item) / 255.0; + cCol.a = static_cast(PyLong_AsLong(item)) / 255.0F; } else { throw Base::TypeError("Type in tuple must be consistent (integer)"); @@ -178,7 +178,7 @@ std::string MaterialPy::representation() const PyObject* MaterialPy::set(PyObject* args) { - char* pstr; + char* pstr {}; if (!PyArg_ParseTuple(args, "s", &pstr)) { return nullptr; } @@ -200,7 +200,7 @@ Py::Object MaterialPy::getAmbientColor() const void MaterialPy::setAmbientColor(Py::Object arg) { - getMaterialPtr()->ambientColor = parseColor(*arg); + getMaterialPtr()->ambientColor = toColor(*arg); } Py::Object MaterialPy::getDiffuseColor() const @@ -215,7 +215,7 @@ Py::Object MaterialPy::getDiffuseColor() const void MaterialPy::setDiffuseColor(Py::Object arg) { - getMaterialPtr()->diffuseColor = parseColor(*arg); + getMaterialPtr()->diffuseColor = toColor(*arg); } Py::Object MaterialPy::getEmissiveColor() const @@ -230,7 +230,7 @@ Py::Object MaterialPy::getEmissiveColor() const void MaterialPy::setEmissiveColor(Py::Object arg) { - getMaterialPtr()->emissiveColor = parseColor(*arg); + getMaterialPtr()->emissiveColor = toColor(*arg); } Py::Object MaterialPy::getSpecularColor() const @@ -245,7 +245,7 @@ Py::Object MaterialPy::getSpecularColor() const void MaterialPy::setSpecularColor(Py::Object arg) { - getMaterialPtr()->specularColor = parseColor(*arg); + getMaterialPtr()->specularColor = toColor(*arg); } Py::Float MaterialPy::getShininess() const diff --git a/src/App/PropertyStandard.cpp b/src/App/PropertyStandard.cpp index ef5b1915d6..6319d289af 100644 --- a/src/App/PropertyStandard.cpp +++ b/src/App/PropertyStandard.cpp @@ -2560,83 +2560,11 @@ PyObject* PropertyMaterial::getPyObject() void PropertyMaterial::setPyObject(PyObject* value) { - App::Color cCol; if (PyObject_TypeCheck(value, &(MaterialPy::Type))) { setValue(*static_cast(value)->getMaterialPtr()); } - else if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) { - PyObject* item; - item = PyTuple_GetItem(value, 0); - if (PyFloat_Check(item)) { - cCol.r = (float)PyFloat_AsDouble(item); - item = PyTuple_GetItem(value, 1); - if (PyFloat_Check(item)) { - cCol.g = (float)PyFloat_AsDouble(item); - } - else { - throw Base::TypeError("Type in tuple must be consistent (float)"); - } - item = PyTuple_GetItem(value, 2); - if (PyFloat_Check(item)) { - cCol.b = (float)PyFloat_AsDouble(item); - } - else { - throw Base::TypeError("Type in tuple must be consistent (float)"); - } - if (PyTuple_Size(value) == 4) { - item = PyTuple_GetItem(value, 3); - if (PyFloat_Check(item)) { - cCol.a = (float)PyFloat_AsDouble(item); - } - else { - throw Base::TypeError("Type in tuple must be consistent (float)"); - } - } - - setValue(cCol); - } - else if (PyLong_Check(item)) { - cCol.r = PyLong_AsLong(item) / 255.0; - item = PyTuple_GetItem(value, 1); - if (PyLong_Check(item)) { - cCol.g = PyLong_AsLong(item) / 255.0; - } - else { - throw Base::TypeError("Type in tuple must be consistent (integer)"); - } - item = PyTuple_GetItem(value, 2); - if (PyLong_Check(item)) { - cCol.b = PyLong_AsLong(item) / 255.0; - } - else { - throw Base::TypeError("Type in tuple must be consistent (integer)"); - } - if (PyTuple_Size(value) == 4) { - item = PyTuple_GetItem(value, 3); - if (PyLong_Check(item)) { - cCol.a = PyLong_AsLong(item) / 255.0; - } - else { - throw Base::TypeError("Type in tuple must be consistent (integer)"); - } - } - - setValue(cCol); - } - else { - throw Base::TypeError("Type in tuple must be float or integer"); - } - } - else if (PyLong_Check(value)) { - cCol.setPackedValue(PyLong_AsUnsignedLong(value)); - - setValue(cCol); - } else { - std::string error = std::string( - "type must be 'Material', integer, tuple of float, or tuple of integer, not "); - error += value->ob_type->tp_name; - throw Base::TypeError(error); + setValue(MaterialPy::toColor(value)); } } @@ -3269,7 +3197,6 @@ void PropertyMaterialList::RestoreDocFileV1(Base::Reader& reader) std::vector values(count); uint32_t value; // must be 32 bit long float valueF; - char valueS[37]; // UUID length is 36 including '-'s for (auto& it : values) { str >> value; it.ambientColor.setPackedValue(value);