Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MedCorba_Swig / batchmode_medcorba_test1.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 #  MED MedCorba_Swig : binding of MED CORBA objects woth Python
23 #  File   : batchmode_medcorba_test.py
24 #  Module : MED
25 #
26 import batchmode_salome
27
28 import SALOME_MED
29
30 from libMedCorba_Swig import *
31
32 from random import *
33
34 import os
35
36 filePath = os.environ["MED_ROOT_DIR"]
37 filePath = os.path.join( filePath, "share", "salome", "resources", "med" )
38
39 #==============================================================================
40
41 def AnalyzeField(field):
42     name = field.getName()
43     desc = field.getDescription()
44     nbComp = field.getNumberOfComponents()
45     itNum = field.getIterationNumber()
46     ordNum = field.getOrderNumber()
47
48     print "Analysis of the field ",name," with the description ",desc
49     print "iteration number ",itNum," order Number ",ordNum
50     print "It has ",nbComp," component(s)"
51
52     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
53     fieldSupport = field.getSupport()
54     fieldMesh = fieldSupport.getMesh()
55     fieldEntity = fieldSupport.getEntity()
56     bool = fieldSupport.isOnAllElements()
57
58     if bool:
59         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
60         if fieldEntity == SALOME_MED.MED_NODE:
61             nbValByComp = fieldMesh.getNumberOfNodes()
62         else:
63             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
64         print "and its dimension (number of values by component of the field) is ",nbValByComp
65     else:
66         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
67         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
68         print "and its dimension (number of values by component of the field) is ",nbValByComp
69
70     for i in range(nbComp):
71         compName = field.getComponentName(i+1)
72         compUnit = field.getComponentUnit(i+1)
73         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
74
75     for i in range(nbValByComp):
76         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
77
78 #==============================================================================
79
80 def getMeshObjectFromStudy(number):
81     mySO = batchmode_salome.myStudy.FindObject("MEDMESH")
82     mysub = mySO.FindSubObject(number)[1]
83     if mysub:
84         Builder = batchmode_salome.myStudy.NewBuilder()
85         anAttr = Builder.FindOrCreateAttribute(mysub, "AttributeIOR")
86         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
87         myObj = obj._narrow(SALOME_MED.MESH)
88         return myObj
89     else:
90         print "ERROR: No Mesh Object stored in this Study"
91         return None
92
93
94 #==============================================================================
95
96 def getFieldDoubleObjectFromStudy(number,subnumber):
97     mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
98     mysub = mySO.FindSubObject(number)[1]
99     if mysub:
100         mysubsub = mysub.FindSubObject(subnumber)[1]
101         if mysubsub:
102             Builder = batchmode_salome.myStudy.NewBuilder()
103             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
104             obj = batchmode_salome.orb.string_to_object(anAttr.Value())
105             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeName")
106             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
107             return myObj
108         else:
109             print "ERROR: No FieldDouble Object stored in this Study"
110             return None
111     else:
112         print "ERROR: No FieldDouble Object stored in this Study"
113         return None
114
115
116 #==============================================================================
117
118 def getFieldIntObjectFromStudy(number,subnumber):
119     mySO = batchmode_salome.myStudy.FindObject("MEDFIELD")
120     mysub = mySO.FindSubObject(number)[1]
121     if mysub:
122         mysubsub = mysub.FindSubObject(subnumber)[1]
123         if mysubsub:
124             Builder = batchmode_salome.myStudy.NewBuilder()
125             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
126             obj = batchmode_salome.orb.string_to_object(anAttr.Value())
127             myObj = obj._narrow(SALOME_MED.FIELDINT)
128             return myObj
129         else:
130             print "ERROR: No FieldInt Object stored in this Study"
131             return None
132     else:
133         print "ERROR: No FieldInt Object stored in this Study"
134         return None
135
136 #==============================================================================
137
138 def getMedObjectFromStudy(fileName=None):
139     myObj=None; Builder = batchmode_salome.myStudy.NewBuilder()
140     if fileName is not None:
141         objNameInStudy = "MED_OBJECT_FROM_FILE_"+fileName
142         mySO = batchmode_salome.myStudy.FindObject(objNameInStudy)
143         if mySO is not None:
144             anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
145             obj = batchmode_salome.orb.string_to_object(anAttr.Value())
146             if obj is not None:
147                 myObj = obj._narrow(SALOME_MED.MED)
148             else:
149                 print "ERROR: ",myObj," has been found in the Study, but with the type different of SALOME_MED.MED!!!"
150         else:
151             print "ERROR: ",objNameInStudy," hasn't been found in the Study!!!"
152     else:
153         SObj_root = batchmode_salome.myStudy.FindObjectByPath("/Med/")
154         if SObj_root is not None:
155             iter = batchmode_salome.myStudy.NewChildIterator(SObj_root)
156             try:
157                 iter.Init(); 
158                 while iter.More():
159                     Obj = iter.Value()
160                     if Obj is not None:
161                         Ok, anAttr = Builder.FindAttribute(Obj, "AttributeIOR")
162                         if Ok:
163                             if len(anAttr.Value()) > 0:
164                                 obj = batchmode_salome.orb.string_to_object(anAttr.Value())
165                                 if obj is not None:
166                                     myObj = obj._narrow(SALOME_MED.MED)
167                                     if myObj is not None:
168                                         break
169                     iter.Next()
170             except:
171                 print "Exception!!!"
172         else: print "Root object Med hasn't been found in the study!!!"
173     return myObj
174         
175 studyCurrent = batchmode_salome.myStudyName
176 studyCurrentId = batchmode_salome.myStudyId
177
178 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "MED")