Salome HOME
Merge from V5_1_main branch 24/11/2010
[modules/smesh.git] / doc / salome / gui / SMESH / input / tui_transforming_meshes.doc
1 /*!
2
3 \page tui_transforming_meshes_page Transforming Meshes
4
5 <br><h2>Transforming Meshes</h2>
6
7 <br>
8 \anchor tui_translation
9 <h3>Translation</h3>
10
11 \code
12 import SMESH_mechanic
13
14 smesh = SMESH_mechanic.smesh 
15 mesh = SMESH_mechanic.mesh 
16
17 # define translation vector
18 point = smesh.PointStruct(-150., -150., 0.)
19 vector =smesh.DirStruct(point) 
20
21 # translate a mesh
22 doCopy = 1
23
24 mesh.Translate([], vector, doCopy)
25 \endcode
26
27 <br>
28 \anchor tui_rotation
29 <h3>Rotation</h3>
30
31 \code
32 import math
33
34 import SMESH_mechanic
35
36 smesh = SMESH_mechanic.smesh
37 mesh = SMESH_mechanic.mesh 
38
39 # define rotation axis and angle
40 axisXYZ = smesh.AxisStruct(0., 0., 0., 5., 5., 20.)
41 angle270 = 1.5 * math.pi
42
43 # rotate a mesh
44 mesh.Rotate([], axisXYZ, angle270, 1)  
45 \endcode
46
47 <br>
48 \anchor tui_scale
49 <h3>Scale</h3>
50
51 \code
52 import geompy
53 Box = geompy.MakeBoxDXDYDZ(200, 200, 200)
54 f = geompy.SubShapeAllSorted(Box, geompy.ShapeType["FACE"])
55
56 import smesh,SMESH
57 import StdMeshers
58 Mesh1 = smesh.Mesh(f[0])
59 Regular_1D = Mesh1.Segment()
60 Nb_Segments_1 = Regular_1D.NumberOfSegments(3)
61 Nb_Segments_1.SetDistrType( 0 )
62 Quadrangle_2D = Mesh1.Quadrangle()
63 isDone = Mesh1.Compute()
64
65 #Perform scale opration for the whole mesh and creation of a new mesh
66 newMesh = Mesh1.ScaleMakeMesh(Mesh1,SMESH.PointStruct(100,100,200),[0.5,0.3,0.7],True,"ScaledMesh")
67
68 #Perform scale operation for the whole mesh and copy elements
69 Mesh1.Scale(Mesh1,SMESH.PointStruct(200,100,100),[0.5,0.5,0.5],True,True)
70
71 #Perform scale opration for two edges and move elements
72 Mesh1.Scale([1,2],SMESH.PointStruct(-100,100,100),[0.8,1.0,0.7],False)
73
74 #Perform scale opration for one face and move elements
75 Mesh1.Scale([21],SMESH.PointStruct(0,200,200),[0.7,0.7,0.7],False)
76 \endcode
77
78 <br>
79 \anchor tui_symmetry
80 <h3>Symmetry</h3>
81
82 \code
83 import math
84
85 import SMESH_mechanic
86
87 smesh = SMESH_mechanic.smesh
88 mesh = SMESH_mechanic.mesh 
89
90 # create a symmetrical copy of the mesh mirrored through a point
91 axis = SMESH.AxisStruct(0, 0, 0, 0, 0, 0)
92
93 mesh.Mirror([], axis, smesh.POINT, 1)
94 \endcode
95
96 <br>
97 \anchor tui_merging_nodes
98 <h3>Merging Nodes</h3>
99
100 \code
101 import SMESH_mechanic
102 mesh = SMESH_mechanic.mesh
103
104 # merge nodes
105 Tolerance = 25.0
106
107 GroupsOfNodes =  mesh.FindCoincidentNodes(Tolerance)
108 mesh.MergeNodes(GroupsOfNodes)  
109 \endcode
110
111 <br>
112 \anchor tui_merging_elements
113 <h3>Merging Elements</h3>
114
115 \code
116 import salome
117 import geompy
118 import smesh
119
120 # create a face to be meshed
121 px = geompy.MakeVertex(100., 0.  , 0.  )
122 py = geompy.MakeVertex(0.  , 100., 0.  )
123 pz = geompy.MakeVertex(0.  , 0.  , 100.)
124
125 vxy = geompy.MakeVector(px, py)
126 arc = geompy.MakeArc(py, pz, px)
127
128 wire = geompy.MakeWire([vxy, arc])
129 isPlanarFace = 1
130
131 face1 = geompy.MakeFace(wire, isPlanarFace)
132 id_face1 = geompy.addToStudy(face1, "Face1")
133
134 # create a circle to be an extrusion path
135 px1 = geompy.MakeVertex( 100.,  100.,  0.)
136 py1 = geompy.MakeVertex(-100., -100.,  0.)
137 pz1 = geompy.MakeVertex(   0.,    0., 50.)
138
139 circle = geompy.MakeCircleThreePnt(py1, pz1, px1)
140 id_circle = geompy.addToStudy(circle, "Path")
141  
142 # create a 2D mesh on the face
143 trias = smesh.Mesh(face1, "Face : 2D mesh")
144
145 algo1D = trias.Segment()
146 algo1D.NumberOfSegments(6)
147 algo2D = trias.Triangle()
148 algo2D.LengthFromEdges()
149
150 trias.Compute()
151
152 # create a path mesh
153 circlemesh = smesh.Mesh(circle, "Path mesh")
154 algo = circlemesh.Segment()
155 algo.NumberOfSegments(10)
156 circlemesh.Compute()
157
158 # extrusion of the mesh
159 trias.ExtrusionAlongPath([], circlemesh, circle,
160                          1, 0, [], 0, smesh.PointStruct(0, 0, 0))
161
162 # merge nodes
163 print "Number of nodes before MergeNodes:", 
164 trias.NbNodes()
165 tolerance = 0.001
166 array_of_nodes_groups = trias.FindCoincidentNodes(tolerance)
167
168 trias.MergeNodes(array_of_nodes_groups)
169
170 print "Number of nodes after MergeNodes:", trias.NbNodes()
171 print ""
172 print "Number of elements before MergeEqualElements:"
173 print "Edges      : ", trias.NbEdges()
174 print "Triangles  : ", trias.NbTriangles()
175 print "Quadrangles: ", trias.NbQuadrangles()
176 print "Volumes    : ", trias.NbVolumes()
177
178 # merge elements
179 trias.MergeEqualElements()
180 print "Number of elements after MergeEqualElements:"
181 print "Edges      : ", trias.NbEdges()
182 print "Triangles  : ", trias.NbTriangles()
183 print "Quadrangles: ", trias.NbQuadrangles()
184 print "Volumes    : ", trias.NbVolumes()
185
186 salome.sg.updateObjBrowser(1)
187 \endcode
188
189 <br><h2>Sewing Meshes</h2>
190
191 <br>
192 \anchor tui_sew_meshes_border_to_side
193 <h3>Sew Meshes Border to Side</h3>
194
195 \code
196 import geompy
197 import smesh
198
199 # create two faces of a box
200 box1 = geompy.MakeBox(0., 0., -10., 30., 20., 25.)
201 facesList1 = geompy.SubShapeAll(box1, geompy.ShapeType["FACE"])
202 face1 = facesList1[2]
203
204 box2 = geompy.MakeBox(0., 5., 0., 20., 20., 15.)
205 facesList2 = geompy.SubShapeAll(box2, geompy.ShapeType["FACE"])
206 face2 = facesList2[1]
207
208 edgesList = geompy.SubShapeAll(face2, geompy.ShapeType["EDGE"])
209 edge1 = edgesList[2]
210
211 aComp = geompy.MakeCompound([face1, face2])
212 geompy.addToStudy(aComp, "Two faces")
213
214 # create a mesh on two faces
215 mesh = smesh.Mesh(aComp, "Two faces : quadrangle mesh")
216
217 algo1D = mesh.Segment()
218 algo1D.NumberOfSegments(9)
219 algo2D = mesh.Quadrangle()
220
221 algo_local = mesh.Segment(edge1)
222 algo_local.Arithmetic1D(1, 4)
223 algo_local.Propagation()
224
225 mesh.Compute()
226
227 # sew border to side
228 # FirstNodeIDOnFreeBorder, SecondNodeIDOnFreeBorder, LastNodeIDOnFreeBorder,
229 # FirstNodeIDOnSide, LastNodeIDOnSide,
230 # CreatePolygons, CreatePolyedrs
231 mesh.SewBorderToSide(5, 45, 6, 113, 109, 0, 0)
232 \endcode
233
234 <br>
235 \anchor tui_sew_conform_free_borders
236 <h3>Sew Conform Free Borders</h3>
237
238 \code
239 import geompy
240 import smesh
241
242 # create two faces of the box
243 box1 = geompy.MakeBox(0., 0., -10., 20., 20., 15.)
244 facesList1 = geompy.SubShapeAll(box1, geompy.ShapeType["FACE"])
245 face1 = facesList1[2]
246
247 box2 = geompy.MakeBox(0., 5., 0., 20., 20., 15.)
248 facesList2 = geompy.SubShapeAll(box2, geompy.ShapeType["FACE"])
249 face2 = facesList2[1]
250
251 edgesList = geompy.SubShapeAll(face2, geompy.ShapeType["EDGE"])
252 edge1 = edgesList[2]
253
254 aComp = geompy.MakeCompound([face1, face2])
255 geompy.addToStudy(aComp, "Two faces")
256
257 # create a mesh on two faces
258 mesh = smesh.Mesh(aComp, "Two faces : quadrangle mesh")
259
260 algo1D = mesh.Segment()
261 algo1D.NumberOfSegments(9)
262 algo2D = mesh.Quadrangle()
263
264 algo_local = mesh.Segment(edge1)
265 algo_local.Arithmetic1D(1, 4)
266 algo_local.Propagation()
267
268 mesh.Compute()
269
270 # sew conform free borders
271 # FirstNodeID1, SecondNodeID1, LastNodeID1, FirstNodeID2, SecondNodeID2
272 mesh.SewConformFreeBorders(5, 45, 6, 3, 24) 
273 \endcode
274
275 <br>
276 \anchor tui_sew_free_borders
277 <h3>Sew Free Borders</h3>
278
279 \code
280 import geompy
281 import smesh
282
283 # create two faces of the box
284 box1 = geompy.MakeBox(0., 0., 0., 20., 20., 15.)
285 facesList1 = geompy.SubShapeAll(box1, geompy.ShapeType["FACE"])
286 face1 = facesList1[2]
287
288 box2 = geompy.MakeBox(0., 5., 0., 20., 20., 15.)
289 facesList2 = geompy.SubShapeAll(box2, geompy.ShapeType["FACE"])
290 face2 = facesList2[1]
291
292 edgesList = geompy.SubShapeAll(face2, geompy.ShapeType["EDGE"])
293 edge1 = edgesList[2]
294
295 aComp = geompy.MakeCompound([face1, face2])
296 geompy.addToStudy(aComp, "Two faces")
297
298 # create a mesh on two faces
299 mesh = smesh.Mesh(aComp, "Two faces : quadrangle mesh")
300
301 algo1D = mesh.Segment()
302 algo1D.NumberOfSegments(4)
303 algo2D = mesh.Quadrangle()
304
305 algo_local = mesh.Segment(edge1)
306 algo_local.Arithmetic1D(1, 4)
307 algo_local.Propagation()
308
309 mesh.Compute()
310
311 # sew free borders
312 # FirstNodeID1, SecondNodeID1, LastNodeID1,
313 # FirstNodeID2, SecondNodeID2, LastNodeID2, CreatePolygons, CreatePolyedrs
314 mesh.SewFreeBorders(6, 21, 5, 1, 12, 3, 0, 0)
315 \endcode
316
317 <br>
318 \anchor tui_sew_side_elements
319 <h3>Sew Side Elements</h3>
320
321 \code
322 import geompy
323 import smesh
324
325 # create two boxes
326 box1 = geompy.MakeBox(0.,  0., 0., 10., 10., 10.)
327 box2 = geompy.MakeBox(0., 15., 0., 20., 25., 10.)
328
329 EdgesList = geompy.SubShapeAll(box2, geompy.ShapeType["EDGE"])
330
331 aComp = geompy.MakeCompound([box1, box2])
332 geompy.addToStudy(aComp, "Two boxes")
333
334 # create a mesh on two boxes
335 mesh = smesh.Mesh(aComp, "Two faces : quadrangle mesh")
336
337 algo1D = mesh.Segment()
338 algo1D.NumberOfSegments(2)
339 algo2D = mesh.Quadrangle()
340
341 algo_local = mesh.Segment(EdgesList[8])
342 algo_local.NumberOfSegments(4)
343 algo_local.Propagation()
344
345 mesh.Compute()
346
347 # sew side elements
348 # IDsOfSide1Elements, IDsOfSide2Elements,
349 # NodeID1OfSide1ToMerge, NodeID1OfSide2ToMerge, NodeID2OfSide1ToMerge, NodeID2OfSide2ToMerge
350 mesh.SewSideElements([69, 70, 71, 72], [91, 92, 89, 90], 8, 38, 23, 58)
351 \endcode
352
353 <br>
354 \anchor tui_duplicate_nodes
355 <h3>Duplicate nodes</h3>
356
357 \code
358 import salome
359 import smesh
360 import SMESH_test1
361
362 mesh = SMESH_test1.mesh
363
364 # Compute mesh
365 mesh.Compute()
366
367 # Without the duplication of border elements
368
369 # Nodes to duplicate
370 nodes1 = mesh.CreateEmptyGroup( smesh.NODE, 'nodes1' )
371 nodes1.Add( [ 289, 278, 302, 285 ] )
372
373 # Group of faces to replace nodes with new ones 
374 faces1 = mesh.CreateEmptyGroup( smesh.FACE, 'faces1' )
375 faces1.Add( [ 519, 556, 557 ] )
376
377 # Duplicate nodes
378 print "\nMesh before the first nodes duplication:"
379 print "Nodes      : ", mesh.NbNodes()
380 print "Edges      : ", mesh.NbEdges()
381 print "Triangles  : ", mesh.NbTriangles()
382
383 groupOfCreatedNodes = mesh.DoubleNodeGroup(nodes1, faces1, theMakeGroup=True)
384 print "New nodes:", groupOfCreatedNodes.GetIDs()
385
386 print "\nMesh after the first nodes duplication:"
387 print "Nodes      : ", mesh.NbNodes()
388 print "Edges      : ", mesh.NbEdges()
389 print "Triangles  : ", mesh.NbTriangles()
390
391 # With the duplication of border elements
392
393 # Edges to duplicate
394 edges = mesh.CreateEmptyGroup( smesh.EDGE, 'edges' )
395 edges.Add( [ 29, 30, 31 ] )
396
397 # Nodes not to duplicate
398 nodes2 = mesh.CreateEmptyGroup( smesh.NODE, 'nodes2' )
399 nodes2.Add( [ 32, 5 ] )
400
401 # Group of faces to replace nodes with new ones 
402 faces2 = mesh.CreateEmptyGroup( smesh.FACE, 'faces2' )
403 faces2.Add( [ 576, 578, 580 ] )
404
405 # Duplicate nodes
406 print "\nMesh before the second nodes duplication:"
407 print "Nodes      : ", mesh.NbNodes()
408 print "Edges      : ", mesh.NbEdges()
409 print "Triangles  : ", mesh.NbTriangles()
410
411 groupOfNewEdges = mesh.DoubleNodeElemGroup( edges, nodes2, faces2, theMakeGroup=True )
412 print "New edges:", groupOfNewEdges.GetIDs()
413
414 print "\nMesh after the second nodes duplication:"
415 print "Nodes      : ", mesh.NbNodes()
416 print "Edges      : ", mesh.NbEdges()
417 print "Triangles  : ", mesh.NbTriangles()
418
419 # Update object browser
420 if salome.sg.hasDesktop():
421     salome.sg.updateObjBrowser(0)
422 \endcode
423
424 <br>
425 \anchor tui_make_2dmesh_from_3d
426 <h3>Create boundary elements</h3>
427
428 \code
429 # The objective of these samples is to illustrate the following use cases:
430 # 1) The mesh MESH1 with 3D cells has no or only a part of its skin (2D cells):
431 #    1.1) Add the 2D skin (missing 2D cells) to MESH1 (what is done now by the algorithm).
432 #    1.2) Create a new 3D Mesh MESH2 that consists of MESH1 and added 2D skin cells.
433 #    1.3) Create a new 2D Mesh MESH3 that consists only of 2D skin cells.
434 # 2) The mesh MESH1 with 3D cells has all its skin (2D cells):
435 #    Create a new 2D Mesh MESH3 that consists only of 2D skin cells.
436 #
437 # In all cases an option to create a group containing these 2D skin cells is available.
438
439 from smesh import *
440
441 box = geompy.MakeBoxDXDYDZ(1,1,1)
442 geompy.addToStudy(box,"box")
443 boxFace = geompy.SubShapeAll(box, geompy.ShapeType["FACE"])[0]
444 geompy.addToStudyInFather(box,boxFace,"boxFace")
445
446 MESH1 = Mesh(box,"MESH1")
447 MESH1.AutomaticHexahedralization()
448
449 init_nb_edges = MESH1.NbEdges()
450 init_nb_faces = MESH1.NbFaces()
451 init_nb_volumes = MESH1.NbVolumes()
452
453 # =========================================================================================
454 # 1) The mesh MESH1 with 3D cells has no or only a part of its skin (2D cells)
455 # =========================================================================================
456 # remove some faces
457 all_faces = MESH1.GetElementsByType(SMESH.FACE)
458 rm_faces = all_faces[:init_nb_faces/5] + all_faces[4*init_nb_faces/5:]
459 MESH1.RemoveElements(rm_faces)
460 assert(MESH1.NbFaces() == init_nb_faces-len(rm_faces))
461
462 # 1.1) Add the 2D skin (missing 2D cells) to MESH1
463 # -------------------------------------------------
464 # add missing faces
465 # 1.1.1) to the whole mesh
466 m,g = MESH1.MakeBoundaryMesh(MESH1)
467 assert(init_nb_faces == MESH1.NbFaces())
468 assert(init_nb_edges == MESH1.NbEdges())
469 assert(m)
470 assert(not g)
471
472 # 1.1.2) to some elements
473 MESH1.RemoveElements(rm_faces)
474 MESH1.MakeBoundaryMesh([])
475 assert(init_nb_faces != MESH1.NbFaces())
476 volumes = MESH1.GetElementsByType(SMESH.VOLUME)
477 for v in volumes:
478     MESH1.MakeBoundaryMesh([v])
479 assert(init_nb_faces == MESH1.NbFaces())
480 assert(init_nb_edges == MESH1.NbEdges())
481
482 # 1.1.3) to a group of elements
483 volGroup1 = MESH1.CreateEmptyGroup(SMESH.VOLUME, "volGroup1")
484 volGroup1.Add( volumes[: init_nb_volumes/2])
485 volGroup2 = MESH1.CreateEmptyGroup(SMESH.VOLUME, "volGroup2")
486 volGroup1.Add( volumes[init_nb_volumes/2:])
487 MESH1.RemoveElements(rm_faces)
488 MESH1.MakeBoundaryMesh(volGroup1)
489 MESH1.MakeBoundaryMesh(volGroup2)
490 assert(init_nb_faces == MESH1.NbFaces())
491 assert(init_nb_edges == MESH1.NbEdges())
492
493 # 1.1.4) to a submesh.
494 # The submesh has no volumes, so it is required to check if it passes without crash and does not create
495 # missing faces
496 faceSubmesh = MESH1.GetSubMesh( boxFace, "boxFace" )
497 MESH1.RemoveElements(rm_faces)
498 MESH1.MakeBoundaryMesh(faceSubmesh)
499 assert(init_nb_faces != MESH1.NbFaces())
500
501 # check group creation
502 MESH1.RemoveElements(rm_faces)
503 groupName = "added to mesh"
504 m,group = MESH1.MakeBoundaryMesh(MESH1,groupName=groupName)
505 assert(group)
506 assert(group.GetName() == groupName)
507 assert(group.Size() == len(rm_faces))
508
509
510 # 1.2) Create a new 3D Mesh MESH2 that consists of MESH1 and added 2D skin cells.
511 # ------------------------------------------------------------------------------
512 MESH1.RemoveElements(rm_faces)
513 meshName = "MESH2"
514 MESH2,group = MESH1.MakeBoundaryMesh(MESH1,meshName=meshName,toCopyElements=True)
515 assert(MESH2)
516 assert(MESH2.GetName() == meshName)
517 assert(MESH2.NbVolumes() == MESH1.NbVolumes())
518 assert(MESH2.NbFaces() == len(rm_faces))
519
520 # check group creation
521 MESH1.RemoveElements(rm_faces)
522 MESH2,group = MESH1.MakeBoundaryMesh(MESH1,meshName="MESH2_0",
523                                      groupName=groupName,toCopyElements=True)
524 assert(group)
525 assert(group.GetName() == groupName)
526 assert(group.Size() == len(rm_faces))
527 assert(group.GetMesh()._is_equivalent(MESH2.GetMesh()))
528
529 # 1.3) Create a new 2D Mesh MESH3 that consists only of 2D skin cells.
530 # -----------------------------------------------------------------------
531 MESH1.RemoveElements(rm_faces)
532 meshName = "MESH3"
533 MESH3,group = MESH1.MakeBoundaryMesh(MESH1,meshName=meshName,toCopyExistingBondary=True)
534 assert(MESH3)
535 assert(not group)
536 assert(MESH3.GetName() == meshName)
537 assert(MESH3.NbVolumes() == 0)
538 assert(MESH3.NbFaces() == init_nb_faces)
539
540 # check group creation
541 MESH1.RemoveElements(rm_faces)
542 MESH3,group = MESH1.MakeBoundaryMesh(MESH1,meshName=meshName,
543                                      groupName=groupName, toCopyExistingBondary=True)
544 assert(group)
545 assert(group.GetName() == groupName)
546 assert(group.Size() == len(rm_faces))
547 assert(group.GetMesh()._is_equivalent(MESH3.GetMesh()))
548 assert(MESH3.NbFaces() == init_nb_faces)
549
550 # ==================================================================
551 # 2) The mesh MESH1 with 3D cells has all its skin (2D cells)
552 # Create a new 2D Mesh MESH3 that consists only of 2D skin cells.
553 # ==================================================================
554 MESH1.MakeBoundaryMesh(MESH1)
555 MESH3,group = MESH1.MakeBoundaryMesh(MESH1,meshName=meshName,toCopyExistingBondary=True)
556 assert(MESH3)
557 assert(not group)
558 assert(MESH3.NbVolumes() == 0)
559 assert(MESH3.NbFaces() == init_nb_faces)
560
561 # check group creation
562 MESH3,group = MESH1.MakeBoundaryMesh(MESH1,meshName=meshName,
563                                      groupName=groupName, toCopyExistingBondary=True)
564 assert(group)
565 assert(group.GetName() == groupName)
566 assert(group.Size() == 0)
567 assert(group.GetMesh()._is_equivalent(MESH3.GetMesh()))
568 assert(MESH3.NbFaces() == init_nb_faces)
569
570 # ================
571 # Make 1D from 2D
572 # ================
573
574 MESH1.Clear()
575 MESH1.Compute()
576 MESH1.RemoveElements( MESH1.GetElementsByType(SMESH.EDGE))
577
578 rm_faces = faceSubmesh.GetIDs()[:2] # to remove few adjacent faces
579 nb_missing_edges = 2 + 2*len(rm_faces)
580
581 MESH1.RemoveElements(rm_faces)
582 mesh,group = MESH1.MakeBoundaryMesh(MESH1, BND_1DFROM2D)
583 assert( MESH1.NbEdges() == nb_missing_edges )
584
585
586 \endcode
587 */