1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # MED MedCorba_Swig : binding of MED CORBA objects woth Python
25 # File : medcorba_test.py
32 from libSALOME_Swig import *
35 from libMedCorba_Swig import *
36 from libMEDClient import *
42 filePath = os.environ["MED_ROOT_DIR"]
43 filePath = os.path.join( filePath, "share", "salome", "resources", "med" )
45 #==============================================================================
47 def AnalyzeField(field):
48 name = field.getName()
49 desc = field.getDescription()
50 nbComp = field.getNumberOfComponents()
51 itNum = field.getIterationNumber()
52 ordNum = field.getOrderNumber()
54 print "Analysis of the field ",name," with the description ",desc
55 print "iteration number ",itNum," order Number ",ordNum
56 print "It has ",nbComp," component(s)"
58 fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
59 fieldSupport = field.getSupport()
60 fieldMesh = fieldSupport.getMesh()
61 fieldEntity = fieldSupport.getEntity()
62 bool = fieldSupport.isOnAllElements()
65 print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
66 if fieldEntity == SALOME_MED.MED_NODE:
67 nbValByComp = fieldMesh.getNumberOfNodes()
69 nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
70 print "and its dimension (number of values by component of the field) is ",nbValByComp
72 print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
73 nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
74 print "and its dimension (number of values by component of the field) is ",nbValByComp
76 for i in range(nbComp):
77 compName = field.getComponentName(i+1)
78 compUnit = field.getComponentUnit(i+1)
79 print "The ",(i+1),"-th component ",compName," with the unit ",compUnit
81 for i in range(nbValByComp):
82 print " * ",fieldValue[i*nbComp:(i+1)*nbComp]
84 #==============================================================================
86 def getMeshObjectFromStudy(meshName):
87 objNameInStudy = "/Med/MEDMESH/"+meshName
88 mySO = salome.myStudy.FindObjectByPath(objNameInStudy)
90 print "ERROR: ",objNameInStudy," cannot be found in the Study"
93 anAttr = mySO.FindAttribute("AttributeIOR")[1]
94 obj = salome.orb.string_to_object(anAttr.Value())
95 myObj = obj._narrow(SALOME_MED.MESH)
97 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
100 #==============================================================================
102 def getMedObjectFromStudy(file):
103 objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
104 compNameInStudy= "MED"
105 listOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
106 listLength = len(listOfSO)
107 if (listLength == 0) :
108 print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
110 elif (listLength > 1) :
111 print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
115 print "ERROR: ",objNameInStudy," cannot be found in the Study"
118 anAttr = mySO.FindAttribute("AttributeIOR")[1]
119 obj = salome.orb.string_to_object(anAttr.Value())
120 myObj = obj._narrow(SALOME_MED.MED)
122 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
125 #==============================================================================
127 studyCurrent = salome.myStudyName
128 studyCurrentId = salome.myStudyId
130 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
133 fileName = "cube_hexa8_quad4.med"
134 #medFile = "carre_en_quad4_seg2.med"
135 medFile = os.path.join( filePath, fileName )
137 print "Loading of the Med Component"
140 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
142 print "Read file", medFile
147 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
148 except SALOME.SALOME_Exception, inst:
149 msg = "SALOME.SALOME_Exception caght, see details:"
150 msg+="\n******* text:\t" + inst.details.text
151 msg+="\n******* type:\t" + str(inst.details.type)
152 msg+="\n******* where:\t" + inst.details.sourceFile + ":" + \
153 str(inst.details.lineNumber)
154 raise RuntimeError, msg
156 sg.updateObjBrowser(1)
158 med_obj = getMedObjectFromStudy(fileName)
160 nbMeshes = med_obj.getNumberOfMeshes()
162 nbFields = med_obj.getNumberOfFields()
164 meshNames = med_obj.getMeshNames()
167 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
170 meshName = meshNames[0]
172 meshcorba = getMeshObjectFromStudy(meshName)
174 nbNodes = meshcorba.getNumberOfNodes()
176 spaceDim = meshcorba.getSpaceDimension()
178 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
181 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
182 nbFam = meshcorba.getNumberOfFamilies(entity)
183 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
184 print "This mesh has",nbFam,"Node Family(ies)"
185 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
186 print "This mesh has",nbFam,"Cell Family(ies)"
187 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
188 print "This mesh has",nbFam,"Face Family(ies)"
189 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
190 print "This mesh has",nbFam,"Edge Family(ies)"
193 for j in range(nbFam):
195 familycorba = meshcorba.getFamily(entity,j+1)
196 familyName = familycorba.getName()
197 familyDescription = familycorba.getDescription()
198 familyEntity = familycorba.getEntity()
199 familyBool = familycorba.isOnAllElements()
200 print " -Name:",familyName
201 print " -Description:",familyDescription
202 print " -Entity:",familyEntity
203 familyIdentifier = familycorba.getIdentifier()
204 nbOfAtt = familycorba.getNumberOfAttributes()
205 print " -Identifier:",familyIdentifier
206 print " -Number Of Attributes:",nbOfAtt
207 attributesids = familycorba.getAttributesIdentifiers()
208 attributesvals = familycorba.getAttributesValues()
209 for k in range(nbOfAtt):
210 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
211 print " -Entities list:"
213 print " -Is on all entities"
215 types = familycorba.getTypes()
216 nbOfTypes = len(types)
217 print " -Number Of Types:",nbOfTypes
218 for k in range(nbOfTypes):
220 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
221 number = familycorba.getNumber(type)
223 print " * Number",number[0:nbOfElmtsOfType]
225 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
228 supportOutLocal = SUPPORTClient( familycorba )
229 supportOutCorba = createCorbaSupport( supportOutLocal )
231 print "\nGenerate a Local scalar double field"
232 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
233 value = [ random() for k in range(lengthValue*nbOfComp) ]
234 fieldScalDblLoc.setValue( value ) ## the only way as field support not defined
235 valueIverif = fieldScalDblLoc.getValue()
237 print " Get ",valueIverif
239 print "\nGenerate a Corba scalar double field"
240 fieldScalDblLoc.setSupport( supportOutLocal )
241 fieldScalDblCorba = createCorbaFieldDouble(supportOutCorba,fieldScalDblLoc)
242 AnalyzeField(fieldScalDblCorba)
244 print "Generate a Local scalar integer field"
245 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
246 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
247 fieldScalIntLoc.setValue( value ) ## the only way as field support not defined
248 valueIverif = fieldScalIntLoc.getValue()
250 print " Get ",valueIverif
252 print "\nGenerate a Corba scalar integer field"
253 fieldScalIntLoc.setSupport( supportOutLocal )
254 fieldScalIntCorba = createCorbaFieldInt(supportOutCorba,fieldScalIntLoc)
255 AnalyzeField(fieldScalIntCorba)
258 print "\nGenerate a Local vector double field"
259 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
260 value = [ random() for k in range(lengthValue*nbOfComp) ]
261 fieldVectDblLoc.setValue(value)
262 valueIverif = fieldVectDblLoc.getValue()
264 print " Get ",valueIverif
266 print "\nGenerate a Corba vector double field"
267 fieldVectDblLoc.setSupport( supportOutLocal )
268 fieldVectDblCorba = createCorbaFieldDouble(supportOutCorba,fieldVectDblLoc)
269 AnalyzeField(fieldVectDblCorba)
271 print "\nGenerate a Local vector integer field"
272 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
273 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
274 fieldVectIntLoc.setValue(value)
275 valueIverif = fieldVectIntLoc.getValue()
277 print " Get ",valueIverif
279 print "\nGenerate a Corba vector integer field"
280 fieldVectIntLoc.setSupport( supportOutLocal )
281 fieldVectIntCorba = createCorbaFieldInt(supportOutCorba,fieldVectIntLoc)
282 AnalyzeField(fieldVectIntCorba)
284 print "Fin du script Python ...."