1 # SALOME SALOME_SWIG : binding of C++ implementation and Python
3 # Copyright (C) 2003 CEA/DEN, EDF R&D
7 # File : salome_test.py
10 print "Test the application loading GEOM, SMESH, VISU, MED, components and doing some"
11 print "operation within the components."
18 import SALOME_ModuleCatalog
20 print "======================================================================"
22 print "======================================================================"
23 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
24 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
26 print "======================================================================"
27 print " Create Study "
28 print "======================================================================"
30 comp = catalog.GetComponent("GEOM")
32 raise RuntimeError,"Component GEOM not found in Module Catalog."
36 print "================================="
37 print " create AttributeReal "
38 print "================================="
39 A = geompy.myBuilder.FindOrCreateAttribute(geompy.father, "AttributeReal")
41 raise RuntimeError, "Can't create AttributeReal attribute"
42 A = A._narrow(SALOMEDS.AttributeReal)
44 if A.Value() != 0.0001:
45 raise RuntimeError, "Error : wrong value of AttributeReal"
48 print " =========== Test Geometry =========================="
51 print "==================================="
53 print "==================================="
55 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
56 idbox = geompy.addToStudy(box,"box")
59 print "============= Test SMESH ============================="
64 comp = catalog.GetComponent("SMESH")
66 raise RuntimeError,"Component SMESH not found in Module Catalog."
68 comp = catalog.GetComponent("MED")
70 raise RuntimeError,"Component MED not found in Module Catalog."
74 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
75 myBuilder = salome.myStudy.NewBuilder()
77 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
78 smeshgui = salome.ImportComponentGUI("SMESH")
79 smeshgui.Init(salome.myStudyId);
81 ShapeTypeCompSolid = 1
91 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
92 idbox = geompy.addToStudy(box,"box")
94 # ---- add first face of box in study
96 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
98 name = geompy.SubShapeName(face, box)
100 idface=geompy.addToStudyInFather(box,face,name)
102 # ---- add shell from box in study
104 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
105 shell = subShellList[0]
106 name = geompy.SubShapeName(shell, box)
108 idshell=geompy.addToStudyInFather(box,shell,name)
110 # ---- add first edge of face in study
112 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
114 name = geompy.SubShapeName(edge, face)
116 idedge=geompy.addToStudyInFather(face,edge,name)
121 # ---- create Hypothesis
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()
131 smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
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()
140 smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
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()
149 smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
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()
158 smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
160 print "-------------------------- Regular_1D"
161 algoReg = smesh.CreateHypothesis( "Regular_1D", "libStdMeshersEngine.so" )
162 listHyp=algoReg.GetCompatibleHypothesis()
165 print algoReg.GetName()
166 print algoReg.GetId()
168 smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
170 print "-------------------------- MEFISTO_2D"
171 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", "libStdMeshersEngine.so" )
172 listHyp=algoMef.GetCompatibleHypothesis()
175 print algoMef.GetName()
176 print algoMef.GetId()
178 smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
180 # ---- add hypothesis to box
182 print "-------------------------- add hypothesis to box"
183 box=salome.IDToObject(idbox)
184 mesh = smesh.CreateMesh(box)
186 smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
188 ret=mesh.AddHypothesis(box,algoReg)
190 ret=mesh.AddHypothesis(box,algoMef)
194 ret=mesh.AddHypothesis(box,hypNbSeg1)
196 ret=mesh.AddHypothesis(box,hypArea1)
200 # ---- add hypothesis to edge
202 print "-------------------------- add hypothesis to edge"
203 edge=salome.IDToObject(idedge)
204 submesh=mesh.GetSubMesh(edge, "SubMeshEdge")
206 ret=mesh.AddHypothesis(edge,algoReg)
208 ret=mesh.AddHypothesis(edge,hypLen1)
211 print "-------------------------- add hypothesis to face"
212 face=salome.IDToObject(idface)
213 submesh = mesh.GetSubMesh(face, "SubMeshFace")
215 ret=mesh.AddHypothesis(face,hypArea2)
218 smesh.Compute(mesh, box)
219 sg.updateObjBrowser(1);
222 print "============= Test Supervisor ============================="
225 comp = catalog.GetComponent("SUPERV")
227 raise RuntimeError,"Component SUPERV not found in Module Catalog."
231 myStudy = salome.myStudy
232 myBuilder = myStudy.NewBuilder()
234 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
235 father = myStudy.FindComponent("SUPERV")
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)
247 dataflow = SuperVision.getStreamGraph(ior)
249 itr = myStudy.NewChildIterator(father)
252 res,A=item.FindAttribute("AttributeName")
254 aName = A._narrow(SALOMEDS.AttributeName)
255 if aName.Value() == name :
256 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
257 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
260 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
261 anIOR = A._narrow(SALOMEDS.AttributeIOR);
262 print "anIOR.SetValue(dataflow.getIOR())"
263 anIOR.SetValue(dataflow.getIOR())
266 obj = myBuilder.NewObject(father)
267 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
268 aName=A._narrow(SALOMEDS.AttributeName)
270 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
271 anIOR = A._narrow(SALOMEDS.AttributeIOR)
272 anIOR.SetValue(dataflow.getIOR())
275 dir= os.getenv("SUPERV_ROOT_DIR")
277 raise RuntimeError, "SUPERV_ROOT_DIR is not defined"
278 xmlfile = dir +"/examples/GraphGeomEssai.xml"
279 print "Load dataflow from the file : "
283 myGraph = StreamGraph ( xmlfile )
285 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
286 print "myGraph.IsValid() = ", myGraph.IsValid()
291 # This DataFlow is "executable" : all pending Ports are defined with Datas
292 print myGraph.IsExecutable()
294 # Starts only execution of that DataFlow and gets control immediatly
297 # That DataFlow is running ==> 0 (false)
298 print myGraph.IsDone()
300 # Events of execution :
301 aStatus,aNode,anEvent,aState = myGraph.Event()
303 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
304 aStatus,aNode,anEvent,aState = myGraph.Event()
305 print "myGraph.IsDone() = ",myGraph.IsDone()
307 # Wait for Completion (but it is already done after event loop ...)
308 print "Done : ",myGraph.DoneW()
311 #print "Type : print myGraph.IsDone()"
312 #print " If execution is finished ==> 1 (true)"
315 raise RuntimeError, "myGraph.Run() is not done"
318 print "Type : myGraph.PrintPorts()"
319 print " to see input and output values of the graph"
322 # Export will create newsupervisionexample.xml and the corresponding .py file
323 tmpdir=os.getenv("TmpDir")
326 file = tmpdir + "/newsupervisionexample"
327 print "--------------\n"+file+"\n--------------\n"
330 ior = salome.orb.object_to_string(myGraph.G)
333 GraphName = myGraph.Name()
335 #nodes = myGraph.Nodes()
336 nodes = myGraph.G.Nodes().FNodes
337 length_bs = len(nodes)
338 print "ListOfNodes length = ", length_bs
341 names.append(node.Name())
345 GraphInLines = StreamGraph( 'GraphInLines' )
346 GraphInLines.SetName( 'GraphInLines' )
347 GraphInLines.SetAuthor( '' )
348 GraphInLines.SetComment( '' )
349 GraphInLines.Coords( 0 , 0 )
351 # Creation of InLine Nodes
353 PyAdd.append( 'def Add(a,b) : ' )
354 PyAdd.append( ' return a+b ' )
356 Add = GraphInLines.INode( 'Add' , PyAdd )
357 Add.InPort( 'a' , 'long' )
358 Add.InPort( 'b' , 'long' )
359 Add.OutPort( 'f' , 'long' )
362 Add.SetComment( 'Python function' )
363 Add.Coords( 351 , 77 )
365 PySub.append( 'def Sub(a,b) : ' )
366 PySub.append( ' return a-b ' )
368 Sub = GraphInLines.INode( 'Sub' , PySub )
369 Sub.InPort( 'a' , 'long' )
370 Sub.InPort( 'b' , 'long' )
371 Sub.OutPort( 'f' , 'long' )
374 Sub.SetComment( 'Python function' )
375 Sub.Coords( 86 , 333 )
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' )
385 Mul.SetComment( 'Python function' )
386 Mul.Coords( 616 , 247 )
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 )
402 # Creation of Input datas
403 Adda = Add.Input( 'a' , 1)
404 Suba = Sub.Input( 'a' , 3)
405 Subb = Sub.Input( 'b' , 4)
407 # Creation of Output variables
408 MulResult = Mul.Port( 'Result' )
414 GraphInLines.PrintPorts()
416 sg.updateObjBrowser(1);
419 print "============= Test VISU and MED ============================="
422 comp = catalog.GetComponent("VISU")
424 raise RuntimeError,"Component VISU not found in Module Catalog."
434 medFileName = "pointe.med"
435 medFile = os.getenv('KERNEL_ROOT_DIR') + '/examples/' + medFileName
436 print "Load ", medFile
438 studyCurrent = salome.myStudyName
440 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
441 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
444 if os.access(medFile, os.R_OK) :
445 if not os.access(medFile, os.W_OK) :
447 medFileNew = "/tmp/" + str(random.randint(0,1000000)) + "_" + medFileName
448 print " -- Copy " + medFile + " to " + medFileNew
449 os.system("cp "+ medFile + " " + medFileNew)
451 os.system("chmod 755 " + medFile)
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
458 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
463 myResult1 = myVisu.ImportMedField(myField1)
464 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
466 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
468 myResult2 = myVisu.ImportFile(medFile);
469 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
472 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
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";
479 if sys.exc_type == SALOME.SALOME_Exception :
480 print "There is no permission to read " + medFile
484 print sys.exc_traceback
486 sg.updateObjBrowser(1);