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