Salome HOME
bos #29483 Merge branch 'eap/29483_gmsh_sub'
[plugins/gmshplugin.git] / tests / gmsh_compound_mesh_2d.py
1 #!/usr/bin/env python
2
3 '''
4 This file creates a compound surface of a square and a circle. Then meshes them with 
5 Gmsh using compound mesh feature of Gmsh.
6 '''
7 import salome
8 salome.salome_init()
9
10 #-------------------------------------
11 ### SHAPER component
12 #-------------------------------------
13
14 from salome.shaper import model
15 model.begin()
16 partSet = model.moduleDocument()
17
18 ### Create Part
19 Part_1 = model.addPart(partSet)
20 Part_1_doc = Part_1.document()
21
22 ### Create Sketch
23 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
24
25 ### Create SketchLine
26 SketchLine_1 = Sketch_1.addLine(25, 0, 0, 0)
27
28 ### Create SketchProjection
29 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
30 SketchPoint_1 = SketchProjection_1.createdFeature()
31 Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchPoint_1.result())
32
33 ### Create SketchLine
34 SketchLine_2 = Sketch_1.addLine(0, 0, 0, 25)
35
36 ### Create SketchLine
37 SketchLine_3 = Sketch_1.addLine(0, 25, 25, 25)
38
39 ### Create SketchLine
40 SketchLine_4 = Sketch_1.addLine(25, 25, 25, 0)
41 Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
42 Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
43 Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
44 Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
45 Sketch_1.setHorizontal(SketchLine_1.result())
46 Sketch_1.setVertical(SketchLine_2.result())
47 Sketch_1.setHorizontal(SketchLine_3.result())
48 Sketch_1.setVertical(SketchLine_4.result())
49 Sketch_1.setEqual(SketchLine_1.result(), SketchLine_4.result())
50 Sketch_1.setLength(SketchLine_1.result(), 25)
51
52 ### Create SketchCircle
53 SketchCircle_1 = Sketch_1.addCircle(25, 25, 12.5)
54 Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchCircle_1.center())
55 Sketch_1.setRadius(SketchCircle_1.results()[1], 12.5)
56 model.do()
57
58 ### Create Face
59 Face_1_objects = [model.selection("FACE", "Sketch_1/Face-SketchLine_3f-SketchLine_4f-SketchCircle_1_2f-SketchCircle_1_2f"),
60                   model.selection("FACE", "Sketch_1/Face-SketchLine_4r-SketchCircle_1_2r-SketchLine_3r-SketchLine_2r-SketchLine_1r"),
61                   model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f-SketchLine_4r-SketchLine_3r")]
62 Face_1 = model.addFace(Part_1_doc, Face_1_objects)
63
64 ### Create Group
65 Group_1_objects = [model.selection("FACE", "Face_1_2"),
66                    model.selection("FACE", "Face_1_3"),
67                    model.selection("FACE", "Face_1_1")]
68 Group_1 = model.addGroup(Part_1_doc, "Faces", Group_1_objects)
69
70 ### Create Shell
71 Shell_1_objects = [model.selection("FACE", "Face_1_1"),
72                    model.selection("FACE", "Face_1_2"),
73                    model.selection("FACE", "Face_1_3")]
74 Shell_1 = model.addShell(Part_1_doc, Shell_1_objects)
75
76 ### Create Group
77 Group_2_objects = [model.selection("FACE", "Shell_1_1/Modified_Face&Face_1_2/Face_1_2"),
78                    model.selection("FACE", "Shell_1_1/Modified_Face&Face_1_3/Face_1_3"),
79                    model.selection("FACE", "Shell_1_1/Modified_Face&Face_1_1/Face_1_1")]
80 Group_2 = model.addGroup(Part_1_doc, "Faces", Group_2_objects)
81
82 model.end()
83
84 #-------------------------------------
85 ### SHAPERSTUDY component
86 #-------------------------------------
87
88 model.publishToShaperStudy()
89 import SHAPERSTUDY
90 Shell_1_1, Group_2_1, = SHAPERSTUDY.shape(model.featureStringId(Shell_1))
91
92 #-------------------------------------
93 ### SMESH component
94 #-------------------------------------
95
96 import  SMESH
97 from salome.smesh import smeshBuilder
98 smesh = smeshBuilder.New()
99
100 Mesh_1 = smesh.Mesh(Shell_1_1)
101 GMSH_2D = Mesh_1.Triangle(algo=smeshBuilder.GMSH_2D)
102 Gmsh_Parameters = GMSH_2D.Parameters()
103 Gmsh_Parameters.Set2DAlgo( 0 )
104 Gmsh_Parameters.SetMaxSize( 10 )
105 Gmsh_Parameters.SetMinSize( 5 )
106 Gmsh_Parameters.SetIs2d( 1 )
107 Gmsh_Parameters.SetCompoundOnShape(Group_2_1)
108 isDone = Mesh_1.Compute()
109
110 errorMsg=''
111 okMsg=''
112
113 #-------------------------------------
114 # Test: Frontal Delaunay
115 #-------------------------------------
116 try:
117   isDone = Mesh_1.Compute()
118   if not isDone:
119     errorMsg+= '\n ERROR: failed to mesh the compound square-circle surface using Delaunay algorithm from Gmsh\n'
120   else:
121     okMsg+= '\n PASSED: Successfully meshed the compound square-circle surface using Delaunay algorithm from Gmsh\n'
122 except:
123       errorMsg+='\n ERROR: Exception raised in Mesh computation'
124
125 #-------------------------------------
126 # Message that test are OK or not
127 #-------------------------------------
128
129 if okMsg!= '':
130   print (okMsg)
131       
132 if errorMsg!= '':
133   raise RuntimeError (errorMsg + "\n Test is KO.")
134
135 if salome.sg.hasDesktop():
136   smesh.SetName(GMSH_2D.GetAlgorithm(), 'GMSH_2D')
137   smesh.SetName(Mesh_1.GetMesh(), 'Mesh_compound_square-circle')
138   smesh.SetName(Gmsh_Parameters, 'Gmsh Parameters')
139   salome.sg.updateObjBrowser()