Salome HOME
Merge 'abn/V8_1_fix' branch into V8_1_BR.
[modules/med.git] / src / MEDCalc / tui / medpresentation.py
1 # Copyright (C) 2011-2016  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, or (at your option) any later version.
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
20 import medcalc
21 import MEDCALC, SALOME
22 from medcalc.medevents import notifyGui_addPresentation, notifyGui_removePresentation, notifyGui_error, notifyGui_modifyPresentation
23
24 __manager = medcalc.medcorba.factory.getPresentationManager()
25
26 def MakeMeshView(meshID,
27                  viewMode=MEDCALC.VIEW_MODE_DEFAULT,
28                  meshMode=MEDCALC.MESH_MODE_DEFAULT):
29   # Create the presentation instance in CORBA engine
30   # The engine in turn creates the ParaView pipeline elements
31   params = MEDCALC.MeshViewParameters(meshID, meshMode)
32   try:
33     presentation_id = __manager.makeMeshView(params, viewMode)
34     notifyGui_addPresentation(meshID, presentation_id)
35     return presentation_id
36   except SALOME.SALOME_Exception as e:
37     notifyGui_error("An error occured while creating the mesh view:\n" + e.details.text)
38     raise Exception(e.details.text)
39
40
41 def MakeScalarMap(proxy,
42                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
43                   displayedComponent=MEDCALC.DISPLAY_DEFAULT,
44                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
45                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
46                   ):
47   # Create the presentation instance in CORBA engine
48   # The engine in turn creates the ParaView pipeline elements
49   params = MEDCALC.ScalarMapParameters(proxy.id, displayedComponent, scalarBarRange, colorMap)
50   try:
51     presentation_id = __manager.makeScalarMap(params, viewMode)
52     notifyGui_addPresentation(proxy.id, presentation_id)
53     return presentation_id
54   except SALOME.SALOME_Exception as e:
55     notifyGui_error("An error occured while creating the scalar map:\n" + e.details.text)
56     raise Exception(e.details.text)
57
58 def MakeContour(proxy,
59                 viewMode=MEDCALC.VIEW_MODE_DEFAULT,
60                 scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
61                 colorMap=MEDCALC.COLOR_MAP_DEFAULT,
62                 nbContours=MEDCALC.NB_CONTOURS_DEFAULT
63                 ):
64   params = MEDCALC.ContourParameters(proxy.id, scalarBarRange, colorMap, nbContours)
65   try:
66     presentation_id = __manager.makeContour(params, viewMode)
67     notifyGui_addPresentation(proxy.id, presentation_id)
68     return presentation_id
69   except SALOME.SALOME_Exception as e:
70     notifyGui_error("An error occured while creating the contour:\n" + e.details.text)
71     raise Exception(e.details.text)
72
73 #
74
75 def MakeVectorField(proxy,
76                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
77                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
78                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
79                   ):
80   # Create the presentation instance in CORBA engine
81   # The engine in turn creates the ParaView pipeline elements
82   params = MEDCALC.VectorFieldParameters(proxy.id, scalarBarRange, colorMap)
83   try:
84     presentation_id = __manager.makeVectorField(params, viewMode)
85     notifyGui_addPresentation(proxy.id, presentation_id)
86     return presentation_id
87   except SALOME.SALOME_Exception as e:
88     notifyGui_error("An error occured while creating the vector field:\n" + e.details.text)
89     raise Exception(e.details.text)
90
91 def MakeSlices(proxy,
92                 viewMode=MEDCALC.VIEW_MODE_DEFAULT,
93                 displayedComponent=MEDCALC.DISPLAY_DEFAULT,
94                 scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
95                 colorMap=MEDCALC.COLOR_MAP_DEFAULT,
96                 sliceOrientation=MEDCALC.SLICE_ORIENTATION_DEFAULT,
97                 nbSlices=MEDCALC.NB_SLICES_DEFAULT):
98   # Create the presentation instance in CORBA engine
99   # The engine in turn creates the ParaView pipeline elements
100   params = MEDCALC.SlicesParameters(proxy.id, displayedComponent,scalarBarRange, colorMap, 
101                                     sliceOrientation, nbSlices)
102   try:
103     presentation_id = __manager.makeSlices(params, viewMode)
104     notifyGui_addPresentation(proxy.id, presentation_id)
105     return presentation_id
106   except SALOME.SALOME_Exception as e:
107     notifyGui_error("An error occured while creating the slices:\n" + e.details.text)
108     raise Exception(e.details.text)
109
110
111 def MakeDeflectionShape(proxy,
112                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
113                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
114                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
115                   ):
116   # Create the presentation instance in CORBA engine
117   # The engine in turn creates the ParaView pipeline elements
118   params = MEDCALC.DeflectionShapeParameters(proxy.id, scalarBarRange, colorMap)
119   try:
120     presentation_id = __manager.makeDeflectionShape(params, viewMode)
121     notifyGui_addPresentation(proxy.id, presentation_id)
122     return presentation_id
123   except SALOME.SALOME_Exception as e:
124     notifyGui_error("An error occured while creating the deflection shape:\n" + e.details.text)
125     raise Exception(e.details.text)
126
127
128 def MakePointSprite(proxy,
129                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
130                   displayedComponent=MEDCALC.DISPLAY_DEFAULT,
131                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
132                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
133                   ):
134   # Create the presentation instance in CORBA engine
135   # The engine in turn creates the ParaView pipeline elements
136   params = MEDCALC.PointSpriteParameters(proxy.id, displayedComponent, scalarBarRange, colorMap)
137   try:
138     presentation_id = __manager.makePointSprite(params, viewMode)
139     notifyGui_addPresentation(proxy.id, presentation_id)
140     return presentation_id
141   except SALOME.SALOME_Exception as e:
142     notifyGui_error("An error occured while creating the point sprite:\n" + e.details.text)
143     raise Exception(e.details.text)
144
145 def RemovePresentation(presentation_id):
146   ok = __manager.removePresentation(presentation_id)
147   if ok:
148     notifyGui_removePresentation(presentation_id)
149 #
150
151 def __GetGENERICParameters(tag, presentation_id):
152   exec "params = __manager.get%sParameters(presentation_id)" % tag
153   return params
154
155 GetMeshViewParameters = lambda pres_id: __GetGENERICParameters("MeshView", pres_id)
156 GetScalarMapParameters = lambda pres_id: __GetGENERICParameters("ScalarMap", pres_id)
157 GetContourParameters = lambda pres_id: __GetGENERICParameters("Contour", pres_id)
158 GetSlicesParameters = lambda pres_id: __GetGENERICParameters("Slices", pres_id)
159 GetPointSpriteParameters = lambda pres_id: __GetGENERICParameters("PointSprite", pres_id)
160 GetVectorFieldParameters = lambda pres_id: __GetGENERICParameters("VectorField", pres_id)
161 GetDeflectionShapeParameters = lambda pres_id: __GetGENERICParameters("DeflectionShape", pres_id)
162
163
164 def __UpdateGENERIC(tag, presentation_id, params):
165   exec "__manager.update%s(presentation_id, params)" % tag
166   notifyGui_modifyPresentation(presentation_id)
167
168 UpdateMeshView = lambda pres_id, params: __UpdateGENERIC("MeshView", pres_id, params)
169 UpdateScalarMap = lambda pres_id, params: __UpdateGENERIC("ScalarMap", pres_id, params)
170 UpdateContour = lambda pres_id, params: __UpdateGENERIC("Contour", pres_id, params)
171 UpdateSlices = lambda pres_id, params: __UpdateGENERIC("Slices", pres_id, params)
172 UpdateVectorField = lambda pres_id, params: __UpdateGENERIC("VectorField", pres_id, params)
173 UpdatePointSprite = lambda pres_id, params: __UpdateGENERIC("PointSprite", pres_id, params)
174 UpdateDeflectionShape = lambda pres_id, params: __UpdateGENERIC("DeflectionShape", pres_id, params)
175
176 def ComputeCellAverageSize(obj):
177   """
178   @return the average cell size
179   """
180   bb, nCells = obj.GetDataInformation().GetBounds(), obj.GetDataInformation().GetNumberOfCells()
181   bb = zip(bb[::2], bb[1::2])
182   deltas = [x[1]-x[0] for x in bb]
183   ## Filter out null dimensions:
184   avgDelta = sum(deltas) / 3.0
185   deltas = [d for d in deltas if abs(d) > 1.0e-12*avgDelta]
186   ##
187   vol = reduce(lambda x,y:x*y, deltas, 1.0) 
188   cellSize = (vol/nCells)**(1.0/float(len(deltas)))
189   return cellSize
190
191 def GetDomainCenter(obj):
192   """
193   @return the center of the domain as the central point of the bounding box
194   """
195   bb = obj.GetDataInformation().GetBounds()
196   bb = zip(bb[::2], bb[1::2])
197   mids = [x[0] + 0.5*(x[1]-x[0]) for x in bb]
198   return mids
199
200 def GetSliceOrigins(obj, nbSlices, normal):
201   """
202   Compute all origin points for the position of the slices.
203   @param normal is a list of 3 floats either 0 or 1 indicating the normal vector of the slices
204   """
205   from math import sqrt
206   bb = obj.GetDataInformation().GetBounds()
207   bb = zip(bb[::2], bb[1::2])
208   origin = [x[0] + 0.5*(x[1]-x[0]) for x in bb]
209   deltas = [x[1]-x[0] for x in bb]
210   # Compute extent of slices:
211   l = [normal[i]*deltas[i]**2 for i in range(3)]   # either the X extend, or the XY diagonal, or the XYZ diagonal
212   plus = lambda x,y: x+y
213   extent = sqrt(reduce(plus, l, 0.0))
214   norm = sqrt(reduce(plus, normal, 0.0))
215   normal = [normal[i]/norm for i in range(3)]
216   origins = []
217   step = extent/nbSlices
218   for j in range(nbSlices):
219     orig_j = [origin[i]+normal[i]*(-0.5*extent + step*(0.5+j)) for i in range(3)]
220     origins.append(orig_j)
221   return origins
222
223 def SelectSourceField(obj, meshName, fieldName, discretisation):
224   """
225   Properly set the AllArrays property of a MEDReader source to point to the correct field.
226   Setting the fieldName to void string is allowed (meaning we work on the mesh only).
227   """
228   if fieldName == "":
229     return
230   tree = obj.GetProperty("FieldsTreeInfo")[::2]
231   it = None
232   for t in tree:
233     arr = t.split("/")
234     arr = arr[:-1] + arr[-1].split("@@][@@")
235     if arr[1] == meshName and arr[3] == fieldName and arr[4] == discretisation:
236       obj.AllArrays = [t]
237       return
238   raise Exception("Field not found")