Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR_TEST.py
1 # Copyright (C) 2007-2012  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 import salome
24 import SALOME
25 import SALOME_MED
26 import PYCALCULATOR_ORB
27 import os
28
29 #==============================================================================
30
31 def AnalyzeField(field):
32     name = field.getName()
33     desc = field.getDescription()
34     nbComp = field.getNumberOfComponents()
35     itNum = field.getIterationNumber()
36     ordNum = field.getOrderNumber()
37
38     print "Analysis of the field ",name," with the description ",desc
39     print "iteration number ",itNum," order Number ",ordNum
40     print "It has ",nbComp," component(s)"
41
42     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
43     fieldSupport = field.getSupport()
44     fieldMesh = fieldSupport.getMesh()
45     fieldEntity = fieldSupport.getEntity()
46     bool = fieldSupport.isOnAllElements()
47
48     if bool:
49         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
50         if fieldEntity == SALOME_MED.MED_NODE:
51             nbValByComp = fieldMesh.getNumberOfNodes()
52         else:
53             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
54         print "and its dimension (number of values by component of the field) is ",nbValByComp
55     else:
56         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
57         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
58         print "and its dimension (number of values by component of the field) is ",nbValByComp
59
60     for i in range(nbComp):
61         compName = field.getComponentName(i+1)
62         compUnit = field.getComponentUnit(i+1)
63         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
64
65     for i in range(nbValByComp):
66         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
67
68 #==============================================================================
69
70 def getFieldIntObjectFromStudy(number,subnumber):
71     mySO = salome.myStudy.FindObject("MEDFIELD")
72     mysub = mySO.FindSubObject(number)[1]
73     if mysub:
74         mysubsub = mysub.FindSubObject(subnumber)[1]
75         if mysubsub:
76             Builder = salome.myStudy.NewBuilder()
77             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
78             obj = salome.orb.string_to_object(anAttr.Value())
79             myObj = obj._narrow(SALOME_MED.FIELDINT)
80             return myObj
81         else:
82             print "ERROR: No FieldInt Object stored in this Study"
83             return None
84     else:
85         print "ERROR: No FieldInt Object stored in this Study"
86         return None
87
88 #==============================================================================
89
90 def getFieldDoubleObjectFromStudy(number,subnumber):
91     mySO = salome.myStudy.FindObject("MEDFIELD")
92     mysub = mySO.FindSubObject(number)[1]
93     if mysub:
94         mysubsub = mysub.FindSubObject(subnumber)[1]
95         if mysubsub:
96             Builder = salome.myStudy.NewBuilder()
97             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
98             obj = salome.orb.string_to_object(anAttr.Value())
99             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
100             return myObj
101         else:
102             print "ERROR: No FieldDouble Object stored in this Study"
103             return None
104     else:
105         print "ERROR: No FieldDouble Object stored in this Study"
106         return None
107
108 #==============================================================================
109
110 studyname = salome.myStudyName
111 studynameId = salome.myStudyId
112
113 print "We are working in the study ",studyname," with the ID ",studynameId
114 print ""
115
116 filePath=os.environ["MED_ROOT_DIR"]
117 filePath=filePath+"/share/salome/resources/med/"
118 medFile=filePath+"pointe.med"
119 fieldname = "fieldcelldoublevector"
120
121 print "Load the Med Component "
122 med_comp = salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
123
124 try:
125     fieldcell  = med_comp.readFieldInFile(medFile,studyname,fieldname,-1,-1)
126 except SALOME.SALOME_Exception, ex:
127     print ex.details
128     print ex.details.type
129     print ex.details.text
130     print ex.details.sourceFile
131     print ex.details.lineNumber
132     raise
133
134 print fieldcell
135 print fieldcell.getName()
136 print fieldcell.getDescription()
137 print fieldcell.getNumberOfComponents()
138
139
140 AnalyzeField(fieldcell)
141
142
143
144 print "Loading of the CalculatorPy Component "
145 calculator = salome.lcc.FindOrLoadComponent("FactoryServerPy", "PYCALCULATOR")
146 print "calculator = ", calculator
147
148 alpha = 2.0
149 print "Multiplication of fieldcelldouble by alpha"
150 fieldcellalpha = calculator.Mul(fieldcell,alpha)
151 print fieldcellalpha
152 AnalyzeField(fieldcellalpha)
153
154 print "Adding of alpha*fieldcelldouble and fieldcelldouble"
155 fieldcelladd = calculator.Add(fieldcell,fieldcellalpha)
156 print fieldcelladd
157 AnalyzeField(fieldcelladd)
158
159 print "Put fieldcelladd in study"
160 fieldcelladd.addInStudy(salome.myStudy,fieldcelladd)
161
162 print "update Salome GUI"
163 salome.sg.updateObjBrowser(1)
164
165 print "Get back Field from study"
166 # we grab the second field in MEDFIELD - first iteration
167 fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
168 print fieldcelldouble
169 AnalyzeField(fieldcelldouble)
170
171
172 print "Fin du test PYCALCULATOR"