Salome HOME
MEDMEM suppression
[modules/med.git] / src / MedCorba_Swig / batchmode_medcorba_test.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  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   : batchmode_medcorba_test.py
26 #  Module : MED
27 #
28 import batchmode_salome
29
30 import SALOME_MED
31
32 from libMedCorba_Swig import *
33 from libMEDClient import *
34
35 from random import *
36
37 import os
38
39 filePath = os.environ["MED_ROOT_DIR"]
40 filePath = os.path.join( filePath, "share", "salome", "resources", "med" )
41
42 #==============================================================================
43
44 def AnalyzeField(field):
45     name = field.getName()
46     desc = field.getDescription()
47     nbComp = field.getNumberOfComponents()
48     itNum = field.getIterationNumber()
49     ordNum = field.getOrderNumber()
50
51     print "Analysis of the field ",name," with the description ",desc
52     print "iteration number ",itNum," order Number ",ordNum
53     print "It has ",nbComp," component(s)"
54
55     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
56     fieldSupport = field.getSupport()
57     fieldMesh = fieldSupport.getMesh()
58     fieldEntity = fieldSupport.getEntity()
59     bool = fieldSupport.isOnAllElements()
60
61     if bool:
62         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
63         if fieldEntity == SALOME_MED.MED_NODE:
64             nbValByComp = fieldMesh.getNumberOfNodes()
65         else:
66             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
67         print "and its dimension (number of values by component of the field) is ",nbValByComp
68     else:
69         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
70         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
71         print "and its dimension (number of values by component of the field) is ",nbValByComp
72
73     for i in range(nbComp):
74         compName = field.getComponentName(i+1)
75         compUnit = field.getComponentUnit(i+1)
76         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
77
78     for i in range(nbValByComp):
79         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
80
81 #==============================================================================
82
83 def getMeshObjectFromStudy(meshName):
84     objNameInStudy = "/Med/MEDMESH/"+meshName
85     mySO = batchmode_salome.myStudy.FindObjectByPath(objNameInStudy)
86     if (mySO == None) :
87         print "ERROR: ",objNameInStudy," cannot be found in the Study"
88         return mySO
89     else:
90         anAttr = mySO.FindAttribute("AttributeIOR")[1]
91         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
92         myObj = obj._narrow(SALOME_MED.MESH)
93         if (myObj == None) :
94             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
95         return myObj
96
97
98 #==============================================================================
99
100 def getFieldDoubleObjectFromStudy(number,subnumber):
101     mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
102     mysub = mySO.FindSubObject(number)[1]
103     if mysub:
104         mysubsub = mysub.FindSubObject(subnumber)[1]
105         if mysubsub:
106             Builder = batchmode_salome.myStudy.NewBuilder()
107             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
108             obj = batchmode_salome.orb.string_to_object(anAttr.Value())
109             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
110             return myObj
111         else:
112             print "ERROR: No FieldDouble Object stored in this Study"
113             return None
114     else:
115         print "ERROR: No FieldDouble Object stored in this Study"
116         return None
117
118
119 #==============================================================================
120
121 def getFieldIntObjectFromStudy(number,subnumber):
122     mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
123     mysub = mySO.FindSubObject(number)[1]
124     if mysub:
125         mysubsub = mysub.FindSubObject(subnumber)[1]
126         if mysubsub:
127             Builder = batchmode_salome.myStudy.NewBuilder()
128             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
129             obj = batchmode_salome.orb.string_to_object(anAttr.Value())
130             myObj = obj._narrow(SALOME_MED.FIELDINT)
131             return myObj
132         else:
133             print "ERROR: No FieldInt Object stored in this Study"
134             return None
135     else:
136         print "ERROR: No FieldInt Object stored in this Study"
137         return None
138
139 #==============================================================================
140
141 def getMedObjectFromStudy(file):
142     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
143     compNameInStudy= "MED"
144     listOfSO = batchmode_salome.myStudy.FindObjectByName(objNameInStudy,
145                                                          compNameInStudy)
146     listLength = len(listOfSO)
147     if (listLength == 0) :
148         print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
149         return None
150     elif (listLength > 1) :
151         print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
152         return None
153     mySO = listOfSO[0]
154     if (mySO == None) :
155         print "ERROR: ",objNameInStudy," cannot be found in the Study"
156         return mySO
157     else:
158         anAttr = mySO.FindAttribute("AttributeIOR")[1]
159         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
160         myObj = obj._narrow(SALOME_MED.MED)
161         if (myObj == None) :
162             print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
163         return myObj
164
165 #==============================================================================
166 #
167 # Since Corba pointeurs will be generated in this Python script
168 # a POA has to be registred to activate those Corba pointeurs
169 # and make them available by any clients (in general). In our
170 # case, it is to make those pointeurs avail able to itself
171 #==============================================================================
172
173 print "Activation of a POA to make available any Corba pointeurs"
174 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
175 poaManager = poa._get_the_POAManager()
176 poaManager.activate()
177
178 #==============================================================================
179
180 studyCurrent = batchmode_salome.myStudyName
181 studyCurrentId = batchmode_salome.myStudyId
182
183 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
184 print ""
185
186 fileName = "cube_hexa8_quad4.med"
187 #medFile = "carre_en_quad4_seg2.med"
188 medFile = os.path.join( filePath, fileName )
189
190 print "Loading of the Med Component"
191 print ""
192
193 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
194
195 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
196
197 med_obj = getMedObjectFromStudy(fileName)
198
199 nbMeshes = med_obj.getNumberOfMeshes()
200
201 nbFields = med_obj.getNumberOfFields()
202
203 meshNames = med_obj.getMeshNames()
204
205 print ""
206 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
207 print ""
208
209 meshName = meshNames[0]
210
211 meshcorba = getMeshObjectFromStudy(meshName)
212
213 nbNodes = meshcorba.getNumberOfNodes()
214
215 spaceDim = meshcorba.getSpaceDimension()
216
217 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
218 print ""
219
220 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
221     nbFam = meshcorba.getNumberOfFamilies(entity)
222     if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
223         print "This mesh has",nbFam,"Node Family(ies)"
224     elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
225         print "This mesh has",nbFam,"Cell Family(ies)"
226     elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
227         print "This mesh has",nbFam,"Face Family(ies)"
228     elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
229         print "This mesh has",nbFam,"Edge Family(ies)"
230
231     if nbFam > 0:
232         for j in range(nbFam):
233             print ""
234             familycorba = meshcorba.getFamily(entity,j+1)
235             familyName = familycorba.getName()
236             familyDescription = familycorba.getDescription()
237             familyEntity = familycorba.getEntity()
238             familyBool = familycorba.isOnAllElements()
239             print "  -Name:",familyName
240             print "  -Description:",familyDescription
241             print "  -Entity:",familyEntity
242             familyIdentifier = familycorba.getIdentifier()
243             nbOfAtt = familycorba.getNumberOfAttributes()
244             print "  -Identifier:",familyIdentifier
245             print "  -Number Of Attributes:",nbOfAtt
246             attributesids = familycorba.getAttributesIdentifiers()
247             attributesvals = familycorba.getAttributesValues()
248             for k in range(nbOfAtt):
249                 print "    * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
250             print "  -Entities list:"
251             if (familyBool):
252                 print "  -Is on all entities"
253             else:
254                 types = familycorba.getTypes()
255                 nbOfTypes = len(types)
256                 print "  -Number Of Types:",nbOfTypes
257                 for k in range(nbOfTypes):
258                     type = types[k]
259                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
260                     number = familycorba.getNumber(type)
261                     print "    * Type",type
262                     print "    * Number",number[0:nbOfElmtsOfType]
263                 print ""
264                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
265                 nbOfComp = 1
266
267                 supportOutLocal = SUPPORTClient( familycorba )
268                 supportOutCorba = createCorbaSupport( supportOutLocal )
269
270                 print "\nGenerate a Local scalar double field"
271                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
272                 value = [ random() for k in range(lengthValue*nbOfComp) ]
273                 fieldScalDblLoc.setValue( value ) ## the only way as field support not defined
274                 valueIverif = fieldScalDblLoc.getValue()
275                 print "     Set ",value
276                 print "     Get ",valueIverif
277
278                 print "\nGenerate a Corba scalar double field"
279                 fieldScalDblLoc.setSupport( supportOutLocal )
280                 fieldScalDblCorba = createCorbaFieldDouble(supportOutCorba,fieldScalDblLoc)
281                 AnalyzeField(fieldScalDblCorba)
282
283                 print "Generate a Local scalar integer field"
284                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
285                 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
286                 fieldScalIntLoc.setValue( value ) ## the only way as field support not defined
287                 valueIverif = fieldScalIntLoc.getValue()
288                 print "     Set ",value
289                 print "     Get ",valueIverif
290
291                 print "\nGenerate a Corba scalar integer field"
292                 fieldScalIntLoc.setSupport( supportOutLocal )
293                 fieldScalIntCorba = createCorbaFieldInt(supportOutCorba,fieldScalIntLoc)
294                 AnalyzeField(fieldScalIntCorba)
295
296                 nbOfComp = spaceDim
297                 print "\nGenerate a Local vector double field"
298                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
299                 value = [ random() for k in range(lengthValue*nbOfComp) ]
300                 fieldVectDblLoc.setValue(value)
301                 valueIverif = fieldVectDblLoc.getValue()
302                 print "     Set ",value
303                 print "     Get ",valueIverif
304
305                 print "\nGenerate a Corba vector double field"
306                 fieldVectDblLoc.setSupport( supportOutLocal )
307                 fieldVectDblCorba = createCorbaFieldDouble(supportOutCorba,fieldVectDblLoc)
308                 AnalyzeField(fieldVectDblCorba)
309
310                 print "\nGenerate a Local vector integer field"
311                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
312                 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
313                 fieldVectIntLoc.setValue(value)
314                 valueIverif = fieldVectIntLoc.getValue()
315                 print "     Set ",value
316                 print "     Get ",valueIverif
317
318                 print "\nGenerate a Corba vector integer field"
319                 fieldVectIntLoc.setSupport( supportOutLocal )
320                 fieldVectIntCorba = createCorbaFieldInt(supportOutCorba,fieldVectIntLoc)
321                 AnalyzeField(fieldVectIntCorba)
322                 print ""
323 print "Fin du script Python ...."
324
325