]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/salome_test.py
Salome HOME
Merge from V7_2_BR 09/08/2013
[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 import geompy
115
116 ShapeTypeCompSolid = 1
117 ShapeTypeSolid = 2
118 ShapeTypeShell = 3
119 ShapeTypeFace = 4
120 ShapeTypeWire = 5
121 ShapeTypeEdge = 6
122 ShapeTypeVertex = 7
123
124 print
125 print "--- Create a box ..."
126 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
127 idbox = geompy.addToStudy(box, "box")
128 box_obj = salome.myStudy.FindObjectByPath("/Geometry/box")
129 if not box_obj:
130     raise RuntimeError, "Error : wrong value of  AttributeReal"
131 print "OK"
132
133 # ---- add shell from box in study
134 print 
135 print "--- Extract shell ..."
136 subShellList = geompy.SubShapeAll(box, ShapeTypeShell)
137 shell = subShellList[0]
138 name = geompy.SubShapeName(shell, box)
139 idshell = geompy.addToStudyInFather(box, shell, name)
140 print name
141 print "OK"
142
143 # ---- add first face of box in study
144 print 
145 print "--- Extract face ..."
146 subShapeList = geompy.SubShapeAll(box, ShapeTypeFace)
147 face = subShapeList[0]
148 name = geompy.SubShapeName(face, box)
149 idface = geompy.addToStudyInFather(box, face, name)
150 print name
151 print "OK"
152
153 # ---- add first edge of face in study
154 print 
155 print "--- Extract edge ..."
156 edgeList = geompy.SubShapeAll(face, ShapeTypeEdge)
157 edge = edgeList[0];
158 name = geompy.SubShapeName(edge, face)
159 idedge = geompy.addToStudyInFather(face, edge, name)
160 print name
161 print "OK"
162
163 # ---- update object browser
164 if salome.hasDesktop():
165     salome.sg.updateObjBrowser(1);
166
167 print
168
169 print "======================================================================"
170 print "           %d. Test Mesh " % step; step+=1
171 print "======================================================================"
172
173 import StdMeshers
174 import SMESH
175
176 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
177 if salome.hasDesktop():
178     smeshgui = salome.ImportComponentGUI("SMESH")
179     smeshgui.Init(salome.myStudyId);
180 else:
181     smesh.SetCurrentStudy(salome.myStudy)
182
183 # ---- create hypotheses 
184
185 if sys.platform == "win32":
186   stdMeshersEngine = "StdMeshersEngine"
187 else:
188   stdMeshersEngine = "libStdMeshersEngine.so"
189
190 print
191 print "--- Create hypotheses ..."
192
193 print
194 print "------ LocalLength ..."
195 hypLen1 = smesh.CreateHypothesis( "LocalLength", stdMeshersEngine )
196 hypLen1.SetLength(100)
197 print hypLen1.GetName()
198 print hypLen1.GetId()
199 print hypLen1.GetLength()
200 if salome.hasDesktop():
201     smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
202 print "OK"
203
204 print
205 print "------ NumberOfSegments ..."
206 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", stdMeshersEngine )
207 hypNbSeg1.SetNumberOfSegments(7)
208 print hypNbSeg1.GetName()
209 print hypNbSeg1.GetId()
210 print hypNbSeg1.GetNumberOfSegments()
211 if salome.hasDesktop():
212     smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
213 print "OK"
214
215 print
216 print "------ MaxElementArea [1] ..."
217 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
218 hypArea1.SetMaxElementArea(2500)
219 print hypArea1.GetName()
220 print hypArea1.GetId()
221 print hypArea1.GetMaxElementArea()
222 if salome.hasDesktop():
223     smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
224 print "OK"
225
226 print
227 print "------ MaxElementArea [2] ..."
228 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
229 hypArea2.SetMaxElementArea(500)
230 print hypArea2.GetName()
231 print hypArea2.GetId()
232 print hypArea2.GetMaxElementArea()
233 if salome.hasDesktop():
234     smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
235 print "OK"
236
237 # ---- create algorithms
238
239 print
240 print "--- Create algorithms ..."
241
242 print
243 print "------ Regular_1D ..."
244 algoReg = smesh.CreateHypothesis( "Regular_1D", stdMeshersEngine )
245 listHyp = algoReg.GetCompatibleHypothesis()
246 for hyp in listHyp:
247     print hyp
248 print algoReg.GetName()
249 print algoReg.GetId()
250 if salome.hasDesktop():
251     smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
252 print "OK"
253
254 print
255 print "------ MEFISTO_2D ..."
256 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", stdMeshersEngine )
257 listHyp=algoMef.GetCompatibleHypothesis()
258 for hyp in listHyp:
259     print hyp
260 print algoMef.GetName()
261 print algoMef.GetId()
262 if salome.hasDesktop():
263     smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
264 print "OK"
265
266 # ---- create mesh on the box, apply hypotheses / algorithms
267
268 print
269 print "--- Create mesh on the box ..."
270 mesh = smesh.CreateMesh(box)
271 if salome.hasDesktop():
272     smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
273 ret = mesh.AddHypothesis(box, algoReg)
274 ret = mesh.AddHypothesis(box, algoMef)
275 ret = mesh.AddHypothesis(box, hypNbSeg1)
276 ret = mesh.AddHypothesis(box, hypArea1)
277 print "OK"
278
279 # ---- create submesh on the edge, add hypothesis
280
281 print
282 print "--- Add 1D sub-mesh on the edge ..."
283 submesh = mesh.GetSubMesh(edge, "SubMeshEdge")
284 ret = mesh.AddHypothesis(edge, algoReg)
285 ret = mesh.AddHypothesis(edge, hypLen1)
286 print "OK"
287
288 # ---- create submesh on the edge, add hypothesis
289
290 print
291 print "--- Add 2D sub-mesh on the face ..."
292 submesh = mesh.GetSubMesh(face, "SubMeshFace")
293 ret = mesh.AddHypothesis(face, hypArea2)
294 print "OK"
295
296 # ---- compute mesh
297 print
298 print "--- Compute mesh ..."
299 smesh.Compute(mesh, box)
300 print "OK"
301
302 # ---- update object browser
303 if salome.hasDesktop():
304     salome.sg.updateObjBrowser(1);
305
306 print
307
308 print "======================================================================"
309 print "           %d. Test Med " % step; step+=1
310 print "======================================================================"
311
312 import xmed
313 from xmed import properties
314 from xmed.fieldproxy import FieldProxy
315
316 xmed.setConsoleGlobals(globals())
317
318 # Load some test data in the MedDataManager
319 filepath  = properties.testFilePath
320 xmed.dataManager.addDatasource(filepath)
321 fieldHandlerList = xmed.dataManager.getFieldHandlerList()
322
323 fieldHandler0 = fieldHandlerList[0]
324 print "---Field Handler 0:\n%s" % fieldHandler0
325 fieldHandler1 = fieldHandlerList[1]
326 print "---Field Handler 1:\n%s" % fieldHandler1
327
328 print "--- The addition of two fields can be done using field handler directly."
329 addFieldHandler = xmed.calculator.add(fieldHandler0, fieldHandler1)
330 print "--- Result handler:\n%s" % addFieldHandler
331
332 print "--- Or with a field proxy that easy the writing of operations."
333 fieldProxy0 = FieldProxy(fieldHandler0)
334 fieldProxy1 = FieldProxy(fieldHandler1)
335
336 resHandler = fieldProxy0 + fieldProxy1
337 if resHandler is None:
338     print "Error: result handler is None!"
339 else:
340     print "--- Result handler:\n%s" % resHandler
341     print "OK"
342
343 print
344
345
346 print "======================================================================"
347 print "           %d. Test Paravis " % step; step+=1
348 print "======================================================================"
349
350 if salome.hasDesktop(): # in gui mode
351
352     print "**** Importing paravis... It can take some time."
353     from presentations import *
354     import paravis
355     import pvsimple
356     
357     my_paravis = paravis.myParavis
358     
359     #====================Stage1: Importing MED file====================
360     
361     print "**** Stage1: Importing MED file"
362     
363     print 'Import "ResOK_0000.med"...............',
364     medFileName = "ResOK_0000.med"
365     medFile = os.path.join(os.getenv('DATA_DIR'), 'MedFiles', medFileName)
366     my_paravis.ImportFile(medFile)
367     med_reader = pvsimple.GetActiveSource()
368     
369     if med_reader is None:
370         print "FAILED"
371     else:
372         print "OK"
373     
374     cell_entity = EntityType.CELL
375     node_entity = EntityType.NODE
376     
377     #====================Stage2: Displaying vector field===============
378     
379     print "**** Stage3: Displaying vector field"
380     
381     print 'Get view...................',
382     view = pvsimple.GetRenderView()
383     if view is None:
384         print "FAILED"
385     else:
386         reset_view(view)
387         print "OK"
388     
389     print "Creating Scalar Map.......",
390     scalarmap = ScalarMapOnField(med_reader, node_entity, 'vitesse', 1)
391     if scalarmap is None:
392         print "FAILED"
393     else:
394         bar = get_bar()
395         bar.Orientation = 'Horizontal'
396         bar.Position = [0.1, 0.1]
397         bar.Position2 = [0.1, 0.25]
398         bar.AspectRatio = 3
399         
400         display_only(scalarmap, view)
401         print "OK"
402     
403     view.ResetCamera()
404     
405     print "Creating Vectors..........",
406     vectors = VectorsOnField(med_reader, node_entity, 'vitesse', 1)
407     if vectors is None:
408         print "FAILED"
409     else:
410         display_only(vectors, view)
411         print "OK"
412     
413     print "Creating Iso Surfaces.....",
414     isosurfaces = IsoSurfacesOnField(med_reader, node_entity, 'vitesse', 1)
415     if isosurfaces is None:
416         print "FAILED"
417     else:
418         display_only(isosurfaces, view)
419         print "OK"
420     
421     print "Creating Cut Planes.......",
422     cutplanes = CutPlanesOnField(med_reader, node_entity, 'vitesse', 1,
423                                  nb_planes=30, orientation=Orientation.YZ)
424     if cutplanes is None:
425         print "FAILED"
426     else:
427         display_only(cutplanes, view)
428         print "OK"
429     
430     print "Creating Scalar Map On Deformed Shape.......",
431     scalarmapondefshape = DeformedShapeAndScalarMapOnField(med_reader,
432                                                            node_entity,
433                                                            'vitesse', 1,
434                                                            None,
435                                                            cell_entity,
436                                                            'pression')
437     if scalarmapondefshape is None:
438         print "FAILED"
439     else:
440         display_only(scalarmapondefshape, view)
441         print "OK"
442
443 else: # not in gui mode, Paravis can not be tested
444     
445     print
446     print "PARAVIS module requires SALOME to be running in GUI mode."
447     print
448     print "Skipping test for PARAVIS..."
449     pass
450
451 # ---- update object browser
452 if salome.hasDesktop():
453     salome.sg.updateObjBrowser(1);