Salome HOME
Merge from BR_V5_DEV 17Feb09
[samples/calculator.git] / src / CALCULATOR / CALCULATOR_TEST_STUDY_WITHOUTIHM.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
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 ####################################################################################################
27 #
28 from libMEDClient import *
29
30 import string
31
32 import salome
33
34 import SALOME_MED
35
36 #CCRTfrom libSALOME_Swig import *
37 #CCRTsg = SALOMEGUI_Swig()
38
39 #CCRT :
40 import SALOMEDS
41
42 import os
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
52 #endCCRT
53
54 def print_ord(i):
55     if i == 0:
56         return 'first'
57     elif i == 1:
58         return 'second'
59     elif i == 2:
60         return 'third'
61     else:
62         return `(i+1)`+'th'
63
64 def changeBlankToUnderScore(stringWithBlank):
65     blank = ' '
66     underscore = '_'
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
74
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
83         return None
84     elif (listLength > 1) :
85         print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
86         return None
87     mySO = listOfSO[0]
88     if (mySO == None) :
89         print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study"
90         return mySO
91     else:
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)
96         if (myObj == None) :
97             print objNameInStudy," has been found in the Study but with the wrong type"
98         return myObj
99
100 def getMeshObjectFromStudy(meshName):
101     objNameInStudy = "/Med/MEDMESH/"+meshName
102     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
103     mySO = myStudy.FindObjectByPath(objNameInStudy)
104     if (mySO == None) :
105         print "getMeshObjectFromStudy",objNameInStudy," cannot be found in the Study"
106         return mySO
107     else:
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)
112         if (myObj == None) :
113             print objNameInStudy," has been found in the Study but with the wrong type"
114         return myObj
115
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)
121     if (mySO == None) :
122         print "getSupportObjectFromStudy",objNameInStudy," cannot be found in the Study"
123         print "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy,":",myStudy.GetObjectNames("/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy)
124         return mySO
125     else:
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)
130         if (myObj == None) :
131             print objNameInStudy," has been found in the Study but with the wrong type"
132         return myObj
133
134 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
135     type = -1
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)
140     if (mySO == None) :
141         print "getFieldObjectFromStudy",objNameInStudy," cannot be found in the Study"
142         print "/Med/MEDFIELD/"+fieldName,":",myStudy.GetObjectNames("/Med/MEDFIELD/"+fieldName)
143         return -1,-1
144     else:
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)
149         type = 0
150         if (myObj == None):
151             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
152             type = 1
153             if (myObj == None) :
154                 print objNameInStudy," has been found in the Study but with the wrong type"
155         return myObj,type
156
157
158 fileName = "pointe.med"
159
160 #CCRTmedComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
161 medComp=lcc.FindOrLoadComponent("FactoryServer", "MED")
162
163 import os
164
165 filePath=os.environ["MED_ROOT_DIR"]
166 filePath=filePath+"/share/salome/resources/med/"
167
168 filePathName = filePath + fileName
169
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)
174
175 print "getting the MED object from the study"
176 medObj = getMedObjectFromStudy(fileName)
177
178 nbOfMeshes = medObj.getNumberOfMeshes()
179 meshNames = medObj.getMeshNames()
180
181 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
182
183 meshName = meshNames[0]
184
185 meshObj = medObj.getMeshByName(meshName)
186
187 fieldName = "fieldcelldoublevector"
188 dt = -1
189 it = -1
190 entitySupport = "MED_MAILLE"
191 supportName = "SupportOnAll_"+entitySupport
192
193 fieldTypedObj,typeField = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
194
195 if(typeField == 1):
196     fieldTypedLocalCopy = FIELDDOUBLEClient(fieldTypedObj)
197 elif (typeField == 0):
198     fieldTypedLocalCopy = FIELDINTClient(fieldTypedObj)
199 else:
200     print "Problem with the type of the field"
201
202 ##############  Load Calculator Component ###################
203 # Calculator Component must be in the Container of MED
204 #
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")
210 #
211
212 #calculator.printField(fieldTypedObj)
213
214 #
215 #
216 ##############  Test Calculator Component ###################
217 #
218 #
219 print "Appel cloneField : fieldTypedObj -> f1,f2,f3,f4"
220 fieldTypedObj.Register()
221 (f1,f2,f3,f4)=calculator.cloneField(fieldTypedObj)  # fieldTypedObj is consumed
222 #
223 ##f1.Register()
224 ##calculator.printField(f1)
225 print "Add fields f2+f3"
226 f_add=calculator.add(f2, f3)
227 ##f_add.Register()
228 ##calculator.printField( f_add ) # f_add is consumed
229
230 #
231 print "Apply linear function"
232 f_lin=calculator.applyLin(f4,2.0,1.0)
233 ##f_lin.Register()
234 ##calculator.printField( f_lin ) # f_lin is consumed
235 #
236 print "Appel Norme Max "
237 f_lin.Register()
238 norme=calculator.normMax(f_lin) # f_lin is consumed
239 print " -> norme = ",norme
240 #
241
242 #
243 #
244 ############  Creation of a MED file with fields created by Caculator  #################
245 #                   via Client classes
246 #
247
248 meshDistant = f_add.getSupport().getMesh()
249
250 meshLocalCopy = MESHClient(meshDistant)
251
252
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 :"
262 coordNames = []
263 coordUnits = []
264 for isd in range(spaceDimension):
265     coordNames.append(meshLocalCopy.getCoordinateName(isd))
266     coordUnits.append(meshLocalCopy.getCoordinateUnit(isd))
267
268 print "          names:", coordNames
269 print "          units", coordUnits
270 print "          values:"
271 coordinates = meshLocalCopy.getCoordinates(MED_FULL_INTERLACE)
272 for k in range(numberOfNodes):
273     kp1 = k+1
274     print "         ---- ", coordinates[k*spaceDimension:(kp1*spaceDimension)]
275 print ""
276 print "          The Cell Nodal Connectivity of the Cells:"
277 nbTypesCell = meshLocalCopy.getNumberOfTypes(MED_CELL)
278 print ""
279 if (nbTypesCell>0):
280     print "      The Mesh has",nbTypesCell,"Type(s) of Cell"
281     types = meshLocalCopy.getTypes(MED_CELL)
282     for k in range(nbTypesCell):
283         type = types[k]
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]
290             pass
291         pass
292     pass
293
294 ##
295 ## TEST METHODS ABOUT POLY ELEMENTS ##
296 ##
297 nbTypesCellWithPoly = meshLocalCopy.getNumberOfTypesWithPoly(MED_CELL)
298 if (nbTypesCell == nbTypesCellWithPoly):
299     print ""
300     print "          No Poly Cells in the mesh"
301     print ""
302     pass
303 else:
304     print ""
305     print "          The Cell Nodal Connectivity of the Poly Cells:"
306     print ""
307     print "      The Mesh has",nbTypesCellWithPoly-nbTypesCell,"Type(s) of Poly Cell"
308     types = meshLocalCopy.getTypesWithPoly(MED_CELL)
309     for k in range(nbTypesCellWithPoly):
310         type = types[k]
311         if type == MED_POLYGON:
312             nbElemType = meshLocalCopy.getNumberOfPolygons()
313         elif type == MED_POLYHEDRA:
314             nbElemType = meshLocalCopy.getNumberOfPolyhedron()
315         else:
316             continue
317         print ""
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 ]
324                 pass
325             pass
326         else:
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 ]
336                     pass
337                 pass
338             pass
339         pass
340     pass
341 pass
342
343 f_addLocal = FIELDDOUBLEClient(f_add)
344
345 f_addLocal.setName(f_addLocal.getName()+"add")
346
347 f_linLocal = FIELDDOUBLEClient(f_lin)
348
349 f_linLocal.setName(f_linLocal.getName()+"lin")
350
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 )
356
357 # writting the mesh and the fields
358 medFileVersion = getMedFileVersionForWriting()
359
360 if (medFileVersion == V22):
361     setMedFileVersionForWriting(V21)
362
363 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed21File, meshLocalCopy.getName(), MED_REMP)
364 meshLocalCopy.write(idMed)
365
366 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed21File, f_addLocal.getName())
367 f_addLocal.write(idMed)
368
369 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed21File, f_linLocal.getName())
370 f_linLocal.write(idMed)
371
372 medFileVersion = getMedFileVersionForWriting()
373
374 if (medFileVersion == V21):
375     setMedFileVersionForWriting(V22)
376
377 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed22File, meshLocalCopy.getName(), MED_REMP)
378 meshLocalCopy.write(idMed)
379
380 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed22File, f_addLocal.getName())
381 f_addLocal.write(idMed)
382
383 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed22File, f_linLocal.getName())
384 f_linLocal.write(idMed)
385
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')
405
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')
415
416 #myStudyManager.Close(myStudy)
417 #myStudy.Close()
418
419 #
420 #
421 ############   Output MED file with fields created by Caculator  #################
422 #                   via Client classes
423 #
424
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
429
430 print "Reading the .med file ",Outmed21File," and pushing corba objects in the SALOME study"
431
432 medComp.readStructFileWithFieldType(Outmed21File,studyname)
433
434 Outf_add  = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectoradd',-1,-1)
435 Outf_lin  = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectorlin',-1,-1)
436
437 myStudy.GetObjectNames('/Med')
438
439 myStudy.GetObjectNames('/Med/MED_OBJECT_FROM_FILE_OutCalculatorpointe21_V3.2.0b1.med')
440
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')
454
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')
460
461 #myStudyManager.Save(myStudy,1)
462 #myStudy.DumpStudy("/tmp","medClient_withoutIHM_add_lin",1)
463
464 myStudyManager.SaveAs(studyname+'.hdf',myStudy,0)
465
466 print ""
467 print "END of the Pyhton script ..... Ctrl D to exit"