]> SALOME platform Git repositories - modules/med.git/blob - src/MedCorba_Swig/batchmode_medcorba_test.py
Salome HOME
DCQ : Merge with Ecole Ete a6.
[modules/med.git] / src / MedCorba_Swig / batchmode_medcorba_test.py
1 #  MED MedCorba_Swig : binding of MED CORBA objects woth Python
2 #
3 #  Copyright (C) 2003  CEA/DEN, EDF R&D
4 #
5 #
6 #
7 #  File   : batchmode_medcorba_test.py
8 #  Module : MED
9
10 import batchmode_salome
11
12 import SALOME_MED
13
14 from libMedCorba_Swig import *
15
16 from random import *
17
18 import os
19
20 filePath=os.environ["KERNEL_ROOT_DIR"]
21 filePath=filePath+"/examples/"
22
23 #==============================================================================
24
25 def AnalyzeField(field):
26     name = field.getName()
27     desc = field.getDescription()
28     nbComp = field.getNumberOfComponents()
29     itNum = field.getIterationNumber()
30     ordNum = field.getOrderNumber()
31
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)"
35
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()
41
42     if bool:
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()
46         else:
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
49     else:
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
53
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
58
59     for i in range(nbValByComp):
60         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
61
62 #==============================================================================
63
64 def getMeshObjectFromStudy(meshName):
65     objNameInStudy = "/Med/MEDMESH/"+meshName
66     mySO = batchmode_salome.myStudy.FindObjectByPath(objNameInStudy)
67     if (mySO == None) :
68         print "ERROR: ",objNameInStudy," cannot be found in the Study"
69         return mySO
70     else:
71         anAttr = mySO.FindAttribute("AttributeIOR")[1]
72         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
73         myObj = obj._narrow(SALOME_MED.MESH)
74         if (myObj == None) :
75             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
76         return myObj
77
78
79 #==============================================================================
80
81 def getFieldDoubleObjectFromStudy(number,subnumber):
82     mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
83     mysub = mySO.FindSubObject(number)[1]
84     if mysub:
85         mysubsub = mysub.FindSubObject(subnumber)[1]
86         if mysubsub:
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)
91             return myObj
92         else:
93             print "ERROR: No FieldDouble Object stored in this Study"
94             return None
95     else:
96         print "ERROR: No FieldDouble Object stored in this Study"
97         return None
98
99
100 #==============================================================================
101
102 def getFieldIntObjectFromStudy(number,subnumber):
103     mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
104     mysub = mySO.FindSubObject(number)[1]
105     if mysub:
106         mysubsub = mysub.FindSubObject(subnumber)[1]
107         if mysubsub:
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)
112             return myObj
113         else:
114             print "ERROR: No FieldInt Object stored in this Study"
115             return None
116     else:
117         print "ERROR: No FieldInt Object stored in this Study"
118         return None
119
120 #==============================================================================
121
122 def getMedObjectFromStudy(file):
123     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
124     compNameInStudy= "MED"
125     listOfSO = batchmode_salome.myStudy.FindObjectByName(objNameInStudy,
126                                                          compNameInStudy)
127     listLength = len(listOfSO)
128     if (listLength == 0) :
129         print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
130         return None
131     elif (listLength > 1) :
132         print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
133         return None
134     mySO = listOfSO[0]
135     if (mySO == None) :
136         print "ERROR: ",objNameInStudy," cannot be found in the Study"
137         return mySO
138     else:
139         anAttr = mySO.FindAttribute("AttributeIOR")[1]
140         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
141         myObj = obj._narrow(SALOME_MED.MED)
142         if (myObj == None) :
143             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
144         return myObj
145
146 #==============================================================================
147 #
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 #==============================================================================
153
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()
158
159 #==============================================================================
160
161 studyCurrent = batchmode_salome.myStudyName
162 studyCurrentId = batchmode_salome.myStudyId
163
164 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
165 print ""
166
167 fileName = "cube_hexa8_quad4.med"
168 #medFile = "carre_en_quad4_seg2.med"
169 medFile = filePath + fileName
170
171 print "Loading of the Med Component"
172 print ""
173
174 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
175
176 med_comp.readStructFile(medFile,studyCurrent)
177
178 med_obj = getMedObjectFromStudy(fileName)
179
180 nbMeshes = med_obj.getNumberOfMeshes()
181
182 nbFields = med_obj.getNumberOfFields()
183
184 meshNames = med_obj.getMeshNames()
185
186 print ""
187 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
188 print ""
189
190 meshName = meshNames[0]
191
192 meshcorba = getMeshObjectFromStudy(meshName)
193
194 nbNodes = meshcorba.getNumberOfNodes()
195
196 spaceDim = meshcorba.getSpaceDimension()
197
198 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
199 print ""
200
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)"
211
212     if nbFam > 0:
213         for j in range(nbFam):
214             print ""
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:"
232             if (familyBool):
233                 print "  -Is on all entities"
234             else:
235                 types = familycorba.getTypes()
236                 nbOfTypes = len(types)
237                 print "  -Number Of Types:",nbOfTypes
238                 for k in range(nbOfTypes):
239                     type = types[k]
240                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
241                     number = familycorba.getNumber(type)
242                     print "    * Type",type
243                     print "    * Number",number[0:nbOfElmtsOfType]
244                 print ""
245                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
246                 nbOfComp = 1
247                 print "Generate a Local scalar double field"
248                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
249                 for k in range(lengthValue):
250                     valueI = []
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)
259                 print ""
260                 print "Generate a Local scalar integer field"
261                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
262                 for k in range(lengthValue):
263                     valueI = []
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)
272                 print ""
273                 nbOfComp = spaceDim
274                 print "Generate a Local vector double field"
275                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
276                 for k in range(lengthValue):
277                     valueI = []
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)
286                 print ""
287                 print "Generate a Local vector integer field"
288                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
289                 for k in range(lengthValue):
290                     valueI = []
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)
299                 print ""
300 print "Fin du script Python ...."
301
302