Back to index

salome-med  6.5.0
medcorba_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 #  MED MedCorba_Swig : binding of MED CORBA objects woth Python
00025 #  File   : medcorba_test.py
00026 #  Module : MED
00027 #
00028 import salome
00029 
00030 import SALOME_MED
00031 
00032 from libSALOME_Swig import *
00033 sg = SALOMEGUI_Swig()
00034 
00035 from libMedCorba_Swig import *
00036 from libMEDClient import *
00037 
00038 from random import *
00039 
00040 import os
00041 
00042 filePath = os.environ["MED_ROOT_DIR"]
00043 filePath = os.path.join( filePath, "share", "salome", "resources", "med" )
00044 
00045 #==============================================================================
00046 
00047 def AnalyzeField(field):
00048     name = field.getName()
00049     desc = field.getDescription()
00050     nbComp = field.getNumberOfComponents()
00051     itNum = field.getIterationNumber()
00052     ordNum = field.getOrderNumber()
00053 
00054     print "Analysis of the field ",name," with the description ",desc
00055     print "iteration number ",itNum," order Number ",ordNum
00056     print "It has ",nbComp," component(s)"
00057 
00058     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
00059     fieldSupport = field.getSupport()
00060     fieldMesh = fieldSupport.getMesh()
00061     fieldEntity = fieldSupport.getEntity()
00062     bool = fieldSupport.isOnAllElements()
00063 
00064     if bool:
00065         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
00066         if fieldEntity == SALOME_MED.MED_NODE:
00067             nbValByComp = fieldMesh.getNumberOfNodes()
00068         else:
00069             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
00070         print "and its dimension (number of values by component of the field) is ",nbValByComp
00071     else:
00072         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
00073         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
00074         print "and its dimension (number of values by component of the field) is ",nbValByComp
00075 
00076     for i in range(nbComp):
00077         compName = field.getComponentName(i+1)
00078         compUnit = field.getComponentUnit(i+1)
00079         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
00080 
00081     for i in range(nbValByComp):
00082         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
00083 
00084 #==============================================================================
00085 
00086 def getMeshObjectFromStudy(meshName):
00087     objNameInStudy = "/Med/MEDMESH/"+meshName
00088     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00089     if (mySO == None) :
00090         print "ERROR: ",objNameInStudy," cannot be found in the Study"
00091         return mySO
00092     else:
00093         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00094         obj = salome.orb.string_to_object(anAttr.Value())
00095         myObj = obj._narrow(SALOME_MED.MESH)
00096         if (myObj == None) :
00097             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
00098         return myObj
00099 
00100 #==============================================================================
00101 
00102 def getMedObjectFromStudy(file):
00103     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
00104     compNameInStudy= "MED"
00105     listOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
00106     listLength = len(listOfSO)
00107     if (listLength == 0) :
00108         print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
00109         return None
00110     elif (listLength > 1) :
00111         print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
00112         return None
00113     mySO = listOfSO[0]
00114     if (mySO == None) :
00115         print "ERROR: ",objNameInStudy," cannot be found in the Study"
00116         return mySO
00117     else:
00118         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00119         obj = salome.orb.string_to_object(anAttr.Value())
00120         myObj = obj._narrow(SALOME_MED.MED)
00121         if (myObj == None) :
00122             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
00123         return myObj
00124 
00125 #==============================================================================
00126 
00127 studyCurrent = salome.myStudyName
00128 studyCurrentId = salome.myStudyId
00129 
00130 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
00131 print ""
00132 
00133 fileName = "cube_hexa8_quad4.med"
00134 #medFile = "carre_en_quad4_seg2.med"
00135 medFile = os.path.join( filePath, fileName )
00136 
00137 print "Loading of the Med Component"
00138 print ""
00139 
00140 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
00141 
00142 print "Read file", medFile
00143 print ""
00144 
00145 import SALOME
00146 try:
00147     med_comp.readStructFileWithFieldType(medFile,studyCurrent)
00148 except SALOME.SALOME_Exception, inst:
00149     msg = "SALOME.SALOME_Exception caght, see details:"
00150     msg+="\n******* text:\t" + inst.details.text
00151     msg+="\n******* type:\t" + str(inst.details.type)
00152     msg+="\n******* where:\t" + inst.details.sourceFile + ":" + \
00153           str(inst.details.lineNumber)
00154     raise RuntimeError, msg
00155     
00156 sg.updateObjBrowser(1)
00157 
00158 med_obj = getMedObjectFromStudy(fileName)
00159 
00160 nbMeshes = med_obj.getNumberOfMeshes()
00161 
00162 nbFields = med_obj.getNumberOfFields()
00163 
00164 meshNames = med_obj.getMeshNames()
00165 
00166 print ""
00167 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
00168 print ""
00169 
00170 meshName = meshNames[0]
00171 
00172 meshcorba = getMeshObjectFromStudy(meshName)
00173 
00174 nbNodes = meshcorba.getNumberOfNodes()
00175 
00176 spaceDim = meshcorba.getSpaceDimension()
00177 
00178 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
00179 print ""
00180 
00181 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00182     nbFam = meshcorba.getNumberOfFamilies(entity)
00183     if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
00184         print "This mesh has",nbFam,"Node Family(ies)"
00185     elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
00186         print "This mesh has",nbFam,"Cell Family(ies)"
00187     elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
00188         print "This mesh has",nbFam,"Face Family(ies)"
00189     elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
00190         print "This mesh has",nbFam,"Edge Family(ies)"
00191 
00192     if nbFam > 0:
00193         for j in range(nbFam):
00194             print ""
00195             familycorba = meshcorba.getFamily(entity,j+1)
00196             familyName = familycorba.getName()
00197             familyDescription = familycorba.getDescription()
00198             familyEntity = familycorba.getEntity()
00199             familyBool = familycorba.isOnAllElements()
00200             print "  -Name:",familyName
00201             print "  -Description:",familyDescription
00202             print "  -Entity:",familyEntity
00203             familyIdentifier = familycorba.getIdentifier()
00204             nbOfAtt = familycorba.getNumberOfAttributes()
00205             print "  -Identifier:",familyIdentifier
00206             print "  -Number Of Attributes:",nbOfAtt
00207             attributesids = familycorba.getAttributesIdentifiers()
00208             attributesvals = familycorba.getAttributesValues()
00209             for k in range(nbOfAtt):
00210                 print "    * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
00211             print "  -Entities list:"
00212             if (familyBool):
00213                 print "  -Is on all entities"
00214             else:
00215                 types = familycorba.getTypes()
00216                 nbOfTypes = len(types)
00217                 print "  -Number Of Types:",nbOfTypes
00218                 for k in range(nbOfTypes):
00219                     type = types[k]
00220                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
00221                     number = familycorba.getNumber(type)
00222                     print "    * Type",type
00223                     print "    * Number",number[0:nbOfElmtsOfType]
00224                 print ""
00225                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
00226                 nbOfComp = 1
00227 
00228                 supportOutLocal = SUPPORTClient( familycorba )
00229                 supportOutCorba = createCorbaSupport( supportOutLocal )
00230 
00231                 print "\nGenerate a Local scalar double field"
00232                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
00233                 value = [ random() for k in range(lengthValue*nbOfComp) ]
00234                 fieldScalDblLoc.setValue( value ) ## the only way as field support not defined
00235                 valueIverif = fieldScalDblLoc.getValue()
00236                 print "     Set ",value
00237                 print "     Get ",valueIverif
00238 
00239                 print "\nGenerate a Corba scalar double field"
00240                 fieldScalDblLoc.setSupport( supportOutLocal )
00241                 fieldScalDblCorba = createCorbaFieldDouble(supportOutCorba,fieldScalDblLoc)
00242                 AnalyzeField(fieldScalDblCorba)
00243 
00244                 print "Generate a Local scalar integer field"
00245                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
00246                 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
00247                 fieldScalIntLoc.setValue( value ) ## the only way as field support not defined
00248                 valueIverif = fieldScalIntLoc.getValue()
00249                 print "     Set ",value
00250                 print "     Get ",valueIverif
00251 
00252                 print "\nGenerate a Corba scalar integer field"
00253                 fieldScalIntLoc.setSupport( supportOutLocal )
00254                 fieldScalIntCorba = createCorbaFieldInt(supportOutCorba,fieldScalIntLoc)
00255                 AnalyzeField(fieldScalIntCorba)
00256 
00257                 nbOfComp = spaceDim
00258                 print "\nGenerate a Local vector double field"
00259                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
00260                 value = [ random() for k in range(lengthValue*nbOfComp) ]
00261                 fieldVectDblLoc.setValue(value)
00262                 valueIverif = fieldVectDblLoc.getValue()
00263                 print "     Set ",value
00264                 print "     Get ",valueIverif
00265 
00266                 print "\nGenerate a Corba vector double field"
00267                 fieldVectDblLoc.setSupport( supportOutLocal )
00268                 fieldVectDblCorba = createCorbaFieldDouble(supportOutCorba,fieldVectDblLoc)
00269                 AnalyzeField(fieldVectDblCorba)
00270 
00271                 print "\nGenerate a Local vector integer field"
00272                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
00273                 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
00274                 fieldVectIntLoc.setValue(value)
00275                 valueIverif = fieldVectIntLoc.getValue()
00276                 print "     Set ",value
00277                 print "     Get ",valueIverif
00278 
00279                 print "\nGenerate a Corba vector integer field"
00280                 fieldVectIntLoc.setSupport( supportOutLocal )
00281                 fieldVectIntCorba = createCorbaFieldInt(supportOutCorba,fieldVectIntLoc)
00282                 AnalyzeField(fieldVectIntCorba)
00283                 print ""
00284 print "Fin du script Python ...."
00285 
00286