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 print "======================================================================"
19 print " Create Study "
20 print "======================================================================"
24 print "================================="
25 print " create AttributeReal "
26 print "================================="
27 A = geompy.myBuilder.FindOrCreateAttribute(geompy.father, "AttributeReal")
29 raise RuntimeError, "Can't create AttributeReal attribute"
30 A = A._narrow(SALOMEDS.AttributeReal)
32 if A.Value() != 0.0001:
33 raise RuntimeError, "Error : wrong value of AttributeReal"
36 print " =========== Test Geometry =========================="
39 print "==================================="
41 print "==================================="
43 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
44 idbox = geompy.addToStudy(box,"box")
47 print "============= Test SMESH ============================="
53 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "Geometry")
54 myBuilder = salome.myStudy.NewBuilder()
56 smeshgui = salome.ImportComponentGUI("SMESH")
57 smeshgui.Init(salome.myStudyId);
59 ShapeTypeCompSolid = 1
69 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
70 idbox = geompy.addToStudy(box,"box")
72 # ---- add first face of box in study
74 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
76 name = geompy.SubShapeName( face._get_Name(), box._get_Name() )
78 idface=geompy.addToStudyInFather(box,face,name)
80 # ---- add shell from box in study
82 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
83 shell = subShellList[0]
84 name = geompy.SubShapeName( shell._get_Name(), box._get_Name() )
86 idshell=geompy.addToStudyInFather(box,shell,name)
88 # ---- add first edge of face in study
90 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
92 name = geompy.SubShapeName( edge._get_Name(), face._get_Name() )
94 idedge=geompy.addToStudyInFather(face,edge,name)
96 # ---- launch SMESH, init a Mesh with the box
100 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
101 smeshgui.SetName(idmesh, "Meshbox");
102 smeshgui.SetShape(idbox, idmesh);
104 # ---- create Hypothesis
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()
115 idlength = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypLen1) );
116 smeshgui.SetName(idlength, "Local_Length_100");
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()
126 idseg = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSeg1) );
127 smeshgui.SetName(idseg, "NumberOfSegments_7");
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()
137 idarea1 = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypArea1) );
138 smeshgui.SetName(idarea1, "MaxElementArea_2500");
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()
148 idarea2 = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypArea2) );
149 smeshgui.SetName(idarea2, "MaxElementArea_500");
151 print "-------------------------- Regular_1D"
152 alg1=gen.CreateHypothesis("Regular_1D")
153 algo1=alg1._narrow(SMESH.SMESH_Algo)
154 listHyp=algo1.GetCompatibleHypothesis()
157 algoReg=alg1._narrow(SMESH.SMESH_Regular_1D)
158 print algoReg.GetName()
159 print algoReg.GetId()
161 idreg = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(algoReg) );
162 smeshgui.SetName(idreg, "Regular_1D");
164 print "-------------------------- MEFISTO_2D"
165 alg2=gen.CreateHypothesis("MEFISTO_2D")
166 algo2=alg2._narrow(SMESH.SMESH_Algo)
167 listHyp=algo2.GetCompatibleHypothesis()
170 algoMef=alg2._narrow(SMESH.SMESH_MEFISTO_2D)
171 print algoMef.GetName()
172 print algoMef.GetId()
174 idmef = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(algoMef) );
175 smeshgui.SetName(idmef, "MEFISTO_2D");
177 # ---- add hypothesis to edge
179 print "-------------------------- add hypothesis to edge"
180 edge=salome.IDToObject(idedge)
181 submesh=mesh.GetElementsOnShape(edge)
182 ret=mesh.AddHypothesis(edge,algoReg)
184 ret=mesh.AddHypothesis(edge,hypLen1)
187 idsm1 = smeshgui.AddSubMeshOnShape( idmesh,
189 salome.orb.object_to_string(submesh),
191 smeshgui.SetName(idsm1, "SubMeshEdge")
192 smeshgui.SetAlgorithms( idsm1, idreg );
193 smeshgui.SetHypothesis( idsm1, idlength );
195 print "-------------------------- add hypothesis to face"
196 face=salome.IDToObject(idface)
197 submesh=mesh.GetElementsOnShape(face)
198 ret=mesh.AddHypothesis(face,hypArea2)
201 idsm2 = smeshgui.AddSubMeshOnShape( idmesh,
203 salome.orb.object_to_string(submesh),
205 smeshgui.SetName(idsm2, "SubMeshFace")
206 smeshgui.SetHypothesis( idsm2, idarea2 );
208 # ---- add hypothesis to box
210 print "-------------------------- add hypothesis to box"
211 box=salome.IDToObject(idbox)
212 submesh=mesh.GetElementsOnShape(box)
213 ret=mesh.AddHypothesis(box,algoReg)
215 ret=mesh.AddHypothesis(box,hypNbSeg1)
217 ret=mesh.AddHypothesis(box,algoMef)
219 ret=mesh.AddHypothesis(box,hypArea1)
222 smeshgui.SetAlgorithms( idmesh, idreg );
223 smeshgui.SetHypothesis( idmesh, idseg );
224 smeshgui.SetAlgorithms( idmesh, idmef );
225 smeshgui.SetHypothesis( idmesh, idarea1 );
227 gen.Compute(mesh, idbox)
228 sg.updateObjBrowser(1);
231 print "============= Test Supervisor ============================="
236 myStudy = salome.myStudy
237 myBuilder = myStudy.NewBuilder()
239 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
240 father = myStudy.FindComponent("SUPERV")
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)
252 dataflow = SuperVision.getGraph(ior)
254 itr = myStudy.NewChildIterator(father)
257 res,A=item.FindAttribute("AttributeName")
259 aName = A._narrow(SALOMEDS.AttributeName)
260 if aName.Value() == name :
261 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
262 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
265 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
266 anIOR = A._narrow(SALOMEDS.AttributeIOR);
267 print "anIOR.SetValue(dataflow.getIOR())"
268 anIOR.SetValue(dataflow.getIOR())
271 obj = myBuilder.NewObject(father)
272 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
273 aName=A._narrow(SALOMEDS.AttributeName)
275 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
276 anIOR = A._narrow(SALOMEDS.AttributeIOR)
277 anIOR.SetValue(dataflow.getIOR())
280 dir= os.getenv("SALOME_ROOT_DIR")
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 : "
288 myGraph = Graph ( xmlfile )
290 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
291 print "myGraph.IsValid() = ", myGraph.IsValid()
295 Add,Sub,Mul,Div = myGraph.Nodes()
298 Addx = Add.Input("x",3.)
299 Addy = Add.Input("y",4.5)
300 Subx = Sub.Input("x",1.5)
308 # This DataFlow is "executable" : all pending Ports are defined with Datas
309 print myGraph.IsExecutable()
311 # Starts only execution of that DataFlow and gets control immediatly
314 # That DataFlow is running ==> 0 (false)
315 print myGraph.IsDone()
317 # Events of execution :
318 aStatus,aNode,anEvent,aState = myGraph.Event()
320 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
321 aStatus,aNode,anEvent,aState = myGraph.Event()
322 print "myGraph.IsDone() = ",myGraph.IsDone()
324 # Wait for Completion (but it is already done after event loop ...)
325 print "Done : ",myGraph.DoneW()
328 print "Result : ",Divz.ToString()
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()
336 #print "Type : print myGraph.IsDone()"
337 #print " If execution is finished ==> 1 (true)"
340 raise RuntimeError, "myGraph.Run() is not done"
343 print "Type : print Divz.ToString()"
344 print " You will get the result"
348 print "Type : myGraph.PrintPorts()"
349 print " to see input and output values of the graph"
353 print "Type : Add.PrintPorts()"
356 print "Type : Sub.PrintPorts()"
359 print "Type : Mul.PrintPorts()"
362 print "Type : Div.PrintPorts()"
363 print " to see input and output values of nodes"
366 # Export will create newsupervisionexample.xml and the corresponding .py file
367 tmpdir=os.getenv("TmpDir")
370 file = tmpdir + "/newsupervisionexample"
371 print "--------------\n"+file+"\n--------------\n"
374 ior = salome.orb.object_to_string(myGraph.G)
377 GraphName = myGraph.Name()
379 #nodes = myGraph.Nodes()
380 nodes = myGraph.G.Nodes().FNodes
381 length_bs = len(nodes)
382 print "ListOfNodes length = ", length_bs
385 names.append(node.Name())
388 print "Load FactorialComponent component, create dataflow using its services and run execution"
391 eval = myPy.Node('FactorialComponent','FactorialComponent','eval')
392 eval.SetContainer('FactoryServerPy')
407 sg.updateObjBrowser(1);
410 print "============= Test VISU and MED ============================="
420 medFile = "pointe.med"
421 medFile = os.getenv('SALOME_ROOT_DIR') + '/../SALOME_ROOT/data/' + medFile
422 print "Load ", medFile
424 studyCurrent = salome.myStudyName
426 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "Med")
427 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "Visu")
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
436 myField = visu_gui.visu.getFieldObjectFromStudy(2,1)
437 aMeshName = "FILED_DOUBLE_MESH"
441 myResult1 = myVisu.ImportMedField(myField)
442 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
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)
448 myResult2 = myVisu.ImportFile(medFile)
451 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity)
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)
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";
462 if sys.exc_type == SALOME.SALOME_Exception :
463 print "There is no permission to read " + medFile
467 print sys.exc_traceback
469 sg.updateObjBrowser(1);