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 *
18 #==============================================================================
20 def AnalyzeField(field):
21 name = field.getName()
22 desc = field.getDescription()
23 nbComp = field.getNumberOfComponents()
24 itNum = field.getIterationNumber()
25 ordNum = field.getOrderNumber()
27 print "Analysis of the field ",name," with the description ",desc
28 print "iteration number ",itNum," order Number ",ordNum
29 print "It has ",nbComp," component(s)"
31 fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
32 fieldSupport = field.getSupport()
33 fieldMesh = fieldSupport.getMesh()
34 fieldEntity = fieldSupport.getEntity()
35 bool = fieldSupport.isOnAllElements()
38 print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
39 if fieldEntity == SALOME_MED.MED_NODE:
40 nbValByComp = fieldMesh.getNumberOfNodes()
42 nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
43 print "and its dimension (number of values by component of the field) is ",nbValByComp
45 print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
46 nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
47 print "and its dimension (number of values by component of the field) is ",nbValByComp
49 for i in range(nbComp):
50 compName = field.getComponentName(i+1)
51 compUnit = field.getComponentUnit(i+1)
52 print "The ",(i+1),"-th component ",compName," with the unit ",compUnit
54 for i in range(nbValByComp):
55 print " * ",fieldValue[i*nbComp:(i+1)*nbComp]
57 #==============================================================================
59 def getMeshObjectFromStudy(number):
60 mySO = batchmode_salome.myStudy.FindObject("MEDMESH")
61 mysub = mySO.FindSubObject(number)[1]
63 Builder = batchmode_salome.myStudy.NewBuilder()
64 anAttr = Builder.FindOrCreateAttribute(mysub, "AttributeIOR")
65 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
66 myObj = obj._narrow(SALOME_MED.MESH)
69 print "ERROR: No Mesh Object stored in this Study"
72 #==============================================================================
74 def getMedObjectFromStudy():
75 mySO = batchmode_salome.myStudy.FindObject("Objet MED")
76 Builder = batchmode_salome.myStudy.NewBuilder()
77 anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
78 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
79 myObj = obj._narrow(SALOME_MED.MED)
82 #==============================================================================
84 # Since Corba pointeurs will be generated in this Python script
85 # a POA has to be registred to activate those Corba pointeurs
86 # and make them available by any clients (in general). In our
87 # case, it is to make those pointeurs avail able to itself
88 #==============================================================================
90 print "Activation of a POA to make available any Corba pointeurs"
91 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
92 poaManager = poa._get_the_POAManager()
95 #==============================================================================
97 studyCurrent = batchmode_salome.myStudyName
98 studyCurrentId = batchmode_salome.myStudyId
100 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
103 #medFile = "carre_en_quad4_seg2.med"
104 medFile = "cube_hexa8_quad4.med"
106 print "Loading of the Med Component"
109 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
111 med_comp.readStructFile(medFile,studyCurrent)
113 med_obj = getMedObjectFromStudy()
115 nbMeshes = med_obj.getNumberOfMeshes()
117 nbFields = med_obj.getNumberOfFields()
120 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
123 meshcorba = getMeshObjectFromStudy(1)
125 name = meshcorba.getName()
127 nbNodes = meshcorba.getNumberOfNodes()
129 spaceDim = meshcorba.getSpaceDimension()
131 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
134 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
135 nbFam = meshcorba.getNumberOfFamilies(entity)
136 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
137 print "This mesh has",nbFam,"Node Family(ies)"
138 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
139 print "This mesh has",nbFam,"Cell Family(ies)"
140 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
141 print "This mesh has",nbFam,"Face Family(ies)"
142 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
143 print "This mesh has",nbFam,"Edge Family(ies)"
146 for j in range(nbFam):
148 familycorba = meshcorba.getFamily(entity,j+1)
149 familyName = familycorba.getName()
150 familyDescription = familycorba.getDescription()
151 familyEntity = familycorba.getEntity()
152 familyBool = familycorba.isOnAllElements()
153 print " -Name:",familyName
154 print " -Description:",familyDescription
155 print " -Entity:",familyEntity
156 familyIdentifier = familycorba.getIdentifier()
157 nbOfAtt = familycorba.getNumberOfAttributes()
158 print " -Identifier:",familyIdentifier
159 print " -Number Of Attributes:",nbOfAtt
160 attributesids = familycorba.getAttributesIdentifiers()
161 attributesvals = familycorba.getAttributesValues()
162 for k in range(nbOfAtt):
163 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
164 print " -Entities list:"
166 print " -Is on all entities"
168 types = familycorba.getTypes()
169 nbOfTypes = len(types)
170 print " -Number Of Types:",nbOfTypes
171 for k in range(nbOfTypes):
173 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
174 number = familycorba.getNumber(type)
176 print " * Number",number[0:nbOfElmtsOfType]
178 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
180 print "Generate a Local scalar double field"
181 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
182 for k in range(lengthValue):
184 for kcomp in range(nbOfComp):
185 valueI.append(random())
186 fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
187 valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
188 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
189 print "Generate a Corba scalar double field"
190 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
191 AnalyzeField(fieldScalDblCorba)
193 print "Generate a Local scalar integer field"
194 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
195 for k in range(lengthValue):
197 for kcomp in range(nbOfComp):
198 valueI.append(randint(0,100))
199 fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
200 valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
201 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
202 print "Generate a Corba scalar integer field"
203 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
204 AnalyzeField(fieldScalIntCorba)
207 print "Generate a Local vector double field"
208 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
209 for k in range(lengthValue):
211 for kcomp in range(nbOfComp):
212 valueI.append(random())
213 fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
214 valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
215 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
216 print "Generate a Corba vector double field"
217 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
218 AnalyzeField(fieldVectDblCorba)
220 print "Generate a Local vector integer field"
221 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
222 for k in range(lengthValue):
224 for kcomp in range(nbOfComp):
225 valueI.append(randint(0,100))
226 fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
227 valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
228 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
229 print "Generate a Corba vector integer field"
230 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
231 AnalyzeField(fieldVectIntCorba)
233 print "Fin du script Python ...."