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 ============================="
62 comp = catalog.GetComponent("SMESH")
64 raise RuntimeError,"Component SMESH not found in Module Catalog."
66 comp = catalog.GetComponent("MED")
68 raise RuntimeError,"Component MED not found in Module Catalog."
73 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
74 myBuilder = salome.myStudy.NewBuilder()
76 smeshgui = salome.ImportComponentGUI("SMESH")
77 smeshgui.Init(salome.myStudyId);
79 ShapeTypeCompSolid = 1
89 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
90 idbox = geompy.addToStudy(box,"box")
92 # ---- add first face of box in study
94 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
96 name = geompy.SubShapeName( face._get_Name(), box._get_Name() )
98 idface=geompy.addToStudyInFather(box,face,name)
100 # ---- add shell from box in study
102 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
103 shell = subShellList[0]
104 name = geompy.SubShapeName( shell._get_Name(), box._get_Name() )
106 idshell=geompy.addToStudyInFather(box,shell,name)
108 # ---- add first edge of face in study
110 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
112 name = geompy.SubShapeName( edge._get_Name(), face._get_Name() )
114 idedge=geompy.addToStudyInFather(face,edge,name)
116 # ---- launch SMESH, init a Mesh with the box
117 gen=smeshpy.smeshpy()
120 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
121 smeshgui.SetName(idmesh, "Meshbox");
122 smeshgui.SetShape(idbox, idmesh);
124 # ---- create Hypothesis
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()
135 idlength = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypLen1) );
136 smeshgui.SetName(idlength, "Local_Length_100");
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()
146 idseg = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSeg1) );
147 smeshgui.SetName(idseg, "NumberOfSegments_7");
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()
157 idarea1 = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypArea1) );
158 smeshgui.SetName(idarea1, "MaxElementArea_2500");
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()
168 idarea2 = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypArea2) );
169 smeshgui.SetName(idarea2, "MaxElementArea_500");
171 print "-------------------------- Regular_1D"
172 alg1=gen.CreateHypothesis("Regular_1D")
173 algo1=alg1._narrow(SMESH.SMESH_Algo)
174 listHyp=algo1.GetCompatibleHypothesis()
177 algoReg=alg1._narrow(SMESH.SMESH_Regular_1D)
178 print algoReg.GetName()
179 print algoReg.GetId()
181 idreg = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(algoReg) );
182 smeshgui.SetName(idreg, "Regular_1D");
184 print "-------------------------- MEFISTO_2D"
185 alg2=gen.CreateHypothesis("MEFISTO_2D")
186 algo2=alg2._narrow(SMESH.SMESH_Algo)
187 listHyp=algo2.GetCompatibleHypothesis()
190 algoMef=alg2._narrow(SMESH.SMESH_MEFISTO_2D)
191 print algoMef.GetName()
192 print algoMef.GetId()
194 idmef = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(algoMef) );
195 smeshgui.SetName(idmef, "MEFISTO_2D");
197 # ---- add hypothesis to edge
199 print "-------------------------- add hypothesis to edge"
200 edge=salome.IDToObject(idedge)
201 submesh=mesh.GetElementsOnShape(edge)
202 ret=mesh.AddHypothesis(edge,algoReg)
204 ret=mesh.AddHypothesis(edge,hypLen1)
207 idsm1 = smeshgui.AddSubMeshOnShape( idmesh,
209 salome.orb.object_to_string(submesh),
211 smeshgui.SetName(idsm1, "SubMeshEdge")
212 smeshgui.SetAlgorithms( idsm1, idreg );
213 smeshgui.SetHypothesis( idsm1, idlength );
215 print "-------------------------- add hypothesis to face"
216 face=salome.IDToObject(idface)
217 submesh=mesh.GetElementsOnShape(face)
218 ret=mesh.AddHypothesis(face,hypArea2)
221 idsm2 = smeshgui.AddSubMeshOnShape( idmesh,
223 salome.orb.object_to_string(submesh),
225 smeshgui.SetName(idsm2, "SubMeshFace")
226 smeshgui.SetHypothesis( idsm2, idarea2 );
228 # ---- add hypothesis to box
230 print "-------------------------- add hypothesis to box"
231 box=salome.IDToObject(idbox)
232 submesh=mesh.GetElementsOnShape(box)
233 ret=mesh.AddHypothesis(box,algoReg)
235 ret=mesh.AddHypothesis(box,hypNbSeg1)
237 ret=mesh.AddHypothesis(box,algoMef)
239 ret=mesh.AddHypothesis(box,hypArea1)
242 smeshgui.SetAlgorithms( idmesh, idreg );
243 smeshgui.SetHypothesis( idmesh, idseg );
244 smeshgui.SetAlgorithms( idmesh, idmef );
245 smeshgui.SetHypothesis( idmesh, idarea1 );
247 gen.Compute(mesh, idbox)
248 sg.updateObjBrowser(1);
251 print "============= Test Supervisor ============================="
254 comp = catalog.GetComponent("SUPERV")
256 raise RuntimeError,"Component SUPERV not found in Module Catalog."
260 myStudy = salome.myStudy
261 myBuilder = myStudy.NewBuilder()
263 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
264 father = myStudy.FindComponent("SUPERV")
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)
276 dataflow = SuperVision.getGraph(ior)
278 itr = myStudy.NewChildIterator(father)
281 res,A=item.FindAttribute("AttributeName")
283 aName = A._narrow(SALOMEDS.AttributeName)
284 if aName.Value() == name :
285 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
286 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
289 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
290 anIOR = A._narrow(SALOMEDS.AttributeIOR);
291 print "anIOR.SetValue(dataflow.getIOR())"
292 anIOR.SetValue(dataflow.getIOR())
295 obj = myBuilder.NewObject(father)
296 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
297 aName=A._narrow(SALOMEDS.AttributeName)
299 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
300 anIOR = A._narrow(SALOMEDS.AttributeIOR)
301 anIOR.SetValue(dataflow.getIOR())
304 dir= os.getenv("SUPERV_ROOT_DIR")
306 raise RuntimeError, "SUPERV_ROOT_DIR is not defined"
307 xmlfile = dir +"/examples/GraphGeomEssai.xml"
308 print "Load dataflow from the file : "
312 myGraph = Graph ( xmlfile )
314 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
315 print "myGraph.IsValid() = ", myGraph.IsValid()
320 # This DataFlow is "executable" : all pending Ports are defined with Datas
321 print myGraph.IsExecutable()
323 # Starts only execution of that DataFlow and gets control immediatly
326 # That DataFlow is running ==> 0 (false)
327 print myGraph.IsDone()
329 # Events of execution :
330 aStatus,aNode,anEvent,aState = myGraph.Event()
332 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
333 aStatus,aNode,anEvent,aState = myGraph.Event()
334 print "myGraph.IsDone() = ",myGraph.IsDone()
336 # Wait for Completion (but it is already done after event loop ...)
337 print "Done : ",myGraph.DoneW()
340 #print "Type : print myGraph.IsDone()"
341 #print " If execution is finished ==> 1 (true)"
344 raise RuntimeError, "myGraph.Run() is not done"
347 print "Type : myGraph.PrintPorts()"
348 print " to see input and output values of the graph"
351 # Export will create newsupervisionexample.xml and the corresponding .py file
352 tmpdir=os.getenv("TmpDir")
355 file = tmpdir + "/newsupervisionexample"
356 print "--------------\n"+file+"\n--------------\n"
359 ior = salome.orb.object_to_string(myGraph.G)
362 GraphName = myGraph.Name()
364 #nodes = myGraph.Nodes()
365 nodes = myGraph.G.Nodes().FNodes
366 length_bs = len(nodes)
367 print "ListOfNodes length = ", length_bs
370 names.append(node.Name())
374 GraphInLines = Graph( 'GraphInLines' )
375 GraphInLines.SetName( 'GraphInLines' )
376 GraphInLines.SetAuthor( '' )
377 GraphInLines.SetComment( '' )
378 GraphInLines.Coords( 0 , 0 )
380 # Creation of InLine Nodes
382 PyAdd.append( 'def Add(a,b) : ' )
383 PyAdd.append( ' return a+b ' )
385 Add = GraphInLines.INode( 'Add' , PyAdd )
386 Add.InPort( 'a' , 'long' )
387 Add.InPort( 'b' , 'long' )
388 Add.OutPort( 'f' , 'long' )
391 Add.SetComment( 'Python function' )
392 Add.Coords( 351 , 77 )
394 PySub.append( 'def Sub(a,b) : ' )
395 PySub.append( ' return a-b ' )
397 Sub = GraphInLines.INode( 'Sub' , PySub )
398 Sub.InPort( 'a' , 'long' )
399 Sub.InPort( 'b' , 'long' )
400 Sub.OutPort( 'f' , 'long' )
403 Sub.SetComment( 'Python function' )
404 Sub.Coords( 86 , 333 )
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' )
414 Mul.SetComment( 'Python function' )
415 Mul.Coords( 616 , 247 )
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 )
431 # Creation of Input datas
432 Adda = Add.Input( 'a' , 1)
433 Suba = Sub.Input( 'a' , 3)
434 Subb = Sub.Input( 'b' , 4)
436 # Creation of Output variables
437 MulResult = Mul.Port( 'Result' )
443 GraphInLines.PrintPorts()
445 sg.updateObjBrowser(1);
448 print "============= Test VISU and MED ============================="
451 comp = catalog.GetComponent("VISU")
453 raise RuntimeError,"Component VISU not found in Module Catalog."
463 medFile = "pointe.med"
464 medFile = os.getenv('KERNEL_ROOT_DIR') + '/examples/' + medFile
465 print "Load ", medFile
467 studyCurrent = salome.myStudyName
469 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
470 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
473 if os.access(medFile, os.R_OK) :
474 if not os.access(medFile, os.W_OK) :
476 medFileNew = "/tmp/" + str(random.randint(0,1000000)) + "_" + medfile
477 print " -- Copy " + medFile + " to " + medFileNew
478 os.system("cp "+ medFile + " " + medFileNew)
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
486 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
491 myResult1 = myVisu.ImportMedField(myField1)
492 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
494 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
496 myResult2 = myVisu.ImportFile(medFile);
497 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
500 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
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";
507 if sys.exc_type == SALOME.SALOME_Exception :
508 print "There is no permission to read " + medFile
512 print sys.exc_traceback
514 sg.updateObjBrowser(1);