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