Salome HOME
Merge from V6_main 01/04/2013
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR.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 # Instruct Python to load dynamic libraries using global resolution of symbols
24 # This is necessary to ensure that different modules will have the same definition
25 # of dynamic types and C++ RTTI will work between them
26 #
27 import DLFCN, sys
28 sys.setdlopenflags(DLFCN.RTLD_NOW | DLFCN.RTLD_GLOBAL)
29
30 import PYCALCULATOR_ORB__POA
31 import SALOME_ComponentPy
32
33 import SALOME_MED
34 from libMEDMEM_Swig import *
35 from libMedCorba_Swig import *
36 from libMEDClient import *
37
38 class PYCALCULATOR (PYCALCULATOR_ORB__POA.PYCALCULATOR_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i ):
39
40     def __init__(self, orb, poa, contID, containerName, instanceName,
41                  interfaceName):
42         print "Begin of PYCALCULATOR::__init__"
43         notif = 0
44         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa, 
45                contID, containerName, instanceName, interfaceName, notif)
46         self._naming_service=SALOME_ComponentPy.SALOME_NamingServicePy_i(self._orb)
47         print "End of PYCALCULATOR::__init__" 
48
49     def getVersion( self ):
50         import salome_version
51         return salome_version.getVersion("PYCALCULATOR", True)
52
53     def Add(self, field1, field2):
54         print "Begin of PYCALCULATOR::Add"
55         print "pointeur on first argument : ",field1
56         print "            second         : ",field2
57
58         bool = 1
59         nbOfComp1 = field1.getNumberOfComponents()
60         name1 = field1.getName()
61         description1 = field1.getDescription()
62
63         nbOfComp2 = field2.getNumberOfComponents()
64         name2 = field2.getName()
65         description2 = field2.getDescription()
66
67         if (nbOfComp1 != nbOfComp2): bool = 0
68
69         compName = []
70         compUnit = []
71         for k in range(nbOfComp1):
72             kp1 = k+1
73             compName.append(field1.getComponentName(kp1))
74             compUnit.append(field1.getComponentUnit(kp1))
75             if (bool):
76                 if ((compName[k] != field2.getComponentName(kp1)) or (compUnit[k] != field2.getComponentUnit(kp1))):
77                     bool = 0
78
79         support1 = field1.getSupport()
80         entity1 = support1.getEntity()
81         mesh1 = support1.getMesh()
82
83         support2 = field2.getSupport()
84         entity2 = support2.getEntity()
85         mesh2 = support2.getMesh()
86
87         if (support1.isOnAllElements()):
88             lengthValue1 = mesh1.getNumberOfElements(entity1,SALOME_MED.MED_ALL_ELEMENTS)
89             number1 = []
90             for k in range(lengthValue1):
91                 number1.append(k)
92         else:
93             lengthValue1 = support1.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
94             number1 = support1.getNumber(SALOME_MED.MED_ALL_ELEMENTS)
95
96         if (support2.isOnAllElements()):
97             lengthValue2 = mesh2.getNumberOfElements(entity2,SALOME_MED.MED_ALL_ELEMENTS)
98             number2 = []
99             for k in range(lengthValue2):
100                 number2.append(k)
101         else:
102             lengthValue2 = support2.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
103             number2 = support2.getNumber(SALOME_MED.MED_ALL_ELEMENTS)
104
105         # comparision of each support: due to the fact that they are CORBA
106         # pointers, the comparision will be done directly throught the numbers
107
108         if (bool):
109             if ((lengthValue1 != lengthValue2) or (entity1 != entity2)):
110                 bool = 0
111
112         if (bool):
113             for k in range(lengthValue1):
114                 if (number1[k] != number2[k]):
115                     bool = 0
116
117         value1 = field1.getValue(SALOME_MED.MED_FULL_INTERLACE)
118
119         value2 = []
120         if (bool):
121             value2 = field2.getValue(SALOME_MED.MED_FULL_INTERLACE)
122
123         valueOut = []
124         if (bool):
125             print "CALCULATORPY::Add : The fields have the same support, they then can be added"
126             for k in range(lengthValue1*nbOfComp1):
127                 valueOut.append((value1[k]+value2[k]))
128         else:
129             print "CALCULATORPY::Add : WARNING !! For some reason the fields have not the same support"
130             for k in range(lengthValue1*nbOfComp1):
131                 valueOut.append(value1[k])
132
133         supportOutLocal = SUPPORTClient( support1 )
134         supportOutCorba = createCorbaSupport( supportOutLocal )
135
136         print "CALCULATORPY::Add : Creation of the local field, nbOfComp = ",nbOfComp1, " length = ",lengthValue1
137
138         fieldOutLocal = createLocalFieldDouble(nbOfComp1,lengthValue1)
139         fieldOutLocal.setValue(valueOut)
140         fieldOutLocal.setName("-new_Add_Field-")
141         fieldOutLocal.setDescription(description1)
142         fieldOutLocal.setSupport( supportOutLocal )
143
144         for k in range(nbOfComp1):
145             kp1 = k + 1
146             fieldOutLocal.setComponentName(kp1,compName[k])
147             fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
148
149         print "CALCULATORPY::Add : Creation of the CORBA field"
150
151         fieldOutCorba = createCorbaFieldDouble(supportOutCorba,fieldOutLocal)
152
153         print "End of CALCULATORPY::Add"
154         return fieldOutCorba
155
156     def Mul(self, field1, x1):
157         print "Begin of CALCULATORPY::Mul"
158         print "pointeur on first argument : ",field1
159         print "            second         : ",x1
160
161         nbOfComp = field1.getNumberOfComponents()
162         name = field1.getName()
163         description = field1.getDescription()
164         support = field1.getSupport()
165         print ".... get Names and Unit"
166         compName = []
167         compUnit = []
168         for k in range(nbOfComp):
169             kp1 = k+1
170             compName.append(field1.getComponentName(kp1))
171             compUnit.append(field1.getComponentUnit(kp1))
172
173
174         print ".... get mesh"
175         mesh = support.getMesh()
176
177         if (support.isOnAllElements()):
178             lengthValue = mesh.getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
179         else:
180             lengthValue = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
181
182         value1 = field1.getValue(SALOME_MED.MED_FULL_INTERLACE)
183         valueOut = []
184         for k in range(lengthValue*nbOfComp):
185             valueOut.append(x1*value1[k])
186
187         supportOutLocal = SUPPORTClient( support )
188         supportOutCorba = createCorbaSupport( supportOutLocal )
189
190         print "CALCULATORPY::Mul : Creation of the local field, nbOfComp = ",nbOfComp, " length = ",lengthValue
191
192         fieldOutLocal = createLocalFieldDouble(nbOfComp,lengthValue)
193         fieldOutLocal.setValue(valueOut)
194         fieldOutLocal.setName("-new_Mul_Field-")
195         fieldOutLocal.setDescription(description)
196         fieldOutLocal.setSupport( supportOutLocal )
197
198         for k in range(nbOfComp):
199             kp1 = k + 1
200             fieldOutLocal.setComponentName(kp1,compName[k])
201             fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
202
203         print "CALCULATORPY::Mul : Creation of the CORBA field"
204
205         fieldOutCorba = createCorbaFieldDouble(supportOutCorba,fieldOutLocal)
206
207         print "End of CALCULATORPY::Mul"
208         return fieldOutCorba
209
210     def Constant(self, field1, x1):
211         print "Begin of CALCULATORPY::Constant"
212         print "pointeur on first argument : ",field1
213         print "            second         : ",x1
214
215         nbOfComp = field1.getNumberOfComponents()
216         name = field1.getName()
217         description = field1.getDescription()
218         support = field1.getSupport()
219         compName = []
220         compUnit = []
221         for k in range(nbOfComp):
222             kp1 = k+1
223             compName.append(field1.getComponentName(kp1))
224             compUnit.append(field1.getComponentUnit(kp1))
225
226         mesh = support.getMesh()
227
228         if (support.isOnAllElements()):
229             lengthValue = mesh.getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
230         else:
231             lengthValue = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
232
233         valueOut = []
234         for k in range(lengthValue*nbOfComp):
235             valueOut.append(x1)
236
237         print "CALCULATORPY::Constant : Creation of the local field, nbOfComp = ",nbOfComp, " length = ",lengthValue
238
239         supportOutLocal = SUPPORTClient( support )
240         supportOutCorba = createCorbaSupport( supportOutLocal )
241
242         fieldOutLocal = createLocalFieldDouble(nbOfComp,lengthValue)
243         fieldOutLocal.setValue(valueOut)
244         fieldOutLocal.setName("-new_Const_Field-")
245         fieldOutLocal.setDescription(description)
246         fieldOutLocal.setSupport( supportOutLocal )
247
248         for k in range(nbOfComp):
249             kp1 = k + 1
250             fieldOutLocal.setComponentName(kp1,compName[k])
251             fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
252
253         print "CALCULATORPY::Constant : Creation of the CORBA field"
254
255         fieldOutCorba = createCorbaFieldDouble(supportOutCorba,fieldOutLocal)
256
257         print "End of CALCULATORPY::Constant"
258         return fieldOutCorba
259
260     def writeMEDfile(self, field1, fileName):
261         print "Begin of CALCULATORPY::writeMEDfile"
262         print "CALCULATORPY::writeMEDfile : pointeur on first argument : ",field1
263         print "                          second         : ",fileName
264         print "CALCULATORPY::writeMEDfile : NOT YET IMPLEMENTED"
265         print "End of CALCULATORPY::writeMEDfile"