Salome HOME
Typo-fix by Kunda
[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, 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 access 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", "MEDFactory", "PVSERVER"]:
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(True);
167
168 print
169
170 print "======================================================================"
171 print "           %d. Test Mesh " % step; step+=1
172 print "======================================================================"
173
174 from salome.StdMeshers import StdMeshersBuilder
175 import SMESH
176 from salome.smesh import smeshBuilder
177
178 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
179 if salome.hasDesktop():
180     smeshgui = salome.ImportComponentGUI("SMESH")
181     smeshgui.Init(salome.myStudyId);
182 else:
183     smesh = smeshBuilder.New(salome.myStudy)
184
185 # ---- create hypotheses 
186
187 stdMeshersEngine = "StdMeshersEngine"
188
189 print
190 print "--- Create hypotheses ..."
191
192 print
193 print "------ LocalLength ..."
194 hypLen1 = smesh.CreateHypothesis( "LocalLength", stdMeshersEngine )
195 hypLen1.SetLength(100)
196 print hypLen1.GetName()
197 print hypLen1.GetId()
198 print hypLen1.GetLength()
199 if salome.hasDesktop():
200     smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
201 print "OK"
202
203 print
204 print "------ NumberOfSegments ..."
205 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", stdMeshersEngine )
206 hypNbSeg1.SetNumberOfSegments(7)
207 print hypNbSeg1.GetName()
208 print hypNbSeg1.GetId()
209 print hypNbSeg1.GetNumberOfSegments()
210 if salome.hasDesktop():
211     smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
212 print "OK"
213
214 print
215 print "------ MaxElementArea [1] ..."
216 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
217 hypArea1.SetMaxElementArea(2500)
218 print hypArea1.GetName()
219 print hypArea1.GetId()
220 print hypArea1.GetMaxElementArea()
221 if salome.hasDesktop():
222     smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
223 print "OK"
224
225 print
226 print "------ MaxElementArea [2] ..."
227 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
228 hypArea2.SetMaxElementArea(500)
229 print hypArea2.GetName()
230 print hypArea2.GetId()
231 print hypArea2.GetMaxElementArea()
232 if salome.hasDesktop():
233     smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
234 print "OK"
235
236 # ---- create algorithms
237
238 print
239 print "--- Create algorithms ..."
240
241 print
242 print "------ Regular_1D ..."
243 algoReg = smesh.CreateHypothesis( "Regular_1D", stdMeshersEngine )
244 listHyp = algoReg.GetCompatibleHypothesis()
245 for hyp in listHyp:
246     print hyp
247 print algoReg.GetName()
248 print algoReg.GetId()
249 if salome.hasDesktop():
250     smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
251 print "OK"
252
253 print
254 print "------ MEFISTO_2D ..."
255 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", stdMeshersEngine )
256 listHyp=algoMef.GetCompatibleHypothesis()
257 for hyp in listHyp:
258     print hyp
259 print algoMef.GetName()
260 print algoMef.GetId()
261 if salome.hasDesktop():
262     smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
263 print "OK"
264
265 # ---- create mesh on the box, apply hypotheses / algorithms
266
267 print
268 print "--- Create mesh on the box ..."
269 mesh = smesh.CreateMesh(box)
270 if salome.hasDesktop():
271     smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
272 ret = mesh.AddHypothesis(box, algoReg)
273 ret = mesh.AddHypothesis(box, algoMef)
274 ret = mesh.AddHypothesis(box, hypNbSeg1)
275 ret = mesh.AddHypothesis(box, hypArea1)
276 print "OK"
277
278 # ---- create submesh on the edge, add hypothesis
279
280 print
281 print "--- Add 1D sub-mesh on the edge ..."
282 submesh = mesh.GetSubMesh(edge, "SubMeshEdge")
283 ret = mesh.AddHypothesis(edge, algoReg)
284 ret = mesh.AddHypothesis(edge, hypLen1)
285 print "OK"
286
287 # ---- create submesh on the edge, add hypothesis
288
289 print
290 print "--- Add 2D sub-mesh on the face ..."
291 submesh = mesh.GetSubMesh(face, "SubMeshFace")
292 ret = mesh.AddHypothesis(face, hypArea2)
293 print "OK"
294
295 # ---- compute mesh
296 print
297 print "--- Compute mesh ..."
298 smesh.Compute(mesh, box)
299 print "OK"
300
301 # ---- update object browser
302 if salome.hasDesktop():
303     salome.sg.updateObjBrowser(True);
304
305 print
306
307 print "======================================================================"
308 print "           %d. Test Med " % step; step+=1
309 print "======================================================================"
310
311 import medcalc
312 #from medcalc import properties
313 from medcalc.fieldproxy import FieldProxy
314
315 medcalc.medconsole.setConsoleGlobals(globals())
316 try:
317     med_root=os.environ["MED_ROOT_DIR"]
318 except KeyError, e:
319     raise RuntimeError("MED_ROOT_DIR should be defined to load the test data")
320
321 filepath = os.path.join(med_root,"share","salome","resources","med","medcalc_testfiles","smallmesh_varfield.med")
322 medcalc.medio.LoadDataSource(filepath)
323 fieldHandlerList = medcalc.medevents.dataManager.getFieldHandlerList()
324
325 fieldHandler0 = fieldHandlerList[0]
326 print "---Field Handler 0:\n%s" % fieldHandler0
327 fieldHandler1 = fieldHandlerList[1]
328 print "---Field Handler 1:\n%s" % fieldHandler1
329
330 print "--- The addition of two fields can be done using field handler directly."
331 addFieldHandler = medcalc.fieldproxy.calculator.add(fieldHandler0,fieldHandler1)
332 print "--- Result handler:\n%s" % addFieldHandler
333
334 print "--- Or with a field proxy that easy the writing of operations."
335 fieldProxy0 = FieldProxy(fieldHandler0)
336 fieldProxy1 = FieldProxy(fieldHandler1)
337
338 resHandler = fieldProxy0 + fieldProxy1
339 if resHandler is None:
340     print "Error: result handler is None!"
341 else:
342     print "--- Result handler:\n%s" % resHandler
343     print "OK"
344
345 print
346
347
348 print "======================================================================"
349 print "           %d. Test Paravis " % step; step+=1
350 print "======================================================================"
351
352 if salome.hasDesktop(): # in gui mode
353
354     print "**** Importing pvserver... It can take some time."
355     import pvserver
356     import pvsimple
357     
358     #====================Stage1: Importing MED file====================
359     
360     print "**** Stage1: Importing MED file"
361     
362     print 'Import "ResOK_0000.med"...............',
363     medFileName = "ResOK_0000.med"
364     medFile = os.path.join(os.getenv('DATA_DIR'), 'MedFiles', medFileName)
365     pvsimple.MEDReader( FileName=medFile )
366     med_reader = pvsimple.GetActiveSource()
367     
368     if med_reader is None:
369         print "FAILED"
370     else:
371         print "OK"
372     
373     #====================Stage2: Displaying presentation===============
374     
375     print "**** Stage2: Displaying presentation"
376     
377     print 'Get view...................',
378     view = pvsimple.GetRenderView()
379     if view is None:
380         print "FAILED"
381     else:
382         print "OK"
383     
384     print "Creating presentation.......",
385     prs = pvsimple.GetRepresentation(med_reader)
386     if prs is None:
387         print "FAILED"
388     else:
389         rep_list = view.Representations
390         for rep in rep_list:
391             if hasattr(rep, 'Visibility'):
392                 rep.Visibility = (rep == prs)
393         pvsimple.Render(view=view) 
394         
395         # ---- surface representation
396         prs.SetRepresentationType('Surface')
397         view.ResetCamera()
398
399         print "OK"
400     
401 else: # not in gui mode, Paravis can not be tested
402     
403     print
404     print "PARAVIS module requires SALOME to be running in GUI mode."
405     print
406     print "Skipping test for PARAVIS..."
407     pass
408
409 # ---- update object browser
410 if salome.hasDesktop():
411     salome.sg.updateObjBrowser(True);