Salome HOME
updated copyright message
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationSlices.cxx
index 9b5d97c3c67c7a95049359e8021a06355dc22144..42c87a7e080bac38eb0efeb07ebb7671c02b45fb 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016  CEA/DEN, EDF R&D
+// Copyright (C) 2016-2023  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
 
 #include "MEDPresentationSlices.hxx"
 
+#include <SALOME_KernelServices.hxx>
+#undef LOG  // should be fixed in KERNEL - double definition
+#include <Basics_Utils.hxx>
+
+#include <sstream>
+
+const std::string MEDPresentationSlices::TYPE_NAME = "MEDPresentationSlices";
+const std::string MEDPresentationSlices::PROP_NB_SLICES = "nbSlices";
+const std::string MEDPresentationSlices::PROP_SLICE_ORIENTATION = "slicesOrientation";
+
+MEDPresentationSlices::MEDPresentationSlices(const MEDCALC::SlicesParameters& params,
+                                               const MEDCALC::ViewModeType viewMode) :
+        MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
+        _params(params)
+{
+  setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, params.nbSlices);
+  setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, params.orientation);
+
+  int id = GeneratePythonId();
+  std::ostringstream oss;
+  oss << "__objLst" << id;
+  _sliceListVar = oss.str();
+}
+
+void
+MEDPresentationSlices::setSliceParametersAndGroup()
+{
+  std::ostringstream oss;
+  int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
+  std::string normal = getNormalVector();
+
+  oss << "__origins = medcalc.GetSliceOrigins(" << _srcObjVar << ", " << nbSlices << ", " << normal << ");";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
+  oss << "for sliceNum in range(" << nbSlices << "):\n";
+  oss << "  " << _sliceListVar << "[sliceNum].SliceType.Normal = " << normal << ";\n";
+  oss << "  " << _sliceListVar << "[sliceNum].SliceType.Origin = __origins[sliceNum];\n";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
+  oss << _objVar << " = pvs.GroupDatasets(Input=" << _sliceListVar << ");";
+  pushAndExecPyLine(oss.str()); oss.str("");
+}
+
+void
+MEDPresentationSlices::deleteGroup()
+{
+  deleteThresholds();
+  std::ostringstream oss;
+  oss << "pvs.Hide(" << _objVar << ");";
+  oss << "pvs.Delete(" << _objVar << ");del " <<_objVar<<";";
+  pushAndExecPyLine(oss.str()); oss.str("");
+}
+
+void
+MEDPresentationSlices::adaptNumberOfSlices()
+{
+  std::ostringstream oss;
+  int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
+
+  oss << "for _ in range(int(max(len(" << _sliceListVar << ")-" << nbSlices << ", 0))):\n";
+  oss << "  pvs.Delete(" << _sliceListVar << ".pop());\n";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
+  oss << "for _ in range(" << nbSlices << "-int(max(len(" << _sliceListVar << "), 0))):\n";
+  oss << "  obj = pvs.Slice(Input=" << _srcObjVar << ");\n";
+  oss << "  obj.SliceType = 'Plane';\n";
+  oss << "  " << _sliceListVar << ".append(obj);\n";
+  pushAndExecPyLine(oss.str()); oss.str("");
+}
+
+void
+MEDPresentationSlices::generateAndDisplay()
+{
+  adaptNumberOfSlices();
+  setSliceParametersAndGroup();
+  recreateViewSetup();
+}
+
+std::string
+MEDPresentationSlices::getNormalVector() const
+{
+  switch(_params.orientation)
+  {
+    case MEDCALC::SLICE_NORMAL_TO_X:
+      return "[1.0, 0.0, 0.0]";
+    case MEDCALC::SLICE_NORMAL_TO_Y:
+      return "[0.0, 1.0, 0.0]";
+    case MEDCALC::SLICE_NORMAL_TO_Z:
+      return "[0.0, 0.0, 1.0]";
+    case MEDCALC::SLICE_NORMAL_TO_XY:
+      return "[1.0, 1.0, 0.0]";
+    case MEDCALC::SLICE_NORMAL_TO_XZ:
+      return "[1.0, 0.0, 1.0]";
+    case MEDCALC::SLICE_NORMAL_TO_YZ:
+      return "[0.0, 1.0, 1.0]";
+    case MEDCALC::SLICE_NORMAL_TO_XYZ:
+      return "[1.0, 1.0, 1.0]";
+    default:
+      const char * mes = "Unexpected getSliceOrientationCommand() error!";
+      STDLOG(mes);
+      throw KERNEL::createSalomeException(mes);
+  }
+  return ""; // never happens
+}
+
+void
+MEDPresentationSlices::selectSliceOrientation(const std::string & obj)
+{
+  std::ostringstream oss1;
+  oss1 << obj << ".SliceType.Normal = " << getNormalVector() << ";";
+  pushAndExecPyLine(oss1.str()); oss1.str("");
+}
+
 void
 MEDPresentationSlices::internalGeneratePipeline()
 {
-  PyGILState_STATE _gil_state = PyGILState_Ensure();
+  MEDPresentation::internalGeneratePipeline();
+
+  MEDPyLockWrapper lock;
 
-  std::string cmd = std::string("import pvsimple as pvs;");
-  cmd += getRenderViewCommand(_params.viewMode); // define __view1
+  std::ostringstream oss;
 
-  cmd += std::string("__obj1 = pvs.MEDReader(FileName='") + _fileName + std::string("');");
-  cmd += std::string("__slice1 = pvs.Slice(Input=__obj1);");
-  cmd += std::string("__disp1 = pvs.Show(__slice1, __view1);");
-  cmd += std::string("pvs.ColorBy(__disp1, ('") + _fieldType + std::string("', '") + _fieldName + std::string("'));");
-  cmd += std::string("__disp1.SetScalarBarVisibility(__view1, True);");
-  cmd += std::string("__disp1.RescaleTransferFunctionToDataRangeOverTime();");
-  cmd += std::string("pvs.Render();");
+  createSource();
+  setTimestamp();
 
-  cmd += getResetCameraCommand();
+  // Populate internal array of available components:
+  fillAvailableFieldComponents();
+  if (_params.nbSlices < 1)
+  {
+      const char * mes = "Invalid number of slices!";
+      STDLOG(mes);
+      throw KERNEL::createSalomeException(mes);
+  }
 
-  //std::cerr << "Python command:" << std::endl;
-  //std::cerr << cmd << std::endl;
-  PyRun_SimpleString(cmd.c_str());
-  // Retrieve Python object for internal storage:
-  PyObject* obj = getPythonObjectFromMain("__slice1");
-  PyObject* disp = getPythonObjectFromMain("__disp1");
-  pushInternal(obj, disp);
+  setOrCreateRenderView(); // instantiate __viewXXX
 
-  PyGILState_Release(_gil_state);
+  // Now create the initial slices list
+  oss << _sliceListVar << " = [];";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
+  generateAndDisplay();
 }
 
 void
 MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
 {
-  // :TODO:
+  if (params.fieldHandlerId != _params.fieldHandlerId)
+    throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
+
+  if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
+    updateComponent<MEDPresentationSlices, MEDCALC::SlicesParameters>(std::string(params.displayedComponent));
+  if (params.scalarBarRange != _params.scalarBarRange ||
+      params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
+      params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
+      params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
+    updateScalarBarRange<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarRange,
+                                                                           params.hideDataOutsideCustomRange,
+                                                                           params.scalarBarRangeArray[0],
+                                                                           params.scalarBarRangeArray[1]);
+  if (params.colorMap != _params.colorMap)
+    updateColorMap<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.colorMap);
+
+  if (params.orientation != _params.orientation)
+    updateOrientation(params.orientation);
+  if (params.nbSlices != _params.nbSlices)
+    {
+      if (params.nbSlices < 1)
+      {
+          const char * mes = "Invalid number of slices!";
+          STDLOG(mes);
+          throw KERNEL::createSalomeException(mes);
+      }
+      updateNbSlices(params.nbSlices);
+    }
+  if (params.visibility != _params.visibility)
+    updateVisibility<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.visibility);
+
+  if (params.scalarBarVisibility != _params.scalarBarVisibility)
+    updateScalarBarVisibility<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarVisibility);
 }
+
+void
+MEDPresentationSlices::updateNbSlices(const int nbSlices)
+{
+  _params.nbSlices = nbSlices;
+
+  // GUI helper:
+  setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, nbSlices);
+
+  // Update the pipeline:
+  {
+    MEDPyLockWrapper lock;
+    deleteGroup();
+    generateAndDisplay();
+  }
+}
+
+void
+MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
+{
+  _params.orientation = orientation;
+
+  // GUI helper:
+  setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, static_cast<int>(orientation));
+
+  // Update the pipeline:
+  {
+    MEDPyLockWrapper lock;
+
+    deleteGroup();
+    generateAndDisplay();
+  }
+}
+