From 308fa5b73a89dd2aaca6c9be0095703181a3bf4a Mon Sep 17 00:00:00 2001 From: marioalexis Date: Sun, 6 Nov 2022 19:05:45 -0300 Subject: [PATCH] TechDraw: Improve code in Python CosmeticEdgePy class --- src/Mod/TechDraw/App/CosmeticEdgePy.xml | 12 +- src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp | 172 ++++++--------------- 2 files changed, 53 insertions(+), 131 deletions(-) diff --git a/src/Mod/TechDraw/App/CosmeticEdgePy.xml b/src/Mod/TechDraw/App/CosmeticEdgePy.xml index ea534e2338..5f1f8ac074 100644 --- a/src/Mod/TechDraw/App/CosmeticEdgePy.xml +++ b/src/Mod/TechDraw/App/CosmeticEdgePy.xml @@ -7,7 +7,7 @@ TwinPointer="CosmeticEdge" Include="Mod/TechDraw/App/Cosmetic.h" Namespace="TechDraw" - FatherInclude="Base/PyObjectBase.h" + FatherInclude="Base/GeometryPyCXX.h" FatherNamespace="Base" Constructor="true" Delete="true"> @@ -41,25 +41,25 @@ Gives the position of one end of this CosmeticEdge as vector. - + Gives the position of one end of this CosmeticEdge as vector. - + Gives the position of center point of this CosmeticEdge as vector. - + Gives the radius of CosmeticEdge in mm. - + @@ -73,7 +73,7 @@ The appearance attributes (style, weight, color, visible) for this CosmeticEdge. - + diff --git a/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp b/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp index 1091c5dec7..e2cfabc651 100644 --- a/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp +++ b/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp @@ -27,11 +27,6 @@ # include #endif -#include -#include -#include -#include - #include "CosmeticEdgePy.h" #include "CosmeticEdgePy.cpp" #include "Cosmetic.h" @@ -77,7 +72,7 @@ PyObject* CosmeticEdgePy::clone(PyObject *args) if (type->tp_new) cpy = type->tp_new(type, this, nullptr); if (!cpy) { - PyErr_SetString(PyExc_TypeError, "failed to create clone of CosmeticEdge"); + PyErr_SetString(PyExc_RuntimeError, "failed to create clone of CosmeticEdge"); return nullptr; } @@ -104,7 +99,7 @@ PyObject* CosmeticEdgePy::copy(PyObject *args) if (type->tp_new) cpy = type->tp_new(type, this, nullptr); if (!cpy) { - PyErr_SetString(PyExc_TypeError, "failed to create copy of CosmeticEdge"); + PyErr_SetString(PyExc_RuntimeError, "failed to create copy of CosmeticEdge"); return nullptr; } @@ -119,55 +114,38 @@ PyObject* CosmeticEdgePy::copy(PyObject *args) return cpy; } -void CosmeticEdgePy::setFormat(Py::Object arg) +void CosmeticEdgePy::setFormat(Py::Dict arg) { - PyObject* pTuple = arg.ptr(); + Py::Tuple dummy; + Py::TupleN color(Py::Float(0.0), Py::Float(0.0), Py::Float(0.0), Py::Float(0.0)); int style = 1; - double weight = 0.50; - double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; - App::Color c(red, blue, green, alpha); - bool visible = 1; - - TechDraw::CosmeticEdge* ce = this->getCosmeticEdgePtr(); - if (PyTuple_Check(pTuple)) { - int tSize = (int) PyTuple_Size(pTuple); - if (tSize > 3) { - PyObject* pStyle = PyTuple_GetItem(pTuple, 0); - style = (int) PyLong_AsLong(pStyle); - PyObject* pWeight = PyTuple_GetItem(pTuple, 1); - weight = PyFloat_AsDouble(pWeight); - PyObject* pColor = PyTuple_GetItem(pTuple, 2); - c = DrawUtil::pyTupleToColor(pColor); - PyObject* pVisible = PyTuple_GetItem(pTuple, 3); - visible = (bool) PyLong_AsLong(pVisible); - - ce->m_format.m_style = style; - ce->m_format.m_weight = weight; - ce->m_format.m_color = c; - ce->m_format.m_visible = visible; - } - } else { - Base::Console().Error("CEPI::setFormat - not a tuple!\n"); + double weight = 0.5; + PyObject* pColor = color.ptr(); + PyObject* visible = Py_True; + static char* kw[] = {"style", "weight", "color", "visible", nullptr}; + if (!PyArg_ParseTupleAndKeywords(dummy.ptr(), arg.ptr(), "|idO!O!", kw, + &style, &weight, &PyTuple_Type, &pColor, &PyBool_Type, &visible)) { + throw Py::ValueError("Expected {'style':int, 'weight':float, 'color':tuple, 'visible':bool} dict"); } + + TechDraw::LineFormat* format = &(this->getCosmeticEdgePtr()->m_format); + format->m_style = style; + format->m_weight = weight; + format->m_color = DrawUtil::pyTupleToColor(pColor); + format->m_visible = Base::asBoolean(visible); } -Py::Object CosmeticEdgePy::getFormat() const +Py::Dict CosmeticEdgePy::getFormat() const { - TechDraw::CosmeticEdge* ce = this->getCosmeticEdgePtr(); + TechDraw::LineFormat* format= &(this->getCosmeticEdgePtr()->m_format); + Py::Dict dict; - PyObject* pStyle = PyLong_FromLong((long) ce->m_format.m_style); - PyObject* pWeight = PyFloat_FromDouble(ce->m_format.m_weight); - PyObject* pColor = DrawUtil::colorToPyTuple(ce->m_format.m_color); - PyObject* pVisible = PyBool_FromLong((long) ce->m_format.m_visible); + dict.setItem("style", Py::Long(format->m_style)); + dict.setItem("weight", Py::Float(format->m_weight)); + dict.setItem("color", Py::Tuple(DrawUtil::colorToPyTuple(format->m_color))); + dict.setItem("visible", Py::Boolean(format->m_visible)); - PyObject* result = PyTuple_New(4); - - PyTuple_SET_ITEM(result, 0, pStyle); - PyTuple_SET_ITEM(result, 1, pWeight); - PyTuple_SET_ITEM(result, 2, pColor); - PyTuple_SET_ITEM(result, 3, pVisible); - - return Py::asObject(result); + return dict; } Py::String CosmeticEdgePy::getTag() const @@ -205,28 +183,16 @@ Py::String CosmeticEdgePy::getTag() const // } //} -Py::Object CosmeticEdgePy::getStart() const +Py::Vector CosmeticEdgePy::getStart() const { Base::Vector3d point = getCosmeticEdgePtr()->permaStart; point = DrawUtil::invertY(point); - return Py::asObject(new Base::VectorPy(point)); + return Py::Vector(point); } -void CosmeticEdgePy::setStart(Py::Object arg) +void CosmeticEdgePy::setStart(Py::Vector arg) { - PyObject* p = arg.ptr(); - Base::Vector3d pNew; - if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) { - pNew = static_cast(p)->value(); - } - else if (PyObject_TypeCheck(p, &PyTuple_Type)) { - pNew = Base::getVectorFromTuple(p); - } - else { - std::string error = std::string("type must be 'Vector', not "); - error += p->ob_type->tp_name; - throw Py::TypeError(error); - } + Base::Vector3d pNew = static_cast(arg); pNew = DrawUtil::invertY(pNew); Base::Vector3d pEnd = getCosmeticEdgePtr()->permaEnd; @@ -239,28 +205,16 @@ void CosmeticEdgePy::setStart(Py::Object arg) // delete oldGeom; } -Py::Object CosmeticEdgePy::getEnd() const +Py::Vector CosmeticEdgePy::getEnd() const { Base::Vector3d point = getCosmeticEdgePtr()->permaEnd; point = DrawUtil::invertY(point); - return Py::asObject(new Base::VectorPy(point)); + return Py::Vector(point); } -void CosmeticEdgePy::setEnd(Py::Object arg) +void CosmeticEdgePy::setEnd(Py::Vector arg) { - PyObject* p = arg.ptr(); - Base::Vector3d pNew; - if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) { - pNew = static_cast(p)->value(); - } - else if (PyObject_TypeCheck(p, &PyTuple_Type)) { - pNew = Base::getVectorFromTuple(p); - } - else { - std::string error = std::string("type must be 'Vector', not "); - error += p->ob_type->tp_name; - throw Py::TypeError(error); - } + Base::Vector3d pNew = static_cast(arg); pNew = DrawUtil::invertY(pNew); Base::Vector3d pStart = getCosmeticEdgePtr()->permaStart; @@ -273,42 +227,26 @@ void CosmeticEdgePy::setEnd(Py::Object arg) // delete oldGeom; } -Py::Object CosmeticEdgePy::getRadius() const +Py::Float CosmeticEdgePy::getRadius() const { TechDraw::GeomType gt = getCosmeticEdgePtr()->m_geometry->geomType; if ( (gt != TechDraw::GeomType::CIRCLE) && (gt != TechDraw::GeomType::ARCOFCIRCLE) ) { - std::string error = "not a circle. Can not set radius"; - throw Py::TypeError(error); + throw Py::TypeError("Not a circle. Can not get radius"); } double r = getCosmeticEdgePtr()->permaRadius; - return Py::asObject(PyFloat_FromDouble(r)); + return Py::Float(r); } -void CosmeticEdgePy::setRadius(Py::Object arg) +void CosmeticEdgePy::setRadius(Py::Float arg) { TechDraw::GeomType gt = getCosmeticEdgePtr()->m_geometry->geomType; - PyObject* p = arg.ptr(); if ( (gt != TechDraw::GeomType::CIRCLE) && (gt != TechDraw::GeomType::ARCOFCIRCLE) ) { - std::string error = std::string(p->ob_type->tp_name); - error += " is not a circle. Can not set radius"; - throw Py::TypeError(error); - } - - double r; - if (PyObject_TypeCheck(p, &PyFloat_Type)) { - r = PyFloat_AsDouble(p); - } - else if (PyObject_TypeCheck(p, &PyLong_Type)) { - r = (double) PyLong_AsLong(p); - } - else { - std::string error = std::string("type must be 'Float' or 'Int', not "); - error += p->ob_type->tp_name; - throw Py::TypeError(error); + throw Py::TypeError("Not a circle. Can not set radius"); } + double r = static_cast(arg); getCosmeticEdgePtr()->permaRadius = r; // auto oldGeom = getCosmeticEdgePtr()->m_geometry; getCosmeticEdgePtr()->m_geometry = @@ -316,44 +254,28 @@ void CosmeticEdgePy::setRadius(Py::Object arg) // delete oldGeom; } -Py::Object CosmeticEdgePy::getCenter() const +Py::Vector CosmeticEdgePy::getCenter() const { TechDraw::GeomType gt = getCosmeticEdgePtr()->m_geometry->geomType; if ( (gt != TechDraw::GeomType::CIRCLE) && (gt != TechDraw::GeomType::ARCOFCIRCLE) ) { - std::string error = "not a circle. Can not get center"; - throw Py::TypeError(error); + throw Py::TypeError("Not a circle. Can not get center"); } Base::Vector3d point = getCosmeticEdgePtr()->permaStart; point = DrawUtil::invertY(point); - return Py::asObject(new Base::VectorPy(point)); + return Py::Vector(point); } -void CosmeticEdgePy::setCenter(Py::Object arg) +void CosmeticEdgePy::setCenter(Py::Vector arg) { TechDraw::GeomType gt = getCosmeticEdgePtr()->m_geometry->geomType; - PyObject* p = arg.ptr(); +// PyObject* p = arg.ptr(); if ( (gt != TechDraw::GeomType::CIRCLE) && (gt != TechDraw::GeomType::ARCOFCIRCLE) ) { - std::string error = std::string(p->ob_type->tp_name); - error += " is not a circle. Can not set center"; - throw Py::TypeError(error); - } - -// PyObject* p = arg.ptr(); - Base::Vector3d pNew; - if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) { - pNew = static_cast(p)->value(); - } - else if (PyObject_TypeCheck(p, &PyTuple_Type)) { - pNew = Base::getVectorFromTuple(p); - } - else { - std::string error = std::string("type must be 'Vector', not "); - error += p->ob_type->tp_name; - throw Py::TypeError(error); + throw Py::TypeError("Not a circle. Can not set center"); } + Base::Vector3d pNew = static_cast(arg); pNew = DrawUtil::invertY(pNew); auto oldGeom = getCosmeticEdgePtr()->m_geometry; TechDraw::CirclePtr oldCircle = std::dynamic_pointer_cast (oldGeom);