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