Salome HOME
updated copyright message
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR.py
1 # Copyright (C) 2007-2023  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 #============================================================================
24 # File   : PYCALCULATOR.py
25 # Author : Vadim SANDLER, OPEN CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 #============================================================================
27
28 # Instruct Python to load dynamic libraries using global resolution of symbols
29 # This is necessary to ensure that different modules will have the same definition
30 # of dynamic types and C++ RTTI will work between them
31 #
32 import os, sys
33 sys.setdlopenflags(os.RTLD_NOW | os.RTLD_GLOBAL)
34
35 import PYCALCULATOR_ORB__POA
36 import SALOME_ComponentPy
37 import SALOME_Embedded_NamingService_ClientPy
38
39 import SALOME_MED
40 from MEDCouplingClient import *
41 from MEDCouplingCorba  import *
42
43 from salome_utils import verbose
44
45 class PYCALCULATOR(PYCALCULATOR_ORB__POA.PYCALCULATOR_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i):
46
47     ## Constructor
48     def __init__(self, orb, poa, contID, containerName, instanceName,
49                  interfaceName):
50
51         if verbose(): print("Begin of PYCALCULATOR::__init__")
52
53         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(
54             self,
55             orb,              # ORB instance
56             poa,              # POA instance
57             contID,           # SALOME container id
58             containerName,    # SALOME container name
59             instanceName,     # component instance name
60             interfaceName,    # component interface name
61             False)            # notification flag (for notification server)
62         
63         emb_ns = self._contId.get_embedded_NS_if_ssl()
64         import CORBA
65         if CORBA.is_nil(emb_ns):
66             self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
67         else:
68             self._naming_service = SALOME_Embedded_NamingService_ClientPy.SALOME_Embedded_NamingService_ClientPy(emb_ns)
69
70         if verbose(): print("End of PYCALCULATOR::__init__")
71
72         pass
73
74     ## base interface method: get version of the component
75     def getVersion( self ):
76         import salome_version
77         return salome_version.getVersion("PYCALCULATOR", True)
78
79     ## interface service: clone field
80     def Clone(self, field):
81         self.beginService("PYCALCULATOR::Clone")
82
83         if verbose(): 
84             print("Begin of PYCALCULATOR::Clone")
85             print("            field : ", field)
86             pass
87
88         frescorba = None
89         
90         try:
91             # create local field from corba field
92             f = MEDCouplingFieldDoubleClient.New(field)
93
94             # create CORBA field
95             frescorba = MEDCouplingFieldDoubleServant._this(f)
96             
97         except Exception as e:
98             if verbose(): print(e)
99             pass
100         
101         if verbose(): 
102             print("End of PYCALCULATOR::Clone")
103             pass
104         
105         self.endService("PYCALCULATOR::Clone")
106
107         return frescorba
108
109     ## interface service: add two fields
110     def Add(self, field1, field2):
111         self.beginService("PYCALCULATOR::Add")
112
113         if verbose(): 
114             print("Begin of PYCALCULATOR::Add")
115             print("            field 1 : ", field1)
116             print("            field 2 : ", field2)
117             pass
118
119         frescorba = None
120         
121         try:
122             # create local fields from corba fields
123             f1 = MEDCouplingFieldDoubleClient.New(field1)
124             f2 = MEDCouplingFieldDoubleClient.New(field2)
125
126             # add fields
127             f2.changeUnderlyingMesh(f1.getMesh(), 0, 1e-12)
128             fres = f1 + f2
129
130             # create CORBA field
131             frescorba = MEDCouplingFieldDoubleServant._this(fres)
132             
133         except Exception as e:
134             if verbose(): print(e)
135             pass
136         
137         if verbose(): 
138             print("End of PYCALCULATOR::Add")
139             pass
140         
141         self.endService("PYCALCULATOR::Add")
142
143         return frescorba
144
145     ## interface service: multiply two fields
146     def Mul(self, field1, field2):
147         self.beginService("PYCALCULATOR::Mul")
148
149         if verbose(): 
150             print("Begin of PYCALCULATOR::Mul")
151             print("            field 1 : ", field1)
152             print("            field 2 : ", field2)
153             pass
154
155         frescorba = None
156         
157         try:
158             # create local fields from corba fields
159             f1 = MEDCouplingFieldDoubleClient.New(field1)
160             f2 = MEDCouplingFieldDoubleClient.New(field2)
161
162             # multiply fields
163             f2.changeUnderlyingMesh(f1.getMesh(), 0, 1e-12)
164             fres = f1 * f2
165
166             # create CORBA field
167             frescorba = MEDCouplingFieldDoubleServant._this(fres)
168             
169         except Exception as e:
170             if verbose(): print(e)
171             pass
172         
173         if verbose(): 
174             print("End of PYCALCULATOR::Mul")
175             pass
176         
177         self.endService("PYCALCULATOR::Mul")
178
179         return frescorba
180
181     ## interface service: add a constant to a field
182     def AddConstant(self, field, val):
183         self.beginService("PYCALCULATOR::AddConstant")
184
185         if verbose(): 
186             print("Begin of PYCALCULATOR::AddConstant")
187             print("            field    : ", field)
188             print("            constant : ", val)
189             pass
190
191         frescorba = None
192         
193         try:
194             # create local field from corba field
195             f = MEDCouplingFieldDoubleClient.New(field)
196
197             # add constant to a field
198             fres = f + val
199
200             # create CORBA field
201             frescorba = MEDCouplingFieldDoubleServant._this(fres)
202             
203         except Exception as e:
204             if verbose(): print(e)
205             pass
206         
207         if verbose(): 
208             print("End of PYCALCULATOR::AddConstant")
209             pass
210         
211         self.endService("PYCALCULATOR::AddConstant")
212
213         return frescorba
214
215     ## interface service: multiply a field to a constant
216     def MulConstant(self, field, val):
217         self.beginService("PYCALCULATOR::MulConstant")
218
219         if verbose(): 
220             print("Begin of PYCALCULATOR::MulConstant")
221             print("            field    : ", field)
222             print("            constant : ", val)
223             pass
224
225         frescorba = None
226         
227         try:
228             # create local field from corba field
229             f = MEDCouplingFieldDoubleClient.New(field)
230
231             # multiply field to a constant
232             fres = f * val
233
234             # create CORBA field
235             frescorba = MEDCouplingFieldDoubleServant._this(fres)
236             
237         except Exception as e:
238             if verbose(): print(e)
239             pass
240         
241         if verbose(): 
242             print("End of PYCALCULATOR::MulConstant")
243             pass
244         
245         self.endService("PYCALCULATOR::MulConstant")
246
247         return frescorba
248
249     pass # end of class PYCALCULATOR