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