1 # MED MedCorba_Swig : binding of MED CORBA objects woth Python
3 # Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 # See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 # File : medcorba_test.py
31 from libMedCorba_Swig import *
37 filePath=os.environ["MED_ROOT_DIR"]
38 filePath=filePath+"/share/salome/resources/"
40 #==============================================================================
42 def AnalyzeField(field):
43 name = field.getName()
44 desc = field.getDescription()
45 nbComp = field.getNumberOfComponents()
46 itNum = field.getIterationNumber()
47 ordNum = field.getOrderNumber()
49 print "Analysis of the field ",name," with the description ",desc
50 print "iteration number ",itNum," order Number ",ordNum
51 print "It has ",nbComp," component(s)"
53 fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
54 fieldSupport = field.getSupport()
55 fieldMesh = fieldSupport.getMesh()
56 fieldEntity = fieldSupport.getEntity()
57 bool = fieldSupport.isOnAllElements()
60 print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
61 if fieldEntity == SALOME_MED.MED_NODE:
62 nbValByComp = fieldMesh.getNumberOfNodes()
64 nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
65 print "and its dimension (number of values by component of the field) is ",nbValByComp
67 print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
68 nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
69 print "and its dimension (number of values by component of the field) is ",nbValByComp
71 for i in range(nbComp):
72 compName = field.getComponentName(i+1)
73 compUnit = field.getComponentUnit(i+1)
74 print "The ",(i+1),"-th component ",compName," with the unit ",compUnit
76 for i in range(nbValByComp):
77 print " * ",fieldValue[i*nbComp:(i+1)*nbComp]
79 #==============================================================================
81 def getMeshObjectFromStudy(number):
82 mySO = salome.myStudy.FindObject("MEDMESH")
83 mysub = mySO.FindSubObject(number)[1]
85 Builder = salome.myStudy.NewBuilder()
86 anAttr = Builder.FindOrCreateAttribute(mysub, "AttributeIOR")
87 obj = salome.orb.string_to_object(anAttr.Value())
88 myObj = obj._narrow(SALOME_MED.MESH)
91 print "ERROR: No Mesh Object stored in this Study"
94 #==============================================================================
96 def getMedObjectFromStudy():
97 mySO = salome.myStudy.FindObject("Objet MED")
98 Builder = salome.myStudy.NewBuilder()
99 anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
100 obj = salome.orb.string_to_object(anAttr.Value())
101 myObj = obj._narrow(SALOME_MED.MED)
104 #==============================================================================
106 studyCurrent = salome.myStudyName
107 studyCurrentId = salome.myStudyId
109 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
112 #medFile = "carre_en_quad4_seg2.med"
113 medFile = filePath + "cube_hexa8_quad4.med"
115 print "Loading of the Med Component"
118 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
120 med_comp.readStructFile(medFile,studyCurrent)
122 med_obj = getMedObjectFromStudy()
124 nbMeshes = med_obj.getNumberOfMeshes()
126 nbFields = med_obj.getNumberOfFields()
129 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
132 meshcorba = getMeshObjectFromStudy(1)
134 name = meshcorba.getName()
136 nbNodes = meshcorba.getNumberOfNodes()
138 spaceDim = meshcorba.getSpaceDimension()
140 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
143 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
144 nbFam = meshcorba.getNumberOfFamilies(entity)
145 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
146 print "This mesh has",nbFam,"Node Family(ies)"
147 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
148 print "This mesh has",nbFam,"Cell Family(ies)"
149 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
150 print "This mesh has",nbFam,"Face Family(ies)"
151 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
152 print "This mesh has",nbFam,"Edge Family(ies)"
155 for j in range(nbFam):
157 familycorba = meshcorba.getFamily(entity,j+1)
158 familyName = familycorba.getName()
159 familyDescription = familycorba.getDescription()
160 familyEntity = familycorba.getEntity()
161 familyBool = familycorba.isOnAllElements()
162 print " -Name:",familyName
163 print " -Description:",familyDescription
164 print " -Entity:",familyEntity
165 familyIdentifier = familycorba.getIdentifier()
166 nbOfAtt = familycorba.getNumberOfAttributes()
167 print " -Identifier:",familyIdentifier
168 print " -Number Of Attributes:",nbOfAtt
169 attributesids = familycorba.getAttributesIdentifiers()
170 attributesvals = familycorba.getAttributesValues()
171 for k in range(nbOfAtt):
172 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
173 print " -Entities list:"
175 print " -Is on all entities"
177 types = familycorba.getTypes()
178 nbOfTypes = len(types)
179 print " -Number Of Types:",nbOfTypes
180 for k in range(nbOfTypes):
182 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
183 number = familycorba.getNumber(type)
185 print " * Number",number[0:nbOfElmtsOfType]
187 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
189 print "Generate a Local scalar double field"
190 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
191 for k in range(lengthValue):
193 for kcomp in range(nbOfComp):
194 valueI.append(random())
195 fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
196 valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
197 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
198 print "Generate a Corba scalar double field"
199 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
200 AnalyzeField(fieldScalDblCorba)
202 print "Generate a Local scalar integer field"
203 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
204 for k in range(lengthValue):
206 for kcomp in range(nbOfComp):
207 valueI.append(randint(0,100))
208 fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
209 valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
210 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
211 print "Generate a Corba scalar integer field"
212 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
213 AnalyzeField(fieldScalIntCorba)
216 print "Generate a Local vector double field"
217 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
218 for k in range(lengthValue):
220 for kcomp in range(nbOfComp):
221 valueI.append(random())
222 fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
223 valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
224 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
225 print "Generate a Corba vector double field"
226 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
227 AnalyzeField(fieldVectDblCorba)
229 print "Generate a Local vector integer field"
230 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
231 for k in range(lengthValue):
233 for kcomp in range(nbOfComp):
234 valueI.append(randint(0,100))
235 fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
236 valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
237 print " Set/Get Entry *",(k+1)," ",valueI[:nbOfComp]," / ",valueIverif[:nbOfComp]
238 print "Generate a Corba vector integer field"
239 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
240 AnalyzeField(fieldVectIntCorba)
242 print "Fin du script Python ...."