TechDraw: Improve code in Python CosmeticEdgePy class

This commit is contained in:
marioalexis
2022-11-06 19:05:45 -03:00
committed by WandererFan
parent 67ec157e59
commit 308fa5b73a
2 changed files with 53 additions and 131 deletions

View File

@@ -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 @@
<Documentation>
<UserDocu>Gives the position of one end of this CosmeticEdge as vector.</UserDocu>
</Documentation>
<Parameter Name="Start" Type="Object"/>
<Parameter Name="Start" Type="Vector"/>
</Attribute>
<Attribute Name="End">
<Documentation>
<UserDocu>Gives the position of one end of this CosmeticEdge as vector.</UserDocu>
</Documentation>
<Parameter Name="End" Type="Object"/>
<Parameter Name="End" Type="Vector"/>
</Attribute>
<Attribute Name="Center">
<Documentation>
<UserDocu>Gives the position of center point of this CosmeticEdge as vector.</UserDocu>
</Documentation>
<Parameter Name="Center" Type="Object"/>
<Parameter Name="Center" Type="Vector"/>
</Attribute>
<Attribute Name="Radius">
<Documentation>
<UserDocu>Gives the radius of CosmeticEdge in mm.</UserDocu>
</Documentation>
<Parameter Name="Radius" Type="Object"/>
<Parameter Name="Radius" Type="Float"/>
</Attribute>
@@ -73,7 +73,7 @@
<Documentation>
<UserDocu>The appearance attributes (style, weight, color, visible) for this CosmeticEdge.</UserDocu>
</Documentation>
<Parameter Name="Format" Type="Object"/>
<Parameter Name="Format" Type="Dict"/>
</Attribute>
</PythonExport>
</GenerateModel>

View File

@@ -27,11 +27,6 @@
# include <boost/uuid/uuid_io.hpp>
#endif
#include <Base/Console.h>
#include <Base/Vector3D.h>
#include <Base/VectorPy.h>
#include <Base/GeometryPyCXX.h>
#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<Base::VectorPy*>(p)->value();
}
else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
pNew = Base::getVectorFromTuple<double>(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<Base::Vector3d>(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<Base::VectorPy*>(p)->value();
}
else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
pNew = Base::getVectorFromTuple<double>(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<Base::Vector3d>(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<double>(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<Base::VectorPy*>(p)->value();
}
else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
pNew = Base::getVectorFromTuple<double>(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<Base::Vector3d>(arg);
pNew = DrawUtil::invertY(pNew);
auto oldGeom = getCosmeticEdgePtr()->m_geometry;
TechDraw::CirclePtr oldCircle = std::dynamic_pointer_cast<TechDraw::Circle> (oldGeom);