]> SALOME platform Git repositories - modules/med.git/blob - src/MedCorba_Swig/batchmode_medcorba_test.py
Salome HOME
correction due to changes in the organisation of the Med Component which is
[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["MED_ROOT_DIR"]
21 filePath=filePath+"/share/salome/resources/"
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 def getMedObjectFromStudy(file):
81     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
82     compNameInStudy= "MED"
83     listOfSO = batchmode_salome.myStudy.FindObjectByName(objNameInStudy,
84                                                          compNameInStudy)
85     listLength = len(listOfSO)
86     if (listLength == 0) :
87         print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
88         return None
89     elif (listLength > 1) :
90         print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
91         return None
92     mySO = listOfSO[0]
93     if (mySO == None) :
94         print "ERROR: ",objNameInStudy," cannot be found in the Study"
95         return mySO
96     else:
97         anAttr = mySO.FindAttribute("AttributeIOR")[1]
98         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
99         myObj = obj._narrow(SALOME_MED.MED)
100         if (myObj == None) :
101             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
102         return myObj
103
104 #==============================================================================
105 #
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 #==============================================================================
111
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()
116
117 #==============================================================================
118
119 studyCurrent = batchmode_salome.myStudyName
120 studyCurrentId = batchmode_salome.myStudyId
121
122 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
123 print ""
124
125 fileName = "cube_hexa8_quad4.med"
126 #medFile = "carre_en_quad4_seg2.med"
127 medFile = filePath + fileName
128
129 print "Loading of the Med Component"
130 print ""
131
132 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
133
134 med_comp.readStructFile(medFile,studyCurrent)
135
136 med_obj = getMedObjectFromStudy(fileName)
137
138 nbMeshes = med_obj.getNumberOfMeshes()
139
140 nbFields = med_obj.getNumberOfFields()
141
142 meshNames = med_obj.getMeshNames()
143
144 print ""
145 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
146 print ""
147
148 meshName = meshNames[0]
149
150 meshcorba = getMeshObjectFromStudy(meshName)
151
152 nbNodes = meshcorba.getNumberOfNodes()
153
154 spaceDim = meshcorba.getSpaceDimension()
155
156 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
157 print ""
158
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)"
169
170     if nbFam > 0:
171         for j in range(nbFam):
172             print ""
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:"
190             if (familyBool):
191                 print "  -Is on all entities"
192             else:
193                 types = familycorba.getTypes()
194                 nbOfTypes = len(types)
195                 print "  -Number Of Types:",nbOfTypes
196                 for k in range(nbOfTypes):
197                     type = types[k]
198                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
199                     number = familycorba.getNumber(type)
200                     print "    * Type",type
201                     print "    * Number",number[0:nbOfElmtsOfType]
202                 print ""
203                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
204                 nbOfComp = 1
205                 print "Generate a Local scalar double field"
206                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
207                 for k in range(lengthValue):
208                     valueI = []
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)
217                 print ""
218                 print "Generate a Local scalar integer field"
219                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
220                 for k in range(lengthValue):
221                     valueI = []
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)
230                 print ""
231                 nbOfComp = spaceDim
232                 print "Generate a Local vector double field"
233                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
234                 for k in range(lengthValue):
235                     valueI = []
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)
244                 print ""
245                 print "Generate a Local vector integer field"
246                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
247                 for k in range(lengthValue):
248                     valueI = []
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)
257                 print ""
258 print "Fin du script Python ...."
259
260