]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/salome_test.py
Salome HOME
0023471: [CEA 2162] Upgrade of the test salome_test.py
[modules/kernel.git] / src / KERNEL_PY / salome_test.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016  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, or (at your option) any later version.
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 print()
25 print("Perform quick test of the application by loading of the GEOM, SMESH, FIELDS, PARAVIS")
26 print("components and doing some operation within the components.")
27 print()
28
29 import salome
30 import SALOME
31 import SALOMEDS
32 import os
33 import sys
34 import time
35 import SALOME_ModuleCatalog
36
37 step = 1
38
39 print("======================================================================")
40 print("           %d. Initialize study " % step); step+=1
41 print("======================================================================")
42
43 # initialize study
44 salome.salome_init()
45 # get study builder
46 builder = salome.myStudy.NewBuilder()
47 print("OK")
48
49 print() 
50
51 print("======================================================================")
52 print("           %d. Retrieve module catalog " % step); step+=1
53 print("======================================================================")
54
55 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
56 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
57 if not catalog:
58
59     raise RuntimeError("Can't access module catalog")
60 print("OK")
61
62 print()
63
64 print("======================================================================")
65 print("           %d. Check modules availability in the module catalog " % step); step+=1
66 print("======================================================================")
67
68 for module in [ "GEOM", "SMESH", "MEDFactory", "PVSERVER"]:
69     print()
70     print("--- Check %s ..." % module)
71     comp = catalog.GetComponent(module)
72     if not comp:
73         raise RuntimeError("Component %s is not found in Module Catalog." % module)
74     print("OK")
75     pass
76
77 print()
78
79 print("======================================================================")
80 print("           %d. Test Data Server " % step); step+=1
81 print("======================================================================")
82
83 print()
84 print("--- Create new component ...")
85 comp = builder.NewComponent("TEST")
86 if not comp:
87     raise RuntimeError("Can't create new component")
88 print("OK")
89
90 print()
91 print("--- Create AttributeName ...")
92 A = builder.FindOrCreateAttribute(comp, "AttributeName")
93 if not A:
94     raise RuntimeError("Can't create AttributeName attribute")
95 A.SetValue("TEST")
96 if A.Value() != "TEST":
97     raise RuntimeError("Error : wrong value of  AttributeName")
98 print("OK")
99
100 print()
101 print("--- Create AttributeReal ...")
102 A = builder.FindOrCreateAttribute(comp, "AttributeReal")
103 if not A:
104     raise RuntimeError("Can't create AttributeReal attribute")
105 A.SetValue(0.0001)
106 if A.Value() != 0.0001:
107     raise RuntimeError("Error : wrong value of  AttributeReal")
108 print("OK")
109
110 print()
111
112 print("======================================================================")
113 print("           %d. Test Geometry " % step); step+=1
114 print("======================================================================")
115
116 from salome.geom import geomBuilder
117 geompy = geomBuilder.New()
118
119 ShapeTypeCompSolid = 1
120 ShapeTypeSolid = 2
121 ShapeTypeShell = 3
122 ShapeTypeFace = 4
123 ShapeTypeWire = 5
124 ShapeTypeEdge = 6
125 ShapeTypeVertex = 7
126
127 print()
128 print("--- Create a box ...")
129 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
130 idbox = geompy.addToStudy(box, "box")
131 box_obj = salome.myStudy.FindObjectByPath("/Geometry/box")
132 if not box_obj:
133     raise RuntimeError("Error : wrong value of  AttributeReal")
134 print("OK")
135
136 # ---- add shell from box in study
137 print() 
138 print("--- Extract shell ...")
139 subShellList = geompy.SubShapeAll(box, ShapeTypeShell)
140 shell = subShellList[0]
141 name = geompy.SubShapeName(shell, box)
142 idshell = geompy.addToStudyInFather(box, shell, name)
143 print(name)
144 print("OK")
145
146 # ---- add first face of box in study
147 print() 
148 print("--- Extract face ...")
149 subShapeList = geompy.SubShapeAll(box, ShapeTypeFace)
150 face = subShapeList[0]
151 name = geompy.SubShapeName(face, box)
152 idface = geompy.addToStudyInFather(box, face, name)
153 print(name)
154 print("OK")
155
156 # ---- add first edge of face in study
157 print() 
158 print("--- Extract edge ...")
159 edgeList = geompy.SubShapeAll(face, ShapeTypeEdge)
160 edge = edgeList[0];
161 name = geompy.SubShapeName(edge, face)
162 idedge = geompy.addToStudyInFather(face, edge, name)
163 print(name)
164 print("OK")
165
166 # ---- update object browser
167 if salome.hasDesktop():
168     salome.sg.updateObjBrowser();
169
170 print()
171
172 print("======================================================================")
173 print("           %d. Test Mesh " % step); step+=1
174 print("======================================================================")
175
176 from salome.StdMeshers import StdMeshersBuilder
177 import SMESH
178 from salome.smesh import smeshBuilder
179
180 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
181 if salome.hasDesktop():
182     smeshgui = salome.ImportComponentGUI("SMESH")
183     smeshgui.Init();
184 else:
185     smesh = smeshBuilder.New()
186
187 # ---- create hypotheses 
188
189 stdMeshersEngine = "StdMeshersEngine"
190
191 print()
192 print("--- Create hypotheses ...")
193
194 print()
195 print("------ LocalLength ...")
196 hypLen1 = smesh.CreateHypothesis( "LocalLength", stdMeshersEngine )
197 hypLen1.SetLength(100)
198 print(hypLen1.GetName())
199 print(hypLen1.GetId())
200 print(hypLen1.GetLength())
201 if salome.hasDesktop():
202     smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
203 print("OK")
204
205 print()
206 print("------ NumberOfSegments ...")
207 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", stdMeshersEngine )
208 hypNbSeg1.SetNumberOfSegments(7)
209 print(hypNbSeg1.GetName())
210 print(hypNbSeg1.GetId())
211 print(hypNbSeg1.GetNumberOfSegments())
212 if salome.hasDesktop():
213     smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
214 print("OK")
215
216 print()
217 print("------ MaxElementArea [1] ...")
218 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
219 hypArea1.SetMaxElementArea(2500)
220 print(hypArea1.GetName())
221 print(hypArea1.GetId())
222 print(hypArea1.GetMaxElementArea())
223 if salome.hasDesktop():
224     smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
225 print("OK")
226
227 print()
228 print("------ MaxElementArea [2] ...")
229 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
230 hypArea2.SetMaxElementArea(500)
231 print(hypArea2.GetName())
232 print(hypArea2.GetId())
233 print(hypArea2.GetMaxElementArea())
234 if salome.hasDesktop():
235     smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
236 print("OK")
237
238 # ---- create algorithms
239
240 print()
241 print("--- Create algorithms ...")
242
243 print()
244 print("------ Regular_1D ...")
245 algoReg = smesh.CreateHypothesis( "Regular_1D", stdMeshersEngine )
246 listHyp = algoReg.GetCompatibleHypothesis()
247 for hyp in listHyp:
248     print(hyp)
249 print(algoReg.GetName())
250 print(algoReg.GetId())
251 if salome.hasDesktop():
252     smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
253 print("OK")
254
255 print()
256 print("------ MEFISTO_2D ...")
257 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", stdMeshersEngine )
258 listHyp=algoMef.GetCompatibleHypothesis()
259 for hyp in listHyp:
260     print(hyp)
261 print(algoMef.GetName())
262 print(algoMef.GetId())
263 if salome.hasDesktop():
264     smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
265 print("OK")
266
267 # ---- create mesh on the box, apply hypotheses / algorithms
268
269 print()
270 print("--- Create mesh on the box ...")
271 mesh = smesh.CreateMesh(box)
272 if salome.hasDesktop():
273     smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
274 ret = mesh.AddHypothesis(box, algoReg)
275 ret = mesh.AddHypothesis(box, algoMef)
276 ret = mesh.AddHypothesis(box, hypNbSeg1)
277 ret = mesh.AddHypothesis(box, hypArea1)
278 print("OK")
279
280 # ---- create submesh on the edge, add hypothesis
281
282 print()
283 print("--- Add 1D sub-mesh on the edge ...")
284 submesh = mesh.GetSubMesh(edge, "SubMeshEdge")
285 ret = mesh.AddHypothesis(edge, algoReg)
286 ret = mesh.AddHypothesis(edge, hypLen1)
287 print("OK")
288
289 # ---- create submesh on the edge, add hypothesis
290
291 print()
292 print("--- Add 2D sub-mesh on the face ...")
293 submesh = mesh.GetSubMesh(face, "SubMeshFace")
294 ret = mesh.AddHypothesis(face, hypArea2)
295 print("OK")
296
297 # ---- compute mesh
298 print()
299 print("--- Compute mesh ...")
300 smesh.Compute(mesh, box)
301 print("OK")
302
303 # ---- update object browser
304 if salome.hasDesktop():
305     salome.sg.updateObjBrowser();
306
307 print()
308
309 print("======================================================================")
310 print("           %d. Test Fields " % step); step+=1
311 print("======================================================================")
312
313 import medcalc
314 #from medcalc import properties
315 from medcalc.fieldproxy import FieldProxy
316
317 medcalc.medconsole.setConsoleGlobals(globals())
318 try:
319     med_root=os.environ["FIELDS_ROOT_DIR"]
320 except KeyError as e:
321     raise RuntimeError("FIELDS_ROOT_DIR should be defined to load the test data")
322
323 filepath = os.path.join(med_root,"share","salome","resources","fields","medcalc_testfiles","smallmesh_varfield.med")
324 medcalc.medio.LoadDataSource(filepath)
325 fieldHandlerList = medcalc.medevents.dataManager.getFieldHandlerList()
326
327 fieldHandler0 = fieldHandlerList[0]
328 print("---Field Handler 0:\n%s" % fieldHandler0)
329 fieldHandler1 = fieldHandlerList[1]
330 print("---Field Handler 1:\n%s" % fieldHandler1)
331
332 print("--- The addition of two fields can be done using field handler directly.")
333 addFieldHandler = medcalc.fieldproxy.calculator.add(fieldHandler0,fieldHandler1)
334 print("--- Result handler:\n%s" % addFieldHandler)
335
336 print("--- Or with a field proxy that easy the writing of operations.")
337 fieldProxy0 = FieldProxy(fieldHandler0)
338 fieldProxy1 = FieldProxy(fieldHandler1)
339
340 resHandler = fieldProxy0 + fieldProxy1
341 if resHandler is None:
342     print("Error: result handler is None!")
343 else:
344     print("--- Result handler:\n%s" % resHandler)
345     print("OK")
346
347 print()
348
349
350 print("======================================================================")
351 print("           %d. Test Paravis " % step); step+=1
352 print("======================================================================")
353
354 if salome.hasDesktop(): # in gui mode
355
356     print("**** Importing pvserver... It can take some time.")
357     import pvsimple as pvs
358
359     #====================Stage1: Importing MED file====================
360
361     print("**** Stage1: Importing MED file")
362     
363     med_file = "ResOK_0000.med"
364     print("Import '{}'...............".format(med_file), end=' ')
365     path = os.path.join(os.getenv('DATA_DIR'), 'MedFiles', med_file)
366     med_reader = pvs.MEDReader(FileName=path)
367     if med_reader is None:
368         print("FAILED")
369     else:
370         print("OK")
371     times = med_reader.TimestepValues.GetData()
372
373     #====================Stage2: Displaying presentation===============
374     
375     print("**** Stage2: Displaying presentation")
376
377     # -----------------------------------------------------------------
378     print('Get view...................', end=' ')
379     view = pvs.GetRenderView()
380     if view is None:
381         print("FAILED")
382     else:
383         print("OK")
384     view.ResetCamera()
385
386     # -----------------------------------------------------------------
387     print("Show mesh..................", end=' ')
388     mesh = pvs.Show(med_reader, view)
389     view.ResetCamera()
390     if mesh is None:
391         print("FAILED")
392     else:
393         print("OK")
394     mesh.Representation = 'Surface With Edges'
395     pvs.Render(view)
396     time.sleep(1)
397
398     # -----------------------------------------------------------------
399     print("Show scalar map............", end=' ')
400     scalar_map = mesh
401     if scalar_map is None:
402         print("FAILED")
403     else:
404         print("OK")
405     scalar_map.Representation = 'Surface'
406     pvs.ColorBy(scalar_map, ('POINTS', 'vitesse', 'Magnitude'))
407     #scalar_map.SetScalarBarVisibility(view, True)
408     view.ViewTime = times[-1]
409     pvs.Render(view)
410     time.sleep(1)
411     scalar_map.Visibility = 0
412     pvs.Render(view)
413
414     # -----------------------------------------------------------------
415     print("Show vectors...............", end=' ')
416     calc = pvs.Calculator(Input=med_reader)
417     calc.ResultArrayName = 'vitesse_3c'
418     calc.Function = 'iHat * vitesse_X + jHat * vitesse_Y + kHat * 0'
419     glyph = pvs.Glyph(Input=calc, GlyphType='Arrow')
420     glyph.OrientationArray = ['POINTS', 'vitesse_3c']
421     glyph.ScaleArray = ['POINTS', 'No scale array']
422     glyph.MaximumGlyphSize = 0.01
423     vectors = pvs.Show(glyph, view)
424     if vectors is None:
425         print("FAILED")
426     else:
427         print("OK")
428     vectors.Representation = 'Surface'
429     pvs.Render(view)
430     time.sleep(1)
431     vectors.Visibility = 0
432     pvs.Render(view)
433
434     # -----------------------------------------------------------------
435     print("Show iso surfaces..........", end=' ')
436     merge_blocks = pvs.MergeBlocks(Input=med_reader)
437     calc = pvs.Calculator(Input=merge_blocks)
438     calc.ResultArrayName = 'vitesse_magnitude'
439     calc.Function = 'sqrt(vitesse_X^2+vitesse_Y^2)'
440     data_range = med_reader.GetPointDataInformation()['vitesse'].GetComponentRange(-1)
441     nb_surfaces = 10
442     surfaces = [data_range[0] + i*(data_range[1]-data_range[0])/(nb_surfaces-1) for i in range(nb_surfaces)]
443     contour = pvs.Contour(Input=calc)
444     contour.ComputeScalars = 1
445     contour.ContourBy = ['POINTS', 'vitesse_magnitude']
446     contour.Isosurfaces = surfaces
447     iso_surfaces = pvs.Show(contour, view)
448     if iso_surfaces is None:
449         print("FAILED")
450     else:
451         print("OK")
452     iso_surfaces.Representation = 'Surface'
453     pvs.ColorBy(iso_surfaces, ('POINTS', 'vitesse', 'Magnitude'))
454     pvs.Render(view)
455     time.sleep(1)
456     iso_surfaces.Visibility = 0
457     pvs.Render(view)
458
459     # -----------------------------------------------------------------
460     print("Show cut planes............", end=' ')
461     slice = pvs.Slice(Input=med_reader)
462     slice.SliceType = "Plane"
463     slice.SliceType.Normal = [1.0, 0.0, 0.0]
464     bounds = med_reader.GetDataInformation().GetBounds()
465     nb_planes = 30
466     displacement = 0.5
467     b_left = bounds[0] + (bounds[1]-bounds[0])*displacement/100
468     b_right = bounds[1] - (bounds[1]-bounds[0])*displacement/100
469     b_range = b_right - b_left
470     positions = [b_left + i*b_range/(nb_planes-1) for i in range(nb_planes)]
471     slice.SliceOffsetValues = positions
472     pvs.Hide3DWidgets(proxy=slice.SliceType)
473     cut_planes = pvs.Show(slice, view)
474     if cut_planes is None:
475         print("FAILED")
476     else:
477         print("OK")
478     cut_planes.Representation = 'Surface'
479     pvs.ColorBy(cut_planes, ('POINTS', 'vitesse', 'Magnitude'))
480     pvs.Render(view)
481     time.sleep(1)
482     cut_planes.Visibility = 0
483     pvs.Render(view)
484
485     # -----------------------------------------------------------------
486     print("Show deformed shape........", end=' ')
487     merge_blocks = pvs.MergeBlocks(Input=med_reader)
488     calc = pvs.Calculator(Input=merge_blocks)
489     calc.ResultArrayName = 'vitesse_3c'
490     calc.Function = 'iHat * vitesse_X + jHat * vitesse_Y + kHat * 0'
491     warp = pvs.WarpByVector(Input=calc)
492     warp.Vectors = ['POINTS', 'vitesse_3c']
493     warp.ScaleFactor = 0.5
494     deformed_shape = pvs.Show(warp, view)
495     if deformed_shape is None:
496         print("FAILED")
497     else:
498         print("OK")
499     deformed_shape.Representation = 'Surface'
500     pvs.ColorBy(deformed_shape, ('CELLS', 'pression'))
501     pvs.Render(view)
502     time.sleep(1)
503     deformed_shape.Visibility = 0
504     pvs.Render(view)
505
506 else: # not in gui mode, Paravis can not be tested
507     
508     print()
509     print("PARAVIS module requires SALOME to be running in GUI mode.")
510     print()
511     print("Skipping test for PARAVIS...")
512     pass
513
514 # ---- update object browser
515 if salome.hasDesktop():
516     salome.sg.updateObjBrowser();