Salome HOME
Add new example python script (from CEA)
[modules/smesh.git] / src / SMESH_SWIG / ex29_refine.py
1 # CEA/LGLS-LETR 2008, Francis KLOSS (OCC)
2 # =======================================
3
4 # Procedure that take a triangulation and split all triangles in 4 others triangles
5
6 import geompy
7 import smesh
8
9 # Values
10 # ------
11
12 # Path for ".med" files
13 path = "/tmp/ex29_"
14
15 # Name of the shape and the mesh
16 name = "Carre"
17
18 # Add a node and needed edges
19 # ---------------------------
20
21 def node(m, f, n1, n2, lnv):
22     x1, y1, z1 = m.GetNodeXYZ(n1)
23     x2, y2, z2 = m.GetNodeXYZ(n2)
24
25     x = (x1 + x2) / 2.0
26     y = (y1 + y2) / 2.0
27     z = (z1 + z2) / 2.0
28
29     i = m.AddNode(x, y, z)
30
31     in1 = m.GetShapeID(n1)
32     in2 = m.GetShapeID(n2)
33
34     if (in1==f) or (in2==f):
35         m.SetNodeOnFace(i, f, 0, 0)
36
37     else:
38         e1 = m.AddEdge([ n1, i  ])
39         e2 = m.AddEdge([ i , n2 ])
40
41         if n1 in lnv:
42             e = in2
43         else:
44             e = in1
45
46         m.SetMeshElementOnShape(e1, e)
47         m.SetMeshElementOnShape(e2, e)
48         m.SetNodeOnEdge(i, e, 0)
49
50     return i
51
52 # Add a triangle and associate to the CAD face
53 # --------------------------------------------
54
55 def triangle(m, f, n1, n2, n3):
56     i = m.AddFace([ n1, n2, n3 ])
57     m.SetMeshElementOnShape(i, f)
58
59 # Split all triangles in 4 triangles
60 # ----------------------------------
61
62 def SplitTrianglesIn4(m):
63     # Get all triangles
64     triangles = m.GetElementsByType(smesh.FACE)
65
66     # Remove all edges
67     m.RemoveElements(m.GetElementsByType(smesh.EDGE))
68
69     # Get the list of nodes (ids) associated with the CAD vertices
70     shape = m.GetShape()
71     lnv = []
72     for v in geompy.SubShapeAll(shape, geompy.ShapeType["VERTEX"]):
73         lnv = lnv + m.GetSubMeshNodesId(v, True)
74
75     # Split every triangle
76     for t in triangles:
77         noeud_1, noeud_2, noeud_3 = m.GetElemNodes(t)
78
79         face = m.GetShapeIDForElem(t)
80
81         noeud_12 = node(m, face, noeud_1, noeud_2, lnv)
82         noeud_23 = node(m, face, noeud_2, noeud_3, lnv)
83         noeud_13 = node(m, face, noeud_1, noeud_3, lnv)
84
85         triangle(m, face, noeud_1 , noeud_12, noeud_13)
86         triangle(m, face, noeud_2 , noeud_23, noeud_12)
87         triangle(m, face, noeud_3 , noeud_13, noeud_23)
88         triangle(m, face, noeud_12, noeud_23, noeud_13)
89
90     # Remove all initial triangles
91     m.RemoveElements(triangles)
92
93     # Merge all identical nodes
94     m.MergeNodes(m.FindCoincidentNodes(0.0001))
95
96 # Build a CAD square
97 # ------------------
98
99 x0 = 0.0 ; y0 = 0.0 ; z0 = 0.0
100 x1 = 1.0 ; y1 = 0.0 ; z1 = 0.0
101 x2 = 1.0 ; y2 = 1.0 ; z2 = 0.0
102 x3 = 0.0 ; y3 = 1.0 ; z3 = 0.0
103
104 P0 = geompy.MakeVertex(x0, y0, z0)
105 P1 = geompy.MakeVertex(x1, y1, z1)
106 P2 = geompy.MakeVertex(x2, y2, z2)
107 P3 = geompy.MakeVertex(x3, y3, z3)
108
109 square = geompy.MakeQuad4Vertices(P0, P1, P2, P3)
110 geompy.addToStudy(square, name)
111
112 # Refine edges and create group of mesh
113 # -------------------------------------
114
115 def refine(m, p1, p2, n, k, name):
116     s = m.GetShape()
117
118     g = geompy.CreateGroup(s, geompy.ShapeType["EDGE"])
119     e = geompy.GetEdge(s, p1, p2)
120     i = geompy.GetSubShapeID(s, e)
121     geompy.AddObject(g, i)
122     m.Group(g, name)
123
124     a = m.Segment(e)
125     a.NumberOfSegments(n, k)
126
127 # Mesh the square
128 # ---------------
129
130 MyMesh = smesh.Mesh(square)
131
132 refine(MyMesh, P1, P2,  8,  7, "Droite")
133 refine(MyMesh, P3, P0,  9, 10, "Gauche")
134 refine(MyMesh, P0, P1,  7,  9, "Bas"   )
135 refine(MyMesh, P2, P3, 12, 14, "Haut"  )
136
137 algo2D = MyMesh.Triangle()
138 algo2D.MaxElementArea(0.07)
139
140 MyMesh.Compute()
141
142 MyMesh.ExportMED(path+"110_triangles.med", 0)
143
144 # Disturb the mesh
145 # ----------------
146
147 MyMesh.MoveNode( 37, 0.05    , 0.368967 , 0 )
148 MyMesh.MoveNode( 38, 0.34    , 0.0762294, 0 )
149 MyMesh.MoveNode( 40, 0.8     , 0.42     , 0 )
150 MyMesh.MoveNode( 42, 0.702662, 0.74     , 0 )
151 MyMesh.MoveNode( 46, 0.4     , 0.374656 , 0 )
152 MyMesh.MoveNode( 47, 0.13    , 0.63     , 0 )
153 MyMesh.MoveNode( 49, 0.222187, 0.3      , 0 )
154 MyMesh.MoveNode( 54, 0.557791, 0.05     , 0 )
155 MyMesh.MoveNode( 55, 0.7     , 0.2      , 0 )
156 MyMesh.MoveNode( 56, 0.73    , 0.52     , 0 )
157 MyMesh.MoveNode( 58, 0.313071, 0.31     , 0 )
158 MyMesh.MoveNode( 59, 0.8     , 0.56     , 0 )
159 MyMesh.MoveNode( 62, 0.592703, 0.95     , 0 )
160 MyMesh.MoveNode( 63, 0.28    , 0.5      , 0 )
161 MyMesh.MoveNode( 65, 0.49    , 0.93     , 0 )
162 MyMesh.MoveNode( 68, 0.501038, 0.65     , 0 )
163 MyMesh.MoveNode( 69, 0.37    , 0.63     , 0 )
164 MyMesh.MoveNode( 70, 0.597025, 0.52     , 0 )
165 MyMesh.MoveNode( 72, 0.899   , 0.878589 , 0 )
166 MyMesh.MoveNode( 73, 0.92    , 0.85     , 0 )
167 MyMesh.MoveNode( 74, 0.820851, 0.75     , 0 )
168
169 NbCells1 = 110
170 MyMesh.ExportMED(path+"110_triangles_2.med", 0)
171
172 # First mesh refining
173 # -------------------
174
175 SplitTrianglesIn4(MyMesh)
176
177 NbCells2 = NbCells1*4
178 print("Mesh with "+str(NbCells2)+" cells computed.")
179
180 MyMesh.ExportMED(path+str(NbCells2)+"_triangles.med", 0)
181
182 # Second mesh refining
183 # --------------------
184
185 SplitTrianglesIn4(MyMesh)
186
187 NbCells3 = NbCells2*4
188 print("Mesh with "+str(NbCells3)+" cells computed.")
189
190 MyMesh.ExportMED(path+str(NbCells3)+"_triangles.med",0)
191
192 # Third mesh refining
193 # -------------------
194
195 SplitTrianglesIn4(MyMesh)
196
197 NbCells4 = NbCells3*4
198 print("Mesh with "+str(NbCells4)+" cells computed.")
199
200 MyMesh.ExportMED(path+str(NbCells4)+"_triangles.med", 0)
201
202 # Update the object browser
203 # -------------------------
204
205 salome.sg.updateObjBrowser(1)