Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR_TEST.py
1 #  Copyright (C) 2005  CEA/DEN, EDF R&D
2 #
3 #  This library is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU Lesser General Public
5 #  License as published by the Free Software Foundation; either
6 #  version 2.1 of the License.
7 #
8 #  This library is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 #  Lesser General Public License for more details.
12 #
13 #  You should have received a copy of the GNU Lesser General Public
14 #  License along with this library; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 import salome
20 import SALOME
21 import SALOME_MED
22 import PYCALCULATOR_ORB
23 import os
24
25 #==============================================================================
26
27 def AnalyzeField(field):
28     name = field.getName()
29     desc = field.getDescription()
30     nbComp = field.getNumberOfComponents()
31     itNum = field.getIterationNumber()
32     ordNum = field.getOrderNumber()
33
34     print "Analysis of the field ",name," with the description ",desc
35     print "iteration number ",itNum," order Number ",ordNum
36     print "It has ",nbComp," component(s)"
37
38     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
39     fieldSupport = field.getSupport()
40     fieldMesh = fieldSupport.getMesh()
41     fieldEntity = fieldSupport.getEntity()
42     bool = fieldSupport.isOnAllElements()
43
44     if bool:
45         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
46         if fieldEntity == SALOME_MED.MED_NODE:
47             nbValByComp = fieldMesh.getNumberOfNodes()
48         else:
49             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
50         print "and its dimension (number of values by component of the field) is ",nbValByComp
51     else:
52         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
53         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
54         print "and its dimension (number of values by component of the field) is ",nbValByComp
55
56     for i in range(nbComp):
57         compName = field.getComponentName(i+1)
58         compUnit = field.getComponentUnit(i+1)
59         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
60
61     for i in range(nbValByComp):
62         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
63
64 #==============================================================================
65
66 def getFieldIntObjectFromStudy(number,subnumber):
67     mySO = salome.myStudy.FindObject("MEDFIELD")
68     mysub = mySO.FindSubObject(number)[1]
69     if mysub:
70         mysubsub = mysub.FindSubObject(subnumber)[1]
71         if mysubsub:
72             Builder = salome.myStudy.NewBuilder()
73             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
74             obj = salome.orb.string_to_object(anAttr.Value())
75             myObj = obj._narrow(SALOME_MED.FIELDINT)
76             return myObj
77         else:
78             print "ERROR: No FieldInt Object stored in this Study"
79             return None
80     else:
81         print "ERROR: No FieldInt Object stored in this Study"
82         return None
83
84 #==============================================================================
85
86 def getFieldDoubleObjectFromStudy(number,subnumber):
87     mySO = salome.myStudy.FindObject("MEDFIELD")
88     mysub = mySO.FindSubObject(number)[1]
89     if mysub:
90         mysubsub = mysub.FindSubObject(subnumber)[1]
91         if mysubsub:
92             Builder = salome.myStudy.NewBuilder()
93             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
94             obj = salome.orb.string_to_object(anAttr.Value())
95             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
96             return myObj
97         else:
98             print "ERROR: No FieldDouble Object stored in this Study"
99             return None
100     else:
101         print "ERROR: No FieldDouble Object stored in this Study"
102         return None
103
104 #==============================================================================
105
106 studyname = salome.myStudyName
107 studynameId = salome.myStudyId
108
109 print "We are working in the study ",studyname," with the ID ",studynameId
110 print ""
111
112 filePath=os.environ["MED_ROOT_DIR"]
113 filePath=filePath+"/share/salome/resources/med/"
114 medFile=filePath+"pointe.med"
115 fieldname = "fieldcelldouble"
116
117 print "Load the Med Component "
118 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
119
120 try:
121     fieldcell  = med_comp.readFieldInFile(medFile,studyname,fieldname,-1,-1)
122 except SALOME.SALOME_Exception, ex:
123     print ex.details
124     print ex.details.type
125     print ex.details.text
126     print ex.details.sourceFile
127     print ex.details.lineNumber
128     raise
129
130 print fieldcell
131 print fieldcell.getName()
132 print fieldcell.getDescription()
133 print fieldcell.getNumberOfComponents()
134
135
136 AnalyzeField(fieldcell)
137
138
139
140 print "Loading of the CalculatorPy Component "
141 calculator = salome.lcc.FindOrLoadComponent("FactoryServerPy", "PYCALCULATOR")
142 print "calculator = "
143 dir(calculator)
144
145 alpha = 2.0
146 print "Multiplication of fieldcelldouble by alpha"
147 fieldcellalpha = calculator.Mul(fieldcell,alpha)
148 print fieldcellalpha
149 AnalyzeField(fieldcellalpha)
150
151 print "Adding of alpha*fieldcelldouble and fieldcelldouble"
152 fieldcelladd = calculator.Add(fieldcell,fieldcellalpha)
153 print fieldcelladd
154 AnalyzeField(fieldcelladd)
155
156 print "Put fieldcelladd in study"
157 fieldcelladd.addInStudy(salome.myStudy,fieldcelladd)
158
159 print "update Salome GUI"
160 salome.sg.updateObjBrowser(1)
161
162 print "Get back Field from study"
163 # we grab the second field in MEDFIELD - first iteration
164 fieldcelldouble = getFieldDoubleObjectFromStudy(2,1)
165 print fieldcelldouble
166 AnalyzeField(fieldcelldouble)
167
168
169 print "Fin du test PYCALCULATOR"