Skip to content

Commit

Permalink
Fix remaining test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
davesrocketshop committed Feb 14, 2024
1 parent 9297f89 commit 17f567f
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 65 deletions.
8 changes: 4 additions & 4 deletions src/App/MaterialPy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ Satin, Metalized, Neon GNC, Chrome, Aluminium, Obsidian, Neon PHC, Jade, Ruby or
<Documentation>
<UserDocu>Ambient color</UserDocu>
</Documentation>
<Parameter Name="AmbientColor" Type="Tuple"/>
<Parameter Name="AmbientColor" Type="Object"/>
</Attribute>
<Attribute Name="DiffuseColor" ReadOnly="false">
<Documentation>
<UserDocu>Diffuse color</UserDocu>
</Documentation>
<Parameter Name="DiffuseColor" Type="Tuple"/>
<Parameter Name="DiffuseColor" Type="Object"/>
</Attribute>
<Attribute Name="EmissiveColor" ReadOnly="false">
<Documentation>
<UserDocu>Emissive color</UserDocu>
</Documentation>
<Parameter Name="EmissiveColor" Type="Tuple"/>
<Parameter Name="EmissiveColor" Type="Object"/>
</Attribute>
<Attribute Name="SpecularColor" ReadOnly="false">
<Documentation>
<UserDocu>Specular color</UserDocu>
</Documentation>
<Parameter Name="SpecularColor" Type="Tuple"/>
<Parameter Name="SpecularColor" Type="Object"/>
</Attribute>
<Attribute Name="Shininess" ReadOnly="false">
<Documentation>
Expand Down
167 changes: 117 additions & 50 deletions src/App/MaterialPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,89 @@

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

#include "MaterialPy.cpp"

Check warning on line 29 in src/App/MaterialPyImp.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

suspicious #include of file with '.cpp' extension [bugprone-suspicious-include]

#include <Base/PyWrapParseTupleAndKeywords.h>

using namespace App;

PyObject *MaterialPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
Color parseColor(PyObject* value)

Check warning on line 35 in src/App/MaterialPyImp.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

function 'parseColor' has cognitive complexity of 41 (threshold 25) [readability-function-cognitive-complexity]
{
Color cCol;
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)");
}
}
}
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)");
}
}
}
else {
throw Base::TypeError("Type in tuple must be float or integer");
}
}
else if (PyLong_Check(value)) {
cCol.setPackedValue(PyLong_AsUnsignedLong(value));
}
else {
std::string error =
std::string("type must be integer or tuple of float or tuple integer, not ");
error += value->ob_type->tp_name;
throw Base::TypeError(error);
}

return cCol;
}

PyObject* MaterialPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
{
// create a new instance of MaterialPy and the Twin object
return new MaterialPy(new Material);
Expand All @@ -45,28 +122,41 @@ int MaterialPy::PyInit(PyObject* args, PyObject* kwds)
PyObject* emissive = nullptr;
PyObject* shininess = nullptr;
PyObject* transparency = nullptr;
static const std::array<const char *, 7> kwds_colors{"DiffuseColor", "AmbientColor", "SpecularColor",
"EmissiveColor", "Shininess", "Transparency", nullptr};

if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "|OOOOOO", kwds_colors,
&diffuse, &ambient, &specular, &emissive, &shininess, &transparency)) {
static const std::array<const char*, 7> kwds_colors {"DiffuseColor",
"AmbientColor",
"SpecularColor",
"EmissiveColor",
"Shininess",
"Transparency",
nullptr};

if (!Base::Wrapped_ParseTupleAndKeywords(args,
kwds,
"|OOOOOO",
kwds_colors,
&diffuse,
&ambient,
&specular,
&emissive,
&shininess,
&transparency)) {
return -1;
}

if (diffuse) {
setDiffuseColor(Py::Tuple(diffuse));
setDiffuseColor(Py::Object(diffuse));
}

if (ambient) {
setAmbientColor(Py::Tuple(ambient));
setAmbientColor(Py::Object(ambient));
}

if (specular) {
setSpecularColor(Py::Tuple(specular));
setSpecularColor(Py::Object(specular));
}

if (emissive) {
setEmissiveColor(Py::Tuple(emissive));
setEmissiveColor(Py::Object(emissive));
}

if (shininess) {
Expand All @@ -86,18 +176,19 @@ std::string MaterialPy::representation() const
return {"<Material object>"};
}

PyObject* MaterialPy::set(PyObject * args)
PyObject* MaterialPy::set(PyObject* args)
{
char *pstr;
if (!PyArg_ParseTuple(args, "s", &pstr))
char* pstr;
if (!PyArg_ParseTuple(args, "s", &pstr)) {
return nullptr;
}

getMaterialPtr()->set(pstr);

Py_Return;
}

Py::Tuple MaterialPy::getAmbientColor() const
Py::Object MaterialPy::getAmbientColor() const
{
Py::Tuple tuple(4);
tuple.setItem(0, Py::Float(getMaterialPtr()->ambientColor.r));
Expand All @@ -107,18 +198,12 @@ Py::Tuple MaterialPy::getAmbientColor() const
return tuple;
}

void MaterialPy::setAmbientColor(Py::Tuple arg)
void MaterialPy::setAmbientColor(Py::Object arg)
{
Color c;
c.r = Py::Float(arg.getItem(0));
c.g = Py::Float(arg.getItem(1));
c.b = Py::Float(arg.getItem(2));
if (arg.size() == 4)
c.a = Py::Float(arg.getItem(3));
getMaterialPtr()->ambientColor = c;
getMaterialPtr()->ambientColor = parseColor(*arg);
}

Py::Tuple MaterialPy::getDiffuseColor() const
Py::Object MaterialPy::getDiffuseColor() const
{
Py::Tuple tuple(4);
tuple.setItem(0, Py::Float(getMaterialPtr()->diffuseColor.r));
Expand All @@ -128,18 +213,12 @@ Py::Tuple MaterialPy::getDiffuseColor() const
return tuple;
}

void MaterialPy::setDiffuseColor(Py::Tuple arg)
void MaterialPy::setDiffuseColor(Py::Object arg)
{
Color c;
c.r = Py::Float(arg.getItem(0));
c.g = Py::Float(arg.getItem(1));
c.b = Py::Float(arg.getItem(2));
if (arg.size() == 4)
c.a = Py::Float(arg.getItem(3));
getMaterialPtr()->diffuseColor = c;
getMaterialPtr()->diffuseColor = parseColor(*arg);
}

Py::Tuple MaterialPy::getEmissiveColor() const
Py::Object MaterialPy::getEmissiveColor() const
{
Py::Tuple tuple(4);
tuple.setItem(0, Py::Float(getMaterialPtr()->emissiveColor.r));
Expand All @@ -149,18 +228,12 @@ Py::Tuple MaterialPy::getEmissiveColor() const
return tuple;
}

void MaterialPy::setEmissiveColor(Py::Tuple arg)
void MaterialPy::setEmissiveColor(Py::Object arg)
{
Color c;
c.r = Py::Float(arg.getItem(0));
c.g = Py::Float(arg.getItem(1));
c.b = Py::Float(arg.getItem(2));
if (arg.size() == 4)
c.a = Py::Float(arg.getItem(3));
getMaterialPtr()->emissiveColor = c;
getMaterialPtr()->emissiveColor = parseColor(*arg);
}

Py::Tuple MaterialPy::getSpecularColor() const
Py::Object MaterialPy::getSpecularColor() const
{
Py::Tuple tuple(4);
tuple.setItem(0, Py::Float(getMaterialPtr()->specularColor.r));
Expand All @@ -170,15 +243,9 @@ Py::Tuple MaterialPy::getSpecularColor() const
return tuple;
}

void MaterialPy::setSpecularColor(Py::Tuple arg)
void MaterialPy::setSpecularColor(Py::Object arg)
{
Color c;
c.r = Py::Float(arg.getItem(0));
c.g = Py::Float(arg.getItem(1));
c.b = Py::Float(arg.getItem(2));
if (arg.size() == 4)
c.a = Py::Float(arg.getItem(3));
getMaterialPtr()->specularColor = c;
getMaterialPtr()->specularColor = parseColor(*arg);
}

Py::Float MaterialPy::getShininess() const
Expand All @@ -201,7 +268,7 @@ void MaterialPy::setTransparency(Py::Float arg)
getMaterialPtr()->transparency = arg;
}

PyObject *MaterialPy::getCustomAttributes(const char* /*attr*/) const
PyObject* MaterialPy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions src/Gui/ViewProviderGeometryObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ViewProviderGeometryObject::ViewProviderGeometryObject()
App::Material mat(App::Material::DEFAULT);
mat.transparency = (float)initialTransparency / 100.0f;
ADD_PROPERTY_TYPE(ShapeMaterial, (mat), osgroup, App::Prop_None, "Shape material");
mat.diffuseColor = App::Color(r, g, b);
ADD_PROPERTY_TYPE(ShapeAppearance, (mat), osgroup, App::Prop_None, "Shape appearrance");
ADD_PROPERTY_TYPE(BoundingBox, (false), dogroup, App::Prop_None, "Display object bounding box");
ADD_PROPERTY_TYPE(Selectable,
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Draft/draftviewproviders/view_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def set_visual_properties(self, vobj, properties):
"ShapeAppearance",
"Layer",
_tip)
vobj.ShapeAppearance = params.get_param_view("DefaultShapeColor") & 0xFFFFFF00
vobj.ShapeAppearance.DiffuseColor = params.get_param_view("DefaultShapeColor") & 0xFFFFFF00

Check warning on line 124 in src/Mod/Draft/draftviewproviders/view_layer.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (103/100) (line-too-long)

if "LineWidth" not in properties:
_tip = QT_TRANSLATE_NOOP("App::Property",
Expand Down
5 changes: 0 additions & 5 deletions src/Mod/Material/App/ModelPropertyPy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
<Author Licence="LGPL" Name="DavidCarter" EMail="dcarter@davidcarter.ca" />
<UserDocu>Material property descriptions.</UserDocu>
</Documentation>
<!-- <Methode Name="mirror">
<Documentation>
<UserDocu>Performs the symmetrical transformation of this geometric object</UserDocu>
</Documentation>
</Methode> -->
<Attribute Name="Name" ReadOnly="true">
<Documentation>
<UserDocu>Property name.</UserDocu>
Expand Down
7 changes: 2 additions & 5 deletions src/Mod/Part/parttests/ColorTransparencyTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,15 @@ def test_default_shape_color(self):
"""
related: https://github.com/FreeCAD/FreeCAD/pull/11866
"""

"""
This test isn't currently valid as it draws from the hard coded default material.
"""

self._pg.SetUnsigned('DefaultShapeColor', 0xff000000) # red
obj = self._doc.addObject('Part::Box')

# self.assertEqual(obj.ViewObject.ShapeColor, (1.0, 0.0, 0.0, 0.0),
# 'default shape color was not set correctly')
self.assertEqual(obj.ViewObject.ShapeAppearance.DiffuseColor, (1.0, 0.0, 0.0, 0.0),
'default shape color was not set correctly')
self.assertNotEqual(obj.ViewObject.ShapeMaterial.DiffuseColor, (1.0, 0.0, 0.0, 0.0),
'default material color was not set correctly')


def test_app_plane_transparency(self):
Expand Down

0 comments on commit 17f567f

Please sign in to comment.