1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # =======================================
22 # Procedure that take a triangulation and split all triangles in 4 others triangles
27 from salome.geom import geomBuilder
28 geompy = geomBuilder.New(salome.myStudy)
30 import SMESH, SALOMEDS
31 from salome.smesh import smeshBuilder
32 smesh = smeshBuilder.New(salome.myStudy)
39 # Path for ".med" files
40 path = "/tmp/ex29_%s_" % os.getenv('USER','unknown')
42 # Name of the shape and the mesh
45 # Add a node and needed edges
46 # ---------------------------
48 def node(m, f, n1, n2, lnv):
49 x1, y1, z1 = m.GetNodeXYZ(n1)
50 x2, y2, z2 = m.GetNodeXYZ(n2)
56 i = m.AddNode(x, y, z)
58 in1 = m.GetShapeID(n1)
59 in2 = m.GetShapeID(n2)
61 if (in1==f) or (in2==f):
62 m.SetNodeOnFace(i, f, 0, 0)
65 e1 = m.AddEdge([ n1, i ])
66 e2 = m.AddEdge([ i , n2 ])
73 m.SetMeshElementOnShape(e1, e)
74 m.SetMeshElementOnShape(e2, e)
75 m.SetNodeOnEdge(i, e, 0)
79 # Add a triangle and associate to the CAD face
80 # --------------------------------------------
82 def triangle(m, f, n1, n2, n3):
83 i = m.AddFace([ n1, n2, n3 ])
84 m.SetMeshElementOnShape(i, f)
86 # Split all triangles in 4 triangles
87 # ----------------------------------
89 def SplitTrianglesIn4(m):
91 triangles = m.GetElementsByType(SMESH.FACE)
94 m.RemoveElements(m.GetElementsByType(SMESH.EDGE))
96 # Get the list of nodes (ids) associated with the CAD vertices
99 for v in geompy.SubShapeAll(shape, geompy.ShapeType["VERTEX"]):
100 lnv = lnv + m.GetSubMeshNodesId(v, True)
102 # Split every triangle
104 noeud_1, noeud_2, noeud_3 = m.GetElemNodes(t)
106 face = m.GetShapeIDForElem(t)
108 noeud_12 = node(m, face, noeud_1, noeud_2, lnv)
109 noeud_23 = node(m, face, noeud_2, noeud_3, lnv)
110 noeud_13 = node(m, face, noeud_1, noeud_3, lnv)
112 triangle(m, face, noeud_1 , noeud_12, noeud_13)
113 triangle(m, face, noeud_2 , noeud_23, noeud_12)
114 triangle(m, face, noeud_3 , noeud_13, noeud_23)
115 triangle(m, face, noeud_12, noeud_23, noeud_13)
117 # Remove all initial triangles
118 m.RemoveElements(triangles)
120 # Merge all identical nodes
121 m.MergeNodes(m.FindCoincidentNodes(0.0001))
126 x0 = 0.0 ; y0 = 0.0 ; z0 = 0.0
127 x1 = 1.0 ; y1 = 0.0 ; z1 = 0.0
128 x2 = 1.0 ; y2 = 1.0 ; z2 = 0.0
129 x3 = 0.0 ; y3 = 1.0 ; z3 = 0.0
131 P0 = geompy.MakeVertex(x0, y0, z0)
132 P1 = geompy.MakeVertex(x1, y1, z1)
133 P2 = geompy.MakeVertex(x2, y2, z2)
134 P3 = geompy.MakeVertex(x3, y3, z3)
136 square = geompy.MakeQuad4Vertices(P0, P1, P2, P3)
137 geompy.addToStudy(square, name)
139 # Refine edges and create group of mesh
140 # -------------------------------------
142 def refine(m, p1, p2, n, k, name):
145 g = geompy.CreateGroup(s, geompy.ShapeType["EDGE"])
146 e = geompy.GetEdge(s, p1, p2)
147 i = geompy.GetSubShapeID(s, e)
148 geompy.AddObject(g, i)
152 a.NumberOfSegments(n, k)
157 MyMesh = smesh.Mesh(square)
159 refine(MyMesh, P1, P2, 8, 7, "Droite")
160 refine(MyMesh, P3, P0, 9, 10, "Gauche")
161 refine(MyMesh, P0, P1, 7, 9, "Bas" )
162 refine(MyMesh, P2, P3, 12, 14, "Haut" )
164 algo2D = MyMesh.Triangle()
165 algo2D.MaxElementArea(0.07)
169 MyMesh.ExportMED(path+"110_triangles.med", 0)
174 MyMesh.MoveNode( 37, 0.05 , 0.368967 , 0 )
175 MyMesh.MoveNode( 38, 0.34 , 0.0762294, 0 )
176 MyMesh.MoveNode( 40, 0.8 , 0.42 , 0 )
177 MyMesh.MoveNode( 42, 0.702662, 0.74 , 0 )
178 MyMesh.MoveNode( 46, 0.4 , 0.374656 , 0 )
179 MyMesh.MoveNode( 47, 0.13 , 0.63 , 0 )
180 MyMesh.MoveNode( 49, 0.222187, 0.3 , 0 )
181 MyMesh.MoveNode( 54, 0.557791, 0.05 , 0 )
182 MyMesh.MoveNode( 55, 0.7 , 0.2 , 0 )
183 MyMesh.MoveNode( 56, 0.73 , 0.52 , 0 )
184 MyMesh.MoveNode( 58, 0.313071, 0.31 , 0 )
185 MyMesh.MoveNode( 59, 0.8 , 0.56 , 0 )
186 MyMesh.MoveNode( 62, 0.592703, 0.95 , 0 )
187 MyMesh.MoveNode( 63, 0.28 , 0.5 , 0 )
188 MyMesh.MoveNode( 65, 0.49 , 0.93 , 0 )
189 MyMesh.MoveNode( 68, 0.501038, 0.65 , 0 )
190 MyMesh.MoveNode( 69, 0.37 , 0.63 , 0 )
191 MyMesh.MoveNode( 70, 0.597025, 0.52 , 0 )
192 MyMesh.MoveNode( 72, 0.899 , 0.878589 , 0 )
193 MyMesh.MoveNode( 73, 0.92 , 0.85 , 0 )
194 MyMesh.MoveNode( 74, 0.820851, 0.75 , 0 )
197 MyMesh.ExportMED(path+"110_triangles_2.med", 0)
199 # First mesh refining
200 # -------------------
202 SplitTrianglesIn4(MyMesh)
204 NbCells2 = NbCells1*4
205 print("Mesh with "+str(NbCells2)+" cells computed.")
207 MyMesh.ExportMED(path+str(NbCells2)+"_triangles.med", 0)
209 # Second mesh refining
210 # --------------------
212 SplitTrianglesIn4(MyMesh)
214 NbCells3 = NbCells2*4
215 print("Mesh with "+str(NbCells3)+" cells computed.")
217 MyMesh.ExportMED(path+str(NbCells3)+"_triangles.med",0)
219 # Third mesh refining
220 # -------------------
222 SplitTrianglesIn4(MyMesh)
224 NbCells4 = NbCells3*4
225 print("Mesh with "+str(NbCells4)+" cells computed.")
227 MyMesh.ExportMED(path+str(NbCells4)+"_triangles.med", 0)
229 # Update the object browser
230 # -------------------------
232 salome.sg.updateObjBrowser(1)