From dcad02bc210faf715a6e0dbcc38ed83fbf6097bc Mon Sep 17 00:00:00 2001 From: abn Date: Mon, 8 Aug 2016 10:41:06 +0200 Subject: [PATCH] [MEDCalc] : better slices + more error handling when invalid params --- src/MEDCalc/cmp/MEDPresentation.cxx | 2 +- src/MEDCalc/cmp/MEDPresentationContour.cxx | 16 +++- .../cmp/MEDPresentationDeflectionShape.cxx | 2 +- src/MEDCalc/cmp/MEDPresentationSlices.cxx | 86 +++++++++++++------ src/MEDCalc/cmp/MEDPresentationSlices.hxx | 6 +- .../cmp/MEDPresentationVectorField.cxx | 2 +- src/MEDCalc/gui/PresentationController.cxx | 2 + src/MEDCalc/test/tui/slices.py | 6 +- 8 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/MEDCalc/cmp/MEDPresentation.cxx b/src/MEDCalc/cmp/MEDPresentation.cxx index 92c22a904..e76ad7693 100644 --- a/src/MEDCalc/cmp/MEDPresentation.cxx +++ b/src/MEDCalc/cmp/MEDPresentation.cxx @@ -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; diff --git a/src/MEDCalc/cmp/MEDPresentationContour.cxx b/src/MEDCalc/cmp/MEDPresentationContour.cxx index d8b6b48d8..ee4628505 100644 --- a/src/MEDCalc/cmp/MEDPresentationContour.cxx +++ b/src/MEDCalc/cmp/MEDPresentationContour.cxx @@ -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(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 diff --git a/src/MEDCalc/cmp/MEDPresentationDeflectionShape.cxx b/src/MEDCalc/cmp/MEDPresentationDeflectionShape.cxx index 9fab15f78..fdb5d5c3a 100644 --- a/src/MEDCalc/cmp/MEDPresentationDeflectionShape.cxx +++ b/src/MEDCalc/cmp/MEDPresentationDeflectionShape.cxx @@ -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(""); } diff --git a/src/MEDCalc/cmp/MEDPresentationSlices.cxx b/src/MEDCalc/cmp/MEDPresentationSlices.cxx index 99c87041a..8654e25bc 100644 --- a/src/MEDCalc/cmp/MEDPresentationSlices.cxx +++ b/src/MEDCalc/cmp/MEDPresentationSlices.cxx @@ -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(); } } diff --git a/src/MEDCalc/cmp/MEDPresentationSlices.hxx b/src/MEDCalc/cmp/MEDPresentationSlices.hxx index 19afa196e..721ff61a7 100644 --- a/src/MEDCalc/cmp/MEDPresentationSlices.hxx +++ b/src/MEDCalc/cmp/MEDPresentationSlices.hxx @@ -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 diff --git a/src/MEDCalc/cmp/MEDPresentationVectorField.cxx b/src/MEDCalc/cmp/MEDPresentationVectorField.cxx index a05075ed1..92b857761 100644 --- a/src/MEDCalc/cmp/MEDPresentationVectorField.cxx +++ b/src/MEDCalc/cmp/MEDPresentationVectorField.cxx @@ -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(""); } diff --git a/src/MEDCalc/gui/PresentationController.cxx b/src/MEDCalc/gui/PresentationController.cxx index d3dcce702..3b8e9e219 100644 --- a/src/MEDCalc/gui/PresentationController.cxx +++ b/src/MEDCalc/gui/PresentationController.cxx @@ -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: diff --git a/src/MEDCalc/test/tui/slices.py b/src/MEDCalc/test/tui/slices.py index bbfcff31d..9a848710f 100644 --- a/src/MEDCalc/test/tui/slices.py +++ b/src/MEDCalc/test/tui/slices.py @@ -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) -- 2.39.2