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["KERNEL_ROOT_DIR"]
21 filePath=filePath+"/examples/"
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"
79 #==============================================================================
81 def getFieldDoubleObjectFromStudy(number,subnumber):
82 mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
83 mysub = mySO.FindSubObject(number)[1]
85 mysubsub = mysub.FindSubObject(subnumber)[1]
87 Builder = batchmode_salome.myStudy.NewBuilder()
88 anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
89 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
90 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
93 print "ERROR: No FieldDouble Object stored in this Study"
96 print "ERROR: No FieldDouble Object stored in this Study"
100 #==============================================================================
102 def getFieldIntObjectFromStudy(number,subnumber):
103 mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
104 mysub = mySO.FindSubObject(number)[1]
106 mysubsub = mysub.FindSubObject(subnumber)[1]
108 Builder = batchmode_salome.myStudy.NewBuilder()
109 anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
110 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
111 myObj = obj._narrow(SALOME_MED.FIELDINT)
114 print "ERROR: No FieldInt Object stored in this Study"
117 print "ERROR: No FieldInt Object stored in this Study"
120 #==============================================================================
122 def getMedObjectFromStudy(file):
123 objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
124 compNameInStudy= "MED"
125 listOfSO = batchmode_salome.myStudy.FindObjectByName(objNameInStudy,
127 listLength = len(listOfSO)
128 if (listLength == 0) :
129 print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
131 elif (listLength > 1) :
132 print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
136 print "ERROR: ",objNameInStudy," cannot be found in the Study"
139 anAttr = mySO.FindAttribute("AttributeIOR")[1]
140 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
141 myObj = obj._narrow(SALOME_MED.MED)
143 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
146 #==============================================================================
148 # Since Corba pointeurs will be generated in this Python script
149 # a POA has to be registred to activate those Corba pointeurs
150 # and make them available by any clients (in general). In our
151 # case, it is to make those pointeurs avail able to itself
152 #==============================================================================
154 print "Activation of a POA to make available any Corba pointeurs"
155 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
156 poaManager = poa._get_the_POAManager()
157 poaManager.activate()
159 #==============================================================================
161 studyCurrent = batchmode_salome.myStudyName
162 studyCurrentId = batchmode_salome.myStudyId
164 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
167 fileName = "cube_hexa8_quad4.med"
168 #medFile = "carre_en_quad4_seg2.med"
169 medFile = filePath + fileName
171 print "Loading of the Med Component"
174 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
176 med_comp.readStructFile(medFile,studyCurrent)
178 med_obj = getMedObjectFromStudy(fileName)
180 nbMeshes = med_obj.getNumberOfMeshes()
182 nbFields = med_obj.getNumberOfFields()
184 meshNames = med_obj.getMeshNames()
187 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
190 meshName = meshNames[0]
192 meshcorba = getMeshObjectFromStudy(meshName)
194 nbNodes = meshcorba.getNumberOfNodes()
196 spaceDim = meshcorba.getSpaceDimension()
198 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
201 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
202 nbFam = meshcorba.getNumberOfFamilies(entity)
203 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
204 print "This mesh has",nbFam,"Node Family(ies)"
205 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
206 print "This mesh has",nbFam,"Cell Family(ies)"
207 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
208 print "This mesh has",nbFam,"Face Family(ies)"
209 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
210 print "This mesh has",nbFam,"Edge Family(ies)"
213 for j in range(nbFam):
215 familycorba = meshcorba.getFamily(entity,j+1)
216 familyName = familycorba.getName()
217 familyDescription = familycorba.getDescription()
218 familyEntity = familycorba.getEntity()
219 familyBool = familycorba.isOnAllElements()
220 print " -Name:",familyName
221 print " -Description:",familyDescription
222 print " -Entity:",familyEntity
223 familyIdentifier = familycorba.getIdentifier()
224 nbOfAtt = familycorba.getNumberOfAttributes()
225 print " -Identifier:",familyIdentifier
226 print " -Number Of Attributes:",nbOfAtt
227 attributesids = familycorba.getAttributesIdentifiers()
228 attributesvals = familycorba.getAttributesValues()
229 for k in range(nbOfAtt):
230 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
231 print " -Entities list:"
233 print " -Is on all entities"
235 types = familycorba.getTypes()
236 nbOfTypes = len(types)
237 print " -Number Of Types:",nbOfTypes
238 for k in range(nbOfTypes):
240 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
241 number = familycorba.getNumber(type)
243 print " * Number",number[0:nbOfElmtsOfType]
245 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
247 print "Generate a Local scalar double field"
248 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
249 for k in range(lengthValue):
251 for kcomp in range(nbOfComp):
252 valueI.append(random())
253 fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
254 valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
255 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
256 print "Generate a Corba scalar double field"
257 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
258 AnalyzeField(fieldScalDblCorba)
260 print "Generate a Local scalar integer field"
261 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
262 for k in range(lengthValue):
264 for kcomp in range(nbOfComp):
265 valueI.append(randint(0,100))
266 fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
267 valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
268 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
269 print "Generate a Corba scalar integer field"
270 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
271 AnalyzeField(fieldScalIntCorba)
274 print "Generate a Local vector double field"
275 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
276 for k in range(lengthValue):
278 for kcomp in range(nbOfComp):
279 valueI.append(random())
280 fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
281 valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
282 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
283 print "Generate a Corba vector double field"
284 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
285 AnalyzeField(fieldVectDblCorba)
287 print "Generate a Local vector integer field"
288 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
289 for k in range(lengthValue):
291 for kcomp in range(nbOfComp):
292 valueI.append(randint(0,100))
293 fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
294 valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
295 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
296 print "Generate a Corba vector integer field"
297 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
298 AnalyzeField(fieldVectIntCorba)
300 print "Fin du script Python ...."