Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MED / Med_Gen_test.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 ####################################################################################################
23 # Test the Med Component: mounting in Memory a .med file and trying to get information through
24 # the CORBA Med API
25 ####################################################################################################
26 #
27 import string
28
29 import salome
30
31 import SALOME_MED
32
33 from libSALOME_Swig import *
34 sg = SALOMEGUI_Swig()
35
36 def print_ord(i):
37     if i == 0:
38         return 'first'
39     elif i == 1:
40         return 'second'
41     elif i == 2:
42         return 'third'
43     else:
44         return `(i+1)`+'th'
45
46 def changeBlankToUnderScore(stringWithBlank):
47     blank = ' '
48     underscore = '_'
49     decompString = string.split(stringWithBlank,blank)
50     length = len(decompString)
51     stringWithUnderScore = decompString[0]
52     for i in range(1,length):
53         stringWithUnderScore += underscore
54         stringWithUnderScore += decompString[i]
55     return stringWithUnderScore
56
57 def getMedObjectFromStudy(file):
58     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
59     compNameInStudy= "MED"
60     listOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
61     listLength = len(listOfSO)
62     if (listLength == 0) :
63         print objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
64         return None
65     elif (listLength > 1) :
66         print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
67         return None
68     mySO = listOfSO[0]
69     if (mySO == None) :
70         print objNameInStudy," cannot be found in the Study"
71         return mySO
72     else:
73         anAttr = mySO.FindAttribute("AttributeIOR")[1]
74         obj = salome.orb.string_to_object(anAttr.Value())
75         myObj = obj._narrow(SALOME_MED.MED)
76         if (myObj == None) :
77             print objNameInStudy," has been found in the Study but with the wrong type"
78         return myObj
79
80 def getMeshObjectFromStudy(meshName):
81     objNameInStudy = "/Med/MEDMESH/"+meshName
82     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
83     if (mySO == None) :
84         print objNameInStudy," cannot be found in the Study"
85         return mySO
86     else:
87         anAttr = mySO.FindAttribute("AttributeIOR")[1]
88         obj = salome.orb.string_to_object(anAttr.Value())
89         myObj = obj._narrow(SALOME_MED.MESH)
90         if (myObj == None) :
91             print objNameInStudy," has been found in the Study but with the wrong type"
92         return myObj
93
94 def getSupportObjectFromStudy(meshName,supportName):
95     meshNameStudy = changeBlankToUnderScore(meshName)
96     objNameInStudy = "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy+"/"+supportName
97     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
98     if (mySO == None) :
99         print objNameInStudy," cannot be found in the Study"
100         return mySO
101     else:
102         anAttr = mySO.FindAttribute("AttributeIOR")[1]
103         obj = salome.orb.string_to_object(anAttr.Value())
104         myObj = obj._narrow(SALOME_MED.SUPPORT)
105         if (myObj == None) :
106             print objNameInStudy," has been found in the Study but with the wrong type"
107         return myObj
108
109 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
110     meshNameStudy = changeBlankToUnderScore(meshName)
111     objNameInStudy = "/Med/MEDFIELD/"+fieldName+"/("+str(dt)+","+str(it)+")_ON_"+supportName+"_OF_"+meshNameStudy
112     mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
113     if (mySO == None) :
114         print objNameInStudy," cannot be found in the Study"
115         return mySO
116     else:
117         anAttr = mySO.FindAttribute("AttributeIOR")[1]
118         obj = salome.orb.string_to_object(anAttr.Value())
119         myObj = obj._narrow(SALOME_MED.FIELDINT)
120         if (myObj == None):
121             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
122             if (myObj == None) :
123                 print objNameInStudy," has been found in the Study but with the wrong type"
124         return myObj
125
126 fileName = "cube_hexa8_quad4.med"
127
128 #fileName = "carre_en_quad4_seg2.med"
129
130 medComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
131
132 import os
133
134 filePath=os.environ["MED_ROOT_DIR"]
135 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
136
137 filePathName = os.path.join(filePath, fileName)
138
139 print "Reading the .med file ",filePathName," and pushing corba objects in the SALOME study"
140 medComp.readStructFileWithFieldType(filePathName,salome.myStudyName)
141 sg.updateObjBrowser(1)
142
143 print "getting the MED object from the study"
144 medObj = getMedObjectFromStudy(fileName)
145
146 nbOfMeshes = medObj.getNumberOfMeshes()
147 meshNames = medObj.getMeshNames()
148
149 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
150 for i in range(nbOfMeshes):
151     meshName = meshNames[i]
152     print "    - the ",print_ord(i)," mesh is named ",meshName
153     print "      getting the MESH object using the API of the corba object MED"
154     meshObj = medObj.getMeshByName(meshName)
155     print "      getting mesh information (including corba object) using the API of the corba object MESH"
156     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
157                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
158         nbFam = meshObj.getNumberOfFamilies(entity)
159         nbGrp = meshObj.getNumberOfGroups(entity)
160         if (entity == SALOME_MED.MED_NODE):
161             print "      this mesh has ",nbFam," Node Family(ies) and ",nbGrp," Node Group(s)"
162         elif (entity == SALOME_MED.MED_CELL):
163             print "                    ",nbFam," Cell Family(ies) and ",nbGrp," Cell Group(s)"
164         elif (entity == SALOME_MED.MED_FACE):
165             print "                    ",nbFam," Face Family(ies) and ",nbGrp," Face Group(s)"
166         elif (entity == SALOME_MED.MED_EDGE):
167             print "                    ",nbFam," Edge Family(ies) and ",nbGrp," Cell Group(s)"
168
169         if nbFam > 0:
170             for j in range(nbFam):
171                 familyObj = meshObj.getFamily(entity,j+1)
172                 print familyObj
173
174         if nbGrp > 0:
175             for j in range(nbGrp):
176                 groupObj = meshObj.getGroup(entity,j+1)
177                 print groupObj
178
179     print ""
180     print "      getting the MESH object from the Study"
181     meshObj = getMeshObjectFromStudy(meshName)
182     print meshObj
183     print "      getting mesh information using the API of the corba object MESH but corba objects are obtained from the Study"
184     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
185                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
186         nbFam = meshObj.getNumberOfFamilies(entity)
187         nbGrp = meshObj.getNumberOfGroups(entity)
188         if (entity == SALOME_MED.MED_NODE):
189             print "      this mesh has ",nbFam," Node Family(ies) and ",nbGrp," Node Group(s)"
190         elif (entity == SALOME_MED.MED_CELL):
191             print "                    ",nbFam," Cell Family(ies) and ",nbGrp," Cell Group(s)"
192         elif (entity == SALOME_MED.MED_FACE):
193             print "                    ",nbFam," Face Family(ies) and ",nbGrp," Face Group(s)"
194         elif (entity == SALOME_MED.MED_EDGE):
195             print "                    ",nbFam," Edge Family(ies) and ",nbGrp," Cell Group(s)"
196
197         if nbFam > 0:
198             for j in range(nbFam):
199                 familyName = (meshObj.getFamily(entity,j+1)).getName()
200                 familyObj = getSupportObjectFromStudy(meshName,familyName)
201                 print familyObj
202
203         if nbGrp > 0:
204             for j in range(nbGrp):
205                 groupName = (meshObj.getGroup(entity,j+1)).getName()
206                 groupObj = getSupportObjectFromStudy(meshName,groupName)
207                 print groupObj
208
209     print "let's get other SUPPORT object from  the Study"
210     for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
211                    SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
212
213         if entity == SALOME_MED.MED_NODE :
214             entitySupport = "MED_NOEUD"
215         elif entity == SALOME_MED.MED_CELL :
216             entitySupport = "MED_MAILLE"
217         elif entity == SALOME_MED.MED_FACE :
218             entitySuppor = "MED_FACE"
219         elif entity == SALOME_MED.MED_EDGE :
220             entitySupport = "MED_ARETE"
221
222         supportName = "SupportOnAll_"+entitySupport
223         supportObj = getSupportObjectFromStudy(meshName,supportName)
224
225 nbOfFields = medObj.getNumberOfFields()
226 print "in the considered .med file there is(are) ",nbOfFields," field(s):"
227 fieldNames = medObj.getFieldNames()
228 for i in range(nbOfFields):
229     fieldName = fieldNames[i]
230     nbOfIt = medObj.getFieldNumberOfIteration(fieldName)
231     print "    - the ",print_ord(i)," field is name ",fieldName," and has ",nbOfIt," iteration(s)"
232     for j in range(nbOfIt):
233         dtitfield = medObj.getFieldIteration(fieldName,j)
234         dt = dtitfield[0]
235         it = dtitfield[1]
236         print "     * Iteration:",dt,"Order number:",it
237         for k in range(nbOfMeshes):
238             meshName = meshNames[k]
239             for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,
240                            SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
241                 if entity == SALOME_MED.MED_NODE :
242                     entitySupport = "MED_NOEUD"
243                 elif entity == SALOME_MED.MED_CELL :
244                     entitySupport = "MED_MAILLE"
245                 elif entity == SALOME_MED.MED_FACE :
246                     entitySuppor = "MED_FACE"
247                 elif entity == SALOME_MED.MED_EDGE :
248                     entitySupport = "MED_ARETE"
249                 supportName = "SupportOnAll_"+entitySupport
250                 print "getting a corba object Field from the study iteration ",dt," order number ",it," on the support ",supportName," from the mesh ",meshName
251                 fieldObj = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
252                 print fieldObj
253
254 print ""
255 print "END of the Pyhton script ..... Ctrl D to exit"