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