Salome HOME
update modules FIELDS
[modules/med.git] / src / MEDCalc / tui / medpresentation.py
index 01defc0daaab6eab800321429cb51c33d9a4072e..870220a09afb8a8ccb87ed597fee1bcaaac1c3c1 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2011-2016  CEA/DEN, EDF R&D
+# Copyright (C) 2011-2019  CEA/DEN, EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 import medcalc
 import MEDCALC, SALOME
 from medcalc.medevents import notifyGui_addPresentation, notifyGui_removePresentation, notifyGui_error, notifyGui_modifyPresentation
+from functools import reduce
 
 __manager = medcalc.medcorba.factory.getPresentationManager()
 
-def MakeMeshView(proxy,
+def MakeMeshView(meshID,
                  viewMode=MEDCALC.VIEW_MODE_DEFAULT,
                  meshMode=MEDCALC.MESH_MODE_DEFAULT):
   # Create the presentation instance in CORBA engine
   # The engine in turn creates the ParaView pipeline elements
-  params = MEDCALC.MeshViewParameters(proxy.id, meshMode)
+  params = MEDCALC.MeshViewParameters(meshID, meshMode)
   try:
     presentation_id = __manager.makeMeshView(params, viewMode)
-    notifyGui_addPresentation(proxy.id, presentation_id)
+    notifyGui_addPresentation(meshID, presentation_id)
     return presentation_id
   except SALOME.SALOME_Exception as e:
-    notifyGui_error("An error occured while creating the mesh view:\n" + e.details.text)
+    notifyGui_error("An error occurred while creating the mesh view:\n" + e.details.text)
     raise Exception(e.details.text)
 
 
@@ -52,7 +53,7 @@ def MakeScalarMap(proxy,
     notifyGui_addPresentation(proxy.id, presentation_id)
     return presentation_id
   except SALOME.SALOME_Exception as e:
-    notifyGui_error("An error occured while creating the scalar map:\n" + e.details.text)
+    notifyGui_error("An error occurred while creating the scalar map:\n" + e.details.text)
     raise Exception(e.details.text)
 
 def MakeContour(proxy,
@@ -67,7 +68,7 @@ def MakeContour(proxy,
     notifyGui_addPresentation(proxy.id, presentation_id)
     return presentation_id
   except SALOME.SALOME_Exception as e:
-    notifyGui_error("An error occured while creating the contour:\n" + e.details.text)
+    notifyGui_error("An error occurred while creating the contour:\n" + e.details.text)
     raise Exception(e.details.text)
 
 #
@@ -85,7 +86,7 @@ def MakeVectorField(proxy,
     notifyGui_addPresentation(proxy.id, presentation_id)
     return presentation_id
   except SALOME.SALOME_Exception as e:
-    notifyGui_error("An error occured while creating the vector field:\n" + e.details.text)
+    notifyGui_error("An error occurred while creating the vector field:\n" + e.details.text)
     raise Exception(e.details.text)
 
 def MakeSlices(proxy,
@@ -104,19 +105,27 @@ def MakeSlices(proxy,
     notifyGui_addPresentation(proxy.id, presentation_id)
     return presentation_id
   except SALOME.SALOME_Exception as e:
-    notifyGui_error("An error occured while creating the slices:\n" + e.details.text)
+    notifyGui_error("An error occurred while creating the slices:\n" + e.details.text)
     raise Exception(e.details.text)
-##
-#
-#def MakeDeflectionShape(proxy,
-#                        viewMode=MEDCALC.VIEW_MODE_DEFAULT
-#                        ):
-#  params = MEDCALC.DeflectionShapeParameters(proxy.id, viewMode)
-#  presentation_id = __manager.makeDeflectionShape(params)
-#  notifyGui_addPresentation(proxy.id, presentation_id)
-#  return presentation_id
-##
-#
+
+
+def MakeDeflectionShape(proxy,
+                  viewMode=MEDCALC.VIEW_MODE_DEFAULT,
+                  scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
+                  colorMap=MEDCALC.COLOR_MAP_DEFAULT
+                  ):
+  # Create the presentation instance in CORBA engine
+  # The engine in turn creates the ParaView pipeline elements
+  params = MEDCALC.DeflectionShapeParameters(proxy.id, scalarBarRange, colorMap)
+  try:
+    presentation_id = __manager.makeDeflectionShape(params, viewMode)
+    notifyGui_addPresentation(proxy.id, presentation_id)
+    return presentation_id
+  except SALOME.SALOME_Exception as e:
+    notifyGui_error("An error occurred while creating the deflection shape:\n" + e.details.text)
+    raise Exception(e.details.text)
+
+
 def MakePointSprite(proxy,
                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
                   displayedComponent=MEDCALC.DISPLAY_DEFAULT,
@@ -131,7 +140,7 @@ def MakePointSprite(proxy,
     notifyGui_addPresentation(proxy.id, presentation_id)
     return presentation_id
   except SALOME.SALOME_Exception as e:
-    notifyGui_error("An error occured while creating the point sprite:\n" + e.details.text)
+    notifyGui_error("An error occurred while creating the point sprite:\n" + e.details.text)
     raise Exception(e.details.text)
 
 def RemovePresentation(presentation_id):
@@ -141,8 +150,8 @@ def RemovePresentation(presentation_id):
 #
 
 def __GetGENERICParameters(tag, presentation_id):
-  exec "params = __manager.get%sParameters(presentation_id)" % tag
-  return params
+  exec("params = __manager.get%sParameters(presentation_id)" % tag)
+  return locals()['params']
 
 GetMeshViewParameters = lambda pres_id: __GetGENERICParameters("MeshView", pres_id)
 GetScalarMapParameters = lambda pres_id: __GetGENERICParameters("ScalarMap", pres_id)
@@ -150,11 +159,11 @@ GetContourParameters = lambda pres_id: __GetGENERICParameters("Contour", pres_id
 GetSlicesParameters = lambda pres_id: __GetGENERICParameters("Slices", pres_id)
 GetPointSpriteParameters = lambda pres_id: __GetGENERICParameters("PointSprite", pres_id)
 GetVectorFieldParameters = lambda pres_id: __GetGENERICParameters("VectorField", pres_id)
-#GetDeflectionShapeParameters = lambda pres_id: __GetGENERICParameters("DeflectionShape", pres_id)
+GetDeflectionShapeParameters = lambda pres_id: __GetGENERICParameters("DeflectionShape", pres_id)
 
 
 def __UpdateGENERIC(tag, presentation_id, params):
-  exec "__manager.update%s(presentation_id, params)" % tag
+  exec("__manager.update%s(presentation_id, params)" % tag)
   notifyGui_modifyPresentation(presentation_id)
 
 UpdateMeshView = lambda pres_id, params: __UpdateGENERIC("MeshView", pres_id, params)
@@ -163,17 +172,21 @@ UpdateContour = lambda pres_id, params: __UpdateGENERIC("Contour", pres_id, para
 UpdateSlices = lambda pres_id, params: __UpdateGENERIC("Slices", pres_id, params)
 UpdateVectorField = lambda pres_id, params: __UpdateGENERIC("VectorField", pres_id, params)
 UpdatePointSprite = lambda pres_id, params: __UpdateGENERIC("PointSprite", pres_id, params)
-#UpdateDeflectionShape = lambda pres_id, params: __UpdateGENERIC("DeflectionShape", pres_id, params)
+UpdateDeflectionShape = lambda pres_id, params: __UpdateGENERIC("DeflectionShape", pres_id, params)
 
 def ComputeCellAverageSize(obj):
   """
   @return the average cell size
   """
   bb, nCells = obj.GetDataInformation().GetBounds(), obj.GetDataInformation().GetNumberOfCells()
-  bb = zip(bb[::2], bb[1::2])
+  bb = list(zip(bb[::2], bb[1::2]))
   deltas = [x[1]-x[0] for x in bb]
+  ## Filter out null dimensions:
+  avgDelta = sum(deltas) / 3.0
+  deltas = [d for d in deltas if abs(d) > 1.0e-12*avgDelta]
+  ##
   vol = reduce(lambda x,y:x*y, deltas, 1.0) 
-  cellSize = (vol/nCells)**(1.0/3.0)  # necessarily 3D in ParaView
+  cellSize = (vol/nCells)**(1.0/float(len(deltas)))
   return cellSize
 
 def GetDomainCenter(obj):
@@ -181,7 +194,7 @@ def GetDomainCenter(obj):
   @return the center of the domain as the central point of the bounding box
   """
   bb = obj.GetDataInformation().GetBounds()
-  bb = zip(bb[::2], bb[1::2])
+  bb = list(zip(bb[::2], bb[1::2]))
   mids = [x[0] + 0.5*(x[1]-x[0]) for x in bb]
   return mids
 
@@ -192,7 +205,7 @@ def GetSliceOrigins(obj, nbSlices, normal):
   """
   from math import sqrt
   bb = obj.GetDataInformation().GetBounds()
-  bb = zip(bb[::2], bb[1::2])
+  bb = list(zip(bb[::2], bb[1::2]))
   origin = [x[0] + 0.5*(x[1]-x[0]) for x in bb]
   deltas = [x[1]-x[0] for x in bb]
   # Compute extent of slices:
@@ -208,3 +221,19 @@ def GetSliceOrigins(obj, nbSlices, normal):
     origins.append(orig_j)
   return origins
 
+def SelectSourceField(obj, meshName, fieldName, discretisation):
+  """
+  Properly set the AllArrays property of a MEDReader source to point to the correct field.
+  Setting the fieldName to void string is allowed (meaning we work on the mesh only).
+  """
+  if fieldName == "":
+    return
+  tree = obj.GetProperty("FieldsTreeInfo")[::2]
+  it = None
+  for t in tree:
+    arr = t.split("/")
+    arr = arr[:-1] + arr[-1].split("@@][@@")
+    if arr[1] == meshName and arr[3] == fieldName and arr[4] == discretisation:
+      obj.AllArrays = [t]
+      return
+  raise Exception("Field not found")