]> SALOME platform Git repositories - modules/med.git/blob - src/MedCorba_Swig/batchmode_medcorba_test.py
Salome HOME
Merging with the MAN_SALOME2 branch
[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(number):
65     mySO = batchmode_salome.myStudy.FindObject("MEDMESH")
66     mysub = mySO.FindSubObject(number)[1]
67     if mysub:
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)
72         return myObj
73     else:
74         print "ERROR: No Mesh Object stored in this Study"
75         return None
76
77 #==============================================================================
78
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)
85     return myObj
86
87 #==============================================================================
88 #
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 #==============================================================================
94
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()
98 poaManager.activate()
99
100 #==============================================================================
101
102 studyCurrent = batchmode_salome.myStudyName
103 studyCurrentId = batchmode_salome.myStudyId
104
105 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
106 print ""
107
108 #medFile = "carre_en_quad4_seg2.med"
109 medFile = filePath + "cube_hexa8_quad4.med"
110
111 print "Loading of the Med Component"
112 print ""
113
114 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
115
116 med_comp.readStructFile(medFile,studyCurrent)
117
118 med_obj = getMedObjectFromStudy()
119
120 nbMeshes = med_obj.getNumberOfMeshes()
121
122 nbFields = med_obj.getNumberOfFields()
123
124 print ""
125 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
126 print ""
127
128 meshcorba = getMeshObjectFromStudy(1)
129
130 name = meshcorba.getName()
131
132 nbNodes = meshcorba.getNumberOfNodes()
133
134 spaceDim = meshcorba.getSpaceDimension()
135
136 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
137 print ""
138
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)"
149
150     if nbFam > 0:
151         for j in range(nbFam):
152             print ""
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:"
170             if (familyBool):
171                 print "  -Is on all entities"
172             else:
173                 types = familycorba.getTypes()
174                 nbOfTypes = len(types)
175                 print "  -Number Of Types:",nbOfTypes
176                 for k in range(nbOfTypes):
177                     type = types[k]
178                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
179                     number = familycorba.getNumber(type)
180                     print "    * Type",type
181                     print "    * Number",number[0:nbOfElmtsOfType]
182                 print ""
183                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
184                 nbOfComp = 1
185                 print "Generate a Local scalar double field"
186                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
187                 for k in range(lengthValue):
188                     valueI = []
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)
197                 print ""
198                 print "Generate a Local scalar integer field"
199                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
200                 for k in range(lengthValue):
201                     valueI = []
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)
210                 print ""
211                 nbOfComp = spaceDim
212                 print "Generate a Local vector double field"
213                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
214                 for k in range(lengthValue):
215                     valueI = []
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)
224                 print ""
225                 print "Generate a Local vector integer field"
226                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
227                 for k in range(lengthValue):
228                     valueI = []
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)
237                 print ""
238 print "Fin du script Python ...."
239
240