1 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 ####################################################################################################
23 # CALCULATOR_TEST_STUDY_WITHOUTIHM.py
24 # Test the calculator component: using Med Client classes with writing in a med file
25 # results from the calculator component
26 ####################################################################################################
28 from libMEDClient import *
36 #CCRTfrom libSALOME_Swig import *
37 #CCRTsg = SALOMEGUI_Swig()
43 host = os.getenv( 'HOST' )
44 orb, lcc, naming_service, contmgr = salome.salome_kernel.salome_kernel_init()
45 obj = naming_service.Resolve('myStudyManager')
46 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
47 print "studyManager found"
48 myStudy = myStudyManager.NewStudy('medClient_withoutIHM_test')
49 studynameId = myStudy._get_StudyId()
50 studyname = myStudy._get_Name()
51 print "We are working in the study ",studyname," with the ID ",studynameId
64 def changeBlankToUnderScore(stringWithBlank):
67 decompString = string.split(stringWithBlank,blank)
68 length = len(decompString)
69 stringWithUnderScore = decompString[0]
70 for i in range(1,length):
71 stringWithUnderScore += underscore
72 stringWithUnderScore += decompString[i]
73 return stringWithUnderScore
75 def getMedObjectFromStudy(file):
76 objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
77 compNameInStudy= "MED"
78 #CCRTlistOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
79 listOfSO = myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
80 listLength = len(listOfSO)
81 if (listLength == 0) :
82 print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
84 elif (listLength > 1) :
85 print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
89 print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study"
92 anAttr = mySO.FindAttribute("AttributeIOR")[1]
93 #CCRTobj = salome.orb.string_to_object(anAttr.Value())
94 obj = orb.string_to_object(anAttr.Value())
95 myObj = obj._narrow(SALOME_MED.MED)
97 print objNameInStudy," has been found in the Study but with the wrong type"
100 def getMeshObjectFromStudy(meshName):
101 objNameInStudy = "/Med/MEDMESH/"+meshName
102 #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
103 mySO = myStudy.FindObjectByPath(objNameInStudy)
105 print "getMeshObjectFromStudy",objNameInStudy," cannot be found in the Study"
108 anAttr = mySO.FindAttribute("AttributeIOR")[1]
109 #CCRTobj = salome.orb.string_to_object(anAttr.Value())
110 obj = orb.string_to_object(anAttr.Value())
111 myObj = obj._narrow(SALOME_MED.MESH)
113 print objNameInStudy," has been found in the Study but with the wrong type"
116 def getSupportObjectFromStudy(meshName,supportName):
117 meshNameStudy = changeBlankToUnderScore(meshName)
118 objNameInStudy = "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy+"/"+supportName
119 #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
120 mySO = myStudy.FindObjectByPath(objNameInStudy)
122 print "getSupportObjectFromStudy",objNameInStudy," cannot be found in the Study"
123 print "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy,":",myStudy.GetObjectNames("/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy)
126 anAttr = mySO.FindAttribute("AttributeIOR")[1]
127 #CCRTobj = salome.orb.string_to_object(anAttr.Value())
128 obj = orb.string_to_object(anAttr.Value())
129 myObj = obj._narrow(SALOME_MED.SUPPORT)
131 print objNameInStudy," has been found in the Study but with the wrong type"
134 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
136 meshNameStudy = changeBlankToUnderScore(meshName)
137 objNameInStudy = "/Med/MEDFIELD/"+fieldName+"/("+str(dt)+","+str(it)+")_ON_"+supportName+"_OF_"+meshNameStudy
138 #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
139 mySO = myStudy.FindObjectByPath(objNameInStudy)
141 print "getFieldObjectFromStudy",objNameInStudy," cannot be found in the Study"
142 print "/Med/MEDFIELD/"+fieldName,":",myStudy.GetObjectNames("/Med/MEDFIELD/"+fieldName)
145 anAttr = mySO.FindAttribute("AttributeIOR")[1]
146 #CCRTobj = salome.orb.string_to_object(anAttr.Value())
147 obj = orb.string_to_object(anAttr.Value())
148 myObj = obj._narrow(SALOME_MED.FIELDINT)
151 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
154 print objNameInStudy," has been found in the Study but with the wrong type"
158 fileName = "pointe.med"
160 #CCRTmedComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
161 medComp=lcc.FindOrLoadComponent("FactoryServer", "MED")
165 filePath=os.environ["MED_ROOT_DIR"]
166 filePath=filePath+"/share/salome/resources/med/"
168 filePathName = filePath + fileName
170 print "Reading the .med file ",filePathName," and pushing corba objects in the SALOME study"
171 #CCRTmedComp.readStructFileWithFieldType(filePathName,salome.myStudyName)
172 medComp.readStructFileWithFieldType(filePathName,studyname)
173 #CCRTsg.updateObjBrowser(1)
175 print "getting the MED object from the study"
176 medObj = getMedObjectFromStudy(fileName)
178 nbOfMeshes = medObj.getNumberOfMeshes()
179 meshNames = medObj.getMeshNames()
181 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
183 meshName = meshNames[0]
185 meshObj = medObj.getMeshByName(meshName)
187 fieldName = "fieldcelldoublevector"
190 entitySupport = "MED_MAILLE"
191 supportName = "SupportOnAll_"+entitySupport
193 fieldTypedObj,typeField = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
196 fieldTypedLocalCopy = FIELDDOUBLEClient(fieldTypedObj)
197 elif (typeField == 0):
198 fieldTypedLocalCopy = FIELDINTClient(fieldTypedObj)
200 print "Problem with the type of the field"
202 ############## Load Calculator Component ###################
203 # Calculator Component must be in the Container of MED
205 print "Load Calculator Component "
206 # we need to import CALCULATOR_ORB to get a typed object (to perform narrowing)
207 import CALCULATOR_ORB
208 #CCRTcalculator = salome.lcc.FindOrLoadComponent("FactoryServer", "CALCULATOR")
209 calculator = lcc.FindOrLoadComponent("FactoryServer", "CALCULATOR")
212 #calculator.printField(fieldTypedObj)
216 ############## Test Calculator Component ###################
219 print "Appel cloneField : fieldTypedObj -> f1,f2,f3,f4"
220 fieldTypedObj.Register()
221 (f1,f2,f3,f4)=calculator.cloneField(fieldTypedObj) # fieldTypedObj is consumed
224 ##calculator.printField(f1)
225 print "Add fields f2+f3"
226 f_add=calculator.add(f2, f3)
228 ##calculator.printField( f_add ) # f_add is consumed
231 print "Apply linear function"
232 f_lin=calculator.applyLin(f4,2.0,1.0)
234 ##calculator.printField( f_lin ) # f_lin is consumed
236 print "Appel Norme Max "
238 norme=calculator.normMax(f_lin) # f_lin is consumed
239 print " -> norme = ",norme
244 ############ Creation of a MED file with fields created by Caculator #################
248 meshDistant = f_add.getSupport().getMesh()
250 meshLocalCopy = MESHClient(meshDistant)
253 print " getting information from the local copy of the distant mesh"
254 name = meshLocalCopy.getName()
255 spaceDimension = meshLocalCopy.getSpaceDimension()
256 meshDimension = meshLocalCopy.getMeshDimension()
257 numberOfNodes = meshLocalCopy.getNumberOfNodes()
258 print " Name = ", name, " space Dim = ", spaceDimension, " mesh Dim = ", meshDimension, " Nb of Nodes = ", numberOfNodes
259 coordSyst = meshLocalCopy.getCoordinatesSystem()
260 print " The coordinates system is",coordSyst
261 print " The Coordinates :"
264 for isd in range(spaceDimension):
265 coordNames.append(meshLocalCopy.getCoordinateName(isd))
266 coordUnits.append(meshLocalCopy.getCoordinateUnit(isd))
268 print " names:", coordNames
269 print " units", coordUnits
271 coordinates = meshLocalCopy.getCoordinates(MED_FULL_INTERLACE)
272 for k in range(numberOfNodes):
274 print " ---- ", coordinates[k*spaceDimension:(kp1*spaceDimension)]
276 print " The Cell Nodal Connectivity of the Cells:"
277 nbTypesCell = meshLocalCopy.getNumberOfTypes(MED_CELL)
280 print " The Mesh has",nbTypesCell,"Type(s) of Cell"
281 types = meshLocalCopy.getTypes(MED_CELL)
282 for k in range(nbTypesCell):
284 nbElemType = meshLocalCopy.getNumberOfElements(MED_CELL,type)
285 print " For the type:",type,"there is(are)",nbElemType,"elemnt(s)"
286 connectivity = meshLocalCopy.getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,type)
287 nbNodesPerCell = type%100
288 for j in range(nbElemType):
289 print " Element",(j+1)," ",connectivity[j*nbNodesPerCell:(j+1)*nbNodesPerCell]
295 ## TEST METHODS ABOUT POLY ELEMENTS ##
297 nbTypesCellWithPoly = meshLocalCopy.getNumberOfTypesWithPoly(MED_CELL)
298 if (nbTypesCell == nbTypesCellWithPoly):
300 print " No Poly Cells in the mesh"
305 print " The Cell Nodal Connectivity of the Poly Cells:"
307 print " The Mesh has",nbTypesCellWithPoly-nbTypesCell,"Type(s) of Poly Cell"
308 types = meshLocalCopy.getTypesWithPoly(MED_CELL)
309 for k in range(nbTypesCellWithPoly):
311 if type == MED_POLYGON:
312 nbElemType = meshLocalCopy.getNumberOfPolygons()
313 elif type == MED_POLYHEDRA:
314 nbElemType = meshLocalCopy.getNumberOfPolyhedron()
318 print " For the type:",type,"there is(are)",nbElemType,"elemnt(s)"
319 if type == MED_POLYGON:
320 connectivity = meshLocalCopy.getPolygonsConnectivity(MED_NODAL,MED_CELL)
321 index = meshLocalCopy.getPolygonsConnectivityIndex(MED_NODAL,MED_CELL)
322 for j in range(nbElemType):
323 print " Polygon",(j+1)," ",connectivity[ index[j]-1 : index[j+1]-1 ]
327 connectivity = meshLocalCopy.getPolyhedronConnectivity(MED_NODAL)
328 fIndex = meshLocalCopy.getPolyhedronFacesIndex()
329 index = meshLocalCopy.getPolyhedronIndex(MED_NODAL)
330 for j in range(nbElemType):
331 print " Polyhedra",(j+1)
332 iF1, iF2 = index[ j ]-1, index[ j+1 ]-1
333 for f in range( iF2 - iF1 ):
334 iN1, iN2 = fIndex[ iF1+f ]-1, fIndex[ iF1+f+1 ]-1
335 print " Face",f+1," ",connectivity[ iN1 : iN2 ]
343 f_addLocal = FIELDDOUBLEClient(f_add)
345 f_addLocal.setName(f_addLocal.getName()+"add")
347 f_linLocal = FIELDDOUBLEClient(f_lin)
349 f_linLocal.setName(f_linLocal.getName()+"lin")
351 #Warning : OutputMedFiles are removed here after =================================
352 Outmed21File="OutCalculatorpointe21_V3.2.0b1.med"
353 os.system( 'rm -fr ' + Outmed21File )
354 Outmed22File="OutCalculatorpointe22_V3.2.0b1.med"
355 os.system( 'rm -fr ' + Outmed22File )
357 # writting the mesh and the fields
358 medFileVersion = getMedFileVersionForWriting()
360 if (medFileVersion == V22):
361 setMedFileVersionForWriting(V21)
363 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed21File, meshLocalCopy.getName(), MED_REMP)
364 meshLocalCopy.write(idMed)
366 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed21File, f_addLocal.getName())
367 f_addLocal.write(idMed)
369 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed21File, f_linLocal.getName())
370 f_linLocal.write(idMed)
372 medFileVersion = getMedFileVersionForWriting()
374 if (medFileVersion == V21):
375 setMedFileVersionForWriting(V22)
377 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed22File, meshLocalCopy.getName(), MED_REMP)
378 meshLocalCopy.write(idMed)
380 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed22File, f_addLocal.getName())
381 f_addLocal.write(idMed)
383 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed22File, f_linLocal.getName())
384 f_linLocal.write(idMed)
386 myStudy.GetObjectNames('/Med')
387 myStudy.GetObjectNames('/Med/MED_OBJECT_FROM_FILE_pointe.med')
388 myStudy.GetObjectNames('/Med/MEDMESH')
389 myStudy.GetObjectNames('/Med/MEDMESH/maa1')
390 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1')
391 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_1')
392 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_2')
393 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_3')
394 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe1')
395 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_1')
396 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_2')
397 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_3')
398 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_4')
399 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe2')
400 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe3')
401 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe4')
402 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe5')
403 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_MAILLE')
404 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_NOEUD')
406 myStudy.GetObjectNames('/Med/MEDFIELD')
407 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevector')
408 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevector/(-1,-1)_ON_SupportOnAll_MED_MAILLE_OF_maa1')
409 myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble')
410 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble/(-1,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
411 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble/1,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
412 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble/(2,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
413 myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodeint')
414 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodeint/(-1,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
416 #myStudyManager.Close(myStudy)
421 ############ Output MED file with fields created by Caculator #################
425 myStudy = myStudyManager.NewStudy('medClient_withoutIHM_add_lin')
426 studynameId = myStudy._get_StudyId()
427 studyname = myStudy._get_Name()
428 print "We are working in the study ",studyname," with the ID ",studynameId
430 print "Reading the .med file ",Outmed21File," and pushing corba objects in the SALOME study"
432 medComp.readStructFileWithFieldType(Outmed21File,studyname)
434 Outf_add = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectoradd',-1,-1)
435 Outf_lin = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectorlin',-1,-1)
437 myStudy.GetObjectNames('/Med')
439 myStudy.GetObjectNames('/Med/MED_OBJECT_FROM_FILE_OutCalculatorpointe21_V3.2.0b1.med')
441 myStudy.GetObjectNames('/Med/MEDMESH')
442 myStudy.GetObjectNames('/Med/MEDMESH/maa1')
443 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1')
444 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_1')
445 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_2')
446 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_3')
447 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe1')
448 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe2')
449 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe3')
450 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe4')
451 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe5')
452 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_MAILLE')
453 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_NOEUD')
455 myStudy.GetObjectNames('/Med/MEDFIELD')
456 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectoradd')
457 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectoradd/(-1,-1)_ON_SupportOnAll_MED_MAILLE_OF_maa1')
458 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectorlin')
459 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectorlin/(-1,-1)_ON_SupportOnAll_MED_MAILLE_OF_maa1')
461 #myStudyManager.Save(myStudy,1)
462 #myStudy.DumpStudy("/tmp","medClient_withoutIHM_add_lin",1)
464 myStudyManager.SaveAs(studyname+'.hdf',myStudy,0)
467 print "END of the Pyhton script ..... Ctrl D to exit"