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