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