From 1bc46f74adb78d8576ea62acebb68719d89a5c23 Mon Sep 17 00:00:00 2001 From: benoit128 Date: Fri, 6 Dec 2024 16:58:17 +0100 Subject: [PATCH] OCC: addSphere, addSquare --- Cassiopee/OCC/OCC/Atomic/addSphere.cpp | 63 +++++++--------- Cassiopee/OCC/OCC/Atomic/addSquare.cpp | 100 +++++++++++++++++++++++++ Cassiopee/OCC/OCC/Atomic/mergeCAD.cpp | 8 +- Cassiopee/OCC/OCC/occ.cpp | 3 +- Cassiopee/OCC/OCC/occ.h | 1 + Cassiopee/OCC/srcs.py | 1 + 6 files changed, 135 insertions(+), 41 deletions(-) create mode 100644 Cassiopee/OCC/OCC/Atomic/addSquare.cpp diff --git a/Cassiopee/OCC/OCC/Atomic/addSphere.cpp b/Cassiopee/OCC/OCC/Atomic/addSphere.cpp index 460c77660..c87589cf3 100644 --- a/Cassiopee/OCC/OCC/Atomic/addSphere.cpp +++ b/Cassiopee/OCC/OCC/Atomic/addSphere.cpp @@ -18,6 +18,7 @@ */ #include "occ.h" +#include "TopoDS.hxx" #include "TopoDS_Shape.hxx" #include "TopTools_IndexedMapOfShape.hxx" #include "TopExp.hxx" @@ -31,7 +32,7 @@ PyObject* K_OCC::addSphere(PyObject* self, PyObject* args) { PyObject* hook; E_Float xc, yc, zc, R; - if (!PYPARSETUPLE_(args, O_ RRRR_, &hook, &xc, &yc, &zc, &R)) return NULL; + if (!PYPARSETUPLE_(args, O_ TRRR_ R_, &hook, &xc, &yc, &zc, &R)) return NULL; void** packet = NULL; #if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1) @@ -40,47 +41,39 @@ PyObject* K_OCC::addSphere(PyObject* self, PyObject* args) packet = (void**) PyCapsule_GetPointer(hook, NULL); #endif - /* previous shape or compound */ - /* - TopoDS_Shape* psh = (TopoDS_Shape*)packet[0]; - - if (psh != NULL) - { - TopAbs_ShapeEnum ptype = psh->ShapeType(); - if (ptype == TopAbs_COMPOUND) - { - printf("previous is a compoud\n"); - // == TopAbs_COMPOUND - //for(TopoDS_Iterator anExp(psh); anExp.More(); anExp.Next()){ - //const TopoDS_Shape &curShape1 = anExp.Value();} - } - else - { printf("previous is a shape\n"); } - }*/ + //TopoDS_Shape* shp = (TopoDS_Shape*) packet[0]; + TopTools_IndexedMapOfShape& surfaces = *(TopTools_IndexedMapOfShape*)packet[1]; /* new sphere */ gp_Pnt center(xc, yc, zc); BRepPrimAPI_MakeSphere makerSphere(center, R); TopoDS_Shape sphere = makerSphere.Shape(); - /* another sphere */ - //gp_Pnt center2(xc+2, yc, zc); - //BRepPrimAPI_MakeSphere makerSphere2(center2, R); - //TopoDS_Shape sphere2 = makerSphere2.Shape(); + // Rebuild a single compound + BRep_Builder builder; + TopoDS_Compound compound; + builder.MakeCompound(compound); + + for (E_Int i = 1; i <= surfaces.Extent(); i++) + { + TopoDS_Face F = TopoDS::Face(surfaces(i)); + builder.Add(compound, F); + } - // Building a Compound - //TopoDS_Compound sh; - //BRep_Builder aBuilder; - //aBuilder.MakeCompound(sh); - //aBuilder.Add(sh, sphere); - //aBuilder.Add(sh, sphere2); - - /* export */ - TopoDS_Shape* newshp = new TopoDS_Shape(sphere); - //TopoDS_Shape* newshp = new TopoDS_Compound(sh); - + TopTools_IndexedMapOfShape* sfs = new TopTools_IndexedMapOfShape(); + TopExp::MapShapes(sphere, TopAbs_FACE, *sfs); + TopTools_IndexedMapOfShape& surfaces2 = *(TopTools_IndexedMapOfShape*)sfs; + for (E_Int i = 1; i <= surfaces2.Extent(); i++) + { + TopoDS_Face F = TopoDS::Face(surfaces2(i)); + builder.Add(compound, F); + } + delete sfs; + + TopoDS_Shape* newshp = new TopoDS_Shape(compound); + + // Rebuild the hook packet[0] = newshp; - // Extract surfaces TopTools_IndexedMapOfShape* ptr = (TopTools_IndexedMapOfShape*)packet[1]; delete ptr; @@ -94,6 +87,8 @@ PyObject* K_OCC::addSphere(PyObject* self, PyObject* args) TopTools_IndexedMapOfShape* se = new TopTools_IndexedMapOfShape(); TopExp::MapShapes(*newshp, TopAbs_EDGE, *se); packet[2] = se; + printf("INFO: after addSphere: Nb edges=%d\n", se->Extent()); + printf("INFO: after addSphere: Nb faces=%d\n", sf->Extent()); Py_INCREF(Py_None); return Py_None; diff --git a/Cassiopee/OCC/OCC/Atomic/addSquare.cpp b/Cassiopee/OCC/OCC/Atomic/addSquare.cpp new file mode 100644 index 000000000..d9b4c1715 --- /dev/null +++ b/Cassiopee/OCC/OCC/Atomic/addSquare.cpp @@ -0,0 +1,100 @@ +/* + 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 . +*/ + +#include "occ.h" +#include "TopoDS.hxx" +#include "TopoDS_Shape.hxx" +#include "TopTools_IndexedMapOfShape.hxx" +#include "TopExp.hxx" +#include "TopExp_Explorer.hxx" +#include "BRepPrimAPI_MakeSphere.hxx" +#include "BRep_Builder.hxx" +#include "BRepBuilderAPI_MakeEdge.hxx" +#include "BRepBuilderAPI_MakeWire.hxx" +#include "BRepBuilderAPI_MakeFace.hxx" + +//===================================================================== +// Add a square to CAD hook +//===================================================================== +PyObject* K_OCC::addSquare(PyObject* self, PyObject* args) +{ + PyObject* hook; E_Float x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; + if (!PYPARSETUPLE_(args, O_ TRRR_ TRRR_ TRRR_ TRRR_, &hook, &x1, &y1, &z1, + &x2, &y2, &z2, &x3, &y3, &z3, &x4, &y4, &z4)) 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]; + TopTools_IndexedMapOfShape& surfaces = *(TopTools_IndexedMapOfShape*)packet[1]; + + /* new square */ + gp_Pnt p1(x1, y1, z1); // Bottom left + gp_Pnt p2(x2, y2, z2); // Bottom right + gp_Pnt p3(x3, y3, z3); // Top right + gp_Pnt p4(x4, y4, z4); // Top left + + TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge(p1, p2); + TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge(p2, p3); + TopoDS_Edge edge3 = BRepBuilderAPI_MakeEdge(p3, p4); + TopoDS_Edge edge4 = BRepBuilderAPI_MakeEdge(p4, p1); + + TopoDS_Wire wire = BRepBuilderAPI_MakeWire(edge1, edge2, edge3, edge4); + TopoDS_Face face = BRepBuilderAPI_MakeFace(wire); + + // Rebuild a single compound + BRep_Builder builder; + TopoDS_Compound compound; + builder.MakeCompound(compound); + + for (E_Int i = 1; i <= surfaces.Extent(); i++) + { + TopoDS_Face F = TopoDS::Face(surfaces(i)); + builder.Add(compound, F); + } + builder.Add(compound, face); + + TopoDS_Shape* newshp = new TopoDS_Shape(compound); + + // 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 addSphere: Nb edges=%d\n", se->Extent()); + printf("INFO: after addSphere: Nb faces=%d\n", sf->Extent()); + + Py_INCREF(Py_None); + return Py_None; + +} diff --git a/Cassiopee/OCC/OCC/Atomic/mergeCAD.cpp b/Cassiopee/OCC/OCC/Atomic/mergeCAD.cpp index beedeeb92..7a49e24d4 100644 --- a/Cassiopee/OCC/OCC/Atomic/mergeCAD.cpp +++ b/Cassiopee/OCC/OCC/Atomic/mergeCAD.cpp @@ -39,7 +39,7 @@ PyObject* K_OCC::mergeCAD(PyObject* self, PyObject* args) #else packet1 = (void**) PyCapsule_GetPointer(hook1, NULL); #endif - TopoDS_Shape* shp1 = (TopoDS_Shape*) packet1[0]; + //TopoDS_Shape* shp1 = (TopoDS_Shape*) packet1[0]; TopTools_IndexedMapOfShape& surfaces1 = *(TopTools_IndexedMapOfShape*)packet1[1]; void** packet2 = NULL; @@ -48,7 +48,7 @@ PyObject* K_OCC::mergeCAD(PyObject* self, PyObject* args) #else packet2 = (void**) PyCapsule_GetPointer(hook2, NULL); #endif - TopoDS_Shape* shp2 = (TopoDS_Shape*) packet2[0]; + //TopoDS_Shape* shp2 = (TopoDS_Shape*) packet2[0]; TopTools_IndexedMapOfShape& surfaces2 = *(TopTools_IndexedMapOfShape*)packet2[1]; // Rebuild a single compound @@ -95,8 +95,4 @@ PyObject* K_OCC::mergeCAD(PyObject* self, PyObject* args) #endif return hook; - - - Py_INCREF(Py_None); - return Py_None; } \ No newline at end of file diff --git a/Cassiopee/OCC/OCC/occ.cpp b/Cassiopee/OCC/OCC/occ.cpp index d3d8fad5a..8595e8f08 100644 --- a/Cassiopee/OCC/OCC/occ.cpp +++ b/Cassiopee/OCC/OCC/occ.cpp @@ -36,7 +36,8 @@ static PyMethodDef Pyocc [] = {"bottle", K_OCC::bottle, METH_VARARGS}, {"addSphere", K_OCC::addSphere, METH_VARARGS}, - + {"addSquare", K_OCC::addSquare, METH_VARARGS}, + {"getNbFaces", K_OCC::getNbFaces, METH_VARARGS}, {"getNbEdges", K_OCC::getNbEdges, METH_VARARGS}, {"getFileAndFormat", K_OCC::getFileAndFormat, METH_VARARGS}, diff --git a/Cassiopee/OCC/OCC/occ.h b/Cassiopee/OCC/OCC/occ.h index 54f15c80b..1216fcde6 100644 --- a/Cassiopee/OCC/OCC/occ.h +++ b/Cassiopee/OCC/OCC/occ.h @@ -35,6 +35,7 @@ namespace K_OCC PyObject* bottle(PyObject* self, PyObject* args); PyObject* addSphere(PyObject* self, PyObject* args); + PyObject* addSquare(PyObject* self, PyObject* args); PyObject* getNbFaces(PyObject* self, PyObject* args); PyObject* getNbEdges(PyObject* self, PyObject* args); diff --git a/Cassiopee/OCC/srcs.py b/Cassiopee/OCC/srcs.py index be2e896d5..fb73180b9 100644 --- a/Cassiopee/OCC/srcs.py +++ b/Cassiopee/OCC/srcs.py @@ -41,6 +41,7 @@ def getFiles(module): 'OCC/Atomic/bottle.cpp', 'OCC/Atomic/addSphere.cpp', + 'OCC/Atomic/addSquare.cpp', 'OCC/Atomic/meshEdge.cpp', 'OCC/Atomic/meshEdge2.cpp',