From 33b4d00872c349ec79867c1d6f23d9ff64090f14 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 25 Oct 2020 05:05:56 +0100 Subject: [PATCH] Part: Geometry - refactor py functions into class for code reuse --- src/Mod/Part/App/Geometry.cpp | 52 ++++++++++++++++++++++++++++++ src/Mod/Part/App/Geometry.h | 9 ++++++ src/Mod/Part/App/GeometryPyImp.cpp | 38 +++++----------------- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 8bbf143b72..9276f242d4 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -383,6 +383,58 @@ Geometry *Geometry::clone(void) const return cpy; } +void Geometry::mirror(Base::Vector3d point) +{ + gp_Pnt pnt(point.x, point.y, point.z); + handle()->Mirror(pnt); +} + +void Geometry::mirror(Base::Vector3d point, Base::Vector3d dir) +{ + gp_Ax1 ax1(gp_Pnt(point.x,point.y,point.z), gp_Dir(dir.x,dir.y,dir.z)); + handle()->Mirror(ax1); +} + +void Geometry::rotate(Base::Placement plm) +{ + Base::Rotation rot(plm.getRotation()); + Base::Vector3d pnt, dir; + + double angle; + + rot.getValue(dir, angle); + pnt = plm.getPosition(); + + gp_Ax1 ax1(gp_Pnt(pnt.x,pnt.y,pnt.z), gp_Dir(dir.x,dir.y,dir.z)); + handle()->Rotate(ax1, angle); +} + +void Geometry::scale(Base::Vector3d vec, double scale) +{ + gp_Pnt pnt(vec.x, vec.y, vec.z); + handle()->Scale(pnt, scale); +} + +void Geometry::transform(Base::Matrix4D mat) +{ + gp_Trsf trf; + trf.SetValues(mat[0][0],mat[0][1],mat[0][2],mat[0][3], + mat[1][0],mat[1][1],mat[1][2],mat[1][3], + mat[2][0],mat[2][1],mat[2][2],mat[2][3] +#if OCC_VERSION_HEX < 0x060800 + , 0.00001,0.00001 +#endif + ); //precision was removed in OCCT CR0025194 + handle()->Transform(trf); +} + +void Geometry::translate(Base::Vector3d vec) +{ + gp_Vec trl(vec.x, vec.y, vec.z); + handle()->Translate(trl); +} + + // ------------------------------------------------- TYPESYSTEM_SOURCE(Part::GeomPoint,Part::Geometry) diff --git a/src/Mod/Part/App/Geometry.h b/src/Mod/Part/App/Geometry.h index 4a00c13f61..2d7e3c0a93 100644 --- a/src/Mod/Part/App/Geometry.h +++ b/src/Mod/Part/App/Geometry.h @@ -60,6 +60,8 @@ #include #include #include +#include +#include #include #include @@ -109,6 +111,13 @@ public: void deleteExtension(Base::Type type); void deleteExtension(std::string name); + void mirror(Base::Vector3d point); + void mirror(Base::Vector3d point, Base::Vector3d dir); + void rotate(Base::Placement plm); + void scale(Base::Vector3d vec, double scale); + void transform(Base::Matrix4D mat); + void translate(Base::Vector3d vec); + protected: /// create a new tag for the geometry object void createNewTag(); diff --git a/src/Mod/Part/App/GeometryPyImp.cpp b/src/Mod/Part/App/GeometryPyImp.cpp index e6a2b36d3f..ef1025f77f 100644 --- a/src/Mod/Part/App/GeometryPyImp.cpp +++ b/src/Mod/Part/App/GeometryPyImp.cpp @@ -82,8 +82,7 @@ PyObject* GeometryPy::mirror(PyObject *args) PyObject* o; if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type),&o)) { Base::Vector3d vec = static_cast(o)->value(); - gp_Pnt pnt(vec.x, vec.y, vec.z); - getGeometryPtr()->handle()->Mirror(pnt); + getGeometryPtr()->mirror(vec); Py_Return; } @@ -93,8 +92,7 @@ PyObject* GeometryPy::mirror(PyObject *args) &(Base::VectorPy::Type),&axis)) { Base::Vector3d pnt = static_cast(o)->value(); Base::Vector3d dir = static_cast(axis)->value(); - gp_Ax1 ax1(gp_Pnt(pnt.x,pnt.y,pnt.z), gp_Dir(dir.x,dir.y,dir.z)); - getGeometryPtr()->handle()->Mirror(ax1); + getGeometryPtr()->mirror(pnt, dir); Py_Return; } @@ -109,15 +107,7 @@ PyObject* GeometryPy::rotate(PyObject *args) return 0; Base::Placement* plm = static_cast(o)->getPlacementPtr(); - Base::Rotation rot(plm->getRotation()); - Base::Vector3d pnt, dir; - double angle; - - rot.getValue(dir, angle); - pnt = plm->getPosition(); - - gp_Ax1 ax1(gp_Pnt(pnt.x,pnt.y,pnt.z), gp_Dir(dir.x,dir.y,dir.z)); - getGeometryPtr()->handle()->Rotate(ax1, angle); + getGeometryPtr()->rotate(*plm); Py_Return; } @@ -128,16 +118,14 @@ PyObject* GeometryPy::scale(PyObject *args) Base::Vector3d vec; if (PyArg_ParseTuple(args, "O!d", &(Base::VectorPy::Type),&o, &scale)) { vec = static_cast(o)->value(); - gp_Pnt pnt(vec.x, vec.y, vec.z); - getGeometryPtr()->handle()->Scale(pnt, scale); + getGeometryPtr()->scale(vec, scale); Py_Return; } PyErr_Clear(); if (PyArg_ParseTuple(args, "O!d", &PyTuple_Type,&o, &scale)) { vec = Base::getVectorFromTuple(o); - gp_Pnt pnt(vec.x, vec.y, vec.z); - getGeometryPtr()->handle()->Scale(pnt, scale); + getGeometryPtr()->scale(vec, scale); Py_Return; } @@ -151,15 +139,7 @@ PyObject* GeometryPy::transform(PyObject *args) if (!PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type),&o)) return 0; Base::Matrix4D mat = static_cast(o)->value(); - gp_Trsf trf; - trf.SetValues(mat[0][0],mat[0][1],mat[0][2],mat[0][3], - mat[1][0],mat[1][1],mat[1][2],mat[1][3], - mat[2][0],mat[2][1],mat[2][2],mat[2][3] -#if OCC_VERSION_HEX < 0x060800 - , 0.00001,0.00001 -#endif - ); //precision was removed in OCCT CR0025194 - getGeometryPtr()->handle()->Transform(trf); + getGeometryPtr()->transform(mat); Py_Return; } @@ -169,16 +149,14 @@ PyObject* GeometryPy::translate(PyObject *args) Base::Vector3d vec; if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type),&o)) { vec = static_cast(o)->value(); - gp_Vec trl(vec.x, vec.y, vec.z); - getGeometryPtr()->handle()->Translate(trl); + getGeometryPtr()->translate(vec); Py_Return; } PyErr_Clear(); if (PyArg_ParseTuple(args, "O!", &PyTuple_Type,&o)) { vec = Base::getVectorFromTuple(o); - gp_Vec trl(vec.x, vec.y, vec.z); - getGeometryPtr()->handle()->Translate(trl); + getGeometryPtr()->translate(vec); Py_Return; }