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("Supervision")
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/GraphEssai.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()
319 Add,Sub,Mul,Div = myGraph.Nodes()
322 Addx = Add.Input("x",3.)
323 Addy = Add.Input("y",4.5)
324 Subx = Sub.Input("x",1.5)
332 # This DataFlow is "executable" : all pending Ports are defined with Datas
333 print myGraph.IsExecutable()
335 # Starts only execution of that DataFlow and gets control immediatly
338 # That DataFlow is running ==> 0 (false)
339 print myGraph.IsDone()
341 # Events of execution :
342 aStatus,aNode,anEvent,aState = myGraph.Event()
344 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
345 aStatus,aNode,anEvent,aState = myGraph.Event()
346 print "myGraph.IsDone() = ",myGraph.IsDone()
348 # Wait for Completion (but it is already done after event loop ...)
349 print "Done : ",myGraph.DoneW()
352 print "Result : ",Divz.ToString()
354 # Intermediate results :
355 print "Intermediate Result Add\z : ",Addz.ToString()
356 print "Intermediate Result Sub\z : ",Subz.ToString()
357 print "Intermediate Result Mul\z : ",Mulz.ToString()
360 #print "Type : print myGraph.IsDone()"
361 #print " If execution is finished ==> 1 (true)"
364 raise RuntimeError, "myGraph.Run() is not done"
367 print "Type : print Divz.ToString()"
368 print " You will get the result"
372 print "Type : myGraph.PrintPorts()"
373 print " to see input and output values of the graph"
377 print "Type : Add.PrintPorts()"
380 print "Type : Sub.PrintPorts()"
383 print "Type : Mul.PrintPorts()"
386 print "Type : Div.PrintPorts()"
387 print " to see input and output values of nodes"
390 # Export will create newsupervisionexample.xml and the corresponding .py file
391 tmpdir=os.getenv("TmpDir")
394 file = tmpdir + "/newsupervisionexample"
395 print "--------------\n"+file+"\n--------------\n"
398 ior = salome.orb.object_to_string(myGraph.G)
401 GraphName = myGraph.Name()
403 #nodes = myGraph.Nodes()
404 nodes = myGraph.G.Nodes().FNodes
405 length_bs = len(nodes)
406 print "ListOfNodes length = ", length_bs
409 names.append(node.Name())
412 print "Load FactorialComponent component, create dataflow using its services and run execution"
415 eval = myPy.Node('FactorialComponent','FactorialComponent','eval')
416 eval.SetContainer('FactoryServerPy')
431 sg.updateObjBrowser(1);
434 print "============= Test VISU and MED ============================="
437 comp = catalog.GetComponent("VISU")
439 raise RuntimeError,"Component VISU not found in Module Catalog."
449 medFile = "pointe.med"
450 medFile = os.getenv('KERNEL_ROOT_DIR') + '/examples/' + medFile
451 print "Load ", medFile
453 studyCurrent = salome.myStudyName
455 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
456 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
459 if os.access(medFile, os.R_OK) :
460 if os.access(medFile, os.W_OK) :
461 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
462 med_obj = visu_gui.visu.getMedObjectFromStudy()
463 print "med_obj - ", med_obj
465 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
470 myResult1 = myVisu.ImportMedField(myField1)
471 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
473 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
475 myResult2 = myVisu.ImportFile(medFile);
476 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
479 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
481 sg.updateObjBrowser(0)
482 else : print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
483 else : print "We have no permission to read medFile, it will not be opened";
486 if sys.exc_type == SALOME.SALOME_Exception :
487 print "There is no permission to read " + medFile
491 print sys.exc_traceback
493 sg.updateObjBrowser(1);