Salome HOME
Implement view python function to be able to view a field's iteration from the python...
authorChristophe Bourcier <christophe.bourcier@cea.fr>
Wed, 18 Nov 2020 09:46:10 +0000 (10:46 +0100)
committerChristophe Bourcier <christophe.bourcier@cea.fr>
Wed, 18 Nov 2020 09:46:10 +0000 (10:46 +0100)
idl/MEDDataManager.idl
src/MEDCalc/cmp/MEDDataManager_i.cxx
src/MEDCalc/cmp/MEDDataManager_i.hxx
src/MEDCalc/cmp/MEDPresentation.cxx
src/MEDCalc/cmp/MEDPresentation.hxx
src/MEDCalc/cmp/MEDPresentationScalarMap.cxx
src/MEDCalc/gui/XmedConsoleDriver.cxx
src/MEDCalc/tui/medconsole.py

index 6f46be875efd0a88ba36ad223f9ae1753abdf8e1..715c4d9e770b217c1dc1c0abd23761b6e245b1f7 100644 (file)
@@ -154,6 +154,8 @@ module MEDCALC
 
     //void saveFields(in FieldHandlerList fieldHandlerList, in string filepath);
 
+    double getFieldTimeStep(in long fieldHandlerId);
+
     void changeUnderlyingMesh(in long fieldHandlerId, in long meshHandlerId)
       raises (SALOME::SALOME_Exception);
 
index 12910999ec6d92fa854cffc42c833effe3e85eef..75e082399553b3a53a9abf3196187ab8f29c9506 100644 (file)
@@ -588,6 +588,23 @@ long MEDDataManager_i::getUMeshId(const MEDCouplingMesh * mesh) {
   return LONG_UNDEFINED;
 }
 
+/**
+ * Get the timestep associated to a field
+ */
+double MEDDataManager_i::getFieldTimeStep(CORBA::Long fieldHandlerId)
+{
+  int iteration, order;
+  // WARN: note that the variables "iteration" and "order" are passed
+  // by reference to the function getTime (see documentation of
+  // MEDCouplingField). As a consequence, the values of these
+  // variables are updated by this function call. This is the means to
+  // retrieve the iteration and order of the field.
+  MEDCALC::FieldHandler * fieldHandler = getFieldHandler(fieldHandlerId);
+  MEDCouplingFieldDouble* fieldDouble = getFieldDouble(fieldHandler);
+  double timestamp = fieldDouble->getTime(iteration, order);
+  return timestamp;
+}
+
 /*!
  * This method returns the physical data of the specified field,
  * i.e. the MEDCoupling field associated to the specified field
index b68fc714dfd6f795832e2b4571b54e1933266f28..4b0a55c819db417be00c807677ef9be14bcbc646 100644 (file)
@@ -161,6 +161,8 @@ private:
   MEDCouplingUMesh * getUMesh(long meshHandlerId);
   long getUMeshId(const MEDCouplingMesh * mesh);
 
+  double getFieldTimeStep(CORBA::Long fieldHandlerId);
+
   INTERP_KERNEL::IntersectionType _getIntersectionType(const char* intersType);
   MEDCoupling::NatureOfField _getNatureOfField(const char* fieldNature);
 
index 5284d0387a2c8d81a8e2427d1ad84818346cf668..e4b1c01f13d48435fb544923e347205ee1582ac2 100644 (file)
@@ -95,7 +95,8 @@ MEDPresentation::initFieldMeshInfos()
   MEDCALC::MeshHandler* meshHandler = dataManager->getMeshHandler(fieldHandler->meshid);
   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
 
-  extractFileName(std::string(dataSHandler->uri));
+  // get the file name of the field (or its memory information)
+  extractFileName(std::string(fieldHandler->source));
 
   _fieldName = fieldHandler->fieldname;
   _mcFieldType = (MEDCoupling::TypeOfField) fieldHandler->type;
@@ -107,13 +108,23 @@ MEDPresentation::initFieldMeshInfos()
 void
 MEDPresentation::extractFileName(const std::string& name)
 {
+  STDLOG("MEDPresentation::extractFileName('" << name << "')");
   _fileName = name;
   if (_fileName.substr(0, 7) != std::string("file://")) {
-    const char* msg = "MEDPresentation(): Data source is not a file! Can not proceed.";
+    const char* msg = "MEDPresentation(): Data source is in memory! Saving it in tmp file.";
     STDLOG(msg);
-    throw MEDPresentationException(msg);
+    // export a med file with this field
+    // we could instead use CORBA to transfer the field to PARAVIS like in MEDCalculatorDBFieldReal::display()
+    _fileName = std::tmpnam(NULL);
+    MEDCALC::FieldIdList fieldIdList;
+    fieldIdList.length(1);
+    fieldIdList[0] = _handlerId;
+    MEDFactoryClient::getDataManager()->saveFields(_fileName.c_str(), fieldIdList);
   }
-  _fileName = _fileName.substr(7, _fileName.size());
+  else
+    // removing "file://"
+    _fileName = _fileName.substr(7, _fileName.size());
+  STDLOG("MEDPresentation::extractFileName _fileName=" << _fileName);
 }
 
 MEDPresentation::~MEDPresentation()
@@ -344,6 +355,25 @@ MEDPresentation::createSource()
     }
 }
 
+/*
+ * Set the timestep of the animation to the timestep of the field.
+ * Especially useful when working on a field's iteration:
+ * in the workspace, in the python console, or using changeUnderlyingMesh.
+ */
+void
+MEDPresentation::setTimestep()
+{
+  // get the timestep of the field
+  double timestep = MEDFactoryClient::getDataManager()->getFieldTimeStep(_handlerId);
+
+  std::ostringstream oss;
+
+  // go to the right timestep in animation (view and VCR toolbar)
+  pushAndExecPyLine("pvs.GetAnimationScene().UpdateAnimationUsingDataTimeSteps()");
+  oss << "pvs.GetAnimationScene().AnimationTime = " << timestep << ";";
+  pushAndExecPyLine(oss.str()); oss.str("");
+}
+
 void
 MEDPresentation::setOrCreateRenderView()
 {
index 5fa62c55e6f60d10c24c3b0240704338c9498eca..3a0870626ada806748d8c057785633b8d0e7a13f 100644 (file)
@@ -84,6 +84,7 @@ protected:
   // The most common elements of the ParaView pipeline:
   void setOrCreateRenderView();
   void createSource();
+  void setTimestep();
   void selectFieldComponent();
   void showObject();
   void colorBy();
index 0ebb60f82d780fe586050d410a0cd23b9d6be4be..654a5cdd519455ee81a027a7ece929aedd215bc0 100644 (file)
@@ -45,6 +45,7 @@ MEDPresentationScalarMap::internalGeneratePipeline()
 
   setOrCreateRenderView(); // instantiate __viewXXX
   createSource();
+  setTimestep();
 
   // Populate internal array of available components:
   fillAvailableFieldComponents();
index fd7a609e755617636bf08dc6746ec8434d155c0c..6bc23849e2ca1e7f604a6aaa31c030f9e3d15386 100644 (file)
@@ -61,7 +61,7 @@ void XmedConsoleDriver::setup() {
     commands += "";
     commands += "from medcalc.medconsole import saveWorkspace, cleanWorkspace";
     commands += "from medcalc.medconsole import putInWorkspace, removeFromWorkspace";
-    commands += "from medcalc.medconsole import accessField";
+    commands += "from medcalc.medconsole import accessField, view";
     commands += "from medcalc.medconsole import getEnvironment, ls, la";
     commands += "";
     this->exec(commands);
index 5f2bfbc4bf1ec50f178f31623e125e5ebd464960..7ae45231a35dedf9c663249b084f9265913002fe 100644 (file)
@@ -18,6 +18,7 @@
 #
 
 import medcalc
+import MEDCALC
 
 dataManager = medcalc.medcorba.factory.getDataManager()
 
@@ -159,3 +160,10 @@ def accessField(fieldHandlerId):
   """
   return medcalc.newFieldProxy(fieldHandlerId)
 #
+
+def view(field):
+  """
+  Display the field at its timestep
+  """
+  medcalc.MakeScalarMap(field, viewMode=MEDCALC.VIEW_MODE_REPLACE, scalarBarRange=MEDCALC.SCALAR_BAR_CURRENT_TIMESTEP)
+  pass