1 # MED MedCorba_Swig : binding of MED CORBA objects woth Python
3 # Copyright (C) 2003 CEA/DEN, EDF R&D
7 # File : batchmode_medcorba_test.py
10 import batchmode_salome
14 from libMedCorba_Swig import *
20 filePath=os.environ["MED_ROOT_DIR"]
21 filePath=filePath+"/share/salome/resources/"
23 #==============================================================================
25 def AnalyzeField(field):
26 name = field.getName()
27 desc = field.getDescription()
28 nbComp = field.getNumberOfComponents()
29 itNum = field.getIterationNumber()
30 ordNum = field.getOrderNumber()
32 print "Analysis of the field ",name," with the description ",desc
33 print "iteration number ",itNum," order Number ",ordNum
34 print "It has ",nbComp," component(s)"
36 fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
37 fieldSupport = field.getSupport()
38 fieldMesh = fieldSupport.getMesh()
39 fieldEntity = fieldSupport.getEntity()
40 bool = fieldSupport.isOnAllElements()
43 print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
44 if fieldEntity == SALOME_MED.MED_NODE:
45 nbValByComp = fieldMesh.getNumberOfNodes()
47 nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
48 print "and its dimension (number of values by component of the field) is ",nbValByComp
50 print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
51 nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
52 print "and its dimension (number of values by component of the field) is ",nbValByComp
54 for i in range(nbComp):
55 compName = field.getComponentName(i+1)
56 compUnit = field.getComponentUnit(i+1)
57 print "The ",(i+1),"-th component ",compName," with the unit ",compUnit
59 for i in range(nbValByComp):
60 print " * ",fieldValue[i*nbComp:(i+1)*nbComp]
62 #==============================================================================
64 def getMeshObjectFromStudy(meshName):
65 objNameInStudy = "/Med/MEDMESH/"+meshName
66 mySO = batchmode_salome.myStudy.FindObjectByPath(objNameInStudy)
68 print "ERROR: ",objNameInStudy," cannot be found in the Study"
71 anAttr = mySO.FindAttribute("AttributeIOR")[1]
72 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
73 myObj = obj._narrow(SALOME_MED.MESH)
75 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
78 #==============================================================================
80 def getMedObjectFromStudy(file):
81 objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
82 compNameInStudy= "MED"
83 listOfSO = batchmode_salome.myStudy.FindObjectByName(objNameInStudy,
85 listLength = len(listOfSO)
86 if (listLength == 0) :
87 print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
89 elif (listLength > 1) :
90 print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
94 print "ERROR: ",objNameInStudy," cannot be found in the Study"
97 anAttr = mySO.FindAttribute("AttributeIOR")[1]
98 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
99 myObj = obj._narrow(SALOME_MED.MED)
101 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
104 #==============================================================================
106 # Since Corba pointeurs will be generated in this Python script
107 # a POA has to be registred to activate those Corba pointeurs
108 # and make them available by any clients (in general). In our
109 # case, it is to make those pointeurs avail able to itself
110 #==============================================================================
112 print "Activation of a POA to make available any Corba pointeurs"
113 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
114 poaManager = poa._get_the_POAManager()
115 poaManager.activate()
117 #==============================================================================
119 studyCurrent = batchmode_salome.myStudyName
120 studyCurrentId = batchmode_salome.myStudyId
122 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
125 fileName = "cube_hexa8_quad4.med"
126 #medFile = "carre_en_quad4_seg2.med"
127 medFile = filePath + fileName
129 print "Loading of the Med Component"
132 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
134 med_comp.readStructFile(medFile,studyCurrent)
136 med_obj = getMedObjectFromStudy(fileName)
138 nbMeshes = med_obj.getNumberOfMeshes()
140 nbFields = med_obj.getNumberOfFields()
142 meshNames = med_obj.getMeshNames()
145 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
148 meshName = meshNames[0]
150 meshcorba = getMeshObjectFromStudy(meshName)
152 nbNodes = meshcorba.getNumberOfNodes()
154 spaceDim = meshcorba.getSpaceDimension()
156 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
159 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
160 nbFam = meshcorba.getNumberOfFamilies(entity)
161 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
162 print "This mesh has",nbFam,"Node Family(ies)"
163 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
164 print "This mesh has",nbFam,"Cell Family(ies)"
165 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
166 print "This mesh has",nbFam,"Face Family(ies)"
167 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
168 print "This mesh has",nbFam,"Edge Family(ies)"
171 for j in range(nbFam):
173 familycorba = meshcorba.getFamily(entity,j+1)
174 familyName = familycorba.getName()
175 familyDescription = familycorba.getDescription()
176 familyEntity = familycorba.getEntity()
177 familyBool = familycorba.isOnAllElements()
178 print " -Name:",familyName
179 print " -Description:",familyDescription
180 print " -Entity:",familyEntity
181 familyIdentifier = familycorba.getIdentifier()
182 nbOfAtt = familycorba.getNumberOfAttributes()
183 print " -Identifier:",familyIdentifier
184 print " -Number Of Attributes:",nbOfAtt
185 attributesids = familycorba.getAttributesIdentifiers()
186 attributesvals = familycorba.getAttributesValues()
187 for k in range(nbOfAtt):
188 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
189 print " -Entities list:"
191 print " -Is on all entities"
193 types = familycorba.getTypes()
194 nbOfTypes = len(types)
195 print " -Number Of Types:",nbOfTypes
196 for k in range(nbOfTypes):
198 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
199 number = familycorba.getNumber(type)
201 print " * Number",number[0:nbOfElmtsOfType]
203 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
205 print "Generate a Local scalar double field"
206 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
207 for k in range(lengthValue):
209 for kcomp in range(nbOfComp):
210 valueI.append(random())
211 fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
212 valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
213 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
214 print "Generate a Corba scalar double field"
215 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
216 AnalyzeField(fieldScalDblCorba)
218 print "Generate a Local scalar integer field"
219 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
220 for k in range(lengthValue):
222 for kcomp in range(nbOfComp):
223 valueI.append(randint(0,100))
224 fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
225 valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
226 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
227 print "Generate a Corba scalar integer field"
228 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
229 AnalyzeField(fieldScalIntCorba)
232 print "Generate a Local vector double field"
233 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
234 for k in range(lengthValue):
236 for kcomp in range(nbOfComp):
237 valueI.append(random())
238 fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
239 valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
240 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
241 print "Generate a Corba vector double field"
242 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
243 AnalyzeField(fieldVectDblCorba)
245 print "Generate a Local vector integer field"
246 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
247 for k in range(lengthValue):
249 for kcomp in range(nbOfComp):
250 valueI.append(randint(0,100))
251 fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
252 valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
253 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
254 print "Generate a Corba vector integer field"
255 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
256 AnalyzeField(fieldVectIntCorba)
258 print "Fin du script Python ...."