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 #####################################################################
257 # SUPERVISOR module is not available since SALOME 5.0 version
258 #####################################################################
260 print "============= Test Supervisor ============================="
262 import salome_version
263 versions = salome_version.getVersions()
265 # SUPERV module is avaiable
266 comp = catalog.GetComponent("SUPERV")
268 raise RuntimeError,"Component SUPERV not found in Module Catalog."
272 myStudy = salome.myStudy
273 myBuilder = myStudy.NewBuilder()
275 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
276 father = myStudy.FindComponent("SUPERV")
278 father = myBuilder.NewComponent("SUPERV")
279 A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
280 FName = A1._narrow(SALOMEDS.AttributeName)
281 FName.SetValue( salome.sg.getComponentUserName("SUPERV") )
282 A2 = myBuilder.FindOrCreateAttribute(father, "AttributePixMap");
283 aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
284 aPixmap.SetPixMap( "ICON_OBJBROWSER_Supervision" );
285 myBuilder.DefineComponentInstance(father,SuperVision)
288 dataflow = SuperVision.getStreamGraph(ior)
290 itr = myStudy.NewChildIterator(father)
293 res,A=item.FindAttribute("AttributeName")
295 aName = A._narrow(SALOMEDS.AttributeName)
296 if aName.Value() == name :
297 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
298 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
301 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
302 anIOR = A._narrow(SALOMEDS.AttributeIOR);
303 print "anIOR.SetValue(dataflow.getIOR())"
304 anIOR.SetValue(dataflow.getIOR())
307 obj = myBuilder.NewObject(father)
308 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
309 aName=A._narrow(SALOMEDS.AttributeName)
311 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
312 anIOR = A._narrow(SALOMEDS.AttributeIOR)
313 anIOR.SetValue(dataflow.getIOR())
316 dir= os.getenv("DATA_DIR")
318 raise RuntimeError, "DATA_DIR is not defined"
319 xmlfile = dir + "/Superv/Graphs/GraphGeomEssaiGates.xml"
320 print "Load dataflow from the file : "
324 myGraph = StreamGraph ( xmlfile )
326 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
327 print "myGraph.IsValid() = ", myGraph.IsValid()
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 "Type : print myGraph.IsDone()"
353 #print " If execution is finished ==> 1 (true)"
356 raise RuntimeError, "myGraph.Run() is not done"
359 print "Type : myGraph.PrintPorts()"
360 print " to see input and output values of the graph"
363 # Export will create newsupervisionexample.xml and the corresponding .py file
364 tmpdir=os.getenv("TmpDir")
367 file = tmpdir + "/newsupervisionexample"
368 print "--------------\n"+file+"\n--------------\n"
371 ior = salome.orb.object_to_string(myGraph.G)
374 GraphName = myGraph.Name()
376 #nodes = myGraph.Nodes()
377 nodes = myGraph.G.Nodes().FNodes
378 length_bs = len(nodes)
379 print "ListOfNodes length = ", length_bs
382 names.append(node.Name())
386 GraphInLines = StreamGraph( 'GraphInLines' )
387 GraphInLines.SetName( 'GraphInLines' )
388 GraphInLines.SetAuthor( '' )
389 GraphInLines.SetComment( '' )
390 GraphInLines.Coords( 0 , 0 )
392 # Creation of InLine Nodes
394 PyAdd.append( 'def Add(a,b) : ' )
395 PyAdd.append( ' return a+b ' )
397 Add = GraphInLines.INode( 'Add' , PyAdd )
398 Add.InPort( 'a' , 'long' )
399 Add.InPort( 'b' , 'long' )
400 Add.OutPort( 'f' , 'long' )
403 Add.SetComment( 'Python function' )
404 Add.Coords( 351 , 77 )
406 PySub.append( 'def Sub(a,b) : ' )
407 PySub.append( ' return a-b ' )
409 Sub = GraphInLines.INode( 'Sub' , PySub )
410 Sub.InPort( 'a' , 'long' )
411 Sub.InPort( 'b' , 'long' )
412 Sub.OutPort( 'f' , 'long' )
415 Sub.SetComment( 'Python function' )
416 Sub.Coords( 86 , 333 )
418 PyMul.append( 'def Mul(a,b) : ' )
419 PyMul.append( ' return a*b ' )
420 Mul = GraphInLines.INode( 'Mul' , PyMul )
421 Mul.InPort( 'a' , 'long' )
422 Mul.InPort( 'b' , 'long' )
423 Mul.OutPort( 'Result' , 'long' )
426 Mul.SetComment( 'Python function' )
427 Mul.Coords( 616 , 247 )
429 # Creation of intermediate Output variables and of Control Links
430 Addf = Add.Port( 'f' )
431 Mula = GraphInLines.Link( Addf , Mul.Port( 'a' ) )
432 Mula.AddCoord( 1 , 570 , 356 )
433 Mula.AddCoord( 2 , 570 , 186 )
434 Subf = Sub.Port( 'f' )
435 Mulb = GraphInLines.Link( Subf , Mul.Port( 'b' ) )
436 Mulb.AddCoord( 1 , 282 , 376 )
437 Mulb.AddCoord( 2 , 282 , 442 )
438 Addb = GraphInLines.Link( Subf , Add.Port( 'b' ) )
439 Addb.AddCoord( 1 , 283 , 209 )
440 Addb.AddCoord( 2 , 283 , 374 )
441 Addb.AddCoord( 3 , 283 , 442 )
443 # Creation of Input datas
444 Adda = Add.Input( 'a' , 1)
445 Suba = Sub.Input( 'a' , 3)
446 Subb = Sub.Input( 'b' , 4)
448 # Creation of Output variables
449 MulResult = Mul.Port( 'Result' )
455 GraphInLines.PrintPorts()
457 sg.updateObjBrowser(1);
461 # SUPERV module is NOT avaiable
462 print "WARNING! Supervisor is not avaiable in this version of SALOME!"
466 print "============= Test VISU and MED ============================="
469 comp = catalog.GetComponent("VISU")
471 raise RuntimeError,"Component VISU not found in Module Catalog."
481 medFileName = "pointe.med"
482 medFile = os.getenv('DATA_DIR') + '/MedFiles/' + medFileName
483 print "Load ", medFile
485 studyCurrent = salome.myStudyName
487 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
488 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
491 if os.access(medFile, os.R_OK) :
492 if not os.access(medFile, os.W_OK) :
494 if sys.platform != "win32":
497 tmpDir = os.getenv('TEMP') + '/'
498 medFileNew = tmpDir + str(random.randint(0,1000000)) + "_" + medFileName
499 print " -- Copy " + medFile + " to " + medFileNew
501 if sys.platform != "win32":
504 copyCommand = "copy /Y"
505 os.system(copyCommand + " " + medFile + " " + medFileNew)
508 os.system("chmod 755 " + medFile)
510 if os.access(medFile, os.W_OK) :
511 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
512 med_obj = visu_gui.visu.getMedObjectFromStudy()
513 print "med_obj - ", med_obj
515 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
520 myResult1 = myVisu.ImportMedField(myField1)
521 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
523 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
525 myResult2 = myVisu.ImportFile(medFile);
526 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
529 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
531 sg.updateObjBrowser(0)
532 else : print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
533 else : print "We have no permission to read medFile, it will not be opened";
536 if sys.exc_type == SALOME.SALOME_Exception :
537 print "There is no permission to read " + medFile
541 print sys.exc_traceback
543 sg.updateObjBrowser(1);