Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[modules/kernel.git] / src / SALOME_SWIG / salome_test.py
1 #  SALOME SALOME_SWIG : binding of C++ implementation and Python
2 #
3 #  Copyright (C) 2003  CEA/DEN, EDF R&D
4 #
5 #
6 #
7 #  File   : salome_test.py
8 #  Module : SALOME
9
10 print "Test the application loading  GEOM, SMESH, VISU, MED, components and doing some"
11 print "operation within the components."
12
13 import salome
14 from salome import sg
15 import SALOMEDS
16 import os
17
18 import SALOME_ModuleCatalog
19
20 print "======================================================================"
21 print "           Get Catalog "
22 print "======================================================================"
23 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
24 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
25
26 print "======================================================================"
27 print "           Create Study "
28 print "======================================================================"
29
30 comp = catalog.GetComponent("GEOM")
31 if comp is None:
32         raise RuntimeError,"Component GEOM not found in Module Catalog."
33
34 import geompy
35
36 print "================================="
37 print "       create AttributeReal      "
38 print "================================="
39 A = geompy.myBuilder.FindOrCreateAttribute(geompy.father, "AttributeReal")
40 if A == None :
41         raise  RuntimeError, "Can't create AttributeReal attribute"
42 A = A._narrow(SALOMEDS.AttributeReal)
43 A.SetValue(0.0001)
44 if A.Value() != 0.0001:
45         raise  RuntimeError, "Error : wrong value of  AttributeReal"
46
47 print
48 print " ===========  Test Geometry  =========================="
49 print
50
51 print "==================================="
52 print "     define a box"
53 print "==================================="
54
55 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
56 idbox = geompy.addToStudy(box,"box")
57
58 print
59 print "=============  Test SMESH  ============================="
60 print
61
62 comp = catalog.GetComponent("SMESH")
63 if comp is None:
64         raise RuntimeError,"Component SMESH not found in Module Catalog."
65
66 comp = catalog.GetComponent("MED")
67 if comp is None:
68         raise RuntimeError,"Component MED not found in Module Catalog."
69
70 import SMESH
71 import smeshpy
72
73 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
74 myBuilder = salome.myStudy.NewBuilder()
75
76 smeshgui = salome.ImportComponentGUI("SMESH")
77 smeshgui.Init(salome.myStudyId);
78
79 ShapeTypeCompSolid = 1
80 ShapeTypeSolid = 2
81 ShapeTypeShell = 3
82 ShapeTypeFace = 4
83 ShapeTypeWire = 5
84 ShapeTypeEdge = 6
85 ShapeTypeVertex = 7
86
87 # ---- define a box
88
89 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
90 idbox = geompy.addToStudy(box,"box")
91
92 # ---- add first face of box in study
93
94 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
95 face=subShapeList[0]
96 name = geompy.SubShapeName( face._get_Name(), box._get_Name() )
97 print name
98 idface=geompy.addToStudyInFather(box,face,name)
99
100 # ---- add shell from box  in study
101
102 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
103 shell = subShellList[0]
104 name = geompy.SubShapeName( shell._get_Name(), box._get_Name() )
105 print name
106 idshell=geompy.addToStudyInFather(box,shell,name)
107
108 # ---- add first edge of face in study
109
110 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
111 edge=edgeList[0];
112 name = geompy.SubShapeName( edge._get_Name(), face._get_Name() )
113 print name
114 idedge=geompy.addToStudyInFather(face,edge,name)
115
116 # ---- launch SMESH, init a Mesh with the box
117 gen=smeshpy.smeshpy()
118 mesh=gen.Init(idbox)
119
120 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
121 smeshgui.SetName(idmesh, "Meshbox");
122 smeshgui.SetShape(idbox, idmesh);
123
124 # ---- create Hypothesis
125
126 print "-------------------------- create Hypothesis"
127 print "-------------------------- LocalLength"
128 hyp1=gen.CreateHypothesis("LocalLength")
129 hypLen1 = hyp1._narrow(SMESH.SMESH_LocalLength)
130 hypLen1.SetLength(100)
131 print hypLen1.GetName()
132 print hypLen1.GetId()
133 print hypLen1.GetLength()
134
135 idlength = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypLen1) );
136 smeshgui.SetName(idlength, "Local_Length_100");
137
138 print "-------------------------- NumberOfSegments"
139 hyp2=gen.CreateHypothesis("NumberOfSegments")
140 hypNbSeg1=hyp2._narrow(SMESH.SMESH_NumberOfSegments)
141 hypNbSeg1.SetNumberOfSegments(7)
142 print hypNbSeg1.GetName()
143 print hypNbSeg1.GetId()
144 print hypNbSeg1.GetNumberOfSegments()
145
146 idseg = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSeg1) );
147 smeshgui.SetName(idseg, "NumberOfSegments_7");
148
149 print "-------------------------- MaxElementArea"
150 hyp3=gen.CreateHypothesis("MaxElementArea")
151 hypArea1=hyp3._narrow(SMESH.SMESH_MaxElementArea)
152 hypArea1.SetMaxElementArea(2500)
153 print hypArea1.GetName()
154 print hypArea1.GetId()
155 print hypArea1.GetMaxElementArea()
156
157 idarea1 = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypArea1) );
158 smeshgui.SetName(idarea1, "MaxElementArea_2500");
159
160 print "-------------------------- MaxElementArea"
161 hyp3=gen.CreateHypothesis("MaxElementArea")
162 hypArea2=hyp3._narrow(SMESH.SMESH_MaxElementArea)
163 hypArea2.SetMaxElementArea(500)
164 print hypArea2.GetName()
165 print hypArea2.GetId()
166 print hypArea2.GetMaxElementArea()
167
168 idarea2 = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypArea2) );
169 smeshgui.SetName(idarea2, "MaxElementArea_500");
170
171 print "-------------------------- Regular_1D"
172 alg1=gen.CreateHypothesis("Regular_1D")
173 algo1=alg1._narrow(SMESH.SMESH_Algo)
174 listHyp=algo1.GetCompatibleHypothesis()
175 for hyp in listHyp:
176     print hyp
177 algoReg=alg1._narrow(SMESH.SMESH_Regular_1D)
178 print algoReg.GetName()
179 print algoReg.GetId()
180
181 idreg = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(algoReg) );
182 smeshgui.SetName(idreg, "Regular_1D");
183
184 print "-------------------------- MEFISTO_2D"
185 alg2=gen.CreateHypothesis("MEFISTO_2D")
186 algo2=alg2._narrow(SMESH.SMESH_Algo)
187 listHyp=algo2.GetCompatibleHypothesis()
188 for hyp in listHyp:
189     print hyp
190 algoMef=alg2._narrow(SMESH.SMESH_MEFISTO_2D)
191 print algoMef.GetName()
192 print algoMef.GetId()
193
194 idmef = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(algoMef) );
195 smeshgui.SetName(idmef, "MEFISTO_2D");
196
197 # ---- add hypothesis to edge
198
199 print "-------------------------- add hypothesis to edge"
200 edge=salome.IDToObject(idedge)
201 submesh=mesh.GetElementsOnShape(edge)
202 ret=mesh.AddHypothesis(edge,algoReg)
203 print ret
204 ret=mesh.AddHypothesis(edge,hypLen1)
205 print ret
206
207 idsm1 = smeshgui.AddSubMeshOnShape( idmesh,
208                                     idedge,
209                                     salome.orb.object_to_string(submesh),
210                                     ShapeTypeEdge )
211 smeshgui.SetName(idsm1, "SubMeshEdge")
212 smeshgui.SetAlgorithms( idsm1, idreg );
213 smeshgui.SetHypothesis( idsm1, idlength );
214
215 print "-------------------------- add hypothesis to face"
216 face=salome.IDToObject(idface)
217 submesh=mesh.GetElementsOnShape(face)
218 ret=mesh.AddHypothesis(face,hypArea2)
219 print ret
220
221 idsm2 = smeshgui.AddSubMeshOnShape( idmesh,
222                                     idface,
223                                     salome.orb.object_to_string(submesh),
224                                     ShapeTypeFace )
225 smeshgui.SetName(idsm2, "SubMeshFace")
226 smeshgui.SetHypothesis( idsm2, idarea2 );
227
228 # ---- add hypothesis to box
229
230 print "-------------------------- add hypothesis to box"
231 box=salome.IDToObject(idbox)
232 submesh=mesh.GetElementsOnShape(box)
233 ret=mesh.AddHypothesis(box,algoReg)
234 print ret
235 ret=mesh.AddHypothesis(box,hypNbSeg1)
236 print ret
237 ret=mesh.AddHypothesis(box,algoMef)
238 print ret
239 ret=mesh.AddHypothesis(box,hypArea1)
240 print ret
241
242 smeshgui.SetAlgorithms( idmesh, idreg );
243 smeshgui.SetHypothesis( idmesh, idseg );
244 smeshgui.SetAlgorithms( idmesh, idmef );
245 smeshgui.SetHypothesis( idmesh, idarea1 );
246
247 gen.Compute(mesh, idbox)
248 sg.updateObjBrowser(1);
249
250 print
251 print "=============  Test  Supervisor  ============================="
252 print
253
254 comp = catalog.GetComponent("SUPERV")
255 if comp is None:
256         raise RuntimeError,"Component SUPERV not found in Module Catalog."
257
258 from SuperV import *
259 import SALOMEDS
260 myStudy = salome.myStudy
261 myBuilder = myStudy.NewBuilder()
262
263 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
264 father = myStudy.FindComponent("SUPERV")
265 if father is None:
266         father = myBuilder.NewComponent("SUPERV")
267         A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
268         FName = A1._narrow(SALOMEDS.AttributeName)
269         FName.SetValue("Supervision")
270         A2 = myBuilder.FindOrCreateAttribute(father, "AttributePixMap");
271         aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
272         aPixmap.SetPixMap( "ICON_OBJBROWSER_Supervision" );
273         myBuilder.DefineComponentInstance(father,SuperVision)
274
275 def addStudy(ior):
276     dataflow = SuperVision.getGraph(ior)
277     name=dataflow.Name()
278     itr = myStudy.NewChildIterator(father)
279     while itr.More():
280         item=itr.Value()
281         res,A=item.FindAttribute("AttributeName")
282         if res:
283             aName = A._narrow(SALOMEDS.AttributeName)
284             if aName.Value() == name :
285                 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
286                 A  = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
287                 print "A = ", A
288                 if A is not None :
289                    #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
290                    anIOR  = A._narrow(SALOMEDS.AttributeIOR);
291                    print "anIOR.SetValue(dataflow.getIOR())"
292                    anIOR.SetValue(dataflow.getIOR()) 
293                 return
294         itr.Next()
295     obj = myBuilder.NewObject(father)
296     A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
297     aName=A._narrow(SALOMEDS.AttributeName)
298     aName.SetValue(name)
299     A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
300     anIOR  = A._narrow(SALOMEDS.AttributeIOR)
301     anIOR.SetValue(dataflow.getIOR())
302
303 import os
304 dir= os.getenv("SUPERV_ROOT_DIR")
305 if dir == None:
306         raise RuntimeError, "SUPERV_ROOT_DIR is not defined"
307 xmlfile = dir +"/examples/GraphEssai.xml"
308 print "Load dataflow from the file : "
309 print xmlfile
310 print
311
312 myGraph = Graph ( xmlfile )
313
314 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
315 print "myGraph.IsValid() = ", myGraph.IsValid()
316
317 # Get Nodes
318 myGraph.PrintNodes()
319 Add,Sub,Mul,Div = myGraph.Nodes()
320
321 # Load Datas
322 Addx = Add.Input("x",3.)
323 Addy = Add.Input("y",4.5)
324 Subx = Sub.Input("x",1.5)
325
326 # Get Output Port
327 Addz = Add.Port('z')
328 Subz = Sub.Port('z')
329 Mulz = Mul.Port('z')
330 Divz = Div.Port('z')
331
332 # This DataFlow is "executable" : all pending Ports are defined with Datas
333 print myGraph.IsExecutable()
334
335 # Starts only execution of that DataFlow and gets control immediatly
336 print myGraph.Run()
337
338 # That DataFlow is running ==> 0 (false)
339 print myGraph.IsDone()
340
341 # Events of execution :
342 aStatus,aNode,anEvent,aState = myGraph.Event()
343 while aStatus :
344     print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
345     aStatus,aNode,anEvent,aState = myGraph.Event()
346 print "myGraph.IsDone() = ",myGraph.IsDone()
347
348 # Wait for Completion (but it is already done after event loop ...)
349 print "Done : ",myGraph.DoneW()
350
351 # Get result
352 print "Result : ",Divz.ToString()
353
354 # Intermediate results :
355 print "Intermediate Result Add\z : ",Addz.ToString()
356 print "Intermediate Result Sub\z : ",Subz.ToString()
357 print "Intermediate Result Mul\z : ",Mulz.ToString()
358
359 print " "
360 #print "Type : print myGraph.IsDone()"
361 #print "       If execution is finished ==> 1 (true)"
362 res=myGraph.IsDone()
363 if res != 1:
364         raise RuntimeError, "myGraph.Run() is not done"
365
366 print " "
367 print "Type : print Divz.ToString()"
368 print "       You will get the result"
369 Divz.ToString()
370
371 print " "
372 print "Type : myGraph.PrintPorts()"
373 print "       to see input and output values of the graph"
374 myGraph.PrintPorts()
375
376 print " "
377 print "Type : Add.PrintPorts()"
378 Add.PrintPorts()
379
380 print "Type : Sub.PrintPorts()"
381 Sub.PrintPorts()
382
383 print "Type : Mul.PrintPorts()"
384 Mul.PrintPorts()
385
386 print "Type : Div.PrintPorts()"
387 print "       to see input and output values of nodes"
388 Div.PrintPorts()
389
390 # Export will create newsupervisionexample.xml and the corresponding .py file
391 tmpdir=os.getenv("TmpDir")
392 if tmpdir is None:
393         tmpdir="/tmp"
394 file = tmpdir + "/newsupervisionexample"
395 print "--------------\n"+file+"\n--------------\n"
396 myGraph.Export(file)
397
398 ior = salome.orb.object_to_string(myGraph.G)
399 addStudy(ior)
400
401 GraphName = myGraph.Name()
402 print "Befor save ",
403 #nodes = myGraph.Nodes()
404 nodes = myGraph.G.Nodes().FNodes
405 length_bs = len(nodes)
406 print "ListOfNodes length = ", length_bs
407 names=[]
408 for node in nodes:
409         names.append(node.Name())
410 print names
411
412 print "Load FactorialComponent component, create dataflow using its services and run execution"
413 myPy = Graph('myPy')
414
415 eval = myPy.Node('FactorialComponent','FactorialComponent','eval')
416 eval.SetContainer('FactoryServerPy')
417
418 myPy.IsValid()
419
420 myPy.PrintPorts()
421
422 myPy.Run( 3 )
423
424 myPy.DoneW()
425
426 myPy.State()
427
428 myPy.PrintPorts()
429
430
431 sg.updateObjBrowser(1);
432
433 print
434 print "=============  Test  VISU  and MED ============================="
435 print
436
437 comp = catalog.GetComponent("VISU")
438 if comp is None:
439         raise RuntimeError,"Component VISU not found in Module Catalog."
440
441 import sys
442 import SALOMEDS
443 import SALOME
444 import SALOME_MED
445 import VISU
446
447 import visu_gui
448
449 medFile = "pointe.med"
450 medFile = os.getenv('KERNEL_ROOT_DIR') + '/examples/' + medFile
451 print "Load ", medFile
452
453 studyCurrent = salome.myStudyName
454
455 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
456 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
457
458 try:
459     if os.access(medFile, os.R_OK) :
460        if os.access(medFile, os.W_OK) :
461            med_comp.readStructFileWithFieldType(medFile,studyCurrent)
462            med_obj = visu_gui.visu.getMedObjectFromStudy()
463            print "med_obj - ", med_obj
464
465            myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
466            aMeshName = "maa1"
467            anEntity = VISU.NODE
468            aTimeStampId = -1
469                    
470            myResult1 = myVisu.ImportMedField(myField1)
471            aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
472            
473            aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
474            
475            myResult2 = myVisu.ImportFile(medFile);
476            aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
477            
478            aTimeStampId = 3
479            aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
480                    
481            sg.updateObjBrowser(0)
482        else :  print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
483     else :  print  "We have no permission to read medFile, it will not be opened"; 
484
485 except:
486     if sys.exc_type == SALOME.SALOME_Exception :
487         print "There is no permission to read " + medFile
488     else :
489         print sys.exc_type 
490         print sys.exc_value
491         print sys.exc_traceback
492
493 sg.updateObjBrowser(1);