Salome HOME
updated copyright message
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR.py
old mode 100755 (executable)
new mode 100644 (file)
index 1a36c38..3505882
-#  Copyright (C) 2005  CEA/DEN, EDF R&D
+# Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 #
-#  This library is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU Lesser General Public
-#  License as published by the Free Software Foundation; either
-#  version 2.1 of the License.
+# Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #
-#  This library is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  Lesser General Public License for more details.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
 #
-#  You should have received a copy of the GNU Lesser General Public
-#  License along with this library; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
+
+#============================================================================
+# File   : PYCALCULATOR.py
+# Author : Vadim SANDLER, OPEN CASCADE S.A.S. (vadim.sandler@opencascade.com)
+#============================================================================
+
+# Instruct Python to load dynamic libraries using global resolution of symbols
+# This is necessary to ensure that different modules will have the same definition
+# of dynamic types and C++ RTTI will work between them
+#
+import os, sys
+sys.setdlopenflags(os.RTLD_NOW | os.RTLD_GLOBAL)
+
 import PYCALCULATOR_ORB__POA
 import SALOME_ComponentPy
+import SALOME_Embedded_NamingService_ClientPy
 
 import SALOME_MED
-from libMEDMEM_Swig import *
-from libMedCorba_Swig import *
+from MEDCouplingClient import *
+from MEDCouplingCorba  import *
+
+from salome_utils import verbose
 
-class PYCALCULATOR (PYCALCULATOR_ORB__POA.PYCALCULATOR_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i ):
+class PYCALCULATOR(PYCALCULATOR_ORB__POA.PYCALCULATOR_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i):
 
+    ## Constructor
     def __init__(self, orb, poa, contID, containerName, instanceName,
                  interfaceName):
-        print "Begin of PYCALCULATOR::__init__"
-        notif = 0
-        SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa, 
-              contID, containerName, instanceName, interfaceName, notif)
-       self._naming_service=SALOME_ComponentPy.SALOME_NamingServicePy_i(self._orb)
-        print "End of PYCALCULATOR::__init__" 
-
-    def Add(self, field1, field2):
-        print "Begin of PYCALCULATOR::Add"
-        print "pointeur on first argument : ",field1
-        print "            second         : ",field2
-
-        bool = 1
-        nbOfComp1 = field1.getNumberOfComponents()
-        name1 = field1.getName()
-        description1 = field1.getDescription()
-
-        nbOfComp2 = field2.getNumberOfComponents()
-        name2 = field2.getName()
-        description2 = field2.getDescription()
-
-        if (nbOfComp1 != nbOfComp2): bool = 0
-
-        compName = []
-        compUnit = []
-        for k in range(nbOfComp1):
-            kp1 = k+1
-            compName.append(field1.getComponentName(kp1))
-            compUnit.append(field1.getComponentUnit(kp1))
-            if (bool):
-                if ((compName[k] != field2.getComponentName(kp1)) or (compUnit[k] != field2.getComponentUnit(kp1))):
-                    bool = 0
-
-        support1 = field1.getSupport()
-        entity1 = support1.getEntity()
-        mesh1 = support1.getMesh()
-
-        support2 = field2.getSupport()
-        entity2 = support2.getEntity()
-        mesh2 = support2.getMesh()
-
-        if (support1.isOnAllElements()):
-            lengthValue1 = mesh1.getNumberOfElements(entity1,SALOME_MED.MED_ALL_ELEMENTS)
-            number1 = []
-            for k in range(lengthValue1):
-                number1.append(k)
-        else:
-            lengthValue1 = support1.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
-            number1 = support1.getNumber(SALOME_MED.MED_ALL_ELEMENTS)
-
-        if (support2.isOnAllElements()):
-            lengthValue2 = mesh2.getNumberOfElements(entity2,SALOME_MED.MED_ALL_ELEMENTS)
-            number2 = []
-            for k in range(lengthValue2):
-                number2.append(k)
-        else:
-            lengthValue2 = support2.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
-            number2 = support2.getNumber(SALOME_MED.MED_ALL_ELEMENTS)
-
-        # comparision of each support: due to the fact that they are CORBA
-        # pointers, the comparision will be done directly throught the numbers
-
-        if (bool):
-            if ((lengthValue1 != lengthValue2) or (entity1 != entity2)):
-                bool = 0
-
-        if (bool):
-            for k in range(lengthValue1):
-                if (number1[k] != number2[k]):
-                    bool = 0
-
-        value1 = field1.getValue(SALOME_MED.MED_FULL_INTERLACE)
-
-        value2 = []
-        if (bool):
-            value2 = field2.getValue(SALOME_MED.MED_FULL_INTERLACE)
-
-        valueOut = []
-        if (bool):
-            print "CALCULATORPY::Add : The fields have the same support, they then can be added"
-            for k in range(lengthValue1*nbOfComp1):
-                valueOut.append((value1[k]+value2[k]))
-        else:
-            print "CALCULATORPY::Add : WARNING !! For some reason the fields have not the same support"
-            for k in range(lengthValue1*nbOfComp1):
-                valueOut.append(value1[k])
-
-        print "CALCULATORPY::Add : Creation of the local field, nbOfComp = ",nbOfComp1, " length = ",lengthValue1
-
-        fieldOutLocal = createLocalFieldDouble(nbOfComp1,lengthValue1)
-        fieldOutLocal.setValue(MED_FULL_INTERLACE,valueOut)
-        fieldOutLocal.setName("-new_Add_Field-")
-        fieldOutLocal.setDescription(description1)
-
-        for k in range(nbOfComp1):
-            kp1 = k + 1
-            fieldOutLocal.setComponentName(kp1,compName[k])
-            fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
-
-        print "CALCULATORPY::Add : Creation of the CORBA field"
-
-        fieldOutCorba = createCorbaFieldDouble(support1,fieldOutLocal)
-
-        print "End of CALCULATORPY::Add"
-        return fieldOutCorba
-
-    def Mul(self, field1, x1):
-        print "Begin of CALCULATORPY::Mul"
-        print "pointeur on first argument : ",field1
-        print "            second         : ",x1
 
-        nbOfComp = field1.getNumberOfComponents()
-        name = field1.getName()
-        description = field1.getDescription()
-        support = field1.getSupport()
-        print ".... get Names and Unit"
-        compName = []
-        compUnit = []
-        for k in range(nbOfComp):
-            kp1 = k+1
-            compName.append(field1.getComponentName(kp1))
-            compUnit.append(field1.getComponentUnit(kp1))
-
-
-        print ".... get mesh"
-        mesh = support.getMesh()
-
-        if (support.isOnAllElements()):
-            lengthValue = mesh.getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
-        else:
-            lengthValue = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
-
-        value1 = field1.getValue(SALOME_MED.MED_FULL_INTERLACE)
-        valueOut = []
-        for k in range(lengthValue*nbOfComp):
-            valueOut.append(x1*value1[k])
-
-        print "CALCULATORPY::Mul : Creation of the local field, nbOfComp = ",nbOfComp, " length = ",lengthValue
-
-        fieldOutLocal = createLocalFieldDouble(nbOfComp,lengthValue)
-        fieldOutLocal.setValue(MED_FULL_INTERLACE,valueOut)
-        fieldOutLocal.setName("-new_Mul_Field-")
-        fieldOutLocal.setDescription(description)
-
-        for k in range(nbOfComp):
-            kp1 = k + 1
-            fieldOutLocal.setComponentName(kp1,compName[k])
-            fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
-
-        print "CALCULATORPY::Mul : Creation of the CORBA field"
-
-        fieldOutCorba = createCorbaFieldDouble(support,fieldOutLocal)
-
-        print "End of CALCULATORPY::Mul"
-        return fieldOutCorba
-
-    def Constant(self, field1, x1):
-        print "Begin of CALCULATORPY::Constant"
-        print "pointeur on first argument : ",field1
-        print "            second         : ",x1
-
-        nbOfComp = field1.getNumberOfComponents()
-        name = field1.getName()
-        description = field1.getDescription()
-        support = field1.getSupport()
-        compName = []
-        compUnit = []
-        for k in range(nbOfComp):
-            kp1 = k+1
-            compName.append(field1.getComponentName(kp1))
-            compUnit.append(field1.getComponentUnit(kp1))
-
-        mesh = support.getMesh()
-
-        if (support.isOnAllElements()):
-            lengthValue = mesh.getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
+        if verbose(): print("Begin of PYCALCULATOR::__init__")
+
+        SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(
+            self,
+            orb,              # ORB instance
+            poa,              # POA instance
+            contID,           # SALOME container id
+            containerName,    # SALOME container name
+            instanceName,     # component instance name
+            interfaceName,    # component interface name
+            False)            # notification flag (for notification server)
+        
+        emb_ns = self._contId.get_embedded_NS_if_ssl()
+        import CORBA
+        if CORBA.is_nil(emb_ns):
+            self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
         else:
-            lengthValue = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
-
-        valueOut = []
-        for k in range(lengthValue*nbOfComp):
-            valueOut.append(x1)
-
-        print "CALCULATORPY::Constant : Creation of the local field, nbOfComp = ",nbOfComp, " length = ",lengthValue
-
-        fieldOutLocal = createLocalFieldDouble(nbOfComp,lengthValue)
-        fieldOutLocal.setValue(MED_FULL_INTERLACE,valueOut)
-        fieldOutLocal.setName("-new_Const_Field-")
-        fieldOutLocal.setDescription(description)
-
-        for k in range(nbOfComp):
-            kp1 = k + 1
-            fieldOutLocal.setComponentName(kp1,compName[k])
-            fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
-
-        print "CALCULATORPY::Constant : Creation of the CORBA field"
-
-        fieldOutCorba = createCorbaFieldDouble(support,fieldOutLocal)
-
-        print "End of CALCULATORPY::Constant"
-        return fieldOutCorba
-
-    def writeMEDfile(self, field1, fileName):
-        print "Begin of CALCULATORPY::writeMEDfile"
-        print "CALCULATORPY::writeMEDfile : pointeur on first argument : ",field1
-        print "                          second         : ",fileName
-        print "CALCULATORPY::writeMEDfile : NOT YET IMPLEMENTED"
-        print "End of CALCULATORPY::writeMEDfile"
+            self._naming_service = SALOME_Embedded_NamingService_ClientPy.SALOME_Embedded_NamingService_ClientPy(emb_ns)
+
+        if verbose(): print("End of PYCALCULATOR::__init__")
+
+        pass
+
+    ## base interface method: get version of the component
+    def getVersion( self ):
+        import salome_version
+        return salome_version.getVersion("PYCALCULATOR", True)
+
+    ## interface service: clone field
+    def Clone(self, field):
+        self.beginService("PYCALCULATOR::Clone")
+
+        if verbose(): 
+            print("Begin of PYCALCULATOR::Clone")
+            print("            field : ", field)
+            pass
+
+        frescorba = None
+        
+        try:
+            # create local field from corba field
+            f = MEDCouplingFieldDoubleClient.New(field)
+
+            # create CORBA field
+            frescorba = MEDCouplingFieldDoubleServant._this(f)
+            
+        except Exception as e:
+            if verbose(): print(e)
+            pass
+        
+        if verbose(): 
+            print("End of PYCALCULATOR::Clone")
+            pass
+        
+        self.endService("PYCALCULATOR::Clone")
+
+        return frescorba
+
+    ## interface service: add two fields
+    def Add(self, field1, field2):
+        self.beginService("PYCALCULATOR::Add")
+
+        if verbose(): 
+            print("Begin of PYCALCULATOR::Add")
+            print("            field 1 : ", field1)
+            print("            field 2 : ", field2)
+            pass
+
+        frescorba = None
+        
+        try:
+            # create local fields from corba fields
+            f1 = MEDCouplingFieldDoubleClient.New(field1)
+            f2 = MEDCouplingFieldDoubleClient.New(field2)
+
+            # add fields
+            f2.changeUnderlyingMesh(f1.getMesh(), 0, 1e-12)
+            fres = f1 + f2
+
+            # create CORBA field
+            frescorba = MEDCouplingFieldDoubleServant._this(fres)
+            
+        except Exception as e:
+            if verbose(): print(e)
+            pass
+        
+        if verbose(): 
+            print("End of PYCALCULATOR::Add")
+            pass
+        
+        self.endService("PYCALCULATOR::Add")
+
+        return frescorba
+
+    ## interface service: multiply two fields
+    def Mul(self, field1, field2):
+        self.beginService("PYCALCULATOR::Mul")
+
+        if verbose(): 
+            print("Begin of PYCALCULATOR::Mul")
+            print("            field 1 : ", field1)
+            print("            field 2 : ", field2)
+            pass
+
+        frescorba = None
+        
+        try:
+            # create local fields from corba fields
+            f1 = MEDCouplingFieldDoubleClient.New(field1)
+            f2 = MEDCouplingFieldDoubleClient.New(field2)
+
+            # multiply fields
+            f2.changeUnderlyingMesh(f1.getMesh(), 0, 1e-12)
+            fres = f1 * f2
+
+            # create CORBA field
+            frescorba = MEDCouplingFieldDoubleServant._this(fres)
+            
+        except Exception as e:
+            if verbose(): print(e)
+            pass
+        
+        if verbose(): 
+            print("End of PYCALCULATOR::Mul")
+            pass
+        
+        self.endService("PYCALCULATOR::Mul")
+
+        return frescorba
+
+    ## interface service: add a constant to a field
+    def AddConstant(self, field, val):
+        self.beginService("PYCALCULATOR::AddConstant")
+
+        if verbose(): 
+            print("Begin of PYCALCULATOR::AddConstant")
+            print("            field    : ", field)
+            print("            constant : ", val)
+            pass
+
+        frescorba = None
+        
+        try:
+            # create local field from corba field
+            f = MEDCouplingFieldDoubleClient.New(field)
+
+            # add constant to a field
+            fres = f + val
+
+            # create CORBA field
+            frescorba = MEDCouplingFieldDoubleServant._this(fres)
+            
+        except Exception as e:
+            if verbose(): print(e)
+            pass
+        
+        if verbose(): 
+            print("End of PYCALCULATOR::AddConstant")
+            pass
+        
+        self.endService("PYCALCULATOR::AddConstant")
+
+        return frescorba
+
+    ## interface service: multiply a field to a constant
+    def MulConstant(self, field, val):
+        self.beginService("PYCALCULATOR::MulConstant")
+
+        if verbose(): 
+            print("Begin of PYCALCULATOR::MulConstant")
+            print("            field    : ", field)
+            print("            constant : ", val)
+            pass
+
+        frescorba = None
+        
+        try:
+            # create local field from corba field
+            f = MEDCouplingFieldDoubleClient.New(field)
+
+            # multiply field to a constant
+            fres = f * val
+
+            # create CORBA field
+            frescorba = MEDCouplingFieldDoubleServant._this(fres)
+            
+        except Exception as e:
+            if verbose(): print(e)
+            pass
+        
+        if verbose(): 
+            print("End of PYCALCULATOR::MulConstant")
+            pass
+        
+        self.endService("PYCALCULATOR::MulConstant")
+
+        return frescorba
+
+    pass # end of class PYCALCULATOR