Salome HOME
[Bug PAL7784] REGR: wrong Export-Import Mesh in med
[modules/smesh.git] / src / SMESH_SWIG / meshpy.py
1 # CEA/LGLS 2004, Francis KLOSS (OCC)
2 # ==================================
3
4 # Import
5 # ------
6
7 import geompy
8 import salome
9
10 import StdMeshers
11 import SMESH
12
13 """
14  \namespace meshpy
15  \brief Module meshpy
16 """
17
18 # Variables
19 # ---------
20
21 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
22 smesh.SetCurrentStudy(salome.myStudy)
23 smeshgui = salome.ImportComponentGUI("SMESH")
24 smeshgui.Init(salome.myStudyId)
25
26 class MeshHexaImpl:
27 """
28  Class MeshHexaImpl for Hexahedrical meshing
29
30  Examples: cube2pyGibi.py, lines 270-295
31            cube2partition.py, lines 72-83
32 """
33     piece = 0
34     name  = 0
35     mesh  = 0
36     cpt   = 0
37
38     def Mesh1D(self, shape, n, propagate=0):
39     """
40      Define Wires discretization.
41      Sets algorithm and hypothesis for 1D discretization of \a shape:
42        - algorithm  "Regular_1D"
43        - hypothesis "NumberOfSegments" with number of segments \a n
44      \param shape Main shape or sub-shape to define wire discretization of
45      \param n Number of segments to split eash wire of the \a shape on
46      \param propagate Boolean flag. If propagate = 1,
47             "Propagation" hypothesis will be applied also to the \a shape
48     """
49         hyp1D=smesh.CreateHypothesis("Regular_1D", "libStdMeshersEngine.so")
50         smeshgui.SetName(salome.ObjectToID(hyp1D), self.name+"/WireDiscretisation/"+str(self.cpt))
51         self.mesh.AddHypothesis(shape, hyp1D)
52
53         hyp=smesh.CreateHypothesis("NumberOfSegments", "libStdMeshersEngine.so")
54         hyp.SetNumberOfSegments(n)
55         smeshgui.SetName(salome.ObjectToID(hyp), self.name+"/Segments_"+str(n)+"/"+str(self.cpt))
56         self.mesh.AddHypothesis(shape, hyp)
57
58         if propagate:
59             hypPro=smesh.CreateHypothesis("Propagation", "libStdMeshersEngine.so")
60             smeshgui.SetName(salome.ObjectToID(hypPro), self.name+"/Propagation/"+str(self.cpt))
61             self.mesh.AddHypothesis(shape, hypPro)
62
63         self.cpt=self.cpt+1
64
65     def __init__(self, piece, n, name):
66     """
67      Constructor
68
69      Creates mesh on the shape \a piece,
70      sets GUI name of this mesh to \a name. \n
71      Sets the following global algorithms and hypotheses:
72        - for 1D discretization:
73            - algorithm  "Regular_1D"
74            - hypothesis "NumberOfSegments" with number of segments \a n
75        - for 2D discretization:
76            - algorithm  "Quadrangle_2D"
77        - for 3D discretization:
78            - algorithm  "Hexa_3D"
79      \param piece Shape to be meshed
80      \param n Global number of segments for wires discretization
81      \param name Name for mesh to be created
82     """
83         self.piece = piece
84         self.name  = name
85
86         self.mesh  = smesh.CreateMesh(piece)
87         smeshgui.SetName(salome.ObjectToID(self.mesh), name)
88
89         self.Mesh1D(piece, n)
90
91         hyp2D=smesh.CreateHypothesis("Quadrangle_2D", "libStdMeshersEngine.so")
92         smeshgui.SetName(salome.ObjectToID(hyp2D), name+"/Quadrangle")
93         self.mesh.AddHypothesis(piece, hyp2D)
94
95         hyp3D=smesh.CreateHypothesis("Hexa_3D", "libStdMeshersEngine.so")
96         smeshgui.SetName(salome.ObjectToID(hyp3D), name+"/ijk")
97         self.mesh.AddHypothesis(piece, hyp3D)
98
99     def local(self, edge, n):
100     """
101      Creates sub-mesh of the mesh, created by constructor.
102      This sub-mesh will be created on edge \a edge.
103      Set algorithm and hypothesis for 1D discretization of the \a edge:
104        - algorithm  "Regular_1D"
105        - hypothesis "NumberOfSegments" with number of segments \a n
106      \param edge Sub-edge of the main shape
107      \param n Number of segments to split the \a edge on
108      \note: \a edge will be automatically published in study under the shape, given in constructor.
109     """
110         geompy.addToStudyInFather(self.piece, edge, geompy.SubShapeName(edge, self.piece))
111         submesh = self.mesh.GetSubMesh(edge, self.name+"/SubMeshEdge/"+str(self.cpt))
112         self.Mesh1D(edge, n)
113
114     def Propagate(self, edge, n):
115     """
116      Creates sub-mesh of the mesh, created by constructor.
117      This sub-mesh will be created on edge \a edge and propagate the hypothesis on all correspondant edges.
118      Set algorithm and hypothesis for 1D discretization of the \a edge and all other propagate edges:
119        - algorithm  "Regular_1D"
120        - hypothesis "NumberOfSegments" with number of segments \a n
121        - hypothesis "Propagation"
122      \param edge Sub-edge of the main shape
123      \param n Number of segments to split the \a edge and all other propagate edges on
124      \note: \a edge will be automatically published in study under the shape, given in constructor.
125     """
126         geompy.addToStudyInFather(self.piece, edge, geompy.SubShapeName(edge, self.piece))
127         submesh = self.mesh.GetSubMesh(edge, self.name+"/SubMeshEdge/"+str(self.cpt))
128         self.Mesh1D(edge, n, 1)
129
130     def Compute(self):
131     """
132      Computes mesh, created by constructor.
133     """
134         smesh.Compute(self.mesh, self.piece)
135         salome.sg.updateObjBrowser(1)
136
137     def Group(self, grp, name=""):
138     """
139      Creates mesh group based on a geometric group
140      \param grp Geometric group
141      \param name Name for mesh group to be created
142     """
143         if name == "":
144             name = grp.GetName()
145         tgeo = geompy.GroupOp.GetType(grp)
146         if tgeo == geompy.ShapeType["VERTEX"]:
147             type = SMESH.NODE
148         elif tgeo == geompy.ShapeType["EDGE"]:
149             type = SMESH.EDGE
150         elif tgeo == geompy.ShapeType["FACE"]:
151             type = SMESH.FACE
152         elif tgeo == geompy.ShapeType["SOLID"]:
153             type = SMESH.VOLUME
154         return self.mesh.CreateGroupFromGEOM(type, name, grp)
155
156     def ExportMED(self, filename, groups=1):
157     """
158      Export mesh in a MED file
159      \param filename Name for MED file to be created
160      \param groups Boolean flag. If groups = 1, mesh groups will be also stored in file
161     """
162         self.mesh.ExportMED(filename, groups)
163
164 MeshHexa = MeshHexaImpl