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 "======================================================================"
21 print " Check, that there is no data of MED component in the Study "
22 print "======================================================================"
24 MedComp = salome.myStudy.FindComponent("MED")
25 if MedComp is not None:
27 print "This script cannot work properly, because there are"
28 print "some MED component data already exists in the study."
29 print "Execution aborted."
31 raise RuntimeError, "Please, run this script only in a new empty study."
33 print "======================================================================"
35 print "======================================================================"
36 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
37 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
39 print "======================================================================"
40 print " Create Study "
41 print "======================================================================"
43 comp = catalog.GetComponent("GEOM")
45 raise RuntimeError,"Component GEOM not found in Module Catalog."
49 print "================================="
50 print " create AttributeReal "
51 print "================================="
52 A = geompy.myBuilder.FindOrCreateAttribute(geompy.father, "AttributeReal")
54 raise RuntimeError, "Can't create AttributeReal attribute"
55 A = A._narrow(SALOMEDS.AttributeReal)
57 if A.Value() != 0.0001:
58 raise RuntimeError, "Error : wrong value of AttributeReal"
61 print " =========== Test Geometry =========================="
64 print "==================================="
66 print "==================================="
68 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
69 idbox = geompy.addToStudy(box,"box")
72 print "============= Test SMESH ============================="
77 comp = catalog.GetComponent("SMESH")
79 raise RuntimeError,"Component SMESH not found in Module Catalog."
81 comp = catalog.GetComponent("MED")
83 raise RuntimeError,"Component MED not found in Module Catalog."
87 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
88 myBuilder = salome.myStudy.NewBuilder()
90 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
91 smeshgui = salome.ImportComponentGUI("SMESH")
92 smeshgui.Init(salome.myStudyId);
94 ShapeTypeCompSolid = 1
104 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
105 idbox = geompy.addToStudy(box,"box")
107 # ---- add first face of box in study
109 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
111 name = geompy.SubShapeName(face, box)
113 idface=geompy.addToStudyInFather(box,face,name)
115 # ---- add shell from box in study
117 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
118 shell = subShellList[0]
119 name = geompy.SubShapeName(shell, box)
121 idshell=geompy.addToStudyInFather(box,shell,name)
123 # ---- add first edge of face in study
125 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
127 name = geompy.SubShapeName(edge, face)
129 idedge=geompy.addToStudyInFather(face,edge,name)
134 # ---- create Hypothesis
136 print "-------------------------- create Hypothesis"
137 print "-------------------------- LocalLength"
138 hypLen1 = smesh.CreateHypothesis( "LocalLength", "libStdMeshersEngine.so" )
139 hypLen1.SetLength(100)
140 print hypLen1.GetName()
141 print hypLen1.GetId()
142 print hypLen1.GetLength()
144 smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
146 print "-------------------------- NumberOfSegments"
147 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", "libStdMeshersEngine.so" )
148 hypNbSeg1.SetNumberOfSegments(7)
149 print hypNbSeg1.GetName()
150 print hypNbSeg1.GetId()
151 print hypNbSeg1.GetNumberOfSegments()
153 smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
155 print "-------------------------- MaxElementArea"
156 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", "libStdMeshersEngine.so" )
157 hypArea1.SetMaxElementArea(2500)
158 print hypArea1.GetName()
159 print hypArea1.GetId()
160 print hypArea1.GetMaxElementArea()
162 smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
164 print "-------------------------- MaxElementArea"
165 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", "libStdMeshersEngine.so" )
166 hypArea2.SetMaxElementArea(500)
167 print hypArea2.GetName()
168 print hypArea2.GetId()
169 print hypArea2.GetMaxElementArea()
171 smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
173 print "-------------------------- Regular_1D"
174 algoReg = smesh.CreateHypothesis( "Regular_1D", "libStdMeshersEngine.so" )
175 listHyp=algoReg.GetCompatibleHypothesis()
178 print algoReg.GetName()
179 print algoReg.GetId()
181 smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
183 print "-------------------------- MEFISTO_2D"
184 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", "libStdMeshersEngine.so" )
185 listHyp=algoMef.GetCompatibleHypothesis()
188 print algoMef.GetName()
189 print algoMef.GetId()
191 smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
193 # ---- add hypothesis to box
195 print "-------------------------- add hypothesis to box"
196 box=salome.IDToObject(idbox)
197 mesh = smesh.CreateMesh(box)
199 smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
201 ret=mesh.AddHypothesis(box,algoReg)
203 ret=mesh.AddHypothesis(box,algoMef)
207 ret=mesh.AddHypothesis(box,hypNbSeg1)
209 ret=mesh.AddHypothesis(box,hypArea1)
213 # ---- add hypothesis to edge
215 print "-------------------------- add hypothesis to edge"
216 edge=salome.IDToObject(idedge)
217 submesh=mesh.GetSubMesh(edge, "SubMeshEdge")
219 ret=mesh.AddHypothesis(edge,algoReg)
221 ret=mesh.AddHypothesis(edge,hypLen1)
224 print "-------------------------- add hypothesis to face"
225 face=salome.IDToObject(idface)
226 submesh = mesh.GetSubMesh(face, "SubMeshFace")
228 ret=mesh.AddHypothesis(face,hypArea2)
231 smesh.Compute(mesh, box)
232 sg.updateObjBrowser(1);
235 print "============= Test Supervisor ============================="
238 comp = catalog.GetComponent("SUPERV")
240 raise RuntimeError,"Component SUPERV not found in Module Catalog."
244 myStudy = salome.myStudy
245 myBuilder = myStudy.NewBuilder()
247 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
248 father = myStudy.FindComponent("SUPERV")
250 father = myBuilder.NewComponent("SUPERV")
251 A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
252 FName = A1._narrow(SALOMEDS.AttributeName)
253 FName.SetValue( salome.sg.getComponentUserName("SUPERV") )
254 A2 = myBuilder.FindOrCreateAttribute(father, "AttributePixMap");
255 aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
256 aPixmap.SetPixMap( "ICON_OBJBROWSER_Supervision" );
257 myBuilder.DefineComponentInstance(father,SuperVision)
260 dataflow = SuperVision.getStreamGraph(ior)
262 itr = myStudy.NewChildIterator(father)
265 res,A=item.FindAttribute("AttributeName")
267 aName = A._narrow(SALOMEDS.AttributeName)
268 if aName.Value() == name :
269 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
270 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
273 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
274 anIOR = A._narrow(SALOMEDS.AttributeIOR);
275 print "anIOR.SetValue(dataflow.getIOR())"
276 anIOR.SetValue(dataflow.getIOR())
279 obj = myBuilder.NewObject(father)
280 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
281 aName=A._narrow(SALOMEDS.AttributeName)
283 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
284 anIOR = A._narrow(SALOMEDS.AttributeIOR)
285 anIOR.SetValue(dataflow.getIOR())
288 dir= os.getenv("DATA_DIR")
290 raise RuntimeError, "DATA_DIR is not defined"
291 xmlfile = dir + "/Superv/Graphs/GraphGeomEssaiGates.xml"
292 print "Load dataflow from the file : "
296 myGraph = StreamGraph ( xmlfile )
298 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
299 print "myGraph.IsValid() = ", myGraph.IsValid()
304 # This DataFlow is "executable" : all pending Ports are defined with Datas
305 print myGraph.IsExecutable()
307 # Starts only execution of that DataFlow and gets control immediatly
310 # That DataFlow is running ==> 0 (false)
311 print myGraph.IsDone()
313 # Events of execution :
314 aStatus,aNode,anEvent,aState = myGraph.Event()
316 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
317 aStatus,aNode,anEvent,aState = myGraph.Event()
318 print "myGraph.IsDone() = ",myGraph.IsDone()
320 # Wait for Completion (but it is already done after event loop ...)
321 print "Done : ",myGraph.DoneW()
324 #print "Type : print myGraph.IsDone()"
325 #print " If execution is finished ==> 1 (true)"
328 raise RuntimeError, "myGraph.Run() is not done"
331 print "Type : myGraph.PrintPorts()"
332 print " to see input and output values of the graph"
335 # Export will create newsupervisionexample.xml and the corresponding .py file
336 tmpdir=os.getenv("TmpDir")
339 file = tmpdir + "/newsupervisionexample"
340 print "--------------\n"+file+"\n--------------\n"
343 ior = salome.orb.object_to_string(myGraph.G)
346 GraphName = myGraph.Name()
348 #nodes = myGraph.Nodes()
349 nodes = myGraph.G.Nodes().FNodes
350 length_bs = len(nodes)
351 print "ListOfNodes length = ", length_bs
354 names.append(node.Name())
358 GraphInLines = StreamGraph( 'GraphInLines' )
359 GraphInLines.SetName( 'GraphInLines' )
360 GraphInLines.SetAuthor( '' )
361 GraphInLines.SetComment( '' )
362 GraphInLines.Coords( 0 , 0 )
364 # Creation of InLine Nodes
366 PyAdd.append( 'def Add(a,b) : ' )
367 PyAdd.append( ' return a+b ' )
369 Add = GraphInLines.INode( 'Add' , PyAdd )
370 Add.InPort( 'a' , 'long' )
371 Add.InPort( 'b' , 'long' )
372 Add.OutPort( 'f' , 'long' )
375 Add.SetComment( 'Python function' )
376 Add.Coords( 351 , 77 )
378 PySub.append( 'def Sub(a,b) : ' )
379 PySub.append( ' return a-b ' )
381 Sub = GraphInLines.INode( 'Sub' , PySub )
382 Sub.InPort( 'a' , 'long' )
383 Sub.InPort( 'b' , 'long' )
384 Sub.OutPort( 'f' , 'long' )
387 Sub.SetComment( 'Python function' )
388 Sub.Coords( 86 , 333 )
390 PyMul.append( 'def Mul(a,b) : ' )
391 PyMul.append( ' return a*b ' )
392 Mul = GraphInLines.INode( 'Mul' , PyMul )
393 Mul.InPort( 'a' , 'long' )
394 Mul.InPort( 'b' , 'long' )
395 Mul.OutPort( 'Result' , 'long' )
398 Mul.SetComment( 'Python function' )
399 Mul.Coords( 616 , 247 )
401 # Creation of intermediate Output variables and of Control Links
402 Addf = Add.Port( 'f' )
403 Mula = GraphInLines.Link( Addf , Mul.Port( 'a' ) )
404 Mula.AddCoord( 1 , 570 , 356 )
405 Mula.AddCoord( 2 , 570 , 186 )
406 Subf = Sub.Port( 'f' )
407 Mulb = GraphInLines.Link( Subf , Mul.Port( 'b' ) )
408 Mulb.AddCoord( 1 , 282 , 376 )
409 Mulb.AddCoord( 2 , 282 , 442 )
410 Addb = GraphInLines.Link( Subf , Add.Port( 'b' ) )
411 Addb.AddCoord( 1 , 283 , 209 )
412 Addb.AddCoord( 2 , 283 , 374 )
413 Addb.AddCoord( 3 , 283 , 442 )
415 # Creation of Input datas
416 Adda = Add.Input( 'a' , 1)
417 Suba = Sub.Input( 'a' , 3)
418 Subb = Sub.Input( 'b' , 4)
420 # Creation of Output variables
421 MulResult = Mul.Port( 'Result' )
427 GraphInLines.PrintPorts()
429 sg.updateObjBrowser(1);
432 print "============= Test VISU and MED ============================="
435 comp = catalog.GetComponent("VISU")
437 raise RuntimeError,"Component VISU not found in Module Catalog."
447 medFileName = "pointe.med"
448 medFile = os.getenv('DATA_DIR') + '/MedFiles/' + medFileName
449 print "Load ", medFile
451 studyCurrent = salome.myStudyName
453 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
454 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
457 if os.access(medFile, os.R_OK) :
458 if not os.access(medFile, os.W_OK) :
460 medFileNew = "/tmp/" + str(random.randint(0,1000000)) + "_" + medFileName
461 print " -- Copy " + medFile + " to " + medFileNew
462 os.system("cp "+ medFile + " " + medFileNew)
464 os.system("chmod 755 " + medFile)
466 if os.access(medFile, os.W_OK) :
467 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
468 med_obj = visu_gui.visu.getMedObjectFromStudy()
469 print "med_obj - ", med_obj
471 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
476 myResult1 = myVisu.ImportMedField(myField1)
477 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
479 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
481 myResult2 = myVisu.ImportFile(medFile);
482 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
485 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
487 sg.updateObjBrowser(0)
488 else : print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
489 else : print "We have no permission to read medFile, it will not be opened";
492 if sys.exc_type == SALOME.SALOME_Exception :
493 print "There is no permission to read " + medFile
497 print sys.exc_traceback
499 sg.updateObjBrowser(1);