From 9326a89323d3b33a7d116c27fc7a1fddda5699cd Mon Sep 17 00:00:00 2001 From: benoit128 Date: Fri, 6 Dec 2024 09:37:29 +0100 Subject: [PATCH] OCC: add rigid transforms --- Cassiopee/OCC/OCC/Atomic/rotate.cpp | 82 ++++++++++++++++++++++++++ Cassiopee/OCC/OCC/Atomic/scale.cpp | 74 +++++++++++++++++++++++ Cassiopee/OCC/OCC/Atomic/translate.cpp | 77 ++++++++++++++++++++++++ Cassiopee/OCC/OCC/occ.cpp | 4 ++ Cassiopee/OCC/OCC/occ.h | 4 ++ Cassiopee/OCC/srcs.py | 4 ++ 6 files changed, 245 insertions(+) create mode 100644 Cassiopee/OCC/OCC/Atomic/rotate.cpp create mode 100644 Cassiopee/OCC/OCC/Atomic/scale.cpp create mode 100644 Cassiopee/OCC/OCC/Atomic/translate.cpp diff --git a/Cassiopee/OCC/OCC/Atomic/rotate.cpp b/Cassiopee/OCC/OCC/Atomic/rotate.cpp new file mode 100644 index 000000000..049602aeb --- /dev/null +++ b/Cassiopee/OCC/OCC/Atomic/rotate.cpp @@ -0,0 +1,82 @@ +/* + Copyright 2013-2024 Onera. + + This file is part of Cassiopee. + + Cassiopee is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Cassiopee is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Cassiopee. If not, see . +*/ +// rotate CAD + +#include "occ.h" +#include "TopExp.hxx" +#include "TopExp_Explorer.hxx" +#include "TopTools_IndexedMapOfShape.hxx" +#include "TopoDS.hxx" +#include "BRepBuilderAPI_Transform.hxx" +#include +#include +#include + +//===================================================================== +// Rotate the full shape or some faces +// from an axis and an angle (point? +//===================================================================== +PyObject* K_OCC::rotate(PyObject* self, PyObject* args) +{ + PyObject* hook; E_Float angle; E_Float xc, yc, zc; E_Float xaxis, yaxis, zaxis; + if (!PYPARSETUPLE_(args, O_ TRRR_ TRRR_ R_, &hook, &xc, &yc, &zc, &xaxis, &yaxis, &zaxis, &angle)) return NULL; + + void** packet = NULL; +#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1) + packet = (void**) PyCObject_AsVoidPtr(hook); +#else + packet = (void**) PyCapsule_GetPointer(hook, NULL); +#endif + + TopoDS_Shape* shp = (TopoDS_Shape*)packet[0]; + + gp_Pnt center(xc, yc, zc); + gp_Dir direction(xaxis, yaxis, zaxis); + gp_Ax1 axis(center, direction); + + gp_Trsf myTrsf; + myTrsf.SetRotation(axis, angle); + + BRepBuilderAPI_Transform myTransform(*shp, myTrsf); + TopoDS_Shape tShape = myTransform.Shape(); + + TopoDS_Shape* newshp = new TopoDS_Shape(); + *newshp = tShape; + + // Rebuild the hook + packet[0] = newshp; + // Extract surfaces + TopTools_IndexedMapOfShape* ptr = (TopTools_IndexedMapOfShape*)packet[1]; + delete ptr; + TopTools_IndexedMapOfShape* sf = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(*newshp, TopAbs_FACE, *sf); + packet[1] = sf; + + // Extract edges + TopTools_IndexedMapOfShape* ptr2 = (TopTools_IndexedMapOfShape*)packet[2]; + delete ptr2; + TopTools_IndexedMapOfShape* se = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(*newshp, TopAbs_EDGE, *se); + packet[2] = se; + //printf("INFO: after fix: Nb edges=%d\n", se->Extent()); + //printf("INFO: after fix: Nb faces=%d\n", sf->Extent()); + + Py_INCREF(Py_None); + return Py_None; +} diff --git a/Cassiopee/OCC/OCC/Atomic/scale.cpp b/Cassiopee/OCC/OCC/Atomic/scale.cpp new file mode 100644 index 000000000..029aeafb1 --- /dev/null +++ b/Cassiopee/OCC/OCC/Atomic/scale.cpp @@ -0,0 +1,74 @@ +/* + Copyright 2013-2024 Onera. + + This file is part of Cassiopee. + + Cassiopee is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Cassiopee is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Cassiopee. If not, see . +*/ +// scale CAD + +#include "occ.h" +#include "TopExp.hxx" +#include "TopExp_Explorer.hxx" +#include "TopTools_IndexedMapOfShape.hxx" +#include "TopoDS.hxx" +#include "BRepBuilderAPI_Transform.hxx" + +//===================================================================== +// Translate the full shape or some faces +// from vector +//===================================================================== +PyObject* K_OCC::scale(PyObject* self, PyObject* args) +{ + PyObject* hook; E_Float factor; E_Float x0, y0, z0; + if (!PYPARSETUPLE_(args, O_ R_ TRRR_, &hook, &factor, &x0, &y0, &z0)) return NULL; + + void** packet = NULL; +#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1) + packet = (void**) PyCObject_AsVoidPtr(hook); +#else + packet = (void**) PyCapsule_GetPointer(hook, NULL); +#endif + + TopoDS_Shape* shp = (TopoDS_Shape*)packet[0]; + + // idem scale + gp_Trsf myTrsf; + myTrsf.SetScale(gp_Pnt(x0, y0, z0), factor); + + BRepBuilderAPI_Transform myTransform(*shp, myTrsf); + TopoDS_Shape tShape = myTransform.Shape(); + + TopoDS_Shape* newshp = new TopoDS_Shape(); + *newshp = tShape; + + // Rebuild the hook + packet[0] = newshp; + // Extract surfaces + TopTools_IndexedMapOfShape* ptr = (TopTools_IndexedMapOfShape*)packet[1]; + delete ptr; + TopTools_IndexedMapOfShape* sf = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(*newshp, TopAbs_FACE, *sf); + packet[1] = sf; + + // Extract edges + TopTools_IndexedMapOfShape* ptr2 = (TopTools_IndexedMapOfShape*)packet[2]; + delete ptr2; + TopTools_IndexedMapOfShape* se = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(*newshp, TopAbs_EDGE, *se); + packet[2] = se; + + Py_INCREF(Py_None); + return Py_None; +} diff --git a/Cassiopee/OCC/OCC/Atomic/translate.cpp b/Cassiopee/OCC/OCC/Atomic/translate.cpp new file mode 100644 index 000000000..3a8bac87b --- /dev/null +++ b/Cassiopee/OCC/OCC/Atomic/translate.cpp @@ -0,0 +1,77 @@ +/* + Copyright 2013-2024 Onera. + + This file is part of Cassiopee. + + Cassiopee is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Cassiopee is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Cassiopee. If not, see . +*/ +// translate CAD + +#include "occ.h" +#include "TopExp.hxx" +#include "TopExp_Explorer.hxx" +#include "TopTools_IndexedMapOfShape.hxx" +#include "TopoDS.hxx" +#include "BRepBuilderAPI_Transform.hxx" + +//===================================================================== +// Translate the full shape or some faces +// from vector +//===================================================================== +PyObject* K_OCC::translate(PyObject* self, PyObject* args) +{ + PyObject* hook; E_Float dx, dy, dz; + if (!PYPARSETUPLE_(args, O_ TRRR_, &hook, &dx, &dy, &dz)) return NULL; + + void** packet = NULL; +#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1) + packet = (void**) PyCObject_AsVoidPtr(hook); +#else + packet = (void**) PyCapsule_GetPointer(hook, NULL); +#endif + + TopoDS_Shape* shp = (TopoDS_Shape*)packet[0]; + + // idem scale + //gp_Trsf myTrsf; + //myTrsf.SetScale(gp_Pnt(0, 0, 0), scaleFactor); // Scale around the origin by 'scaleFactor' + + gp_Trsf myTrsf; + myTrsf.SetTranslation(gp_Vec(dx, dy, dz)); // Translate by (dx, dy, dz) + + BRepBuilderAPI_Transform myTransform(*shp, myTrsf); + TopoDS_Shape tShape = myTransform.Shape(); + + TopoDS_Shape* newshp = new TopoDS_Shape(); + *newshp = tShape; + + // Rebuild the hook + packet[0] = newshp; + // Extract surfaces + TopTools_IndexedMapOfShape* ptr = (TopTools_IndexedMapOfShape*)packet[1]; + delete ptr; + TopTools_IndexedMapOfShape* sf = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(*newshp, TopAbs_FACE, *sf); + packet[1] = sf; + + // Extract edges + TopTools_IndexedMapOfShape* ptr2 = (TopTools_IndexedMapOfShape*)packet[2]; + delete ptr2; + TopTools_IndexedMapOfShape* se = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(*newshp, TopAbs_EDGE, *se); + packet[2] = se; + + Py_INCREF(Py_None); + return Py_None; +} diff --git a/Cassiopee/OCC/OCC/occ.cpp b/Cassiopee/OCC/OCC/occ.cpp index ff3d0af9b..6b9c248e5 100644 --- a/Cassiopee/OCC/OCC/occ.cpp +++ b/Cassiopee/OCC/OCC/occ.cpp @@ -75,6 +75,10 @@ static PyMethodDef Pyocc [] = {"fillHole", K_OCC::fillHole, METH_VARARGS}, {"addFillet", K_OCC::addFillet, METH_VARARGS}, + {"translate", K_OCC::translate, METH_VARARGS}, + {"scale", K_OCC::scale, METH_VARARGS}, + {"rotate", K_OCC::rotate, METH_VARARGS}, + {"getOppData", K_OCC::getOppData, METH_VARARGS}, {NULL, NULL} diff --git a/Cassiopee/OCC/OCC/occ.h b/Cassiopee/OCC/OCC/occ.h index 3a60460fd..af2287971 100644 --- a/Cassiopee/OCC/OCC/occ.h +++ b/Cassiopee/OCC/OCC/occ.h @@ -74,6 +74,10 @@ namespace K_OCC PyObject* fillHole(PyObject* self, PyObject* args); PyObject* addFillet(PyObject* self, PyObject* args); + PyObject* translate(PyObject* self, PyObject* args); + PyObject* scale(PyObject* self, PyObject* args); + PyObject* rotate(PyObject* self, PyObject* args); + PyObject* getOppData(PyObject* self, PyObject* args); } diff --git a/Cassiopee/OCC/srcs.py b/Cassiopee/OCC/srcs.py index 47dbb723e..ac745c713 100644 --- a/Cassiopee/OCC/srcs.py +++ b/Cassiopee/OCC/srcs.py @@ -63,6 +63,10 @@ def getFiles(module): 'OCC/Atomic/fillHole.cpp', 'OCC/Atomic/addFillet.cpp', + 'OCC/Atomic/translate.cpp', + 'OCC/Atomic/scale.cpp', + 'OCC/Atomic/rotate.cpp', + 'OCC/Atomic/getOppData.cpp'] import KCore.Dist as Dist