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(number):
65 mySO = batchmode_salome.myStudy.FindObject("MEDMESH")
66 mysub = mySO.FindSubObject(number)[1]
68 Builder = batchmode_salome.myStudy.NewBuilder()
69 anAttr = Builder.FindOrCreateAttribute(mysub, "AttributeIOR")
70 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
71 myObj = obj._narrow(SALOME_MED.MESH)
74 print "ERROR: No Mesh Object stored in this Study"
77 #==============================================================================
79 def getMedObjectFromStudy():
80 mySO = batchmode_salome.myStudy.FindObject("Objet MED")
81 Builder = batchmode_salome.myStudy.NewBuilder()
82 anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
83 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
84 myObj = obj._narrow(SALOME_MED.MED)
87 #==============================================================================
89 # Since Corba pointeurs will be generated in this Python script
90 # a POA has to be registred to activate those Corba pointeurs
91 # and make them available by any clients (in general). In our
92 # case, it is to make those pointeurs avail able to itself
93 #==============================================================================
95 print "Activation of a POA to make available any Corba pointeurs"
96 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
97 poaManager = poa._get_the_POAManager()
100 #==============================================================================
102 studyCurrent = batchmode_salome.myStudyName
103 studyCurrentId = batchmode_salome.myStudyId
105 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
108 #medFile = "carre_en_quad4_seg2.med"
109 medFile = filePath + "cube_hexa8_quad4.med"
111 print "Loading of the Med Component"
114 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
116 med_comp.readStructFile(medFile,studyCurrent)
118 med_obj = getMedObjectFromStudy()
120 nbMeshes = med_obj.getNumberOfMeshes()
122 nbFields = med_obj.getNumberOfFields()
125 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
128 meshcorba = getMeshObjectFromStudy(1)
130 name = meshcorba.getName()
132 nbNodes = meshcorba.getNumberOfNodes()
134 spaceDim = meshcorba.getSpaceDimension()
136 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
139 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
140 nbFam = meshcorba.getNumberOfFamilies(entity)
141 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
142 print "This mesh has",nbFam,"Node Family(ies)"
143 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
144 print "This mesh has",nbFam,"Cell Family(ies)"
145 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
146 print "This mesh has",nbFam,"Face Family(ies)"
147 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
148 print "This mesh has",nbFam,"Edge Family(ies)"
151 for j in range(nbFam):
153 familycorba = meshcorba.getFamily(entity,j+1)
154 familyName = familycorba.getName()
155 familyDescription = familycorba.getDescription()
156 familyEntity = familycorba.getEntity()
157 familyBool = familycorba.isOnAllElements()
158 print " -Name:",familyName
159 print " -Description:",familyDescription
160 print " -Entity:",familyEntity
161 familyIdentifier = familycorba.getIdentifier()
162 nbOfAtt = familycorba.getNumberOfAttributes()
163 print " -Identifier:",familyIdentifier
164 print " -Number Of Attributes:",nbOfAtt
165 attributesids = familycorba.getAttributesIdentifiers()
166 attributesvals = familycorba.getAttributesValues()
167 for k in range(nbOfAtt):
168 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
169 print " -Entities list:"
171 print " -Is on all entities"
173 types = familycorba.getTypes()
174 nbOfTypes = len(types)
175 print " -Number Of Types:",nbOfTypes
176 for k in range(nbOfTypes):
178 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
179 number = familycorba.getNumber(type)
181 print " * Number",number[0:nbOfElmtsOfType]
183 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
185 print "Generate a Local scalar double field"
186 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
187 for k in range(lengthValue):
189 for kcomp in range(nbOfComp):
190 valueI.append(random())
191 fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
192 valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
193 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
194 print "Generate a Corba scalar double field"
195 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
196 AnalyzeField(fieldScalDblCorba)
198 print "Generate a Local scalar integer field"
199 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
200 for k in range(lengthValue):
202 for kcomp in range(nbOfComp):
203 valueI.append(randint(0,100))
204 fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
205 valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
206 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
207 print "Generate a Corba scalar integer field"
208 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
209 AnalyzeField(fieldScalIntCorba)
212 print "Generate a Local vector double field"
213 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
214 for k in range(lengthValue):
216 for kcomp in range(nbOfComp):
217 valueI.append(random())
218 fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
219 valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
220 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
221 print "Generate a Corba vector double field"
222 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
223 AnalyzeField(fieldVectDblCorba)
225 print "Generate a Local vector integer field"
226 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
227 for k in range(lengthValue):
229 for kcomp in range(nbOfComp):
230 valueI.append(randint(0,100))
231 fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
232 valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
233 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
234 print "Generate a Corba vector integer field"
235 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
236 AnalyzeField(fieldVectIntCorba)
238 print "Fin du script Python ...."