Salome HOME
[MEDCalc] : better slices + more error handling when invalid params
authorabn <adrien.bruneton@cea.fr>
Mon, 8 Aug 2016 08:41:06 +0000 (10:41 +0200)
committerabn <adrien.bruneton@cea.fr>
Mon, 8 Aug 2016 08:41:06 +0000 (10:41 +0200)
src/MEDCalc/cmp/MEDPresentation.cxx
src/MEDCalc/cmp/MEDPresentationContour.cxx
src/MEDCalc/cmp/MEDPresentationDeflectionShape.cxx
src/MEDCalc/cmp/MEDPresentationSlices.cxx
src/MEDCalc/cmp/MEDPresentationSlices.hxx
src/MEDCalc/cmp/MEDPresentationVectorField.cxx
src/MEDCalc/gui/PresentationController.cxx
src/MEDCalc/test/tui/slices.py

index 92c22a9049ca97f1ff95fb781e4cd61bf0efada3..e76ad76935ddf784b4250e34fe39073d49a4e4b1 100644 (file)
@@ -130,7 +130,7 @@ void
 MEDPresentation::execPyLine(const std::string & lin)
 {
   MEDPyLockWrapper lock;
-//  STDLOG("@@@@ MEDPresentation::execPyLine() about to exec >> " << lin);
+  STDLOG("@@@@ MEDPresentation::execPyLine() about to exec >> " << lin);
   if(PyRun_SimpleString(lin.c_str()))
     {
       std::ostringstream oss;
index d8b6b48d8a565280308ee5433930e21a9230f636..ee4628505aef623b148b0ff522c007eebc8cff3b 100644 (file)
@@ -66,6 +66,12 @@ MEDPresentationContour::internalGeneratePipeline()
       STDLOG(msg);
       throw KERNEL::createSalomeException(msg);
     }
+  if (_params.nbContours < 1)
+    {
+      const char * mes = "Invalid number of contours!";
+      STDLOG(mes);
+      throw KERNEL::createSalomeException(mes);
+    }
 
   setOrCreateRenderView(); // instanciate __viewXXX
 
@@ -103,7 +109,15 @@ MEDPresentationContour::updatePipeline(const MEDCALC::ContourParameters& params)
     updateColorMap<MEDPresentationContour, MEDCALC::ContourParameters>(params.colorMap);
 
   if (params.nbContours != _params.nbContours)
-    updateNbContours(params.nbContours);
+    {
+      if (params.nbContours < 1)
+        {
+          const char * mes = "Invalid number of contours!";
+          STDLOG(mes);
+          throw KERNEL::createSalomeException(mes);
+        }
+      updateNbContours(params.nbContours);
+    }
 }
 
 void
index 9fab15f781984012318cf9bea63a32d9805127e8..fdb5d5c3ac34bb467228e02b0d810ff1dbf7dfd6 100644 (file)
@@ -42,7 +42,7 @@ MEDPresentationDeflectionShape::autoScale()
   std::ostringstream oss;
   oss << "import medcalc;";
   pushAndExecPyLine(oss.str()); oss.str("");
-  oss << _objVar << ".ScaleFactor = 3.0*medcalc.ComputeCellAverageSize(__srcObj0)/(" << _rangeVar
+  oss << _objVar << ".ScaleFactor = 3.0*medcalc.ComputeCellAverageSize(" << _srcObjVar << ")/(" << _rangeVar
       << "[1]-" << _rangeVar << "[0]);";
   pushAndExecPyLine(oss.str()); oss.str("");
 }
index 99c87041a667f4e7f570345048540f50090d5fae..8654e25bc27f59d40cb6890ab3b3393e67757456 100644 (file)
@@ -36,10 +36,15 @@ MEDPresentationSlices::MEDPresentationSlices(const MEDCALC::SlicesParameters& pa
 {
   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::generateSlices()
+MEDPresentationSlices::setSliceParametersAndGroup()
 {
   std::ostringstream oss;
   int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
@@ -47,23 +52,47 @@ MEDPresentationSlices::generateSlices()
 
   oss << "__origins = medcalc.GetSliceOrigins(" << _srcObjVar << ", " << nbSlices << ", " << normal << ");";
   pushAndExecPyLine(oss.str()); oss.str("");
-  pushAndExecPyLine("__objLst = [];");
+
   oss << "for sliceNum in range(" << nbSlices << "):\n";
-  oss << "  obj = pvs.Slice(Input=" << _srcObjVar << ")\n";
-  oss << "  obj.SliceType = 'Plane'\n";
-  oss << "  obj.SliceType.Normal = " << normal << "\n";
-  oss << "  obj.SliceType.Origin = __origins[sliceNum]\n";
-  oss << "  __objLst.append(obj)\n\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()
+{
+  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 << _objVar << " = pvs.GroupDatasets(Input=__objLst);";
+  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()
 {
-  generateSlices();
+  adaptNumberOfSlices();
+  setSliceParametersAndGroup();
+
   showObject();
 
   colorBy(_pvFieldType);
@@ -73,17 +102,6 @@ MEDPresentationSlices::generateAndDisplay()
   resetCameraAndRender();
 }
 
-
-void
-MEDPresentationSlices::clearPreviousSlices()
-{
-  std::ostringstream oss;
-
-  pushAndExecPyLine("for sliceNum, _ in enumerate(__objLst):\n  pvs.Delete(__objLst[sliceNum]);");
-  oss <<            "pvs.Delete(" << _objVar << ");";
-  pushAndExecPyLine(oss.str()); oss.str("");
-}
-
 std::string
 MEDPresentationSlices::getNormalVector() const
 {
@@ -126,15 +144,25 @@ MEDPresentationSlices::internalGeneratePipeline()
 
   MEDPyLockWrapper lock;
 
-  std::ostringstream oss_o, oss;
+  std::ostringstream oss;
 
   createSource();
 
   // Populate internal array of available components:
   fillAvailableFieldComponents();
+  if (_params.nbSlices < 1)
+  {
+      const char * mes = "Invalid number of slices!";
+      STDLOG(mes);
+      throw KERNEL::createSalomeException(mes);
+  }
+
   setOrCreateRenderView(); // instanciate __viewXXX
 
-  // Now create the initial number of slices
+  // Now create the initial slices list
+  oss << _sliceListVar << " = [];";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
   generateAndDisplay();
 }
 
@@ -154,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
@@ -168,7 +204,7 @@ MEDPresentationSlices::updateNbSlices(const int nbSlices)
   // Update the pipeline:
   {
     MEDPyLockWrapper lock;
-    clearPreviousSlices();
+    deleteGroup();
     generateAndDisplay();
   }
 }
@@ -185,7 +221,7 @@ MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType ori
   {
     MEDPyLockWrapper lock;
 
-    clearPreviousSlices();
+    deleteGroup();
     generateAndDisplay();
   }
 }
index 19afa196e92d5440f2870e547dd309dd7b09eaa2..721ff61a7889a24f431ee4d8a88b87dbbe52a285 100644 (file)
@@ -43,8 +43,9 @@ protected:
   void updateOrientation(const MEDCALC::SliceOrientationType orientation);
   virtual void internalGeneratePipeline();
 
-  void generateSlices();
-  void clearPreviousSlices();
+  void deleteGroup();
+  void setSliceParametersAndGroup();
+  void adaptNumberOfSlices();
   void generateAndDisplay();
   void selectSliceOrientation(const std::string & obj);
 
@@ -52,6 +53,7 @@ protected:
 
 private:
   MEDCALC::SlicesParameters _params;
+  std::string _sliceListVar;
 };
 
 #endif
index a05075ed14bc6b6d9b85b7930ba2c91fef5d6860..92b857761d5d8aed46ba286c33a49955c0fddd5b 100644 (file)
@@ -42,7 +42,7 @@ MEDPresentationVectorField::autoScale()
   std::ostringstream oss;
 //  oss << "import medcalc;";
 //  pushAndExecPyLine(oss.str()); oss.str("");
-  oss << _objVar << ".ScaleFactor = 2.0*medcalc.ComputeCellAverageSize(__srcObj0)/(" << _rangeVar
+  oss << _objVar << ".ScaleFactor = 2.0*medcalc.ComputeCellAverageSize(" << _srcObjVar << ")/(" << _rangeVar
       << "[1]-" << _rangeVar << "[0]);";
   pushAndExecPyLine(oss.str()); oss.str("");
 }
index d3dcce702b0580fe4bd3baf3b4fd6afb3c381a13..3b8e9e21948bf30037e5f25ae397e76f5138f393 100644 (file)
@@ -755,6 +755,8 @@ PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
   }
   else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) {
       updateTreeViewForPresentationRemoval(event->presentationId);
+      // Hide parameter widget if necessary:
+      onPresentationSelected(-1, "", "");
   }
   else if ( event->type == MEDCALC::EVENT_MODIFY_PRESENTATION ) {
       // Update parameter widget if shown:
index bbfcff31d2d6aefa584d8804ec7ea827b274e3b6..9a848710fc12a85e7ee1319f587839176b3bbb95 100644 (file)
@@ -33,7 +33,11 @@ source_id = medcalc.LoadDataSource(datafile)
 presentation_id = medcalc.MakeSlices(accessField(0), MEDCALC.VIEW_MODE_REPLACE, 
                                      colorMap=MEDCALC.COLOR_MAP_BLUE_TO_RED_RAINBOW,
                                      sliceOrientation=MEDCALC.SLICE_NORMAL_TO_Z,  # normal to Z to see something!
-                                     nbSlices=3) 
+                                     nbSlices=2) 
+sleep(1)
+params = medcalc.GetSlicesParameters(presentation_id)
+params.nbSlices = 4
+medcalc.UpdateSlices(presentation_id, params)
 sleep(1)
 medcalc.RemovePresentation(presentation_id)
 sleep(1)