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