Salome HOME
44a3a7e09a215c8fde7f5cac3673f5be30e72c88
[tools/medcoupling.git] / src / MEDCoupling_Swig / geom2medcoupling.py
1 #! /usr/bin/env python3
2 #  -*- coding: utf-8 -*-
3 # Copyright (C) 2021  CEA/DEN, EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author : Anthony GEAY (EDF R&D)
22
23 import medcoupling as mc
24
25 def __to_geomshape_3D(mcmesh):
26     """
27     Precondition mcmesh is a MEDCouplingUMesh containing exactly one linear 3D cell.
28     """
29     import salome
30     salome.standalone()
31     salome.salome_init()
32     import GEOM
33     from salome.geom import geomBuilder
34     geompy = geomBuilder.New()
35
36     mcmesh2 = mcmesh.deepCopyConnectivityOnly()
37     vertices = [ geompy.MakeVertex(*list(elt)) for elt in mcmesh2.getCoords()]
38
39     mcfaces = mcmesh2.buildDescendingConnectivity()[0]
40     shell_1 = geompy.MakeShell(
41         [
42             geompy.MakeFaceWires(
43                 [
44                     geompy.MakePolyline([ vertices[nodeidx] for nodeidx in elt.getAllConn()[1:] ], True)
45                 ]
46                 , 1
47             )
48         for elt in mcfaces ]
49         )
50     return geompy.MakeSolid([shell_1])
51
52 def to_geomshape(mcmesh):
53     """
54     Method converting a unique 3D linear cell in a MEDCoupling mesh to GEOM Shape solid
55
56     :param mcmesh: Mesh with single 3D linear cell.
57     :type mcmesh: mc.MEDCouplingUMesh
58     :return: GEOM shape
59     :rtype: GEOM_Object
60     """
61     if not isinstance(mcmesh,mc.MEDCouplingUMesh):
62         raise RuntimeError("Input mesh is expected to be of type mc.MEDCouplingUMesh !")
63     if mcmesh.getNumberOfCells() != 1:
64         raise RuntimeError("Input mesh is expected to contain exactly one cell !")
65     if not mc.MEDCouplingUMesh.IsLinearGeometricType( mc.MEDCoupling1SGTUMesh(mcmesh).getCellModelEnum() ) :
66         raise RuntimeError("The unique cell in the mesh is expected to be linear !")
67     dico = { 3 : __to_geomshape_3D }
68     mdim = mcmesh.getMeshDimension()
69     if mdim not in dico:
70         raise RuntimeError( "Input mesh is expected to have mesh dimension in {}".format(list(dico.keys())) )
71     if mcmesh.getSpaceDimension() != 3:
72         mcmesh.changeSpaceDimension(3,0.0)
73     return (dico[mdim])(mcmesh)