Salome HOME
Merging with the MAN_SALOME2 branch
[modules/med.git] / src / MedCorba_Swig / medcorba_test.py
1 #  MED MedCorba_Swig : binding of MED CORBA objects woth Python
2 #
3 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5
6 #  This library is free software; you can redistribute it and/or 
7 #  modify it under the terms of the GNU Lesser General Public 
8 #  License as published by the Free Software Foundation; either 
9 #  version 2.1 of the License. 
10
11 #  This library is distributed in the hope that it will be useful, 
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 #  Lesser General Public License for more details. 
15
16 #  You should have received a copy of the GNU Lesser General Public 
17 #  License along with this library; if not, write to the Free Software 
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19
20 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 #
22 #
23 #
24 #  File   : medcorba_test.py
25 #  Module : MED
26
27 import salome
28
29 import SALOME_MED
30
31 from libMedCorba_Swig import *
32
33 from random import *
34
35 import os
36
37 filePath=os.environ["MED_ROOT_DIR"]
38 filePath=filePath+"/share/salome/resources/"
39
40 #==============================================================================
41
42 def AnalyzeField(field):
43     name = field.getName()
44     desc = field.getDescription()
45     nbComp = field.getNumberOfComponents()
46     itNum = field.getIterationNumber()
47     ordNum = field.getOrderNumber()
48
49     print "Analysis of the field ",name," with the description ",desc
50     print "iteration number ",itNum," order Number ",ordNum
51     print "It has ",nbComp," component(s)"
52
53     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
54     fieldSupport = field.getSupport()
55     fieldMesh = fieldSupport.getMesh()
56     fieldEntity = fieldSupport.getEntity()
57     bool = fieldSupport.isOnAllElements()
58
59     if bool:
60         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
61         if fieldEntity == SALOME_MED.MED_NODE:
62             nbValByComp = fieldMesh.getNumberOfNodes()
63         else:
64             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
65         print "and its dimension (number of values by component of the field) is ",nbValByComp
66     else:
67         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
68         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
69         print "and its dimension (number of values by component of the field) is ",nbValByComp
70
71     for i in range(nbComp):
72         compName = field.getComponentName(i+1)
73         compUnit = field.getComponentUnit(i+1)
74         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
75
76     for i in range(nbValByComp):
77         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
78
79 #==============================================================================
80
81 def getMeshObjectFromStudy(number):
82     mySO = salome.myStudy.FindObject("MEDMESH")
83     mysub = mySO.FindSubObject(number)[1]
84     if mysub:
85         Builder = salome.myStudy.NewBuilder()
86         anAttr = Builder.FindOrCreateAttribute(mysub, "AttributeIOR")
87         obj = salome.orb.string_to_object(anAttr.Value())
88         myObj = obj._narrow(SALOME_MED.MESH)
89         return myObj
90     else:
91         print "ERROR: No Mesh Object stored in this Study"
92         return None
93
94 #==============================================================================
95
96 def getMedObjectFromStudy():
97     mySO = salome.myStudy.FindObject("Objet MED")
98     Builder = salome.myStudy.NewBuilder()
99     anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
100     obj = salome.orb.string_to_object(anAttr.Value())
101     myObj = obj._narrow(SALOME_MED.MED)
102     return myObj
103
104 #==============================================================================
105
106 studyCurrent = salome.myStudyName
107 studyCurrentId = salome.myStudyId
108
109 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
110 print ""
111
112 #medFile = "carre_en_quad4_seg2.med"
113 medFile = filePath + "cube_hexa8_quad4.med"
114
115 print "Loading of the Med Component"
116 print ""
117
118 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
119
120 med_comp.readStructFile(medFile,studyCurrent)
121
122 med_obj = getMedObjectFromStudy()
123
124 nbMeshes = med_obj.getNumberOfMeshes()
125
126 nbFields = med_obj.getNumberOfFields()
127
128 print ""
129 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
130 print ""
131
132 meshcorba = getMeshObjectFromStudy(1)
133
134 name = meshcorba.getName()
135
136 nbNodes = meshcorba.getNumberOfNodes()
137
138 spaceDim = meshcorba.getSpaceDimension()
139
140 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
141 print ""
142
143 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
144     nbFam = meshcorba.getNumberOfFamilies(entity)
145     if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
146         print "This mesh has",nbFam,"Node Family(ies)"
147     elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
148         print "This mesh has",nbFam,"Cell Family(ies)"
149     elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
150         print "This mesh has",nbFam,"Face Family(ies)"
151     elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
152         print "This mesh has",nbFam,"Edge Family(ies)"
153
154     if nbFam > 0:
155         for j in range(nbFam):
156             print ""
157             familycorba = meshcorba.getFamily(entity,j+1)
158             familyName = familycorba.getName()
159             familyDescription = familycorba.getDescription()
160             familyEntity = familycorba.getEntity()
161             familyBool = familycorba.isOnAllElements()
162             print "  -Name:",familyName
163             print "  -Description:",familyDescription
164             print "  -Entity:",familyEntity
165             familyIdentifier = familycorba.getIdentifier()
166             nbOfAtt = familycorba.getNumberOfAttributes()
167             print "  -Identifier:",familyIdentifier
168             print "  -Number Of Attributes:",nbOfAtt
169             attributesids = familycorba.getAttributesIdentifiers()
170             attributesvals = familycorba.getAttributesValues()
171             for k in range(nbOfAtt):
172                 print "    * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
173             print "  -Entities list:"
174             if (familyBool):
175                 print "  -Is on all entities"
176             else:
177                 types = familycorba.getTypes()
178                 nbOfTypes = len(types)
179                 print "  -Number Of Types:",nbOfTypes
180                 for k in range(nbOfTypes):
181                     type = types[k]
182                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
183                     number = familycorba.getNumber(type)
184                     print "    * Type",type
185                     print "    * Number",number[0:nbOfElmtsOfType]
186                 print ""
187                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
188                 nbOfComp = 1
189                 print "Generate a Local scalar double field"
190                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
191                 for k in range(lengthValue):
192                     valueI = []
193                     for kcomp in range(nbOfComp):
194                         valueI.append(random())
195                     fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
196                     valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
197                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
198                 print "Generate a Corba scalar double field"
199                 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
200                 AnalyzeField(fieldScalDblCorba)
201                 print ""
202                 print "Generate a Local scalar integer field"
203                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
204                 for k in range(lengthValue):
205                     valueI = []
206                     for kcomp in range(nbOfComp):
207                         valueI.append(randint(0,100))
208                     fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
209                     valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
210                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
211                 print "Generate a Corba scalar integer field"
212                 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
213                 AnalyzeField(fieldScalIntCorba)
214                 print ""
215                 nbOfComp = spaceDim
216                 print "Generate a Local vector double field"
217                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
218                 for k in range(lengthValue):
219                     valueI = []
220                     for kcomp in range(nbOfComp):
221                         valueI.append(random())
222                     fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
223                     valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
224                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
225                 print "Generate a Corba vector double field"
226                 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
227                 AnalyzeField(fieldVectDblCorba)
228                 print ""
229                 print "Generate a Local vector integer field"
230                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
231                 for k in range(lengthValue):
232                     valueI = []
233                     for kcomp in range(nbOfComp):
234                         valueI.append(randint(0,100))
235                     fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
236                     valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
237                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
238                 print "Generate a Corba vector integer field"
239                 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
240                 AnalyzeField(fieldVectIntCorba)
241                 print ""
242 print "Fin du script Python ...."
243
244