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