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