Salome HOME
[MEDCalc] : better slices + more error handling when invalid params
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationSlices.cxx
index a71284f03e641c3e4ade2a64daab696bbb68a51b..8654e25bc27f59d40cb6890ab3b3393e67757456 100644 (file)
@@ -30,62 +30,112 @@ const std::string MEDPresentationSlices::PROP_NB_SLICES = "nbSlices";
 const std::string MEDPresentationSlices::PROP_SLICE_ORIENTATION = "slicesOrientation";
 
 MEDPresentationSlices::MEDPresentationSlices(const MEDCALC::SlicesParameters& params,
-                                               const MEDCALC::MEDPresentationViewMode viewMode) :
+                                               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();
 }
 
-std::string
-MEDPresentationSlices::getNbSlicesCommand() const
+void
+MEDPresentationSlices::setSliceParametersAndGroup()
 {
-  std::ostringstream oss1;
-  // TODO
-//  oss1 << _objVar << ".SliceType.Normal = " << norm << ";";
-  return oss1.str();
+  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("");
 }
 
-std::string
-MEDPresentationSlices::getOrientationCommand() const
+void
+MEDPresentationSlices::deleteGroup()
 {
-  std::ostringstream oss1;
-  std::string norm;
+  std::ostringstream oss;
+  oss << "pvs.Delete(" << _objVar << ");";
+  pushAndExecPyLine(oss.str()); oss.str("");
+}
+
+void
+MEDPresentationSlices::adaptNumberOfSlices()
+{
+  std::ostringstream oss;
+  int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
+
+  oss << "for _ in range(max(len(" << _sliceListVar << ")-" << nbSlices << ", 0)):\n";
+  oss << "  pvs.Delete(" << _sliceListVar << ".pop());\n";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
+  oss << "for _ in range(" << nbSlices << "-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();
+
+  showObject();
+
+  colorBy(_pvFieldType);
+  showScalarBar();
+  selectColorMap();
+  rescaleTransferFunction();
+  resetCameraAndRender();
+}
+
+std::string
+MEDPresentationSlices::getNormalVector() const
+{
   switch(_params.orientation)
   {
     case MEDCALC::SLICE_NORMAL_TO_X:
-      norm = "[1.0, 0.0, 0.0]";
-      break;
+      return "[1.0, 0.0, 0.0]";
     case MEDCALC::SLICE_NORMAL_TO_Y:
-      norm = "[0.0, 1.0, 0.0]";
-      break;
+      return "[0.0, 1.0, 0.0]";
     case MEDCALC::SLICE_NORMAL_TO_Z:
-      norm = "[0.0, 0.0, 1.0]";
-      break;
+      return "[0.0, 0.0, 1.0]";
     case MEDCALC::SLICE_NORMAL_TO_XY:
-      norm = "[1.0, 1.0, 0.0]";
-      break;
+      return "[1.0, 1.0, 0.0]";
     case MEDCALC::SLICE_NORMAL_TO_XZ:
-      norm = "[1.0, 0.0, 1.0]";
-      break;
+      return "[1.0, 0.0, 1.0]";
     case MEDCALC::SLICE_NORMAL_TO_YZ:
-      norm = "[0.0, 1.0, 1.0]";
-      break;
+      return "[0.0, 1.0, 1.0]";
     case MEDCALC::SLICE_NORMAL_TO_XYZ:
-      norm = "[1.0, 1.0, 1.0]";
-      break;
+      return "[1.0, 1.0, 1.0]";
     default:
       const char * mes = "Unexpected getSliceOrientationCommand() error!";
       STDLOG(mes);
       throw KERNEL::createSalomeException(mes);
   }
-
-  oss1 << _objVar << ".SliceType.Normal = " << norm << ";";
-  return oss1.str();
+  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()
@@ -94,36 +144,26 @@ MEDPresentationSlices::internalGeneratePipeline()
 
   MEDPyLockWrapper lock;
 
-  std::ostringstream oss_o, oss;
-  std::string view(getRenderViewVar());
+  std::ostringstream oss;
 
-  oss << _srcObjVar << " = pvs.MEDReader(FileName='" << _fileName << "');";
-  pushAndExecPyLine(oss.str()); oss.str("");
+  createSource();
 
   // Populate internal array of available components:
   fillAvailableFieldComponents();
-  pushAndExecPyLine( getRenderViewCommand() ); // instanciate __viewXXX
-  oss << _objVar << " = pvs.Slice(Input=" << _srcObjVar << ");";
-  pushAndExecPyLine(oss.str()); oss.str("");
-  oss << _objVar << ".SliceType = 'Plane';";
-  pushAndExecPyLine(oss.str()); oss.str("");
+  if (_params.nbSlices < 1)
+  {
+      const char * mes = "Invalid number of slices!";
+      STDLOG(mes);
+      throw KERNEL::createSalomeException(mes);
+  }
 
-  // Set slice orientation
-  pushAndExecPyLine(getOrientationCommand());
+  setOrCreateRenderView(); // instanciate __viewXXX
 
-  oss << _dispVar << " = pvs.Show(" << _objVar << ", " << view << ");";
+  // Now create the initial slices list
+  oss << _sliceListVar << " = [];";
   pushAndExecPyLine(oss.str()); oss.str("");
-  oss << "pvs.ColorBy(" << _dispVar << ", ('" << _fieldType << "', '" << _fieldName << "'));";
-  pushAndExecPyLine(oss.str()); oss.str("");
-  oss << _dispVar <<  ".SetScalarBarVisibility(" << view << ", True);";
-  pushAndExecPyLine(oss.str()); oss.str("");
-  oss << getRescaleCommand();
-  pushAndExecPyLine(oss.str()); oss.str("");
-  oss << _lutVar << " = pvs.GetColorTransferFunction('" << _fieldName << "');";
-  pushAndExecPyLine(oss.str()); oss.str("");
-  pushAndExecPyLine(getColorMapCommand()); oss.str("");
-  pushAndExecPyLine(getResetCameraCommand());
-  pushAndExecPyLine("pvs.Render();");
+
+  generateAndDisplay();
 }
 
 void
@@ -142,7 +182,15 @@ MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
   if (params.orientation != _params.orientation)
     updateOrientation(params.orientation);
   if (params.nbSlices != _params.nbSlices)
-    updateNbSlices(params.nbSlices);
+    {
+      if (params.nbSlices < 1)
+      {
+          const char * mes = "Invalid number of slices!";
+          STDLOG(mes);
+          throw KERNEL::createSalomeException(mes);
+      }
+      updateNbSlices(params.nbSlices);
+    }
 }
 
 void
@@ -156,26 +204,25 @@ MEDPresentationSlices::updateNbSlices(const int nbSlices)
   // Update the pipeline:
   {
     MEDPyLockWrapper lock;
-    std::string cmd = getNbSlicesCommand();
-    pushAndExecPyLine(cmd);
-    pushAndExecPyLine("pvs.Render();");
+    deleteGroup();
+    generateAndDisplay();
   }
 }
 
 void
-MEDPresentationSlices::updateOrientation(const MEDCALC::MEDPresentationSliceOrientation orientation)
+MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
 {
   _params.orientation = orientation;
 
   // GUI helper:
-  setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, orientation);
+  setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, static_cast<int>(orientation));
 
   // Update the pipeline:
   {
     MEDPyLockWrapper lock;
-    std::string cmd = getOrientationCommand();
-    pushAndExecPyLine(cmd);
-    pushAndExecPyLine("pvs.Render();");
+
+    deleteGroup();
+    generateAndDisplay();
   }
 }