Salome HOME
Migrate from MEDMEM API (obsolete) to MEDCoupling/MEDLoader API
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR_TEST.py
index 174e8de7c4f966afb84e0d14e3844a4981d3bfda..53d39c3ef9b1184311ec44650050b41dee7faba8 100755 (executable)
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 
-import salome
-import SALOME
-import SALOME_MED
-import PYCALCULATOR_ORB
-import os
-
-#==============================================================================
-
-def AnalyzeField(field):
-    name = field.getName()
-    desc = field.getDescription()
-    nbComp = field.getNumberOfComponents()
-    itNum = field.getIterationNumber()
-    ordNum = field.getOrderNumber()
-
-    print "Analysis of the field ",name," with the description ",desc
-    print "iteration number ",itNum," order Number ",ordNum
-    print "It has ",nbComp," component(s)"
-
-    fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
-    fieldSupport = field.getSupport()
-    fieldMesh = fieldSupport.getMesh()
-    fieldEntity = fieldSupport.getEntity()
-    bool = fieldSupport.isOnAllElements()
-
-    if bool:
-        print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
-        if fieldEntity == SALOME_MED.MED_NODE:
-            nbValByComp = fieldMesh.getNumberOfNodes()
-        else:
-            nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
-        print "and its dimension (number of values by component of the field) is ",nbValByComp
-    else:
-        print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
-        nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
-        print "and its dimension (number of values by component of the field) is ",nbValByComp
-
-    for i in range(nbComp):
-        compName = field.getComponentName(i+1)
-        compUnit = field.getComponentUnit(i+1)
-        print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
-
-    for i in range(nbValByComp):
-        print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
-
-#==============================================================================
-
-def getFieldIntObjectFromStudy(number,subnumber):
-    mySO = salome.myStudy.FindObject("MEDFIELD")
-    mysub = mySO.FindSubObject(number)[1]
-    if mysub:
-        mysubsub = mysub.FindSubObject(subnumber)[1]
-        if mysubsub:
-            Builder = salome.myStudy.NewBuilder()
-            anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
-            obj = salome.orb.string_to_object(anAttr.Value())
-            myObj = obj._narrow(SALOME_MED.FIELDINT)
-            return myObj
-        else:
-            print "ERROR: No FieldInt Object stored in this Study"
-            return None
-    else:
-        print "ERROR: No FieldInt Object stored in this Study"
-        return None
-
-#==============================================================================
-
-def getFieldDoubleObjectFromStudy(number,subnumber):
-    mySO = salome.myStudy.FindObject("MEDFIELD")
-    mysub = mySO.FindSubObject(number)[1]
-    if mysub:
-        mysubsub = mysub.FindSubObject(subnumber)[1]
-        if mysubsub:
-            Builder = salome.myStudy.NewBuilder()
-            anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
-            obj = salome.orb.string_to_object(anAttr.Value())
-            myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
-            return myObj
-        else:
-            print "ERROR: No FieldDouble Object stored in this Study"
-            return None
-    else:
-        print "ERROR: No FieldDouble Object stored in this Study"
-        return None
+#============================================================================
+# File   : PYCALCULATOR_TEST.py
+# Author : Vadim SANDLER, OPEN CASCADE S.A.S. (vadim.sandler@opencascade.com)
+#============================================================================
 
-#==============================================================================
-
-studyname = salome.myStudyName
-studynameId = salome.myStudyId
-
-print "We are working in the study ",studyname," with the ID ",studynameId
-print ""
-
-filePath=os.environ["MED_ROOT_DIR"]
-filePath=filePath+"/share/salome/resources/med/"
-medFile=filePath+"pointe.med"
-fieldname = "fieldcelldoublevector"
-
-print "Load the Med Component "
-med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
-
-try:
-    fieldcell  = med_comp.readFieldInFile(medFile,studyname,fieldname,-1,-1)
-except SALOME.SALOME_Exception, ex:
-    print ex.details
-    print ex.details.type
-    print ex.details.text
-    print ex.details.sourceFile
-    print ex.details.lineNumber
-    raise
-
-print fieldcell
-print fieldcell.getName()
-print fieldcell.getDescription()
-print fieldcell.getNumberOfComponents()
-
-
-AnalyzeField(fieldcell)
-
-
-
-print "Loading of the CalculatorPy Component "
-calculator = salome.lcc.FindOrLoadComponent("FactoryServerPy", "PYCALCULATOR")
-print "calculator = ", calculator
+import os
 
-alpha = 2.0
-print "Multiplication of fieldcelldouble by alpha"
-fieldcellalpha = calculator.Mul(fieldcell,alpha)
-print fieldcellalpha
-AnalyzeField(fieldcellalpha)
+import salome
+salome.salome_init()
 
-print "Adding of alpha*fieldcelldouble and fieldcelldouble"
-fieldcelladd = calculator.Add(fieldcell,fieldcellalpha)
-print fieldcelladd
-AnalyzeField(fieldcelladd)
+import PYCALCULATOR_ORB
 
-print "Put fieldcelladd in study"
-fieldcelladd.addInStudy(salome.myStudy,fieldcelladd)
+from MEDCouplingCorba import *
+from MEDCoupling import *
+from MEDLoader import *
 
-print "update Salome GUI"
-salome.sg.updateObjBrowser(1)
+pc = salome.lcc.FindOrLoadComponent('FactoryServer','PYCALCULATOR')
 
-print "Get back Field from study"
-# we grab the second field in MEDFIELD - first iteration
-fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
-print fieldcelldouble
-AnalyzeField(fieldcelldouble)
+medFile   = os.path.join(os.getenv("MED_ROOT_DIR"), "share/salome/resources/med/pointe.med")
+meshName  = "maa1"
+fieldName = "fieldcelldoublevector"
+print medFile, meshName, fieldName
 
+f = MEDLoader.ReadFieldCell(medFile, meshName, 0, fieldName, -1, -1)
+forig = MEDCouplingFieldDoubleServant._this(f)
 
-print "Fin du test PYCALCULATOR"
+fcopy = pc.Clone(forig)
+f1    = pc.Add(forig, fcopy)
+f2    = pc.Mul(forig, fcopy)
+f3    = pc.AddConstant(forig, 3.5)
+f4    = pc.MulConstant(forig, 3.5)