2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # SALOME SALOME_SWIG : binding of C++ implementation and Python
25 # File : salome_test.py
28 print "Test the application loading GEOM, SMESH, VISU, MED, components and doing some"
29 print "operation within the components."
37 import SALOME_ModuleCatalog
39 print "======================================================================"
40 print " Check, that there is no data of MED component in the Study "
41 print "======================================================================"
43 MedComp = salome.myStudy.FindComponent("MED")
44 if MedComp is not None:
46 print "This script cannot work properly, because there are"
47 print "some MED component data already exists in the study."
48 print "Execution aborted."
50 raise RuntimeError, "Please, run this script only in a new empty study."
52 print "======================================================================"
54 print "======================================================================"
55 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
56 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
58 print "======================================================================"
59 print " Create Study "
60 print "======================================================================"
62 comp = catalog.GetComponent("GEOM")
64 raise RuntimeError,"Component GEOM not found in Module Catalog."
68 print "================================="
69 print " create AttributeReal "
70 print "================================="
71 A = geompy.myBuilder.FindOrCreateAttribute(geompy.father, "AttributeReal")
73 raise RuntimeError, "Can't create AttributeReal attribute"
74 A = A._narrow(SALOMEDS.AttributeReal)
76 if A.Value() != 0.0001:
77 raise RuntimeError, "Error : wrong value of AttributeReal"
80 print " =========== Test Geometry =========================="
83 print "==================================="
85 print "==================================="
87 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
88 idbox = geompy.addToStudy(box,"box")
91 print "============= Test SMESH ============================="
96 comp = catalog.GetComponent("SMESH")
98 raise RuntimeError,"Component SMESH not found in Module Catalog."
100 comp = catalog.GetComponent("MED")
102 raise RuntimeError,"Component MED not found in Module Catalog."
106 geom = salome.lcc.FindOrLoadComponent("FactoryServer", "GEOM")
107 myBuilder = salome.myStudy.NewBuilder()
109 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
110 smeshgui = salome.ImportComponentGUI("SMESH")
111 smeshgui.Init(salome.myStudyId);
113 ShapeTypeCompSolid = 1
123 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
124 idbox = geompy.addToStudy(box,"box")
126 # ---- add first face of box in study
128 subShapeList=geompy.SubShapeAll(box,ShapeTypeFace)
130 name = geompy.SubShapeName(face, box)
132 idface=geompy.addToStudyInFather(box,face,name)
134 # ---- add shell from box in study
136 subShellList=geompy.SubShapeAll(box,ShapeTypeShell)
137 shell = subShellList[0]
138 name = geompy.SubShapeName(shell, box)
140 idshell=geompy.addToStudyInFather(box,shell,name)
142 # ---- add first edge of face in study
144 edgeList = geompy.SubShapeAll(face,ShapeTypeEdge)
146 name = geompy.SubShapeName(edge, face)
148 idedge=geompy.addToStudyInFather(face,edge,name)
153 # ---- create Hypothesis
155 if sys.platform == "win32":
156 stdMeshersEngine = "StdMeshersEngine"
158 stdMeshersEngine = "libStdMeshersEngine.so"
160 print "-------------------------- create Hypothesis"
161 print "-------------------------- LocalLength"
162 hypLen1 = smesh.CreateHypothesis( "LocalLength", stdMeshersEngine )
163 hypLen1.SetLength(100)
164 print hypLen1.GetName()
165 print hypLen1.GetId()
166 print hypLen1.GetLength()
168 smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
170 print "-------------------------- NumberOfSegments"
171 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", stdMeshersEngine )
172 hypNbSeg1.SetNumberOfSegments(7)
173 print hypNbSeg1.GetName()
174 print hypNbSeg1.GetId()
175 print hypNbSeg1.GetNumberOfSegments()
177 smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
179 print "-------------------------- MaxElementArea"
180 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
181 hypArea1.SetMaxElementArea(2500)
182 print hypArea1.GetName()
183 print hypArea1.GetId()
184 print hypArea1.GetMaxElementArea()
186 smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
188 print "-------------------------- MaxElementArea"
189 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
190 hypArea2.SetMaxElementArea(500)
191 print hypArea2.GetName()
192 print hypArea2.GetId()
193 print hypArea2.GetMaxElementArea()
195 smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
197 print "-------------------------- Regular_1D"
198 algoReg = smesh.CreateHypothesis( "Regular_1D", stdMeshersEngine )
199 listHyp=algoReg.GetCompatibleHypothesis()
202 print algoReg.GetName()
203 print algoReg.GetId()
205 smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
207 print "-------------------------- MEFISTO_2D"
208 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", stdMeshersEngine )
209 listHyp=algoMef.GetCompatibleHypothesis()
212 print algoMef.GetName()
213 print algoMef.GetId()
215 smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
217 # ---- add hypothesis to box
219 print "-------------------------- add hypothesis to box"
220 box=salome.IDToObject(idbox)
221 mesh = smesh.CreateMesh(box)
223 smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
225 ret=mesh.AddHypothesis(box,algoReg)
227 ret=mesh.AddHypothesis(box,algoMef)
231 ret=mesh.AddHypothesis(box,hypNbSeg1)
233 ret=mesh.AddHypothesis(box,hypArea1)
237 # ---- add hypothesis to edge
239 print "-------------------------- add hypothesis to edge"
240 edge=salome.IDToObject(idedge)
241 submesh=mesh.GetSubMesh(edge, "SubMeshEdge")
243 ret=mesh.AddHypothesis(edge,algoReg)
245 ret=mesh.AddHypothesis(edge,hypLen1)
248 print "-------------------------- add hypothesis to face"
249 face=salome.IDToObject(idface)
250 submesh = mesh.GetSubMesh(face, "SubMeshFace")
252 ret=mesh.AddHypothesis(face,hypArea2)
255 smesh.Compute(mesh, box)
256 sg.updateObjBrowser(1);
258 #####################################################################
259 # SUPERVISOR module is not available since SALOME 5.0 version
260 #####################################################################
262 print "============= Test Supervisor ============================="
264 import salome_version
265 versions = salome_version.getVersions()
267 # SUPERV module is avaiable
268 comp = catalog.GetComponent("SUPERV")
270 raise RuntimeError,"Component SUPERV not found in Module Catalog."
274 myStudy = salome.myStudy
275 myBuilder = myStudy.NewBuilder()
277 SuperVision = lcc.FindOrLoadComponent("SuperVisionContainer","SUPERV")
278 father = myStudy.FindComponent("SUPERV")
280 father = myBuilder.NewComponent("SUPERV")
281 A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
282 FName = A1._narrow(SALOMEDS.AttributeName)
283 FName.SetValue( salome.sg.getComponentUserName("SUPERV") )
284 A2 = myBuilder.FindOrCreateAttribute(father, "AttributePixMap");
285 aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
286 aPixmap.SetPixMap( "ICON_OBJBROWSER_Supervision" );
287 myBuilder.DefineComponentInstance(father,SuperVision)
290 dataflow = SuperVision.getStreamGraph(ior)
292 itr = myStudy.NewChildIterator(father)
295 res,A=item.FindAttribute("AttributeName")
297 aName = A._narrow(SALOMEDS.AttributeName)
298 if aName.Value() == name :
299 print myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
300 A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
303 #res,A = myBuilder.FindOrCreateAttribute(item, "AttributeIOR")
304 anIOR = A._narrow(SALOMEDS.AttributeIOR);
305 print "anIOR.SetValue(dataflow.getIOR())"
306 anIOR.SetValue(dataflow.getIOR())
309 obj = myBuilder.NewObject(father)
310 A=myBuilder.FindOrCreateAttribute(obj, "AttributeName")
311 aName=A._narrow(SALOMEDS.AttributeName)
313 A=myBuilder.FindOrCreateAttribute(obj, "AttributeIOR")
314 anIOR = A._narrow(SALOMEDS.AttributeIOR)
315 anIOR.SetValue(dataflow.getIOR())
318 dir= os.getenv("DATA_DIR")
320 raise RuntimeError, "DATA_DIR is not defined"
321 xmlfile = dir + "/Superv/Graphs/GraphGeomEssaiGates.xml"
322 print "Load dataflow from the file : "
326 myGraph = StreamGraph ( xmlfile )
328 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
329 print "myGraph.IsValid() = ", myGraph.IsValid()
334 # This DataFlow is "executable" : all pending Ports are defined with Datas
335 print myGraph.IsExecutable()
337 # Starts only execution of that DataFlow and gets control immediatly
340 # That DataFlow is running ==> 0 (false)
341 print myGraph.IsDone()
343 # Events of execution :
344 aStatus,aNode,anEvent,aState = myGraph.Event()
346 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
347 aStatus,aNode,anEvent,aState = myGraph.Event()
348 print "myGraph.IsDone() = ",myGraph.IsDone()
350 # Wait for Completion (but it is already done after event loop ...)
351 print "Done : ",myGraph.DoneW()
354 #print "Type : print myGraph.IsDone()"
355 #print " If execution is finished ==> 1 (true)"
358 raise RuntimeError, "myGraph.Run() is not done"
361 print "Type : myGraph.PrintPorts()"
362 print " to see input and output values of the graph"
365 # Export will create newsupervisionexample.xml and the corresponding .py file
366 tmpdir=os.getenv("TmpDir")
369 file = tmpdir + "/newsupervisionexample"
370 print "--------------\n"+file+"\n--------------\n"
373 ior = salome.orb.object_to_string(myGraph.G)
376 GraphName = myGraph.Name()
378 #nodes = myGraph.Nodes()
379 nodes = myGraph.G.Nodes().FNodes
380 length_bs = len(nodes)
381 print "ListOfNodes length = ", length_bs
384 names.append(node.Name())
388 GraphInLines = StreamGraph( 'GraphInLines' )
389 GraphInLines.SetName( 'GraphInLines' )
390 GraphInLines.SetAuthor( '' )
391 GraphInLines.SetComment( '' )
392 GraphInLines.Coords( 0 , 0 )
394 # Creation of InLine Nodes
396 PyAdd.append( 'def Add(a,b) : ' )
397 PyAdd.append( ' return a+b ' )
399 Add = GraphInLines.INode( 'Add' , PyAdd )
400 Add.InPort( 'a' , 'long' )
401 Add.InPort( 'b' , 'long' )
402 Add.OutPort( 'f' , 'long' )
405 Add.SetComment( 'Python function' )
406 Add.Coords( 351 , 77 )
408 PySub.append( 'def Sub(a,b) : ' )
409 PySub.append( ' return a-b ' )
411 Sub = GraphInLines.INode( 'Sub' , PySub )
412 Sub.InPort( 'a' , 'long' )
413 Sub.InPort( 'b' , 'long' )
414 Sub.OutPort( 'f' , 'long' )
417 Sub.SetComment( 'Python function' )
418 Sub.Coords( 86 , 333 )
420 PyMul.append( 'def Mul(a,b) : ' )
421 PyMul.append( ' return a*b ' )
422 Mul = GraphInLines.INode( 'Mul' , PyMul )
423 Mul.InPort( 'a' , 'long' )
424 Mul.InPort( 'b' , 'long' )
425 Mul.OutPort( 'Result' , 'long' )
428 Mul.SetComment( 'Python function' )
429 Mul.Coords( 616 , 247 )
431 # Creation of intermediate Output variables and of Control Links
432 Addf = Add.Port( 'f' )
433 Mula = GraphInLines.Link( Addf , Mul.Port( 'a' ) )
434 Mula.AddCoord( 1 , 570 , 356 )
435 Mula.AddCoord( 2 , 570 , 186 )
436 Subf = Sub.Port( 'f' )
437 Mulb = GraphInLines.Link( Subf , Mul.Port( 'b' ) )
438 Mulb.AddCoord( 1 , 282 , 376 )
439 Mulb.AddCoord( 2 , 282 , 442 )
440 Addb = GraphInLines.Link( Subf , Add.Port( 'b' ) )
441 Addb.AddCoord( 1 , 283 , 209 )
442 Addb.AddCoord( 2 , 283 , 374 )
443 Addb.AddCoord( 3 , 283 , 442 )
445 # Creation of Input datas
446 Adda = Add.Input( 'a' , 1)
447 Suba = Sub.Input( 'a' , 3)
448 Subb = Sub.Input( 'b' , 4)
450 # Creation of Output variables
451 MulResult = Mul.Port( 'Result' )
457 GraphInLines.PrintPorts()
459 sg.updateObjBrowser(1);
463 # SUPERV module is NOT avaiable
464 print "WARNING! Supervisor is not avaiable in this version of SALOME!"
468 print "============= Test VISU and MED ============================="
471 comp = catalog.GetComponent("VISU")
473 raise RuntimeError,"Component VISU not found in Module Catalog."
483 medFileName = "pointe.med"
484 if sys.platform != "win32":
485 medFile = os.getenv('DATA_DIR') + '/MedFiles/' + medFileName
487 medFile = os.getenv('DATA_DIR') + '\\MedFiles\\' + medFileName
489 print "Load ", medFile
491 studyCurrent = salome.myStudyName
493 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
494 myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
497 if os.access(medFile, os.R_OK) :
498 if not os.access(medFile, os.W_OK) :
500 if sys.platform != "win32":
503 tmpDir = os.getenv('TEMP') + '\\'
504 medFileNew = tmpDir + str(random.randint(0,1000000)) + "_" + medFileName
505 print " -- Copy " + medFile + " to " + medFileNew
507 if sys.platform != "win32":
510 copyCommand = "copy /Y"
511 os.system(copyCommand + " " + medFile + " " + medFileNew)
514 if sys.platform != "win32":
515 os.system("chmod 755 " + medFile)
518 if os.access(medFile, os.W_OK) :
519 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
520 med_obj = visu_gui.visu.getMedObjectFromStudy()
521 print "med_obj - ", med_obj
523 myField1 = visu_gui.visu.getFieldObjectFromStudy(2,1)
528 myResult1 = myVisu.ImportMedField(myField1)
529 aMesh1 = myVisu.MeshOnEntity(myResult1, aMeshName, anEntity);
531 aScalarMap1= myVisu.ScalarMapOnField(myResult1, aMeshName, anEntity, myField1.getName(), aTimeStampId)
533 myResult2 = myVisu.ImportFile(medFile);
534 aMesh2 = myVisu.MeshOnEntity(myResult2, aMeshName, anEntity);
537 aScalarMap2= myVisu.ScalarMapOnField(myResult2, aMeshName, anEntity, myField1.getName(), aTimeStampId)
539 sg.updateObjBrowser(0)
540 else : print "We have no permission to rewrite medFile, so readStructFileWithFieldType can't open this file";
541 else : print "We have no permission to read medFile, it will not be opened";
544 if sys.exc_type == SALOME.SALOME_Exception :
545 print "There is no permission to read " + medFile
549 print sys.exc_traceback
551 sg.updateObjBrowser(1);