Back to index

salome-med  6.5.0
medClient_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 ####################################################################################################
00025 # Test the MedClient classes: mounting in Memory a .med file and using this file as a client of
00026 # the MED component we try
00027 ####################################################################################################
00028 #
00029 from libMEDClient import *
00030 
00031 import string
00032 
00033 import salome
00034 
00035 import SALOME_MED
00036 
00037 from libSALOME_Swig import *
00038 sg = SALOMEGUI_Swig()
00039 
00040 def print_ord(i):
00041     if i == 0:
00042         return 'first'
00043     elif i == 1:
00044         return 'second'
00045     elif i == 2:
00046         return 'third'
00047     else:
00048         return `(i+1)`+'th'
00049 
00050 def changeBlankToUnderScore(stringWithBlank):
00051     blank = ' '
00052     underscore = '_'
00053     decompString = string.split(stringWithBlank,blank)
00054     length = len(decompString)
00055     stringWithUnderScore = decompString[0]
00056     for i in range(1,length):
00057         stringWithUnderScore += underscore
00058         stringWithUnderScore += decompString[i]
00059     return stringWithUnderScore
00060 
00061 def getMedObjectFromStudy(file):
00062     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
00063     compNameInStudy= "MED"
00064     listOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
00065     listLength = len(listOfSO)
00066     if (listLength == 0) :
00067         print objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
00068         return None
00069     elif (listLength > 1) :
00070         print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
00071         return None
00072     mySO = listOfSO[0]
00073     if (mySO == None) :
00074         print objNameInStudy," cannot be found in the Study"
00075         return mySO
00076     else:
00077         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00078         obj = salome.orb.string_to_object(anAttr.Value())
00079         myObj = obj._narrow(SALOME_MED.MED)
00080         if (myObj == None) :
00081             print objNameInStudy," has been found in the Study but with the wrong type"
00082         return myObj
00083 
00084 def getMeshObjectFromStudy(meshName):
00085     objNameInStudy = "/Med/MEDMESH/"+meshName
00086     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00087     if (mySO == None) :
00088         print objNameInStudy," cannot be found in the Study"
00089         return mySO
00090     else:
00091         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00092         obj = salome.orb.string_to_object(anAttr.Value())
00093         myObj = obj._narrow(SALOME_MED.MESH)
00094         if (myObj == None) :
00095             print objNameInStudy," has been found in the Study but with the wrong type"
00096         return myObj
00097 
00098 def getSupportObjectFromStudy(meshName,supportName):
00099     meshNameStudy = changeBlankToUnderScore(meshName)
00100     objNameInStudy = "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy+"/"+supportName
00101     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00102     if (mySO == None) :
00103         print objNameInStudy," cannot be found in the Study"
00104         return mySO
00105     else:
00106         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00107         obj = salome.orb.string_to_object(anAttr.Value())
00108         myObj = obj._narrow(SALOME_MED.SUPPORT)
00109         if (myObj == None) :
00110             print objNameInStudy," has been found in the Study but with the wrong type"
00111         return myObj
00112 
00113 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
00114     type = -1
00115     meshNameStudy = changeBlankToUnderScore(meshName)
00116     objNameInStudy = "/Med/MEDFIELD/"+fieldName+"/("+str(dt)+","+str(it)+")_ON_"+supportName+"_OF_"+meshNameStudy
00117     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00118     if (mySO == None) :
00119         print objNameInStudy," cannot be found in the Study"
00120         return -1,-1
00121     else:
00122         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00123         obj = salome.orb.string_to_object(anAttr.Value())
00124         myObj = obj._narrow(SALOME_MED.FIELDINT)
00125         type = 0
00126         if (myObj == None):
00127             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
00128             type = 1
00129             if (myObj == None) :
00130                 print objNameInStudy," has been found in the Study but with the wrong type"
00131         return myObj,type
00132 
00133 fileName = "cube_hexa8_quad4.med"
00134 
00135 #fileName = "carre_en_quad4_seg2.med"
00136 # fileName = "polygones.med"
00137 # fileName = "poly3D.med"
00138 
00139 medComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
00140 
00141 import os
00142 
00143 filePath=os.environ["MED_ROOT_DIR"]
00144 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
00145 
00146 filePathName = os.path.join( filePath, fileName )
00147 
00148 print "Reading the .med file ",filePathName," and pushing corba objects in the SALOME study"
00149 medComp.readStructFileWithFieldType(filePathName,salome.myStudyName)
00150 sg.updateObjBrowser(1)
00151 
00152 print "getting the MED object from the study"
00153 medObj = getMedObjectFromStudy(fileName)
00154 
00155 nbOfMeshes = medObj.getNumberOfMeshes()
00156 meshNames = medObj.getMeshNames()
00157 
00158 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
00159 for i in range(nbOfMeshes):
00160     meshName = meshNames[i]
00161     print "    - the ",print_ord(i)," mesh is named ",meshName
00162     print "      getting the distant MESH object using the API of the corba object MED"
00163     meshObj = medObj.getMeshByName(meshName)
00164 
00165 
00166 
00167     print "      getting mesh information using the API of the corba object MESH but corba objects are obtained from the Study"
00168     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00169                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00170         nbFam = meshObj.getNumberOfFamilies(entity)
00171         nbGrp = meshObj.getNumberOfGroups(entity)
00172         if (entity == SALOME_MED.MED_NODE):
00173             print "      this mesh has ",nbFam," Node Family(ies) and ",nbGrp," Node Group(s)"
00174         elif (entity == SALOME_MED.MED_CELL):
00175             print "                    ",nbFam," Cell Family(ies) and ",nbGrp," Cell Group(s)"
00176         elif (entity == SALOME_MED.MED_FACE):
00177             print "                    ",nbFam," Face Family(ies) and ",nbGrp," Face Group(s)"
00178         elif (entity == SALOME_MED.MED_EDGE):
00179             print "                    ",nbFam," Edge Family(ies) and ",nbGrp," Cell Group(s)"
00180 
00181         if nbFam > 0:
00182             for j in range(nbFam):
00183                 familyName = (meshObj.getFamily(entity,j+1)).getName()
00184                 familyObj = getSupportObjectFromStudy(meshName,familyName)
00185                 print familyObj
00186 
00187         if nbGrp > 0:
00188             for j in range(nbGrp):
00189                 groupName = (meshObj.getGroup(entity,j+1)).getName()
00190                 groupObj = getSupportObjectFromStudy(meshName,groupName)
00191                 print groupObj
00192 
00193     print "let's get other SUPPORT object from  the Study"
00194     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00195                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00196 
00197         if entity == SALOME_MED.MED_NODE :
00198             entitySupport = "MED_NOEUD"
00199         elif entity == SALOME_MED.MED_CELL :
00200             entitySupport = "MED_MAILLE"
00201         elif entity == SALOME_MED.MED_FACE :
00202             entitySuppor = "MED_FACE"
00203         elif entity == SALOME_MED.MED_EDGE :
00204             entitySupport = "MED_ARETE"
00205 
00206         supportName = "SupportOnAll_"+entitySupport
00207         supportObj = getSupportObjectFromStudy(meshName,supportName)
00208 
00209 
00210 
00211     meshLocalCopy0 = MESHClient(meshObj)
00212     print "\n=============== Test printing of MESHClient (bug NPAL14100) ======================="
00213     print meshLocalCopy0
00214     print "============= End Test printing of MESHClient (bug NPAL14100) =======================\n\n"
00215 
00216 
00217     meshLocalCopy = MESHClient(meshObj)
00218     print "      getting information from the local copy of the distant mesh"
00219     name = meshLocalCopy.getName()
00220     spaceDimension = meshLocalCopy.getSpaceDimension()
00221     meshDimension = meshLocalCopy.getMeshDimension()
00222     numberOfNodes = meshLocalCopy.getNumberOfNodes()
00223     print "          Name = ", name, " space Dim = ", spaceDimension, " mesh Dim = ", meshDimension, " Nb of Nodes = ", numberOfNodes
00224     coordSyst = meshLocalCopy.getCoordinatesSystem()
00225     print "          The coordinates system is",coordSyst
00226     print "          The Coordinates :"
00227     coordNames = []
00228     coordUnits = []
00229     for isd in range(spaceDimension):
00230         coordNames.append(meshLocalCopy.getCoordinateName(isd))
00231         coordUnits.append(meshLocalCopy.getCoordinateUnit(isd))
00232 
00233     print "          names:", coordNames
00234     print "          units", coordUnits
00235     print "          values:"
00236     coordinates = meshLocalCopy.getCoordinates(MED_FULL_INTERLACE)
00237     for k in range(numberOfNodes):
00238         kp1 = k+1
00239         print "         ---- ", coordinates[k*spaceDimension:(kp1*spaceDimension)]
00240     print ""
00241     print "          The Cell Nodal Connectivity of the Cells:"
00242     nbTypesCell = meshLocalCopy.getNumberOfTypes(MED_CELL)
00243     print ""
00244     if (nbTypesCell>0):
00245         print "      The Mesh has",nbTypesCell,"Type(s) of Cell"
00246         types = meshLocalCopy.getTypes(MED_CELL)
00247         for k in range(nbTypesCell):
00248             type = types[k]
00249             nbElemType = meshLocalCopy.getNumberOfElements(MED_CELL,type)
00250             print "     For the type:",type,"there is(are)",nbElemType,"elemnt(s)"
00251             connectivity = meshLocalCopy.getConnectivity(MED_NODAL,MED_CELL,MED_ALL_ELEMENTS)
00252             index = meshLocalCopy.getConnectivityIndex(MED_NODAL,MED_CELL)
00253             if type == MED_POLYHEDRA:
00254                 pass
00255             else:
00256                 nbNodesPerCell = type%100
00257                 for j in range(nbElemType):
00258                     print "       Element",(j+1)," ",connectivity[ index[j]-1 : index[j+1]-1 ]
00259                     pass
00260                 pass
00261             pass
00262         pass
00263     pass
00264 
00265 nbOfFields = medObj.getNumberOfFields()
00266 print "in the considered .med file there is(are) ",nbOfFields," field(s):"
00267 fieldNames = medObj.getFieldNames()
00268 for i in range(nbOfFields):
00269     fieldName = fieldNames[i]
00270     nbOfIt = medObj.getFieldNumberOfIteration(fieldName)
00271     print "    - the ",print_ord(i)," field is name ",fieldName," and has ",nbOfIt," iteration(s)"
00272     for j in range(nbOfIt):
00273         dtitfield = medObj.getFieldIteration(fieldName,j)
00274         dt = dtitfield[0]
00275         it = dtitfield[1]
00276         print "     * Iteration:",dt,"Order number:",it
00277         meshName = meshNames[0]
00278         for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00279                        SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00280             if entity == SALOME_MED.MED_NODE :
00281                 entitySupport = "MED_NOEUD"
00282             elif entity == SALOME_MED.MED_CELL :
00283                 entitySupport = "MED_MAILLE"
00284             elif entity == SALOME_MED.MED_FACE :
00285                 entitySuppor = "MED_FACE"
00286             elif entity == SALOME_MED.MED_EDGE :
00287                 entitySupport = "MED_ARETE"
00288             supportName = "SupportOnAll_"+entitySupport
00289             print "         getting a corba object Field from the study iteration ",dt," order number ",it," on the support ",supportName," from the mesh ",meshName
00290             fieldObj,type = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
00291             print fieldObj
00292 
00293             if ((fieldObj != None) and (fieldObj != -1)):
00294                 if(type == 1):
00295                     fieldTyped = FIELDDOUBLEClient(fieldObj)
00296                 elif (type == 0):
00297                     fieldTyped = FIELDINTClient(fieldObj)
00298 
00299                 type = fieldTyped.getValueType()
00300                 print "     * Iteration:",dt,"Order number:",it,"Type:",type
00301                 name = fieldTyped.getName()
00302                 desc = fieldTyped.getDescription()
00303                 nbOfComp = fieldTyped.getNumberOfComponents()
00304                 print "     Field",name," : ",desc
00305                 print "     Number Of Components:",nbOfComp
00306                 iterationNb = fieldTyped.getIterationNumber()
00307                 orderNb = fieldTyped.getOrderNumber()
00308                 time = fieldTyped.getTime()
00309                 print "     Iteration Number",iterationNb
00310                 print "     Order Number",orderNb
00311                 print "     Time",time
00312                 support = fieldTyped.getSupport()
00313                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00314                 print "     Values:",nbOf
00315                 for k in range(nbOf):
00316                     valueI = fieldTyped.getRow(k+1)
00317                     print "     *",valueI[:nbOfComp]
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 
00330 print ""
00331 print "END of the Pyhton script ..... Ctrl D to exit"