Salome HOME
This commit was generated by cvs2git to create tag 'V1_4_0b2'.
[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( salome.sg.getComponentUserName("SUPERV") )
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/GraphGeomEssai.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
320 # This DataFlow is "executable" : all pending Ports are defined with Datas
321 print myGraph.IsExecutable()
322
323 # Starts only execution of that DataFlow and gets control immediatly
324 print myGraph.Run()
325
326 # That DataFlow is running ==> 0 (false)
327 print myGraph.IsDone()
328
329 # Events of execution :
330 aStatus,aNode,anEvent,aState = myGraph.Event()
331 while aStatus :
332     print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
333     aStatus,aNode,anEvent,aState = myGraph.Event()
334 print "myGraph.IsDone() = ",myGraph.IsDone()
335
336 # Wait for Completion (but it is already done after event loop ...)
337 print "Done : ",myGraph.DoneW()
338
339 print " "
340 #print "Type : print myGraph.IsDone()"
341 #print "       If execution is finished ==> 1 (true)"
342 res=myGraph.IsDone()
343 if res != 1:
344         raise RuntimeError, "myGraph.Run() is not done"
345
346 print " "
347 print "Type : myGraph.PrintPorts()"
348 print "       to see input and output values of the graph"
349 myGraph.PrintPorts()
350
351 # Export will create newsupervisionexample.xml and the corresponding .py file
352 tmpdir=os.getenv("TmpDir")
353 if tmpdir is None:
354         tmpdir="/tmp"
355 file = tmpdir + "/newsupervisionexample"
356 print "--------------\n"+file+"\n--------------\n"
357 myGraph.Export(file)
358
359 ior = salome.orb.object_to_string(myGraph.G)
360 addStudy(ior)
361
362 GraphName = myGraph.Name()
363 print "Befor save ",
364 #nodes = myGraph.Nodes()
365 nodes = myGraph.G.Nodes().FNodes
366 length_bs = len(nodes)
367 print "ListOfNodes length = ", length_bs
368 names=[]
369 for node in nodes:
370         names.append(node.Name())
371 print names
372
373 # Graph creation 
374 GraphInLines = Graph( 'GraphInLines' )
375 GraphInLines.SetName( 'GraphInLines' )
376 GraphInLines.SetAuthor( '' )
377 GraphInLines.SetComment( '' )
378 GraphInLines.Coords( 0 , 0 )
379
380 # Creation of InLine Nodes
381 PyAdd = []
382 PyAdd.append( 'def Add(a,b) :  ' )
383 PyAdd.append( '    return a+b  ' )
384 PyAdd.append( '' )
385 Add = GraphInLines.INode( 'Add' , PyAdd )
386 Add.InPort( 'a' , 'long' )
387 Add.InPort( 'b' , 'long' )
388 Add.OutPort( 'f' , 'long' )
389 Add.SetName( 'Add' )
390 Add.SetAuthor( '' )
391 Add.SetComment( 'Python function' )
392 Add.Coords( 351 , 77 )
393 PySub = []
394 PySub.append( 'def Sub(a,b) : ' )
395 PySub.append( '    return a-b ' )
396 PySub.append( '' )
397 Sub = GraphInLines.INode( 'Sub' , PySub )
398 Sub.InPort( 'a' , 'long' )
399 Sub.InPort( 'b' , 'long' )
400 Sub.OutPort( 'f' , 'long' )
401 Sub.SetName( 'Sub' )
402 Sub.SetAuthor( '' )
403 Sub.SetComment( 'Python function' )
404 Sub.Coords( 86 , 333 )
405 PyMul = []
406 PyMul.append( 'def Mul(a,b) : ' )
407 PyMul.append( '    return a*b ' )
408 Mul = GraphInLines.INode( 'Mul' , PyMul )
409 Mul.InPort( 'a' , 'long' )
410 Mul.InPort( 'b' , 'long' )
411 Mul.OutPort( 'Result' , 'long' )
412 Mul.SetName( 'Mul' )
413 Mul.SetAuthor( '' )
414 Mul.SetComment( 'Python function' )
415 Mul.Coords( 616 , 247 )
416
417 # Creation of intermediate Output variables and of Control Links
418 Addf = Add.Port( 'f' )
419 Mula = GraphInLines.Link( Addf , Mul.Port( 'a' ) )
420 Mula.AddCoord( 1 , 570 , 356 )
421 Mula.AddCoord( 2 , 570 , 186 )
422 Subf = Sub.Port( 'f' )
423 Mulb = GraphInLines.Link( Subf , Mul.Port( 'b' ) )
424 Mulb.AddCoord( 1 , 282 , 376 )
425 Mulb.AddCoord( 2 , 282 , 442 )
426 Addb = GraphInLines.Link( Subf , Add.Port( 'b' ) )
427 Addb.AddCoord( 1 , 283 , 209 )
428 Addb.AddCoord( 2 , 283 , 374 )
429 Addb.AddCoord( 3 , 283 , 442 )
430
431 # Creation of Input datas
432 Adda = Add.Input( 'a' , 1)
433 Suba = Sub.Input( 'a' , 3)
434 Subb = Sub.Input( 'b' , 4)
435
436 # Creation of Output variables
437 MulResult = Mul.Port( 'Result' )
438
439 GraphInLines.Run()
440
441 GraphInLines.DoneW()
442
443 GraphInLines.PrintPorts()
444
445 sg.updateObjBrowser(1);
446
447 print
448 print "=============  Test  VISU  and MED ============================="
449 print
450
451 comp = catalog.GetComponent("VISU")
452 if comp is None:
453         raise RuntimeError,"Component VISU not found in Module Catalog."
454
455 import sys
456 import SALOMEDS
457 import SALOME
458 import SALOME_MED
459 import VISU
460
461 import visu_gui
462
463 medFile = "pointe.med"
464 medFile = os.getenv('KERNEL_ROOT_DIR') + '/examples/' + medFile
465 print "Load ", medFile
466
467 studyCurrent = salome.myStudyName
468
469 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
470 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
471
472 try:
473     if os.access(medFile, os.R_OK) :
474        if not os.access(medFile, os.W_OK) :
475                import random
476                medFileNew = "/tmp/" + str(random.randint(0,1000000)) + "_" + medfile
477                print " -- Copy " + medFile + " to " + medFileNew
478                os.system("cp "+ medFile + " " + medFileNew)
479                medFile = medFileNew
480
481        if os.access(medFile, os.W_OK) :
482            med_comp.readStructFileWithFieldType(medFile,studyCurrent)
483            med_obj = visu_gui.visu.getMedObjectFromStudy()
484            print "med_obj - ", med_obj
485
486            myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
487            aMeshName = "maa1"
488            anEntity = VISU.NODE
489            aTimeStampId = -1
490                    
491            myResult1 = myVisu.ImportMedField(myField1)
492            aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
493            
494            aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
495            
496            myResult2 = myVisu.ImportFile(medFile);
497            aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
498            
499            aTimeStampId = 3
500            aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
501                    
502            sg.updateObjBrowser(0)
503        else :  print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
504     else :  print  "We have no permission to read medFile, it will not be opened"; 
505
506 except:
507     if sys.exc_type == SALOME.SALOME_Exception :
508         print "There is no permission to read " + medFile
509     else :
510         print sys.exc_type 
511         print sys.exc_value
512         print sys.exc_traceback
513
514 sg.updateObjBrowser(1);