Salome HOME
Copyright update 2021
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationContour.cxx
index fee4659e59a133cb89f096b5503117c35bfa34e8..f04f44f6b2f3de2a8d7910f27c35d3f3a1b98c78 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016  CEA/DEN, EDF R&D
+// Copyright (C) 2016-2021  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
@@ -36,15 +36,24 @@ MEDPresentationContour::MEDPresentationContour(const MEDCALC::ContourParameters&
   setIntProperty(MEDPresentationContour::PROP_NB_CONTOUR, params.nbContours);
 }
 
-std::string
-MEDPresentationContour::getContourCommand() const
+void
+MEDPresentationContour::initFieldMeshInfos()
 {
-  std::ostringstream oss1;
+  MEDPresentation::initFieldMeshInfos();
+  _colorByType = "POINTS";
+}
 
-  oss1 << "min_max = " << _srcObjVar << ".PointData.GetArray('" << _fieldName << "').GetRange();\n";
-  oss1 << "delta = (min_max[1]-min_max[0])/float(" << _params.nbContours << ");\n";
-  oss1 << _objVar << ".Isosurfaces = [min_max[0]+0.5*delta+i*delta for i in range(" << _params.nbContours << ")];\n";
-  return oss1.str();
+void
+MEDPresentationContour::setNumberContours()
+{
+  std::ostringstream oss;
+
+  oss << "min_max = " << _srcObjVar << ".PointData.GetArray('" << _fieldName << "').GetRange();";
+  pushAndExecPyLine(oss.str()); oss.str("");
+  oss << "delta = (min_max[1]-min_max[0])/float(" << _params.nbContours << ");";
+  pushAndExecPyLine(oss.str()); oss.str("");
+  oss << _objVar << ".Isosurfaces = [min_max[0]+0.5*delta+i*delta for i in range(" << _params.nbContours << ")];";
+  pushAndExecPyLine(oss.str()); oss.str("");
 }
 
 void
@@ -54,11 +63,8 @@ MEDPresentationContour::internalGeneratePipeline()
 
   MEDPyLockWrapper lock;
 
-  std::ostringstream oss_o, oss;
-  std::string view(getRenderViewVar());
-
-  oss << _srcObjVar << " = pvs.MEDReader(FileName='" << _fileName << "');";
-  pushAndExecPyLine(oss.str()); oss.str("");
+  createSource();
+  setTimestamp();
 
   // Populate internal array of available components:
   fillAvailableFieldComponents();
@@ -68,42 +74,39 @@ MEDPresentationContour::internalGeneratePipeline()
       STDLOG(msg);
       throw KERNEL::createSalomeException(msg);
     }
-
-  pushAndExecPyLine( getRenderViewCommand() ); // instanciate __viewXXX
-
-  if(_fieldType == "CELLS")
+  if (_params.nbContours < 1)
     {
-      // In case of a CELLS field need to convert to POINT field, and update source
-      STDLOG("Applying CellDatatoPointData filter");
-      std::ostringstream oss2, oss4;
-      // Apply Cell data to point data:
-      oss2 << "__obj" << GeneratePythonId();
-      oss << oss2.str() << " = pvs.CellDatatoPointData(Input=" << _srcObjVar << ");";
-      pushAndExecPyLine(oss.str()); oss.str("");
-      // Now the source becomes the result of the CellDatatoPointData:
-      _srcObjVar = oss2.str();
+      const char * mes = "Invalid number of contours!";
+      STDLOG(mes);
+      throw KERNEL::createSalomeException(mes);
     }
+
+  setOrCreateRenderView(); // instantiate __viewXXX, needs to be after the exception above otherwise previous elements in the view will be hidden.
+
+  // Contour needs point data:
+  applyCellToPointIfNeeded();
+
+  std::ostringstream oss;
   oss << _objVar << " = pvs.Contour(Input=" << _srcObjVar << ");";
   pushAndExecPyLine(oss.str()); oss.str("");
+
+  showObject();
+
   oss << _objVar << ".ContourBy = ['POINTS', '" << _fieldName << "'];";
   pushAndExecPyLine(oss.str()); oss.str("");
 
+  // Colorize contour
+  oss << _objVar << ".ComputeScalars = 1;";
+  pushAndExecPyLine(oss.str()); oss.str("");
+
   // Set number of contours
-  pushAndExecPyLine(getContourCommand());
+  setNumberContours();
 
-  oss << _dispVar << " = pvs.Show(" << _objVar << ", " << view << ");";
-  pushAndExecPyLine(oss.str()); oss.str("");
-  oss << "pvs.ColorBy(" << _dispVar << ", ('POINTS', '" << _fieldName << "'));";  // necessarily POINTS
-  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();");
+  colorBy();    // see initFieldInfo() - necessarily POINTS because of the conversion above
+  showScalarBar();
+  selectColorMap();
+  rescaleTransferFunction();
+  resetCameraAndRender();
 }
 
 void
@@ -118,7 +121,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
@@ -132,8 +143,7 @@ MEDPresentationContour::updateNbContours(const int nbContours)
   // Update the pipeline:
   {
     MEDPyLockWrapper lock;
-    std::string cmd = getContourCommand();
-    pushAndExecPyLine(cmd);
+    setNumberContours();
     pushAndExecPyLine("pvs.Render();");
   }
 }