Back to index

salome-med  6.5.0
Med_Gen_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 Med Component: mounting in Memory a .med file and trying to get information through
00026 # the CORBA Med API
00027 ####################################################################################################
00028 #
00029 import string
00030 
00031 import salome
00032 
00033 import SALOME_MED
00034 
00035 from libSALOME_Swig import *
00036 sg = SALOMEGUI_Swig()
00037 
00038 def print_ord(i):
00039     if i == 0:
00040         return 'first'
00041     elif i == 1:
00042         return 'second'
00043     elif i == 2:
00044         return 'third'
00045     else:
00046         return `(i+1)`+'th'
00047 
00048 def changeBlankToUnderScore(stringWithBlank):
00049     blank = ' '
00050     underscore = '_'
00051     decompString = string.split(stringWithBlank,blank)
00052     length = len(decompString)
00053     stringWithUnderScore = decompString[0]
00054     for i in range(1,length):
00055         stringWithUnderScore += underscore
00056         stringWithUnderScore += decompString[i]
00057     return stringWithUnderScore
00058 
00059 def getMedObjectFromStudy(file):
00060     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
00061     compNameInStudy= "MED"
00062     listOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
00063     listLength = len(listOfSO)
00064     if (listLength == 0) :
00065         print objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
00066         return None
00067     elif (listLength > 1) :
00068         print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
00069         return None
00070     mySO = listOfSO[0]
00071     if (mySO == None) :
00072         print objNameInStudy," cannot be found in the Study"
00073         return mySO
00074     else:
00075         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00076         obj = salome.orb.string_to_object(anAttr.Value())
00077         myObj = obj._narrow(SALOME_MED.MED)
00078         if (myObj == None) :
00079             print objNameInStudy," has been found in the Study but with the wrong type"
00080         return myObj
00081 
00082 def getMeshObjectFromStudy(meshName):
00083     objNameInStudy = "/Med/MEDMESH/"+meshName
00084     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00085     if (mySO == None) :
00086         print objNameInStudy," cannot be found in the Study"
00087         return mySO
00088     else:
00089         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00090         obj = salome.orb.string_to_object(anAttr.Value())
00091         myObj = obj._narrow(SALOME_MED.MESH)
00092         if (myObj == None) :
00093             print objNameInStudy," has been found in the Study but with the wrong type"
00094         return myObj
00095 
00096 def getSupportObjectFromStudy(meshName,supportName):
00097     meshNameStudy = changeBlankToUnderScore(meshName)
00098     objNameInStudy = "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy+"/"+supportName
00099     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00100     if (mySO == None) :
00101         print objNameInStudy," cannot be found in the Study"
00102         return mySO
00103     else:
00104         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00105         obj = salome.orb.string_to_object(anAttr.Value())
00106         myObj = obj._narrow(SALOME_MED.SUPPORT)
00107         if (myObj == None) :
00108             print objNameInStudy," has been found in the Study but with the wrong type"
00109         return myObj
00110 
00111 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
00112     meshNameStudy = changeBlankToUnderScore(meshName)
00113     objNameInStudy = "/Med/MEDFIELD/"+fieldName+"/("+str(dt)+","+str(it)+")_ON_"+supportName+"_OF_"+meshNameStudy
00114     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
00115     if (mySO == None) :
00116         print objNameInStudy," cannot be found in the Study"
00117         return mySO
00118     else:
00119         anAttr = mySO.FindAttribute("AttributeIOR")[1]
00120         obj = salome.orb.string_to_object(anAttr.Value())
00121         myObj = obj._narrow(SALOME_MED.FIELDINT)
00122         if (myObj == None):
00123             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
00124             if (myObj == None) :
00125                 print objNameInStudy," has been found in the Study but with the wrong type"
00126         return myObj
00127 
00128 fileName = "cube_hexa8_quad4.med"
00129 
00130 #fileName = "carre_en_quad4_seg2.med"
00131 
00132 medComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
00133 
00134 import os
00135 
00136 filePath=os.environ["MED_ROOT_DIR"]
00137 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
00138 
00139 filePathName = os.path.join(filePath, fileName)
00140 
00141 print "Reading the .med file ",filePathName," and pushing corba objects in the SALOME study"
00142 medComp.readStructFileWithFieldType(filePathName,salome.myStudyName)
00143 sg.updateObjBrowser(1)
00144 
00145 print "getting the MED object from the study"
00146 medObj = getMedObjectFromStudy(fileName)
00147 
00148 nbOfMeshes = medObj.getNumberOfMeshes()
00149 meshNames = medObj.getMeshNames()
00150 
00151 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
00152 for i in range(nbOfMeshes):
00153     meshName = meshNames[i]
00154     print "    - the ",print_ord(i)," mesh is named ",meshName
00155     print "      getting the MESH object using the API of the corba object MED"
00156     meshObj = medObj.getMeshByName(meshName)
00157     print "      getting mesh information (including corba object) using the API of the corba object MESH"
00158     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00159                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00160         nbFam = meshObj.getNumberOfFamilies(entity)
00161         nbGrp = meshObj.getNumberOfGroups(entity)
00162         if (entity == SALOME_MED.MED_NODE):
00163             print "      this mesh has ",nbFam," Node Family(ies) and ",nbGrp," Node Group(s)"
00164         elif (entity == SALOME_MED.MED_CELL):
00165             print "                    ",nbFam," Cell Family(ies) and ",nbGrp," Cell Group(s)"
00166         elif (entity == SALOME_MED.MED_FACE):
00167             print "                    ",nbFam," Face Family(ies) and ",nbGrp," Face Group(s)"
00168         elif (entity == SALOME_MED.MED_EDGE):
00169             print "                    ",nbFam," Edge Family(ies) and ",nbGrp," Cell Group(s)"
00170 
00171         if nbFam > 0:
00172             for j in range(nbFam):
00173                 familyObj = meshObj.getFamily(entity,j+1)
00174                 print familyObj
00175 
00176         if nbGrp > 0:
00177             for j in range(nbGrp):
00178                 groupObj = meshObj.getGroup(entity,j+1)
00179                 print groupObj
00180 
00181     print ""
00182     print "      getting the MESH object from the Study"
00183     meshObj = getMeshObjectFromStudy(meshName)
00184     print meshObj
00185     print "      getting mesh information using the API of the corba object MESH but corba objects are obtained from the Study"
00186     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00187                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00188         nbFam = meshObj.getNumberOfFamilies(entity)
00189         nbGrp = meshObj.getNumberOfGroups(entity)
00190         if (entity == SALOME_MED.MED_NODE):
00191             print "      this mesh has ",nbFam," Node Family(ies) and ",nbGrp," Node Group(s)"
00192         elif (entity == SALOME_MED.MED_CELL):
00193             print "                    ",nbFam," Cell Family(ies) and ",nbGrp," Cell Group(s)"
00194         elif (entity == SALOME_MED.MED_FACE):
00195             print "                    ",nbFam," Face Family(ies) and ",nbGrp," Face Group(s)"
00196         elif (entity == SALOME_MED.MED_EDGE):
00197             print "                    ",nbFam," Edge Family(ies) and ",nbGrp," Cell Group(s)"
00198 
00199         if nbFam > 0:
00200             for j in range(nbFam):
00201                 familyName = (meshObj.getFamily(entity,j+1)).getName()
00202                 familyObj = getSupportObjectFromStudy(meshName,familyName)
00203                 print familyObj
00204 
00205         if nbGrp > 0:
00206             for j in range(nbGrp):
00207                 groupName = (meshObj.getGroup(entity,j+1)).getName()
00208                 groupObj = getSupportObjectFromStudy(meshName,groupName)
00209                 print groupObj
00210 
00211     print "let's get other SUPPORT object from  the Study"
00212     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00213                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00214 
00215         if entity == SALOME_MED.MED_NODE :
00216             entitySupport = "MED_NOEUD"
00217         elif entity == SALOME_MED.MED_CELL :
00218             entitySupport = "MED_MAILLE"
00219         elif entity == SALOME_MED.MED_FACE :
00220             entitySuppor = "MED_FACE"
00221         elif entity == SALOME_MED.MED_EDGE :
00222             entitySupport = "MED_ARETE"
00223 
00224         supportName = "SupportOnAll_"+entitySupport
00225         supportObj = getSupportObjectFromStudy(meshName,supportName)
00226 
00227 nbOfFields = medObj.getNumberOfFields()
00228 print "in the considered .med file there is(are) ",nbOfFields," field(s):"
00229 fieldNames = medObj.getFieldNames()
00230 for i in range(nbOfFields):
00231     fieldName = fieldNames[i]
00232     nbOfIt = medObj.getFieldNumberOfIteration(fieldName)
00233     print "    - the ",print_ord(i)," field is name ",fieldName," and has ",nbOfIt," iteration(s)"
00234     for j in range(nbOfIt):
00235         dtitfield = medObj.getFieldIteration(fieldName,j)
00236         dt = dtitfield[0]
00237         it = dtitfield[1]
00238         print "     * Iteration:",dt,"Order number:",it
00239         for k in range(nbOfMeshes):
00240             meshName = meshNames[k]
00241             for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
00242                            SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
00243                 if entity == SALOME_MED.MED_NODE :
00244                     entitySupport = "MED_NOEUD"
00245                 elif entity == SALOME_MED.MED_CELL :
00246                     entitySupport = "MED_MAILLE"
00247                 elif entity == SALOME_MED.MED_FACE :
00248                     entitySuppor = "MED_FACE"
00249                 elif entity == SALOME_MED.MED_EDGE :
00250                     entitySupport = "MED_ARETE"
00251                 supportName = "SupportOnAll_"+entitySupport
00252                 print "getting a corba object Field from the study iteration ",dt," order number ",it," on the support ",supportName," from the mesh ",meshName
00253                 fieldObj = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
00254                 print fieldObj
00255 
00256 print ""
00257 print "END of the Pyhton script ..... Ctrl D to exit"