Skip to content

Commit

Permalink
OCC: add mergeCAD
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit128 committed Dec 6, 2024
1 parent 9326a89 commit 1b332dc
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
29 changes: 27 additions & 2 deletions Cassiopee/CPlot/apps/tkTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import CPlot.PyTree as CPlot
import CPlot.Tk as CTK
import KCore.Vector as Vector
import Converter.Internal as Internal
import time

# local widgets list
Expand Down Expand Up @@ -115,6 +116,14 @@ def rotate(event=None):
noz = CTK.Nz[nz]
a = T.rotate(CTK.t[2][nob][2][noz], (X[0],X[1],X[2]), axe, angle)
CTK.replace(CTK.t, nob, noz, a)

# update CAD if necessary
if CTK.CADHOOK is not None:
if len(nzs) == len(Internal.getZones(CTK.t)):
import OCC
print('rotate cad')
OCC.occ.rotate(CTK.CADHOOK, X, axe, angle)

CTK.TXT.insert('START', 'Zones have been rotated.\n')
CTK.TKTREE.updateApp()
CPlot.render()
Expand Down Expand Up @@ -157,6 +166,14 @@ def translate():
noz = CTK.Nz[nz]
a = T.translate(CTK.t[2][nob][2][noz], (v[0], v[1], v[2]))
CTK.replace(CTK.t, nob, noz, a)

# update CAD if necessary
if CTK.CADHOOK is not None:
if len(nzs) == len(Internal.getZones(CTK.t)):
import OCC
print('translating cad')
OCC.occ.translate(CTK.CADHOOK, (v[0], v[1], v[2]))

CTK.TXT.insert('START', 'Zones have been translated.\n')
CTK.TKTREE.updateApp()
CPlot.render()
Expand Down Expand Up @@ -271,6 +288,14 @@ def scale():
z = T.contract(z, (X[0],X[1],X[2]), axe1, axe3, v[1])
a = T.contract(z, (X[0],X[1],X[2]), axe1, axe2, v[2])
CTK.replace(CTK.t, nob, noz, a)

# update CAD if necessary
if CTK.CADHOOK is not None:
if len(nzs) == len(Internal.getZones(CTK.t)) and len(v) == 1:
import OCC
print('scale cad')
OCC.occ.scale(CTK.CADHOOK, v[0], X)

CTK.TXT.insert('START', 'Zones have been scaled.\n')
CTK.TKTREE.updateApp()
CPlot.render()
Expand Down Expand Up @@ -344,9 +369,9 @@ def createApp(win):

# - VARS -
# -0- Translation vector -
V = TK.StringVar(win); V.set('0;0;0'); VARS.append(V)
V = TK.StringVar(win); V.set('0; 0; 0'); VARS.append(V)
# -1- Scale factors -
V = TK.StringVar(win); V.set('1;1;1'); VARS.append(V)
V = TK.StringVar(win); V.set('1; 1; 1'); VARS.append(V)
# -2- Rotate axis
V = TK.StringVar(win); V.set('around X'); VARS.append(V)
# -3- Rotation angle
Expand Down
102 changes: 102 additions & 0 deletions Cassiopee/OCC/OCC/Atomic/mergeCAD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
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 <http://www.gnu.org/licenses/>.
*/

#include "occ.h"
#include "TopoDS.hxx"
#include "TopTools_IndexedMapOfShape.hxx"
#include "TopExp.hxx"
#include "TopExp_Explorer.hxx"
#include "BRep_Builder.hxx"

// ============================================================================
/* Merge two CAD hooks in a single hook
Caller must dealloc input hooks */
// ============================================================================
PyObject* K_OCC::mergeCAD(PyObject* self, PyObject* args)
{
PyObject* hook1; PyObject* hook2;
if (!PYPARSETUPLE_(args, OO_, &hook1, &hook2)) return NULL;

void** packet1 = NULL;
#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1)
packet1 = (void**) PyCObject_AsVoidPtr(hook1);
#else
packet1 = (void**) PyCapsule_GetPointer(hook1, NULL);
#endif
TopoDS_Shape* shp1 = (TopoDS_Shape*) packet1[0];
TopTools_IndexedMapOfShape& surfaces1 = *(TopTools_IndexedMapOfShape*)packet1[1];

void** packet2 = NULL;
#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1)
packet2 = (void**) PyCObject_AsVoidPtr(hook2);
#else
packet2 = (void**) PyCapsule_GetPointer(hook2, NULL);
#endif
TopoDS_Shape* shp2 = (TopoDS_Shape*) packet2[0];
TopTools_IndexedMapOfShape& surfaces2 = *(TopTools_IndexedMapOfShape*)packet2[1];

// Rebuild a single compound
BRep_Builder builder;
TopoDS_Compound compound;
builder.MakeCompound(compound);

for (E_Int i = 1; i <= surfaces1.Extent(); i++)
{
TopoDS_Face F = TopoDS::Face(surfaces1(i));
builder.Add(compound, F);
}

for (E_Int i = 1; i <= surfaces2.Extent(); i++)
{
TopoDS_Face F = TopoDS::Face(surfaces2(i));
builder.Add(compound, F);
}

TopoDS_Shape* newshp = new TopoDS_Shape(compound);

// capsule
PyObject* hook;
E_Int sizePacket = 5;
void** packet = new void* [sizePacket];
packet[0] = newshp;

// Extract surfaces
TopTools_IndexedMapOfShape* sf = new TopTools_IndexedMapOfShape();
TopExp::MapShapes(*newshp, TopAbs_FACE, *sf);
packet[1] = sf;

// Extract edges
TopTools_IndexedMapOfShape* se = new TopTools_IndexedMapOfShape();
TopExp::MapShapes(*newshp, TopAbs_EDGE, *se);
packet[2] = se;
printf("INFO: after merge: Nb edges=%d\n", se->Extent());
printf("INFO: after merge: Nb faces=%d\n", sf->Extent());

#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1)
hook = PyCObject_FromVoidPtr(packet, NULL);
#else
hook = PyCapsule_New(packet, NULL, NULL);
#endif

return hook;


Py_INCREF(Py_None);
return Py_None;
}
2 changes: 2 additions & 0 deletions Cassiopee/OCC/OCC/Atomic/rotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ PyObject* K_OCC::rotate(PyObject* self, PyObject* args)
gp_Dir direction(xaxis, yaxis, zaxis);
gp_Ax1 axis(center, direction);

//printf("angle=%g\n", angle);
angle = angle * M_PI / 180.;
gp_Trsf myTrsf;
myTrsf.SetRotation(axis, angle);

Expand Down
1 change: 0 additions & 1 deletion Cassiopee/OCC/OCC/Atomic/trim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "ShapeFix_Shape.hxx"
#include "ShapeFix_Wireframe.hxx"
#include "BRepBuilderAPI_MakeWire.hxx"
#include "ShapeUpgrade_UnifySameDomain.hxx"
#include "BRepFeat_SplitShape.hxx"
#include "BRep_Builder.hxx"
#include "TopExp.hxx"
Expand Down
1 change: 1 addition & 0 deletions Cassiopee/OCC/OCC/occ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static PyMethodDef Pyocc [] =
{"readCAD", K_OCC::readCAD, METH_VARARGS},
{"writeCAD", K_OCC::writeCAD, METH_VARARGS},
{"createEmptyCAD", K_OCC::createEmptyCAD, METH_VARARGS},
{"mergeCAD", K_OCC::mergeCAD, METH_VARARGS},
{"freeHook", K_OCC::freeHook, METH_VARARGS},

{"bottle", K_OCC::bottle, METH_VARARGS},
Expand Down
1 change: 1 addition & 0 deletions Cassiopee/OCC/OCC/occ.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace K_OCC
PyObject* readCAD(PyObject* self, PyObject* args);
PyObject* writeCAD(PyObject* self, PyObject* args);
PyObject* createEmptyCAD(PyObject* self, PyObject* args);
PyObject* mergeCAD(PyObject* self, PyObject* args);
PyObject* freeHook(PyObject* self, PyObject* args);

PyObject* bottle(PyObject* self, PyObject* args);
Expand Down
1 change: 1 addition & 0 deletions Cassiopee/OCC/srcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def getFiles(module):
'OCC/Atomic/readCAD.cpp',
'OCC/Atomic/writeCAD.cpp',
'OCC/Atomic/createEmptyCAD.cpp',
'OCC/Atomic/mergeCAD.cpp',
'OCC/Atomic/freeHook.cpp',

'OCC/Atomic/bottle.cpp',
Expand Down

0 comments on commit 1b332dc

Please sign in to comment.