1 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # SALOME SALOME_SWIG : binding of C++ implementation and Python
23 # File : salome_test.py
26 print "Test the application loading GEOM, SMESH, VISU, MED, components and doing some"
27 print "operation within the components."
35 import SALOME_ModuleCatalog
37 print "======================================================================"
38 print " Check, that there is no data of MED component in the Study "
39 print "======================================================================"
41 MedComp = salome.myStudy.FindComponent("MED")
42 if MedComp is not None:
44 print "This script cannot work properly, because there are"
45 print "some MED component data already exists in the study."
46 print "Execution aborted."
48 raise RuntimeError, "Please, run this script only in a new empty study."
50 print "======================================================================"
52 print "======================================================================"
53 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
54 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
56 print "======================================================================"
57 print " Create Study "
58 print "======================================================================"
60 comp = catalog.GetComponent("GEOM")
62 raise RuntimeError,"Component GEOM not found in Module Catalog."
66 print "================================="
67 print " create AttributeReal "
68 print "================================="
69 A = geompy.myBuilder.FindOrCreateAttribute(geompy.father, "AttributeReal")
71 raise RuntimeError, "Can't create AttributeReal attribute"
72 A = A._narrow(SALOMEDS.AttributeReal)
74 if A.Value() != 0.0001:
75 raise RuntimeError, "Error : wrong value of AttributeReal"
78 print " =========== Test Geometry =========================="
81 print "==================================="
83 print "==================================="
85 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
86 idbox = geompy.addToStudy(box,"box")
89 print "============= Test SMESH ============================="
94 comp = catalog.GetComponent("SMESH")
96 raise RuntimeError,"Component SMESH not found in Module Catalog."
98 comp = catalog.GetComponent("MED")
100 raise RuntimeError,"Component MED not found in Module Catalog."
104 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
105 myBuilder = salome.myStudy.NewBuilder()
107 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
108 smeshgui = salome.ImportComponentGUI("SMESH")
109 smeshgui.Init(salome.myStudyId);
111 ShapeTypeCompSolid = 1
121 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
122 idbox = geompy.addToStudy(box,"box")
124 # ---- add first face of box in study
126 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
128 name = geompy.SubShapeName(face, box)
130 idface=geompy.addToStudyInFather(box,face,name)
132 # ---- add shell from box in study
134 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
135 shell = subShellList[0]
136 name = geompy.SubShapeName(shell, box)
138 idshell=geompy.addToStudyInFather(box,shell,name)
140 # ---- add first edge of face in study
142 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
144 name = geompy.SubShapeName(edge, face)
146 idedge=geompy.addToStudyInFather(face,edge,name)
151 # ---- create Hypothesis
153 if sys.platform == "win32":
154 stdMeshersEngine = "StdMeshersEngine"
156 stdMeshersEngine = "libStdMeshersEngine.so"
158 print "-------------------------- create Hypothesis"
159 print "-------------------------- LocalLength"
160 hypLen1 = smesh.CreateHypothesis( "LocalLength", stdMeshersEngine )
161 hypLen1.SetLength(100)
162 print hypLen1.GetName()
163 print hypLen1.GetId()
164 print hypLen1.GetLength()
166 smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
168 print "-------------------------- NumberOfSegments"
169 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", stdMeshersEngine )
170 hypNbSeg1.SetNumberOfSegments(7)
171 print hypNbSeg1.GetName()
172 print hypNbSeg1.GetId()
173 print hypNbSeg1.GetNumberOfSegments()
175 smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
177 print "-------------------------- MaxElementArea"
178 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
179 hypArea1.SetMaxElementArea(2500)
180 print hypArea1.GetName()
181 print hypArea1.GetId()
182 print hypArea1.GetMaxElementArea()
184 smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
186 print "-------------------------- MaxElementArea"
187 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
188 hypArea2.SetMaxElementArea(500)
189 print hypArea2.GetName()
190 print hypArea2.GetId()
191 print hypArea2.GetMaxElementArea()
193 smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
195 print "-------------------------- Regular_1D"
196 algoReg = smesh.CreateHypothesis( "Regular_1D", stdMeshersEngine )
197 listHyp=algoReg.GetCompatibleHypothesis()
200 print algoReg.GetName()
201 print algoReg.GetId()
203 smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
205 print "-------------------------- MEFISTO_2D"
206 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", stdMeshersEngine )
207 listHyp=algoMef.GetCompatibleHypothesis()
210 print algoMef.GetName()
211 print algoMef.GetId()
213 smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
215 # ---- add hypothesis to box
217 print "-------------------------- add hypothesis to box"
218 box=salome.IDToObject(idbox)
219 mesh = smesh.CreateMesh(box)
221 smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
223 ret=mesh.AddHypothesis(box,algoReg)
225 ret=mesh.AddHypothesis(box,algoMef)
229 ret=mesh.AddHypothesis(box,hypNbSeg1)
231 ret=mesh.AddHypothesis(box,hypArea1)
235 # ---- add hypothesis to edge
237 print "-------------------------- add hypothesis to edge"
238 edge=salome.IDToObject(idedge)
239 submesh=mesh.GetSubMesh(edge, "SubMeshEdge")
241 ret=mesh.AddHypothesis(edge,algoReg)
243 ret=mesh.AddHypothesis(edge,hypLen1)
246 print "-------------------------- add hypothesis to face"
247 face=salome.IDToObject(idface)
248 submesh = mesh.GetSubMesh(face, "SubMeshFace")
250 ret=mesh.AddHypothesis(face,hypArea2)
253 smesh.Compute(mesh, box)
254 sg.updateObjBrowser(1);
256 # SUPERVISOR module is not available since SALOME 5.0 version
259 if (defineSuperV != 0) & (sys.platform != "win32"):
261 print "============= Test Supervisor ============================="
264 comp = catalog.GetComponent("SUPERV")
266 raise RuntimeError,"Component SUPERV not found in Module Catalog."
270 myStudy = salome.myStudy
271 myBuilder = myStudy.NewBuilder()
273 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
274 father = myStudy.FindComponent("SUPERV")
276 father = myBuilder.NewComponent("SUPERV")
277 A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
278 FName = A1._narrow(SALOMEDS.AttributeName)
279 FName.SetValue( salome.sg.getComponentUserName("SUPERV") )
280 A2 = myBuilder.FindOrCreateAttribute(father, "AttributePixMap");
281 aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
282 aPixmap.SetPixMap( "ICON_OBJBROWSER_Supervision" );
283 myBuilder.DefineComponentInstance(father,SuperVision)
286 dataflow = SuperVision.getStreamGraph(ior)
288 itr = myStudy.NewChildIterator(father)
291 res,A=item.FindAttribute("AttributeName")
293 aName = A._narrow(SALOMEDS.AttributeName)
294 if aName.Value() == name :
295 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
296 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
299 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
300 anIOR = A._narrow(SALOMEDS.AttributeIOR);
301 print "anIOR.SetValue(dataflow.getIOR())"
302 anIOR.SetValue(dataflow.getIOR())
305 obj = myBuilder.NewObject(father)
306 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
307 aName=A._narrow(SALOMEDS.AttributeName)
309 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
310 anIOR = A._narrow(SALOMEDS.AttributeIOR)
311 anIOR.SetValue(dataflow.getIOR())
314 dir= os.getenv("DATA_DIR")
316 raise RuntimeError, "DATA_DIR is not defined"
317 xmlfile = dir + "/Superv/Graphs/GraphGeomEssaiGates.xml"
318 print "Load dataflow from the file : "
322 myGraph = StreamGraph ( xmlfile )
324 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
325 print "myGraph.IsValid() = ", myGraph.IsValid()
330 # This DataFlow is "executable" : all pending Ports are defined with Datas
331 print myGraph.IsExecutable()
333 # Starts only execution of that DataFlow and gets control immediatly
336 # That DataFlow is running ==> 0 (false)
337 print myGraph.IsDone()
339 # Events of execution :
340 aStatus,aNode,anEvent,aState = myGraph.Event()
342 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
343 aStatus,aNode,anEvent,aState = myGraph.Event()
344 print "myGraph.IsDone() = ",myGraph.IsDone()
346 # Wait for Completion (but it is already done after event loop ...)
347 print "Done : ",myGraph.DoneW()
350 #print "Type : print myGraph.IsDone()"
351 #print " If execution is finished ==> 1 (true)"
354 raise RuntimeError, "myGraph.Run() is not done"
357 print "Type : myGraph.PrintPorts()"
358 print " to see input and output values of the graph"
361 # Export will create newsupervisionexample.xml and the corresponding .py file
362 tmpdir=os.getenv("TmpDir")
365 file = tmpdir + "/newsupervisionexample"
366 print "--------------\n"+file+"\n--------------\n"
369 ior = salome.orb.object_to_string(myGraph.G)
372 GraphName = myGraph.Name()
374 #nodes = myGraph.Nodes()
375 nodes = myGraph.G.Nodes().FNodes
376 length_bs = len(nodes)
377 print "ListOfNodes length = ", length_bs
380 names.append(node.Name())
384 GraphInLines = StreamGraph( 'GraphInLines' )
385 GraphInLines.SetName( 'GraphInLines' )
386 GraphInLines.SetAuthor( '' )
387 GraphInLines.SetComment( '' )
388 GraphInLines.Coords( 0 , 0 )
390 # Creation of InLine Nodes
392 PyAdd.append( 'def Add(a,b) : ' )
393 PyAdd.append( ' return a+b ' )
395 Add = GraphInLines.INode( 'Add' , PyAdd )
396 Add.InPort( 'a' , 'long' )
397 Add.InPort( 'b' , 'long' )
398 Add.OutPort( 'f' , 'long' )
401 Add.SetComment( 'Python function' )
402 Add.Coords( 351 , 77 )
404 PySub.append( 'def Sub(a,b) : ' )
405 PySub.append( ' return a-b ' )
407 Sub = GraphInLines.INode( 'Sub' , PySub )
408 Sub.InPort( 'a' , 'long' )
409 Sub.InPort( 'b' , 'long' )
410 Sub.OutPort( 'f' , 'long' )
413 Sub.SetComment( 'Python function' )
414 Sub.Coords( 86 , 333 )
416 PyMul.append( 'def Mul(a,b) : ' )
417 PyMul.append( ' return a*b ' )
418 Mul = GraphInLines.INode( 'Mul' , PyMul )
419 Mul.InPort( 'a' , 'long' )
420 Mul.InPort( 'b' , 'long' )
421 Mul.OutPort( 'Result' , 'long' )
424 Mul.SetComment( 'Python function' )
425 Mul.Coords( 616 , 247 )
427 # Creation of intermediate Output variables and of Control Links
428 Addf = Add.Port( 'f' )
429 Mula = GraphInLines.Link( Addf , Mul.Port( 'a' ) )
430 Mula.AddCoord( 1 , 570 , 356 )
431 Mula.AddCoord( 2 , 570 , 186 )
432 Subf = Sub.Port( 'f' )
433 Mulb = GraphInLines.Link( Subf , Mul.Port( 'b' ) )
434 Mulb.AddCoord( 1 , 282 , 376 )
435 Mulb.AddCoord( 2 , 282 , 442 )
436 Addb = GraphInLines.Link( Subf , Add.Port( 'b' ) )
437 Addb.AddCoord( 1 , 283 , 209 )
438 Addb.AddCoord( 2 , 283 , 374 )
439 Addb.AddCoord( 3 , 283 , 442 )
441 # Creation of Input datas
442 Adda = Add.Input( 'a' , 1)
443 Suba = Sub.Input( 'a' , 3)
444 Subb = Sub.Input( 'b' , 4)
446 # Creation of Output variables
447 MulResult = Mul.Port( 'Result' )
453 GraphInLines.PrintPorts()
455 sg.updateObjBrowser(1);
460 print "============= Test VISU and MED ============================="
463 comp = catalog.GetComponent("VISU")
465 raise RuntimeError,"Component VISU not found in Module Catalog."
475 medFileName = "pointe.med"
476 medFile = os.getenv('DATA_DIR') + '/MedFiles/' + medFileName
477 print "Load ", medFile
479 studyCurrent = salome.myStudyName
481 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
482 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
485 if os.access(medFile, os.R_OK) :
486 if not os.access(medFile, os.W_OK) :
488 if sys.platform != "win32":
491 tmpDir = os.getenv('TEMP') + '/'
492 medFileNew = tmpDir + str(random.randint(0,1000000)) + "_" + medFileName
493 print " -- Copy " + medFile + " to " + medFileNew
495 if sys.platform != "win32":
498 copyCommand = "copy /Y"
499 os.system(copyCommand + " " + medFile + " " + medFileNew)
502 os.system("chmod 755 " + medFile)
504 if os.access(medFile, os.W_OK) :
505 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
506 med_obj = visu_gui.visu.getMedObjectFromStudy()
507 print "med_obj - ", med_obj
509 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
514 myResult1 = myVisu.ImportMedField(myField1)
515 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
517 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
519 myResult2 = myVisu.ImportFile(medFile);
520 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
523 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
525 sg.updateObjBrowser(0)
526 else : print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
527 else : print "We have no permission to read medFile, it will not be opened";
530 if sys.exc_type == SALOME.SALOME_Exception :
531 print "There is no permission to read " + medFile
535 print sys.exc_traceback
537 sg.updateObjBrowser(1);