Salome HOME
b9c520a4bda7c53c8c6fbd3fbe11431541f872f0
[modules/kernel.git] / src / KERNEL_PY / salome_test.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  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.
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, MED, 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 SALOME_ModuleCatalog
35
36 step = 1
37
38 print "======================================================================"
39 print "           %d. Initialize study " % step; step+=1
40 print "======================================================================"
41
42 # initialize study
43 salome.salome_init()
44 # get study builder
45 builder = salome.myStudy.NewBuilder()
46 print "OK"
47
48 print 
49
50 print "======================================================================"
51 print "           %d. Retrieve module catalog " % step; step+=1
52 print "======================================================================"
53
54 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
55 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
56 if not catalog:
57     raise RuntimeError, "Can't accesss module catalog"
58 print "OK"
59
60 print
61
62 print "======================================================================"
63 print "           %d. Check modules availability in the module catalog " % step; step+=1
64 print "======================================================================"
65
66 for module in [ "GEOM", "SMESH", "MEDOPFactory", "PARAVIS"]:
67     print
68     print "--- Check %s ..." % module
69     comp = catalog.GetComponent(module)
70     if not comp:
71         raise RuntimeError, "Component %s is not found in Module Catalog." % module
72     print "OK"
73     pass
74
75 print
76
77 print "======================================================================"
78 print "           %d. Test Data Server " % step; step+=1
79 print "======================================================================"
80
81 print
82 print "--- Create new component ..."
83 comp = builder.NewComponent("TEST")
84 if not comp:
85     raise RuntimeError, "Can't create new component"
86 print "OK"
87
88 print
89 print "--- Create AttributeName ..."
90 A = builder.FindOrCreateAttribute(comp, "AttributeName")
91 if not A:
92     raise RuntimeError, "Can't create AttributeName attribute"
93 A.SetValue("TEST")
94 if A.Value() != "TEST":
95     raise RuntimeError, "Error : wrong value of  AttributeName"
96 print "OK"
97
98 print
99 print "--- Create AttributeReal ..."
100 A = builder.FindOrCreateAttribute(comp, "AttributeReal")
101 if not A:
102     raise RuntimeError, "Can't create AttributeReal attribute"
103 A.SetValue(0.0001)
104 if A.Value() != 0.0001:
105     raise RuntimeError, "Error : wrong value of  AttributeReal"
106 print "OK"
107
108 print
109
110 print "======================================================================"
111 print "           %d. Test Geometry " % step; step+=1
112 print "======================================================================"
113
114 from salome.geom import geomBuilder
115 geompy = geomBuilder.New(salome.myStudy)
116
117 ShapeTypeCompSolid = 1
118 ShapeTypeSolid = 2
119 ShapeTypeShell = 3
120 ShapeTypeFace = 4
121 ShapeTypeWire = 5
122 ShapeTypeEdge = 6
123 ShapeTypeVertex = 7
124
125 print
126 print "--- Create a box ..."
127 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
128 idbox = geompy.addToStudy(box, "box")
129 box_obj = salome.myStudy.FindObjectByPath("/Geometry/box")
130 if not box_obj:
131     raise RuntimeError, "Error : wrong value of  AttributeReal"
132 print "OK"
133
134 # ---- add shell from box in study
135 print 
136 print "--- Extract shell ..."
137 subShellList = geompy.SubShapeAll(box, ShapeTypeShell)
138 shell = subShellList[0]
139 name = geompy.SubShapeName(shell, box)
140 idshell = geompy.addToStudyInFather(box, shell, name)
141 print name
142 print "OK"
143
144 # ---- add first face of box in study
145 print 
146 print "--- Extract face ..."
147 subShapeList = geompy.SubShapeAll(box, ShapeTypeFace)
148 face = subShapeList[0]
149 name = geompy.SubShapeName(face, box)
150 idface = geompy.addToStudyInFather(box, face, name)
151 print name
152 print "OK"
153
154 # ---- add first edge of face in study
155 print 
156 print "--- Extract edge ..."
157 edgeList = geompy.SubShapeAll(face, ShapeTypeEdge)
158 edge = edgeList[0];
159 name = geompy.SubShapeName(edge, face)
160 idedge = geompy.addToStudyInFather(face, edge, name)
161 print name
162 print "OK"
163
164 # ---- update object browser
165 if salome.hasDesktop():
166     salome.sg.updateObjBrowser(1);
167
168 print
169
170 print "======================================================================"
171 print "           %d. Test Mesh " % step; step+=1
172 print "======================================================================"
173
174 import StdMeshers
175 import SMESH
176
177 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
178 if salome.hasDesktop():
179     smeshgui = salome.ImportComponentGUI("SMESH")
180     smeshgui.Init(salome.myStudyId);
181 else:
182     smesh.SetCurrentStudy(salome.myStudy)
183
184 # ---- create hypotheses 
185
186 if sys.platform == "win32":
187   stdMeshersEngine = "StdMeshersEngine"
188 else:
189   stdMeshersEngine = "libStdMeshersEngine.so"
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(1);
306
307 print
308
309 print "======================================================================"
310 print "           %d. Test Med " % step; step+=1
311 print "======================================================================"
312
313 import xmed
314 from xmed import properties
315 from xmed.fieldproxy import FieldProxy
316
317 xmed.setConsoleGlobals(globals())
318
319 # Load some test data in the MedDataManager
320 filepath  = properties.testFilePath
321 xmed.dataManager.addDatasource(filepath)
322 fieldHandlerList = xmed.dataManager.getFieldHandlerList()
323
324 fieldHandler0 = fieldHandlerList[0]
325 print "---Field Handler 0:\n%s" % fieldHandler0
326 fieldHandler1 = fieldHandlerList[1]
327 print "---Field Handler 1:\n%s" % fieldHandler1
328
329 print "--- The addition of two fields can be done using field handler directly."
330 addFieldHandler = xmed.calculator.add(fieldHandler0, fieldHandler1)
331 print "--- Result handler:\n%s" % addFieldHandler
332
333 print "--- Or with a field proxy that easy the writing of operations."
334 fieldProxy0 = FieldProxy(fieldHandler0)
335 fieldProxy1 = FieldProxy(fieldHandler1)
336
337 resHandler = fieldProxy0 + fieldProxy1
338 if resHandler is None:
339     print "Error: result handler is None!"
340 else:
341     print "--- Result handler:\n%s" % resHandler
342     print "OK"
343
344 print
345
346
347 print "======================================================================"
348 print "           %d. Test Paravis " % step; step+=1
349 print "======================================================================"
350
351 if salome.hasDesktop(): # in gui mode
352
353     print "**** Importing paravis... It can take some time."
354     from presentations import *
355     import paravis
356     import pvsimple
357     
358     my_paravis = paravis.myParavis
359     
360     #====================Stage1: Importing MED file====================
361     
362     print "**** Stage1: Importing MED file"
363     
364     print 'Import "ResOK_0000.med"...............',
365     medFileName = "ResOK_0000.med"
366     medFile = os.path.join(os.getenv('DATA_DIR'), 'MedFiles', medFileName)
367     my_paravis.ImportFile(medFile)
368     med_reader = pvsimple.GetActiveSource()
369     
370     if med_reader is None:
371         print "FAILED"
372     else:
373         print "OK"
374     
375     cell_entity = EntityType.CELL
376     node_entity = EntityType.NODE
377     
378     #====================Stage2: Displaying vector field===============
379     
380     print "**** Stage3: Displaying vector field"
381     
382     print 'Get view...................',
383     view = pvsimple.GetRenderView()
384     if view is None:
385         print "FAILED"
386     else:
387         reset_view(view)
388         print "OK"
389     
390     print "Creating Scalar Map.......",
391     scalarmap = ScalarMapOnField(med_reader, node_entity, 'vitesse', 1)
392     if scalarmap is None:
393         print "FAILED"
394     else:
395         bar = get_bar()
396         bar.Orientation = 'Horizontal'
397         bar.Position = [0.1, 0.1]
398         bar.Position2 = [0.1, 0.25]
399         bar.AspectRatio = 3
400         
401         display_only(scalarmap, view)
402         print "OK"
403     
404     view.ResetCamera()
405     
406     print "Creating Vectors..........",
407     vectors = VectorsOnField(med_reader, node_entity, 'vitesse', 1)
408     if vectors is None:
409         print "FAILED"
410     else:
411         display_only(vectors, view)
412         print "OK"
413     
414     print "Creating Iso Surfaces.....",
415     isosurfaces = IsoSurfacesOnField(med_reader, node_entity, 'vitesse', 1)
416     if isosurfaces is None:
417         print "FAILED"
418     else:
419         display_only(isosurfaces, view)
420         print "OK"
421     
422     print "Creating Cut Planes.......",
423     cutplanes = CutPlanesOnField(med_reader, node_entity, 'vitesse', 1,
424                                  nb_planes=30, orientation=Orientation.YZ)
425     if cutplanes is None:
426         print "FAILED"
427     else:
428         display_only(cutplanes, view)
429         print "OK"
430     
431     print "Creating Scalar Map On Deformed Shape.......",
432     scalarmapondefshape = DeformedShapeAndScalarMapOnField(med_reader,
433                                                            node_entity,
434                                                            'vitesse', 1,
435                                                            None,
436                                                            cell_entity,
437                                                            'pression')
438     if scalarmapondefshape is None:
439         print "FAILED"
440     else:
441         display_only(scalarmapondefshape, view)
442         print "OK"
443
444 else: # not in gui mode, Paravis can not be tested
445     
446     print
447     print "PARAVIS module requires SALOME to be running in GUI mode."
448     print
449     print "Skipping test for PARAVIS..."
450     pass
451
452 # ---- update object browser
453 if salome.hasDesktop():
454     salome.sg.updateObjBrowser(1);