Salome HOME
sources v1.2
[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 #==============================================================================
19
20 def AnalyzeField(field):
21     name = field.getName()
22     desc = field.getDescription()
23     nbComp = field.getNumberOfComponents()
24     itNum = field.getIterationNumber()
25     ordNum = field.getOrderNumber()
26
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)"
30
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()
36
37     if bool:
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()
41         else:
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
44     else:
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
48
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
53
54     for i in range(nbValByComp):
55         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
56
57 #==============================================================================
58
59 def getMeshObjectFromStudy(number):
60     mySO = batchmode_salome.myStudy.FindObject("MEDMESH")
61     mysub = mySO.FindSubObject(number)[1]
62     if mysub:
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)
67         return myObj
68     else:
69         print "ERROR: No Mesh Object stored in this Study"
70         return None
71
72 #==============================================================================
73
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)
80     return myObj
81
82 #==============================================================================
83 #
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 #==============================================================================
89
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()
93 poaManager.activate()
94
95 #==============================================================================
96
97 studyCurrent = batchmode_salome.myStudyName
98 studyCurrentId = batchmode_salome.myStudyId
99
100 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
101 print ""
102
103 #medFile = "carre_en_quad4_seg2.med"
104 medFile = "cube_hexa8_quad4.med"
105
106 print "Loading of the Med Component"
107 print ""
108
109 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "Med")
110
111 med_comp.readStructFile(medFile,studyCurrent)
112
113 med_obj = getMedObjectFromStudy()
114
115 nbMeshes = med_obj.getNumberOfMeshes()
116
117 nbFields = med_obj.getNumberOfFields()
118
119 print ""
120 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
121 print ""
122
123 meshcorba = getMeshObjectFromStudy(1)
124
125 name = meshcorba.getName()
126
127 nbNodes = meshcorba.getNumberOfNodes()
128
129 spaceDim = meshcorba.getSpaceDimension()
130
131 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
132 print ""
133
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)"
144
145     if nbFam > 0:
146         for j in range(nbFam):
147             print ""
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:"
165             if (familyBool):
166                 print "  -Is on all entities"
167             else:
168                 types = familycorba.getTypes()
169                 nbOfTypes = len(types)
170                 print "  -Number Of Types:",nbOfTypes
171                 for k in range(nbOfTypes):
172                     type = types[k]
173                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
174                     number = familycorba.getNumber(type)
175                     print "    * Type",type
176                     print "    * Number",number[0:nbOfElmtsOfType]
177                 print ""
178                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
179                 nbOfComp = 1
180                 print "Generate a Local scalar double field"
181                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
182                 for k in range(lengthValue):
183                     valueI = []
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)
192                 print ""
193                 print "Generate a Local scalar integer field"
194                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
195                 for k in range(lengthValue):
196                     valueI = []
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)
205                 print ""
206                 nbOfComp = spaceDim
207                 print "Generate a Local vector double field"
208                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
209                 for k in range(lengthValue):
210                     valueI = []
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)
219                 print ""
220                 print "Generate a Local vector integer field"
221                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
222                 for k in range(lengthValue):
223                     valueI = []
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)
232                 print ""
233 print "Fin du script Python ...."
234
235