Salome HOME
c1a6d562b45386015c55d60103593a6d7f0c140c
[modules/gui.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 import StdMeshers
63
64 comp = catalog.GetComponent("SMESH")
65 if comp is None:
66         raise RuntimeError,"Component SMESH not found in Module Catalog."
67
68 comp = catalog.GetComponent("MED")
69 if comp is None:
70         raise RuntimeError,"Component MED not found in Module Catalog."
71
72 import SMESH
73
74 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
75 myBuilder = salome.myStudy.NewBuilder()
76
77 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
78 smeshgui = salome.ImportComponentGUI("SMESH")
79 smeshgui.Init(salome.myStudyId);
80
81 ShapeTypeCompSolid = 1
82 ShapeTypeSolid = 2
83 ShapeTypeShell = 3
84 ShapeTypeFace = 4
85 ShapeTypeWire = 5
86 ShapeTypeEdge = 6
87 ShapeTypeVertex = 7
88
89 # ---- define a box
90
91 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
92 idbox = geompy.addToStudy(box,"box")
93
94 # ---- add first face of box in study
95
96 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
97 face=subShapeList[0]
98 name = geompy.SubShapeName(face, box)
99 print name
100 idface=geompy.addToStudyInFather(box,face,name)
101
102 # ---- add shell from box  in study
103
104 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
105 shell = subShellList[0]
106 name = geompy.SubShapeName(shell, box)
107 print name
108 idshell=geompy.addToStudyInFather(box,shell,name)
109
110 # ---- add first edge of face in study
111
112 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
113 edge=edgeList[0];
114 name = geompy.SubShapeName(edge, face)
115 print name
116 idedge=geompy.addToStudyInFather(face,edge,name)
117
118
119 # ---- SMESH 
120
121 # ---- create Hypothesis
122
123 print "-------------------------- create Hypothesis"
124 print "-------------------------- LocalLength"
125 hypLen1 = smesh.CreateHypothesis( "LocalLength", "libStdMeshersEngine.so" )
126 hypLen1.SetLength(100)
127 print hypLen1.GetName()
128 print hypLen1.GetId()
129 print hypLen1.GetLength()
130
131 smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
132
133 print "-------------------------- NumberOfSegments"
134 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", "libStdMeshersEngine.so" )
135 hypNbSeg1.SetNumberOfSegments(7)
136 print hypNbSeg1.GetName()
137 print hypNbSeg1.GetId()
138 print hypNbSeg1.GetNumberOfSegments()
139
140 smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
141
142 print "-------------------------- MaxElementArea"
143 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", "libStdMeshersEngine.so" )
144 hypArea1.SetMaxElementArea(2500)
145 print hypArea1.GetName()
146 print hypArea1.GetId()
147 print hypArea1.GetMaxElementArea()
148
149 smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
150
151 print "-------------------------- MaxElementArea"
152 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", "libStdMeshersEngine.so" )
153 hypArea2.SetMaxElementArea(500)
154 print hypArea2.GetName()
155 print hypArea2.GetId()
156 print hypArea2.GetMaxElementArea()
157
158 smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
159
160 print "-------------------------- Regular_1D"
161 algoReg = smesh.CreateHypothesis( "Regular_1D", "libStdMeshersEngine.so" )
162 listHyp=algoReg.GetCompatibleHypothesis()
163 for hyp in listHyp:
164     print hyp
165 print algoReg.GetName()
166 print algoReg.GetId()
167
168 smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
169
170 print "-------------------------- MEFISTO_2D"
171 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", "libStdMeshersEngine.so" )
172 listHyp=algoMef.GetCompatibleHypothesis()
173 for hyp in listHyp:
174     print hyp
175 print algoMef.GetName()
176 print algoMef.GetId()
177
178 smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
179
180 # ---- add hypothesis to box
181
182 print "-------------------------- add hypothesis to box"
183 box=salome.IDToObject(idbox)
184 mesh = smesh.CreateMesh(box)
185
186 smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
187
188 ret=mesh.AddHypothesis(box,algoReg)
189 print ret
190 ret=mesh.AddHypothesis(box,algoMef)
191 print ret
192
193
194 ret=mesh.AddHypothesis(box,hypNbSeg1)
195 print ret
196 ret=mesh.AddHypothesis(box,hypArea1)
197 print ret
198
199
200 # ---- add hypothesis to edge
201
202 print "-------------------------- add hypothesis to edge"
203 edge=salome.IDToObject(idedge)
204 submesh=mesh.GetSubMesh(edge, "SubMeshEdge")
205
206 ret=mesh.AddHypothesis(edge,algoReg)
207 print ret
208 ret=mesh.AddHypothesis(edge,hypLen1)
209 print ret
210
211 print "-------------------------- add hypothesis to face"
212 face=salome.IDToObject(idface)
213 submesh   = mesh.GetSubMesh(face, "SubMeshFace")
214
215 ret=mesh.AddHypothesis(face,hypArea2)
216 print ret
217
218 smesh.Compute(mesh, box)
219 sg.updateObjBrowser(1);
220
221 print
222 print "=============  Test  Supervisor  ============================="
223 print
224
225 comp = catalog.GetComponent("SUPERV")
226 if comp is None:
227         raise RuntimeError,"Component SUPERV not found in Module Catalog."
228
229 from SuperV import *
230 import SALOMEDS
231 myStudy = salome.myStudy
232 myBuilder = myStudy.NewBuilder()
233
234 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
235 father = myStudy.FindComponent("SUPERV")
236 if father is None:
237         father = myBuilder.NewComponent("SUPERV")
238         A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
239         FName = A1._narrow(SALOMEDS.AttributeName)
240         FName.SetValue( salome.sg.getComponentUserName("SUPERV") )
241         A2 = myBuilder.FindOrCreateAttribute(father, "AttributePixMap");
242         aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
243         aPixmap.SetPixMap( "ICON_OBJBROWSER_Supervision" );
244         myBuilder.DefineComponentInstance(father,SuperVision)
245
246 def addStudy(ior):
247     dataflow = SuperVision.getStreamGraph(ior)
248     name=dataflow.Name()
249     itr = myStudy.NewChildIterator(father)
250     while itr.More():
251         item=itr.Value()
252         res,A=item.FindAttribute("AttributeName")
253         if res:
254             aName = A._narrow(SALOMEDS.AttributeName)
255             if aName.Value() == name :
256                 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
257                 A  = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
258                 print "A = ", A
259                 if A is not None :
260                    #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
261                    anIOR  = A._narrow(SALOMEDS.AttributeIOR);
262                    print "anIOR.SetValue(dataflow.getIOR())"
263                    anIOR.SetValue(dataflow.getIOR()) 
264                 return
265         itr.Next()
266     obj = myBuilder.NewObject(father)
267     A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
268     aName=A._narrow(SALOMEDS.AttributeName)
269     aName.SetValue(name)
270     A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
271     anIOR  = A._narrow(SALOMEDS.AttributeIOR)
272     anIOR.SetValue(dataflow.getIOR())
273
274 import os
275 dir= os.getenv("DATA_DIR")
276 if dir == None:
277         raise RuntimeError, "DATA_DIR is not defined"
278 xmlfile = dir +"/Graphs/GraphGeomEssai.xml"
279 print "Load dataflow from the file : "
280 print xmlfile
281 print
282
283 myGraph = StreamGraph ( xmlfile )
284
285 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
286 print "myGraph.IsValid() = ", myGraph.IsValid()
287
288 # Get Nodes
289 myGraph.PrintNodes()
290
291 # This DataFlow is "executable" : all pending Ports are defined with Datas
292 print myGraph.IsExecutable()
293
294 # Starts only execution of that DataFlow and gets control immediatly
295 print myGraph.Run()
296
297 # That DataFlow is running ==> 0 (false)
298 print myGraph.IsDone()
299
300 # Events of execution :
301 aStatus,aNode,anEvent,aState = myGraph.Event()
302 while aStatus :
303     print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
304     aStatus,aNode,anEvent,aState = myGraph.Event()
305 print "myGraph.IsDone() = ",myGraph.IsDone()
306
307 # Wait for Completion (but it is already done after event loop ...)
308 print "Done : ",myGraph.DoneW()
309
310 print " "
311 #print "Type : print myGraph.IsDone()"
312 #print "       If execution is finished ==> 1 (true)"
313 res=myGraph.IsDone()
314 if res != 1:
315         raise RuntimeError, "myGraph.Run() is not done"
316
317 print " "
318 print "Type : myGraph.PrintPorts()"
319 print "       to see input and output values of the graph"
320 myGraph.PrintPorts()
321
322 # Export will create newsupervisionexample.xml and the corresponding .py file
323 tmpdir=os.getenv("TmpDir")
324 if tmpdir is None:
325         tmpdir="/tmp"
326 file = tmpdir + "/newsupervisionexample"
327 print "--------------\n"+file+"\n--------------\n"
328 myGraph.Export(file)
329
330 ior = salome.orb.object_to_string(myGraph.G)
331 addStudy(ior)
332
333 GraphName = myGraph.Name()
334 print "Befor save ",
335 #nodes = myGraph.Nodes()
336 nodes = myGraph.G.Nodes().FNodes
337 length_bs = len(nodes)
338 print "ListOfNodes length = ", length_bs
339 names=[]
340 for node in nodes:
341         names.append(node.Name())
342 print names
343
344 # Graph creation 
345 GraphInLines = StreamGraph( 'GraphInLines' )
346 GraphInLines.SetName( 'GraphInLines' )
347 GraphInLines.SetAuthor( '' )
348 GraphInLines.SetComment( '' )
349 GraphInLines.Coords( 0 , 0 )
350
351 # Creation of InLine Nodes
352 PyAdd = []
353 PyAdd.append( 'def Add(a,b) :  ' )
354 PyAdd.append( '    return a+b  ' )
355 PyAdd.append( '' )
356 Add = GraphInLines.INode( 'Add' , PyAdd )
357 Add.InPort( 'a' , 'long' )
358 Add.InPort( 'b' , 'long' )
359 Add.OutPort( 'f' , 'long' )
360 Add.SetName( 'Add' )
361 Add.SetAuthor( '' )
362 Add.SetComment( 'Python function' )
363 Add.Coords( 351 , 77 )
364 PySub = []
365 PySub.append( 'def Sub(a,b) : ' )
366 PySub.append( '    return a-b ' )
367 PySub.append( '' )
368 Sub = GraphInLines.INode( 'Sub' , PySub )
369 Sub.InPort( 'a' , 'long' )
370 Sub.InPort( 'b' , 'long' )
371 Sub.OutPort( 'f' , 'long' )
372 Sub.SetName( 'Sub' )
373 Sub.SetAuthor( '' )
374 Sub.SetComment( 'Python function' )
375 Sub.Coords( 86 , 333 )
376 PyMul = []
377 PyMul.append( 'def Mul(a,b) : ' )
378 PyMul.append( '    return a*b ' )
379 Mul = GraphInLines.INode( 'Mul' , PyMul )
380 Mul.InPort( 'a' , 'long' )
381 Mul.InPort( 'b' , 'long' )
382 Mul.OutPort( 'Result' , 'long' )
383 Mul.SetName( 'Mul' )
384 Mul.SetAuthor( '' )
385 Mul.SetComment( 'Python function' )
386 Mul.Coords( 616 , 247 )
387
388 # Creation of intermediate Output variables and of Control Links
389 Addf = Add.Port( 'f' )
390 Mula = GraphInLines.Link( Addf , Mul.Port( 'a' ) )
391 Mula.AddCoord( 1 , 570 , 356 )
392 Mula.AddCoord( 2 , 570 , 186 )
393 Subf = Sub.Port( 'f' )
394 Mulb = GraphInLines.Link( Subf , Mul.Port( 'b' ) )
395 Mulb.AddCoord( 1 , 282 , 376 )
396 Mulb.AddCoord( 2 , 282 , 442 )
397 Addb = GraphInLines.Link( Subf , Add.Port( 'b' ) )
398 Addb.AddCoord( 1 , 283 , 209 )
399 Addb.AddCoord( 2 , 283 , 374 )
400 Addb.AddCoord( 3 , 283 , 442 )
401
402 # Creation of Input datas
403 Adda = Add.Input( 'a' , 1)
404 Suba = Sub.Input( 'a' , 3)
405 Subb = Sub.Input( 'b' , 4)
406
407 # Creation of Output variables
408 MulResult = Mul.Port( 'Result' )
409
410 GraphInLines.Run()
411
412 GraphInLines.DoneW()
413
414 GraphInLines.PrintPorts()
415
416 sg.updateObjBrowser(1);
417
418 print
419 print "=============  Test  VISU  and MED ============================="
420 print
421
422 comp = catalog.GetComponent("VISU")
423 if comp is None:
424         raise RuntimeError,"Component VISU not found in Module Catalog."
425
426 import sys
427 import SALOMEDS
428 import SALOME
429 import SALOME_MED
430 import VISU
431
432 import visu_gui
433
434 medFileName = "pointe.med"
435 medFile = os.getenv('DATA_DIR') + '/MedFiles/' + medFileName
436 print "Load ", medFile
437
438 studyCurrent = salome.myStudyName
439
440 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
441 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
442
443 try:
444     if os.access(medFile, os.R_OK) :
445        if not os.access(medFile, os.W_OK) :
446                import random
447                medFileNew = "/tmp/" + str(random.randint(0,1000000)) + "_" + medFileName
448                print " -- Copy " + medFile + " to " + medFileNew
449                os.system("cp "+ medFile + " " + medFileNew)
450                medFile = medFileNew
451                os.system("chmod 755 " + medFile)
452
453        if os.access(medFile, os.W_OK) :
454            med_comp.readStructFileWithFieldType(medFile,studyCurrent)
455            med_obj = visu_gui.visu.getMedObjectFromStudy()
456            print "med_obj - ", med_obj
457
458            myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
459            aMeshName = "maa1"
460            anEntity = VISU.NODE
461            aTimeStampId = -1
462                    
463            myResult1 = myVisu.ImportMedField(myField1)
464            aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
465            
466            aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
467            
468            myResult2 = myVisu.ImportFile(medFile);
469            aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
470            
471            aTimeStampId = 3
472            aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
473                    
474            sg.updateObjBrowser(0)
475        else :  print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
476     else :  print  "We have no permission to read medFile, it will not be opened"; 
477
478 except:
479     if sys.exc_type == SALOME.SALOME_Exception :
480         print "There is no permission to read " + medFile
481     else :
482         print sys.exc_type 
483         print sys.exc_value
484         print sys.exc_traceback
485
486 sg.updateObjBrowser(1);