Salome HOME
Merge from V6_main 01/04/2013
[modules/smesh.git] / src / SMESH_SWIG / ex29_refine.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # =======================================
22 # Procedure that take a triangulation and split all triangles in 4 others triangles
23 #
24 import geompy
25 import smesh
26
27 import os
28
29 # Values
30 # ------
31
32 # Path for ".med" files
33 path = "/tmp/ex29_%s_" % os.getenv('USER','unknown')
34
35 # Name of the shape and the mesh
36 name = "Carre"
37
38 # Add a node and needed edges
39 # ---------------------------
40
41 def node(m, f, n1, n2, lnv):
42     x1, y1, z1 = m.GetNodeXYZ(n1)
43     x2, y2, z2 = m.GetNodeXYZ(n2)
44
45     x = (x1 + x2) / 2.0
46     y = (y1 + y2) / 2.0
47     z = (z1 + z2) / 2.0
48
49     i = m.AddNode(x, y, z)
50
51     in1 = m.GetShapeID(n1)
52     in2 = m.GetShapeID(n2)
53
54     if (in1==f) or (in2==f):
55         m.SetNodeOnFace(i, f, 0, 0)
56
57     else:
58         e1 = m.AddEdge([ n1, i  ])
59         e2 = m.AddEdge([ i , n2 ])
60
61         if n1 in lnv:
62             e = in2
63         else:
64             e = in1
65
66         m.SetMeshElementOnShape(e1, e)
67         m.SetMeshElementOnShape(e2, e)
68         m.SetNodeOnEdge(i, e, 0)
69
70     return i
71
72 # Add a triangle and associate to the CAD face
73 # --------------------------------------------
74
75 def triangle(m, f, n1, n2, n3):
76     i = m.AddFace([ n1, n2, n3 ])
77     m.SetMeshElementOnShape(i, f)
78
79 # Split all triangles in 4 triangles
80 # ----------------------------------
81
82 def SplitTrianglesIn4(m):
83     # Get all triangles
84     triangles = m.GetElementsByType(smesh.FACE)
85
86     # Remove all edges
87     m.RemoveElements(m.GetElementsByType(smesh.EDGE))
88
89     # Get the list of nodes (ids) associated with the CAD vertices
90     shape = m.GetShape()
91     lnv = []
92     for v in geompy.SubShapeAll(shape, geompy.ShapeType["VERTEX"]):
93         lnv = lnv + m.GetSubMeshNodesId(v, True)
94
95     # Split every triangle
96     for t in triangles:
97         noeud_1, noeud_2, noeud_3 = m.GetElemNodes(t)
98
99         face = m.GetShapeIDForElem(t)
100
101         noeud_12 = node(m, face, noeud_1, noeud_2, lnv)
102         noeud_23 = node(m, face, noeud_2, noeud_3, lnv)
103         noeud_13 = node(m, face, noeud_1, noeud_3, lnv)
104
105         triangle(m, face, noeud_1 , noeud_12, noeud_13)
106         triangle(m, face, noeud_2 , noeud_23, noeud_12)
107         triangle(m, face, noeud_3 , noeud_13, noeud_23)
108         triangle(m, face, noeud_12, noeud_23, noeud_13)
109
110     # Remove all initial triangles
111     m.RemoveElements(triangles)
112
113     # Merge all identical nodes
114     m.MergeNodes(m.FindCoincidentNodes(0.0001))
115
116 # Build a CAD square
117 # ------------------
118
119 x0 = 0.0 ; y0 = 0.0 ; z0 = 0.0
120 x1 = 1.0 ; y1 = 0.0 ; z1 = 0.0
121 x2 = 1.0 ; y2 = 1.0 ; z2 = 0.0
122 x3 = 0.0 ; y3 = 1.0 ; z3 = 0.0
123
124 P0 = geompy.MakeVertex(x0, y0, z0)
125 P1 = geompy.MakeVertex(x1, y1, z1)
126 P2 = geompy.MakeVertex(x2, y2, z2)
127 P3 = geompy.MakeVertex(x3, y3, z3)
128
129 square = geompy.MakeQuad4Vertices(P0, P1, P2, P3)
130 geompy.addToStudy(square, name)
131
132 # Refine edges and create group of mesh
133 # -------------------------------------
134
135 def refine(m, p1, p2, n, k, name):
136     s = m.GetShape()
137
138     g = geompy.CreateGroup(s, geompy.ShapeType["EDGE"])
139     e = geompy.GetEdge(s, p1, p2)
140     i = geompy.GetSubShapeID(s, e)
141     geompy.AddObject(g, i)
142     m.Group(g, name)
143
144     a = m.Segment(e)
145     a.NumberOfSegments(n, k)
146
147 # Mesh the square
148 # ---------------
149
150 MyMesh = smesh.Mesh(square)
151
152 refine(MyMesh, P1, P2,  8,  7, "Droite")
153 refine(MyMesh, P3, P0,  9, 10, "Gauche")
154 refine(MyMesh, P0, P1,  7,  9, "Bas"   )
155 refine(MyMesh, P2, P3, 12, 14, "Haut"  )
156
157 algo2D = MyMesh.Triangle()
158 algo2D.MaxElementArea(0.07)
159
160 MyMesh.Compute()
161
162 MyMesh.ExportMED(path+"110_triangles.med", 0)
163
164 # Disturb the mesh
165 # ----------------
166
167 MyMesh.MoveNode( 37, 0.05    , 0.368967 , 0 )
168 MyMesh.MoveNode( 38, 0.34    , 0.0762294, 0 )
169 MyMesh.MoveNode( 40, 0.8     , 0.42     , 0 )
170 MyMesh.MoveNode( 42, 0.702662, 0.74     , 0 )
171 MyMesh.MoveNode( 46, 0.4     , 0.374656 , 0 )
172 MyMesh.MoveNode( 47, 0.13    , 0.63     , 0 )
173 MyMesh.MoveNode( 49, 0.222187, 0.3      , 0 )
174 MyMesh.MoveNode( 54, 0.557791, 0.05     , 0 )
175 MyMesh.MoveNode( 55, 0.7     , 0.2      , 0 )
176 MyMesh.MoveNode( 56, 0.73    , 0.52     , 0 )
177 MyMesh.MoveNode( 58, 0.313071, 0.31     , 0 )
178 MyMesh.MoveNode( 59, 0.8     , 0.56     , 0 )
179 MyMesh.MoveNode( 62, 0.592703, 0.95     , 0 )
180 MyMesh.MoveNode( 63, 0.28    , 0.5      , 0 )
181 MyMesh.MoveNode( 65, 0.49    , 0.93     , 0 )
182 MyMesh.MoveNode( 68, 0.501038, 0.65     , 0 )
183 MyMesh.MoveNode( 69, 0.37    , 0.63     , 0 )
184 MyMesh.MoveNode( 70, 0.597025, 0.52     , 0 )
185 MyMesh.MoveNode( 72, 0.899   , 0.878589 , 0 )
186 MyMesh.MoveNode( 73, 0.92    , 0.85     , 0 )
187 MyMesh.MoveNode( 74, 0.820851, 0.75     , 0 )
188
189 NbCells1 = 110
190 MyMesh.ExportMED(path+"110_triangles_2.med", 0)
191
192 # First mesh refining
193 # -------------------
194
195 SplitTrianglesIn4(MyMesh)
196
197 NbCells2 = NbCells1*4
198 print("Mesh with "+str(NbCells2)+" cells computed.")
199
200 MyMesh.ExportMED(path+str(NbCells2)+"_triangles.med", 0)
201
202 # Second mesh refining
203 # --------------------
204
205 SplitTrianglesIn4(MyMesh)
206
207 NbCells3 = NbCells2*4
208 print("Mesh with "+str(NbCells3)+" cells computed.")
209
210 MyMesh.ExportMED(path+str(NbCells3)+"_triangles.med",0)
211
212 # Third mesh refining
213 # -------------------
214
215 SplitTrianglesIn4(MyMesh)
216
217 NbCells4 = NbCells3*4
218 print("Mesh with "+str(NbCells4)+" cells computed.")
219
220 MyMesh.ExportMED(path+str(NbCells4)+"_triangles.med", 0)
221
222 # Update the object browser
223 # -------------------------
224
225 geompy.salome.sg.updateObjBrowser(1)