Back to index

salome-kernel  6.5.0
salome_test.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 print
00025 print "Perform quick test of the application by loading of the GEOM, SMESH, VISU, MED"
00026 print "components and doing some operation within the components."
00027 print
00028 
00029 import salome
00030 import SALOME
00031 import SALOMEDS
00032 import os
00033 import sys
00034 import SALOME_ModuleCatalog
00035 
00036 step = 1
00037 
00038 print "======================================================================"
00039 print "           %d. Initialize study " % step; step+=1
00040 print "======================================================================"
00041 
00042 # initialize study
00043 salome.salome_init()
00044 # get study builder
00045 builder = salome.myStudy.NewBuilder()
00046 print "OK"
00047 
00048 print 
00049 
00050 print "======================================================================"
00051 print "           %d. Retrieve module catalog " % step; step+=1
00052 print "======================================================================"
00053 
00054 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
00055 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
00056 if not catalog:
00057     raise RuntimeError, "Can't accesss module catalog"
00058 print "OK"
00059 
00060 print
00061 
00062 print "======================================================================"
00063 print "           %d. Check modules availability in the module catalog " % step; step+=1
00064 print "======================================================================"
00065 
00066 print
00067 print "--- Check GEOM ..."
00068 comp = catalog.GetComponent("GEOM")
00069 if not comp:
00070     raise RuntimeError, "Component GEOM is not found in Module Catalog."
00071 print "OK"
00072 
00073 print
00074 print "--- Check SMESH ..."
00075 comp = catalog.GetComponent("SMESH")
00076 if not comp:
00077     raise RuntimeError, "Component SMESH is not found in Module Catalog."
00078 print "OK"
00079 
00080 print
00081 print "--- Check MED ..."
00082 comp = catalog.GetComponent("MED")
00083 if not comp:
00084     raise RuntimeError, "Component MED is not found in Module Catalog."
00085 print "OK"
00086 
00087 print
00088 print "--- Check VISU ..."
00089 comp = catalog.GetComponent("VISU")
00090 if not comp:
00091     raise RuntimeError, "Component VISU is not found in Module Catalog."
00092 print "OK"
00093 
00094 print
00095 
00096 print "======================================================================"
00097 print "           %d. Test Data Server " % step; step+=1
00098 print "======================================================================"
00099 
00100 print
00101 print "--- Create new component ..."
00102 comp = builder.NewComponent("TEST")
00103 if not comp:
00104     raise RuntimeError, "Can't create new component"
00105 print "OK"
00106 
00107 print
00108 print "--- Create AttributeName ..."
00109 A = builder.FindOrCreateAttribute(comp, "AttributeName")
00110 if not A:
00111     raise RuntimeError, "Can't create AttributeName attribute"
00112 A.SetValue("TEST")
00113 if A.Value() != "TEST":
00114     raise RuntimeError, "Error : wrong value of  AttributeName"
00115 print "OK"
00116 
00117 print
00118 print "--- Create AttributeReal ..."
00119 A = builder.FindOrCreateAttribute(comp, "AttributeReal")
00120 if not A:
00121     raise RuntimeError, "Can't create AttributeReal attribute"
00122 A.SetValue(0.0001)
00123 if A.Value() != 0.0001:
00124     raise RuntimeError, "Error : wrong value of  AttributeReal"
00125 print "OK"
00126 
00127 print
00128 
00129 print "======================================================================"
00130 print "           %d. Test Geometry " % step; step+=1
00131 print "======================================================================"
00132 
00133 import geompy
00134 
00135 ShapeTypeCompSolid = 1
00136 ShapeTypeSolid = 2
00137 ShapeTypeShell = 3
00138 ShapeTypeFace = 4
00139 ShapeTypeWire = 5
00140 ShapeTypeEdge = 6
00141 ShapeTypeVertex = 7
00142 
00143 print
00144 print "--- Create a box ..."
00145 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
00146 idbox = geompy.addToStudy(box, "box")
00147 box_obj = salome.myStudy.FindObjectByPath("/Geometry/box")
00148 if not box_obj:
00149     raise RuntimeError, "Error : wrong value of  AttributeReal"
00150 print "OK"
00151 
00152 # ---- add shell from box in study
00153 print 
00154 print "--- Extract shell ..."
00155 subShellList = geompy.SubShapeAll(box, ShapeTypeShell)
00156 shell = subShellList[0]
00157 name = geompy.SubShapeName(shell, box)
00158 idshell = geompy.addToStudyInFather(box, shell, name)
00159 print name
00160 print "OK"
00161 
00162 # ---- add first face of box in study
00163 print 
00164 print "--- Extract face ..."
00165 subShapeList = geompy.SubShapeAll(box, ShapeTypeFace)
00166 face = subShapeList[0]
00167 name = geompy.SubShapeName(face, box)
00168 idface = geompy.addToStudyInFather(box, face, name)
00169 print name
00170 print "OK"
00171 
00172 # ---- add first edge of face in study
00173 print 
00174 print "--- Extract edge ..."
00175 edgeList = geompy.SubShapeAll(face, ShapeTypeEdge)
00176 edge = edgeList[0];
00177 name = geompy.SubShapeName(edge, face)
00178 idedge = geompy.addToStudyInFather(face, edge, name)
00179 print name
00180 print "OK"
00181 
00182 # ---- update object browser
00183 if salome.hasDesktop():
00184     salome.sg.updateObjBrowser(1);
00185 
00186 print
00187 
00188 print "======================================================================"
00189 print "           %d. Test Mesh " % step; step+=1
00190 print "======================================================================"
00191 
00192 import StdMeshers
00193 import SMESH
00194 
00195 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
00196 if salome.hasDesktop():
00197     smeshgui = salome.ImportComponentGUI("SMESH")
00198     smeshgui.Init(salome.myStudyId);
00199 else:
00200     smesh.SetCurrentStudy(salome.myStudy)
00201 
00202 # ---- create hypotheses 
00203 
00204 if sys.platform == "win32":
00205   stdMeshersEngine = "StdMeshersEngine"
00206 else:
00207   stdMeshersEngine = "libStdMeshersEngine.so"
00208 
00209 print
00210 print "--- Create hypotheses ..."
00211 
00212 print
00213 print "------ LocalLength ..."
00214 hypLen1 = smesh.CreateHypothesis( "LocalLength", stdMeshersEngine )
00215 hypLen1.SetLength(100)
00216 print hypLen1.GetName()
00217 print hypLen1.GetId()
00218 print hypLen1.GetLength()
00219 if salome.hasDesktop():
00220     smeshgui.SetName(salome.ObjectToID(hypLen1), "Local_Length_100")
00221 print "OK"
00222 
00223 print
00224 print "------ NumberOfSegments ..."
00225 hypNbSeg1= smesh.CreateHypothesis( "NumberOfSegments", stdMeshersEngine )
00226 hypNbSeg1.SetNumberOfSegments(7)
00227 print hypNbSeg1.GetName()
00228 print hypNbSeg1.GetId()
00229 print hypNbSeg1.GetNumberOfSegments()
00230 if salome.hasDesktop():
00231     smeshgui.SetName(salome.ObjectToID(hypNbSeg1), "NumberOfSegments_7")
00232 print "OK"
00233 
00234 print
00235 print "------ MaxElementArea [1] ..."
00236 hypArea1 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
00237 hypArea1.SetMaxElementArea(2500)
00238 print hypArea1.GetName()
00239 print hypArea1.GetId()
00240 print hypArea1.GetMaxElementArea()
00241 if salome.hasDesktop():
00242     smeshgui.SetName(salome.ObjectToID(hypArea1), "MaxElementArea_2500")
00243 print "OK"
00244 
00245 print
00246 print "------ MaxElementArea [2] ..."
00247 hypArea2 = smesh.CreateHypothesis( "MaxElementArea", stdMeshersEngine )
00248 hypArea2.SetMaxElementArea(500)
00249 print hypArea2.GetName()
00250 print hypArea2.GetId()
00251 print hypArea2.GetMaxElementArea()
00252 if salome.hasDesktop():
00253     smeshgui.SetName(salome.ObjectToID(hypArea2), "MaxElementArea_500")
00254 print "OK"
00255 
00256 # ---- create algorithms
00257 
00258 print
00259 print "--- Create algorithms ..."
00260 
00261 print
00262 print "------ Regular_1D ..."
00263 algoReg = smesh.CreateHypothesis( "Regular_1D", stdMeshersEngine )
00264 listHyp = algoReg.GetCompatibleHypothesis()
00265 for hyp in listHyp:
00266     print hyp
00267 print algoReg.GetName()
00268 print algoReg.GetId()
00269 if salome.hasDesktop():
00270     smeshgui.SetName(salome.ObjectToID(algoReg), "Regular_1D" )
00271 print "OK"
00272 
00273 print
00274 print "------ MEFISTO_2D ..."
00275 algoMef = smesh.CreateHypothesis( "MEFISTO_2D", stdMeshersEngine )
00276 listHyp=algoMef.GetCompatibleHypothesis()
00277 for hyp in listHyp:
00278     print hyp
00279 print algoMef.GetName()
00280 print algoMef.GetId()
00281 if salome.hasDesktop():
00282     smeshgui.SetName(salome.ObjectToID(algoMef), "MEFISTO_2D" )
00283 print "OK"
00284 
00285 # ---- create mesh on the box, apply hypotheses / algorithms
00286 
00287 print
00288 print "--- Create mesh on the box ..."
00289 mesh = smesh.CreateMesh(box)
00290 if salome.hasDesktop():
00291     smeshgui.SetName( salome.ObjectToID(mesh), "MeshBox" );
00292 ret = mesh.AddHypothesis(box, algoReg)
00293 ret = mesh.AddHypothesis(box, algoMef)
00294 ret = mesh.AddHypothesis(box, hypNbSeg1)
00295 ret = mesh.AddHypothesis(box, hypArea1)
00296 print "OK"
00297 
00298 # ---- create submesh on the edge, add hypothesis
00299 
00300 print
00301 print "--- Add 1D sub-mesh on the edge ..."
00302 submesh = mesh.GetSubMesh(edge, "SubMeshEdge")
00303 ret = mesh.AddHypothesis(edge, algoReg)
00304 ret = mesh.AddHypothesis(edge, hypLen1)
00305 print "OK"
00306 
00307 # ---- create submesh on the edge, add hypothesis
00308 
00309 print
00310 print "--- Add 2D sub-mesh on the face ..."
00311 submesh = mesh.GetSubMesh(face, "SubMeshFace")
00312 ret = mesh.AddHypothesis(face, hypArea2)
00313 print "OK"
00314 
00315 # ---- compute mesh
00316 print
00317 print "--- Compute mesh ..."
00318 smesh.Compute(mesh, box)
00319 print "OK"
00320 
00321 # ---- update object browser
00322 if salome.hasDesktop():
00323     salome.sg.updateObjBrowser(1);
00324 
00325 print
00326 
00327 print "======================================================================"
00328 print "           %d. Test Med " % step; step+=1
00329 print "======================================================================"
00330 
00331 import SALOME_MED
00332 
00333 medFileName = "pointe.med"
00334 medFile = os.path.join(os.getenv('DATA_DIR'), 'MedFiles', medFileName)
00335 
00336 med_comp = salome.myStudy.FindComponent("MED")
00337 
00338 if not med_comp:
00339     med  = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
00340 
00341     print
00342     print "--- Read med file structure from %s ..." % medFile
00343     med.readStructFileWithFieldType(medFile, salome.myStudyName)
00344     print "OK"
00345     pass
00346 else:
00347     print
00348     print "This script cannot work properly, because there is"
00349     print "some MED component data already existing in the study."
00350     print "Execution aborted."
00351     print
00352     print "Skipping test for MED..."
00353     pass
00354 
00355 print
00356 
00357 print "======================================================================"
00358 print "           %d. Test Post-Pro " % step; step+=1
00359 print "======================================================================"
00360 
00361 import VISU
00362 
00363 aMeshName = "maa1"
00364 anEntity = VISU.NODE
00365 field_name = "fieldnodedouble"
00366 
00367 if salome.hasDesktop(): # in gui mode
00368     
00369     import visu_gui
00370     visu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
00371 
00372     if not med_comp:
00373         
00374         print
00375         print "--- Get med object from study ..."
00376         med_obj = visu_gui.visu.getMedObjectFromStudy()
00377         if not med_obj:
00378             raise RuntimeError, "Med object is not found in the study"
00379         print "OK"
00380 
00381         print
00382         print "--- Get field from study ..."
00383         field = visu_gui.visu.getFieldObjectFromStudy(3,1)
00384         if not field:
00385             raise RuntimeError, "Field object is not found in the study"
00386         print "OK"
00387 
00388         print
00389         print "--- Import field to the VISU ..."
00390         aTimeStampId = -1
00391         result1 = visu.ImportMedField(field)
00392         if not result1:
00393             raise RuntimeError, "Can't import field"
00394         print "OK"
00395 
00396         print
00397         print "--- Create mesh presentation ..."
00398         mesh1 = visu.MeshOnEntity(result1, aMeshName, anEntity);
00399         if not mesh1:
00400             raise RuntimeError, "Can't create mesh presentation"
00401         print "OK"
00402 
00403         print
00404         print "--- Create scalar map ..."
00405         scalarMap1 = visu.ScalarMapOnField(result1, aMeshName, anEntity, field_name, aTimeStampId)
00406         if not scalarMap1:
00407             raise RuntimeError, "Can't create scalar map"
00408         print "OK"
00409 
00410         pass # if not med_comp
00411 
00412     print
00413     print "--- Import med file %s to the VISU ..." % medFile
00414     result2 = visu.ImportFile(medFile);
00415     if not result2:
00416         raise RuntimeError, "Can't import file"
00417     print "OK"
00418 
00419     print
00420     print "--- Create mesh presentation ..."
00421     mesh2 = visu.MeshOnEntity(result2, aMeshName, anEntity);
00422     if not mesh2:
00423         raise RuntimeError, "Can't create mesh presentation"
00424     print "OK"
00425 
00426     print
00427     print "--- Create scalar map ..."
00428     scalarMap2 = visu.ScalarMapOnField(result2, aMeshName, anEntity, field_name, 3)
00429     if not scalarMap2:
00430         raise RuntimeError, "Can't create scalar map"
00431     print "OK"
00432     pass
00433 
00434 else: # not in gui mode, visu can not be tested
00435     
00436     print
00437     print "VISU module requires SALOME to be running in GUI mode."
00438     print
00439     print "Skipping test for VISU..."
00440     pass
00441 
00442 # ---- update object browser
00443 if salome.hasDesktop():
00444     salome.sg.updateObjBrowser(1);