1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 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 : batchmode_medcorba_test.py
28 import batchmode_salome
32 from libMedCorba_Swig import *
33 from libMEDClient import *
39 filePath = os.environ["MED_ROOT_DIR"]
40 filePath = os.path.join( filePath, "share", "salome", "resources", "med" )
42 #==============================================================================
44 def AnalyzeField(field):
45 name = field.getName()
46 desc = field.getDescription()
47 nbComp = field.getNumberOfComponents()
48 itNum = field.getIterationNumber()
49 ordNum = field.getOrderNumber()
51 print "Analysis of the field ",name," with the description ",desc
52 print "iteration number ",itNum," order Number ",ordNum
53 print "It has ",nbComp," component(s)"
55 fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
56 fieldSupport = field.getSupport()
57 fieldMesh = fieldSupport.getMesh()
58 fieldEntity = fieldSupport.getEntity()
59 bool = fieldSupport.isOnAllElements()
62 print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
63 if fieldEntity == SALOME_MED.MED_NODE:
64 nbValByComp = fieldMesh.getNumberOfNodes()
66 nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
67 print "and its dimension (number of values by component of the field) is ",nbValByComp
69 print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
70 nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
71 print "and its dimension (number of values by component of the field) is ",nbValByComp
73 for i in range(nbComp):
74 compName = field.getComponentName(i+1)
75 compUnit = field.getComponentUnit(i+1)
76 print "The ",(i+1),"-th component ",compName," with the unit ",compUnit
78 for i in range(nbValByComp):
79 print " * ",fieldValue[i*nbComp:(i+1)*nbComp]
81 #==============================================================================
83 def getMeshObjectFromStudy(meshName):
84 objNameInStudy = "/Med/MEDMESH/"+meshName
85 mySO = batchmode_salome.myStudy.FindObjectByPath(objNameInStudy)
87 print "ERROR: ",objNameInStudy," cannot be found in the Study"
90 anAttr = mySO.FindAttribute("AttributeIOR")[1]
91 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
92 myObj = obj._narrow(SALOME_MED.MESH)
94 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
98 #==============================================================================
100 def getFieldDoubleObjectFromStudy(number,subnumber):
101 mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
102 mysub = mySO.FindSubObject(number)[1]
104 mysubsub = mysub.FindSubObject(subnumber)[1]
106 Builder = batchmode_salome.myStudy.NewBuilder()
107 anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
108 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
109 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
112 print "ERROR: No FieldDouble Object stored in this Study"
115 print "ERROR: No FieldDouble Object stored in this Study"
119 #==============================================================================
121 def getFieldIntObjectFromStudy(number,subnumber):
122 mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
123 mysub = mySO.FindSubObject(number)[1]
125 mysubsub = mysub.FindSubObject(subnumber)[1]
127 Builder = batchmode_salome.myStudy.NewBuilder()
128 anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
129 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
130 myObj = obj._narrow(SALOME_MED.FIELDINT)
133 print "ERROR: No FieldInt Object stored in this Study"
136 print "ERROR: No FieldInt Object stored in this Study"
139 #==============================================================================
141 def getMedObjectFromStudy(file):
142 objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
143 compNameInStudy= "MED"
144 listOfSO = batchmode_salome.myStudy.FindObjectByName(objNameInStudy,
146 listLength = len(listOfSO)
147 if (listLength == 0) :
148 print "ERROR: ",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
150 elif (listLength > 1) :
151 print "ERROR: there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
155 print "ERROR: ",objNameInStudy," cannot be found in the Study"
158 anAttr = mySO.FindAttribute("AttributeIOR")[1]
159 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
160 myObj = obj._narrow(SALOME_MED.MED)
162 print "ERROR: ",objNameInStudy," has been found in the Study but with the wrong type"
165 #==============================================================================
167 # Since Corba pointeurs will be generated in this Python script
168 # a POA has to be registred to activate those Corba pointeurs
169 # and make them available by any clients (in general). In our
170 # case, it is to make those pointeurs avail able to itself
171 #==============================================================================
173 print "Activation of a POA to make available any Corba pointeurs"
174 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
175 poaManager = poa._get_the_POAManager()
176 poaManager.activate()
178 #==============================================================================
180 studyCurrent = batchmode_salome.myStudyName
181 studyCurrentId = batchmode_salome.myStudyId
183 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
186 fileName = "cube_hexa8_quad4.med"
187 #medFile = "carre_en_quad4_seg2.med"
188 medFile = os.path.join( filePath, fileName )
190 print "Loading of the Med Component"
193 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
195 med_comp.readStructFileWithFieldType(medFile,studyCurrent)
197 med_obj = getMedObjectFromStudy(fileName)
199 nbMeshes = med_obj.getNumberOfMeshes()
201 nbFields = med_obj.getNumberOfFields()
203 meshNames = med_obj.getMeshNames()
206 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
209 meshName = meshNames[0]
211 meshcorba = getMeshObjectFromStudy(meshName)
213 nbNodes = meshcorba.getNumberOfNodes()
215 spaceDim = meshcorba.getSpaceDimension()
217 print "The mesh from the Study is ",meshName,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
220 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
221 nbFam = meshcorba.getNumberOfFamilies(entity)
222 if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
223 print "This mesh has",nbFam,"Node Family(ies)"
224 elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
225 print "This mesh has",nbFam,"Cell Family(ies)"
226 elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
227 print "This mesh has",nbFam,"Face Family(ies)"
228 elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
229 print "This mesh has",nbFam,"Edge Family(ies)"
232 for j in range(nbFam):
234 familycorba = meshcorba.getFamily(entity,j+1)
235 familyName = familycorba.getName()
236 familyDescription = familycorba.getDescription()
237 familyEntity = familycorba.getEntity()
238 familyBool = familycorba.isOnAllElements()
239 print " -Name:",familyName
240 print " -Description:",familyDescription
241 print " -Entity:",familyEntity
242 familyIdentifier = familycorba.getIdentifier()
243 nbOfAtt = familycorba.getNumberOfAttributes()
244 print " -Identifier:",familyIdentifier
245 print " -Number Of Attributes:",nbOfAtt
246 attributesids = familycorba.getAttributesIdentifiers()
247 attributesvals = familycorba.getAttributesValues()
248 for k in range(nbOfAtt):
249 print " * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
250 print " -Entities list:"
252 print " -Is on all entities"
254 types = familycorba.getTypes()
255 nbOfTypes = len(types)
256 print " -Number Of Types:",nbOfTypes
257 for k in range(nbOfTypes):
259 nbOfElmtsOfType = familycorba.getNumberOfElements(type)
260 number = familycorba.getNumber(type)
262 print " * Number",number[0:nbOfElmtsOfType]
264 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
267 supportOutLocal = SUPPORTClient( familycorba )
268 supportOutCorba = createCorbaSupport( supportOutLocal )
270 print "\nGenerate a Local scalar double field"
271 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
272 value = [ random() for k in range(lengthValue*nbOfComp) ]
273 fieldScalDblLoc.setValue( value ) ## the only way as field support not defined
274 valueIverif = fieldScalDblLoc.getValue()
276 print " Get ",valueIverif
278 print "\nGenerate a Corba scalar double field"
279 fieldScalDblLoc.setSupport( supportOutLocal )
280 fieldScalDblCorba = createCorbaFieldDouble(supportOutCorba,fieldScalDblLoc)
281 AnalyzeField(fieldScalDblCorba)
283 print "Generate a Local scalar integer field"
284 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
285 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
286 fieldScalIntLoc.setValue( value ) ## the only way as field support not defined
287 valueIverif = fieldScalIntLoc.getValue()
289 print " Get ",valueIverif
291 print "\nGenerate a Corba scalar integer field"
292 fieldScalIntLoc.setSupport( supportOutLocal )
293 fieldScalIntCorba = createCorbaFieldInt(supportOutCorba,fieldScalIntLoc)
294 AnalyzeField(fieldScalIntCorba)
297 print "\nGenerate a Local vector double field"
298 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
299 value = [ random() for k in range(lengthValue*nbOfComp) ]
300 fieldVectDblLoc.setValue(value)
301 valueIverif = fieldVectDblLoc.getValue()
303 print " Get ",valueIverif
305 print "\nGenerate a Corba vector double field"
306 fieldVectDblLoc.setSupport( supportOutLocal )
307 fieldVectDblCorba = createCorbaFieldDouble(supportOutCorba,fieldVectDblLoc)
308 AnalyzeField(fieldVectDblCorba)
310 print "\nGenerate a Local vector integer field"
311 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
312 value = [ randint(0,100) for k in range(lengthValue*nbOfComp) ]
313 fieldVectIntLoc.setValue(value)
314 valueIverif = fieldVectIntLoc.getValue()
316 print " Get ",valueIverif
318 print "\nGenerate a Corba vector integer field"
319 fieldVectIntLoc.setSupport( supportOutLocal )
320 fieldVectIntCorba = createCorbaFieldInt(supportOutCorba,fieldVectIntLoc)
321 AnalyzeField(fieldVectIntCorba)
323 print "Fin du script Python ...."