]> SALOME platform Git repositories - samples/calculator.git/blob - src/CALCULATOR/CALCULATOR_TEST_STUDY_WITHOUTIHM.py
Salome HOME
Merge from V6_main 01/04/2013
[samples/calculator.git] / src / CALCULATOR / CALCULATOR_TEST_STUDY_WITHOUTIHM.py
1 # Copyright (C) 2007-2013  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 ####################################################################################################
24 # CALCULATOR_TEST_STUDY_WITHOUTIHM.py
25 # Test the calculator component: using Med Client classes with writing in a med file
26 # results from the calculator component
27 ####################################################################################################
28 #
29 from libMEDClient import *
30
31 import string
32
33 import salome
34
35 import SALOME_MED
36
37 #CCRTfrom libSALOME_Swig import *
38 #CCRTsg = SALOMEGUI_Swig()
39
40 #CCRT :
41 import SALOMEDS
42
43 import os
44 host = os.getenv( 'HOST' )
45 orb, lcc, naming_service, contmgr = salome.salome_kernel.salome_kernel_init()
46 obj = naming_service.Resolve('myStudyManager')
47 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
48 print "studyManager found"
49 myStudy = myStudyManager.NewStudy('medClient_withoutIHM_test')
50 studynameId = myStudy._get_StudyId()
51 studyname = myStudy._get_Name()
52 print "We are working in the study ",studyname," with the ID ",studynameId
53 #endCCRT
54
55 def print_ord(i):
56     if i == 0:
57         return 'first'
58     elif i == 1:
59         return 'second'
60     elif i == 2:
61         return 'third'
62     else:
63         return `(i+1)`+'th'
64
65 def changeBlankToUnderScore(stringWithBlank):
66     blank = ' '
67     underscore = '_'
68     decompString = string.split(stringWithBlank,blank)
69     length = len(decompString)
70     stringWithUnderScore = decompString[0]
71     for i in range(1,length):
72         stringWithUnderScore += underscore
73         stringWithUnderScore += decompString[i]
74     return stringWithUnderScore
75
76 def getMedObjectFromStudy(file):
77     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
78     compNameInStudy= "MED"
79     #CCRTlistOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
80     listOfSO = myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
81     listLength = len(listOfSO)
82     if (listLength == 0) :
83         print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
84         return None
85     elif (listLength > 1) :
86         print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
87         return None
88     mySO = listOfSO[0]
89     if (mySO == None) :
90         print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study"
91         return mySO
92     else:
93         anAttr = mySO.FindAttribute("AttributeIOR")[1]
94         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
95         obj = orb.string_to_object(anAttr.Value())
96         myObj = obj._narrow(SALOME_MED.MED)
97         if (myObj == None) :
98             print objNameInStudy," has been found in the Study but with the wrong type"
99         return myObj
100
101 def getMeshObjectFromStudy(meshName):
102     objNameInStudy = "/Med/MEDMESH/"+meshName
103     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
104     mySO = myStudy.FindObjectByPath(objNameInStudy)
105     if (mySO == None) :
106         print "getMeshObjectFromStudy",objNameInStudy," cannot be found in the Study"
107         return mySO
108     else:
109         anAttr = mySO.FindAttribute("AttributeIOR")[1]
110         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
111         obj = orb.string_to_object(anAttr.Value())
112         myObj = obj._narrow(SALOME_MED.MESH)
113         if (myObj == None) :
114             print objNameInStudy," has been found in the Study but with the wrong type"
115         return myObj
116
117 def getSupportObjectFromStudy(meshName,supportName):
118     meshNameStudy = changeBlankToUnderScore(meshName)
119     objNameInStudy = "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy+"/"+supportName
120     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
121     mySO = myStudy.FindObjectByPath(objNameInStudy)
122     if (mySO == None) :
123         print "getSupportObjectFromStudy",objNameInStudy," cannot be found in the Study"
124         print "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy,":",myStudy.GetObjectNames("/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy)
125         return mySO
126     else:
127         anAttr = mySO.FindAttribute("AttributeIOR")[1]
128         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
129         obj = orb.string_to_object(anAttr.Value())
130         myObj = obj._narrow(SALOME_MED.SUPPORT)
131         if (myObj == None) :
132             print objNameInStudy," has been found in the Study but with the wrong type"
133         return myObj
134
135 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
136     type = -1
137     meshNameStudy = changeBlankToUnderScore(meshName)
138     objNameInStudy = "/Med/MEDFIELD/"+fieldName+"/("+str(dt)+","+str(it)+")_ON_"+supportName+"_OF_"+meshNameStudy
139     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
140     mySO = myStudy.FindObjectByPath(objNameInStudy)
141     if (mySO == None) :
142         print "getFieldObjectFromStudy",objNameInStudy," cannot be found in the Study"
143         print "/Med/MEDFIELD/"+fieldName,":",myStudy.GetObjectNames("/Med/MEDFIELD/"+fieldName)
144         return -1,-1
145     else:
146         anAttr = mySO.FindAttribute("AttributeIOR")[1]
147         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
148         obj = orb.string_to_object(anAttr.Value())
149         myObj = obj._narrow(SALOME_MED.FIELDINT)
150         type = 0
151         if (myObj == None):
152             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
153             type = 1
154             if (myObj == None) :
155                 print objNameInStudy," has been found in the Study but with the wrong type"
156         return myObj,type
157
158
159 fileName = "pointe.med"
160
161 from MEDCoupling import *
162 from MEDLoader import *
163 from MEDCouplingCorba import *
164 from MEDCouplingClient import *
165 import MEDCouplingCorbaServant_idl
166
167 #CCRTmedComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
168 medComp=lcc.FindOrLoadComponent("FactoryServer", "MED")
169
170 import os
171
172 filePath=os.environ["MED_ROOT_DIR"]
173 filePath=filePath+"/share/salome/resources/med/"
174
175 filePathName = filePath + fileName
176
177 print "Reading the .med file ",filePathName," and pushing corba objects in the SALOME study"
178 #CCRTmedComp.readStructFileWithFieldType(filePathName,salome.myStudyName)
179 ##medComp.readStructFileWithFieldType(filePathName,studyname)
180 #CCRTsg.updateObjBrowser(1)
181
182 ##print "getting the MED object from the study"
183 ##medObj = getMedObjectFromStudy(fileName)
184
185 meshNames = MEDLoader.GetMeshNames(filePathName)
186 nbOfMeshes = len(meshNames)#medObj.getNumberOfMeshes()
187
188 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
189
190 meshName = meshNames[0]
191
192 #meshObj = medObj.getMeshByName(meshName)
193 meshObj3D = MEDLoader.ReadUMeshFromFile(filePathName,meshName,0)
194
195 fieldName = "fieldcelldoublevector"
196 fieldTypedObj = MEDLoader.ReadFieldCell(filePathName,meshName,0,fieldName,-1,-1)
197 fieldTypedObjCORBA=MEDCouplingFieldDoubleServant._this(fieldTypedObj)
198
199 ##############  Load Calculator Component ###################
200 # Calculator Component must be in the Container of MED
201 #
202 print "Load Calculator Component "
203 # we need to import CALCULATOR_ORB to get a typed object (to perform narrowing)
204 import CALCULATOR_ORB
205 #CCRTcalculator = salome.lcc.FindOrLoadComponent("FactoryServer", "CALCULATOR")
206 calculator = lcc.FindOrLoadComponent("FactoryServer", "CALCULATOR")
207 #
208
209 #calculator.printField(fieldTypedObj)
210
211 #
212 #
213 ##############  Test Calculator Component ###################
214 #
215 #
216 print "Appel cloneField : fieldTypedObj -> f1,f2,f3,f4"
217 #fieldTypedObj.Register()
218 (f1,f2,f3,f4)=calculator.cloneField(fieldTypedObjCORBA)  # fieldTypedObj is consumed
219 #
220 ##f1.Register()
221 ##calculator.printField(f1)
222 print "Add fields f2+f3"
223 f_add=calculator.add(f2, f3)
224 ##f_add.Register()
225 ##calculator.printField( f_add ) # f_add is consumed
226
227 #
228 print "Apply linear function"
229 f_lin=calculator.applyLin(f4,2.0,1.0)
230 ##f_lin.Register()
231 ##calculator.printField( f_lin ) # f_lin is consumed
232 #
233 print "Appel Norme Max "
234 #f_lin.Register()
235 norme=calculator.normMax(f_lin) # f_lin is consumed
236 print " -> norme = ",norme
237 #
238
239 #
240 #
241 ############  Creation of a MED file with fields created by Caculator  #################
242 #                   via Client classes
243 #
244 f_addLocal=MEDCouplingFieldDoubleClient.New(f_add)
245 f_add.UnRegister()
246 f_addLocal.setName(fieldName+"add")
247 f_linLocal=MEDCouplingFieldDoubleClient.New(f_lin)
248 f_lin.UnRegister()
249 f_linLocal.setName(fieldName+"lin")
250
251 meshLocal = f_addLocal.getMesh()
252
253 print "      getting information from the local copy of the distant mesh"
254 name = meshLocal.getName()
255 spaceDimension = meshLocal.getSpaceDimension()
256 meshDimension = meshLocal.getMeshDimension()
257 numberOfNodes = meshLocal.getNumberOfNodes()
258 print "          Name = ", name, " space Dim = ", spaceDimension, " mesh Dim = ", meshDimension, " Nb of Nodes = ", numberOfNodes
259
260
261 #Warning : OutputMedFiles are removed here after =================================
262 #Outmed21File="OutCalculatorpointe21_V3.2.0b1.med"
263 #os.system( 'rm -fr ' + Outmed21File )
264 Outmed22File="OutCalculatorpointe22_V3.2.0b1.med"
265 os.system( 'rm -fr ' + Outmed22File )
266
267 MEDLoader.WriteUMesh(Outmed22File,meshLocal,True)
268 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(Outmed22File,f_addLocal)
269 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(Outmed22File,f_linLocal)
270
271 print ""
272 print "END of the Pyhton script ..... Ctrl D to exit"