Back to index

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