//
// Authors: A Bruneton (CEA), C Aguerre (EdF)
+#include "MEDPyLockWrapper.hxx"
#include "MEDFactoryClient.hxx"
#include "MEDPresentation.hxx"
#include "MEDPresentationException.hxx"
#include "MEDCouplingRefCountObject.hxx"
-#include <iostream>
+#include <SALOME_KernelServices.hxx>
+#undef LOG
+#include <Basics_Utils.hxx>
-MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, std::string name)
- : _fieldHandlerId(fieldHandlerId), _pipeline(0), _display(0), _properties()
+#include <sstream>
+
+const std::string MEDPresentation::PROP_NAME = "name";
+const std::string MEDPresentation::PROP_NB_COMPONENTS = "nbComponents";
+const std::string MEDPresentation::PROP_SELECTED_COMPONENT = "selectedComponent";
+const std::string MEDPresentation::PROP_COMPONENT = "component_";
+const std::string MEDPresentation::PROP_COLOR_MAP = "colorMap";
+const std::string MEDPresentation::PROP_SCALAR_BAR_RANGE = "scalarBarRange";
+
+MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const std::string& name,
+ const MEDCALC::MEDPresentationViewMode viewMode,
+ const MEDCALC::MEDPresentationColorMap colorMap,
+ const MEDCALC::MEDPresentationScalarBarRange sbRange)
+ : _fieldHandlerId(fieldHandlerId), _propertiesStr(),
+ //_pipeline(0), _display(0)
+ _selectedComponentIndex(-1),
+ _viewMode(viewMode),
+ _colorMap(colorMap),
+ _sbRange(sbRange),
+ _renderViewPyId(-1), // will be set by getRenderViewCommand()
+ _globalDict(0)
{
MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
_fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
if (_fileName.substr(0, 7) != std::string("file://")) {
- const char* msg = "Data source is not a file! Can not proceed.";
+ const char* msg = "MEDPresentation(): Data source is not a file! Can not proceed.";
+ STDLOG(msg);
throw MEDPresentationException(msg);
}
_fileName = _fileName.substr(7, _fileName.size());
- setProperty("name", name);
+ setStringProperty(MEDPresentation::PROP_NAME, name);
+
+ setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, 0);
+ setIntProperty(MEDPresentation::PROP_SELECTED_COMPONENT, 0);
+
+ setIntProperty(MEDPresentation::PROP_COLOR_MAP, static_cast<int>(colorMap));
+ setIntProperty(MEDPresentation::PROP_SCALAR_BAR_RANGE, static_cast<int>(sbRange));
+
+ // Python variables:
+ int id = GeneratePythonId();
+ std::ostringstream oss_o, oss_d, oss_l, oss_s;
+ oss_o << "__obj" << id;
+ oss_s << "__srcObj" << id;
+ oss_d << "__disp" << id;
+ oss_l << "__lut" << id;
+ _objVar = oss_o.str();
+ _srcObjVar = oss_s.str();
+ _dispVar = oss_d.str();
+ _lutVar = oss_l.str();
+}
+
+MEDPresentation::~MEDPresentation()
+{
+ STDLOG("~MEDPresentation(): clear display");
+ {
+ MEDPyLockWrapper lock;
+ std::ostringstream oss_v, oss;
+ oss_v << "__view" << _renderViewPyId;
+ oss << "pvs.Hide(" << _objVar << ", view=" << oss_v.str() << ");";
+ oss << "pvs.Render();";
+
+ PyRun_SimpleString(oss.str().c_str());
+ }
}
-void MEDPresentation::generatePipeline()
+void
+MEDPresentation::generatePipeline()
{
// Might be more complicated in the future:
this->internalGeneratePipeline();
}
-void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
+//void
+//MEDPresentation::pushPyObjects(PyObjectId obj, PyObjectId disp)
+//{
+// _pipeline.push_back(obj);
+// _display.push_back(disp);
+//}
+
+void
+MEDPresentation::pushAndExecPyLine(const std::string & lin)
{
- _pipeline.push_back(obj);
- _display.push_back(disp);
+ execPyLine(lin);
+ _pythonCmds.push_back(lin);
}
-void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
+void
+MEDPresentation::execPyLine(const std::string & lin)
{
- // LIMITED!!! For now switch the first display element to Wireframe
- /*
- PyLockWrapper lock;
- PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
- */
+ MEDPyLockWrapper lock;
+ STDLOG("@@@@ MEDPresentation::execPyLine() about to exec >> " << lin);
+ if(PyRun_SimpleString(lin.c_str()))
+ {
+ std::ostringstream oss;
+ oss << "MEDPresentation::execPyLine(): following Python command failed!\n";
+ oss << ">> " << lin;
+ STDLOG(oss.str());
+ throw KERNEL::createSalomeException(oss.str().c_str());
+ }
+}
- _properties[propName] = propValue;
+void
+MEDPresentation::setStringProperty(const std::string& propName, const std::string& propValue)
+{
+ _propertiesStr[propName] = propValue;
}
const std::string
-MEDPresentation::getProperty(const std::string& propName)
+MEDPresentation::getStringProperty(const std::string& propName) const
{
- if (_properties.find(propName) != _properties.end()) {
- return _properties[propName];
+ std::map<std::string, std::string>::const_iterator it = _propertiesStr.find(propName);
+ if (it != _propertiesStr.end()) {
+ return (*it).second;
}
else {
- std::cerr << "getProperty(): no property named " << propName << std::endl;
- return std::string();
+ STDLOG("MEDPresentation::getStringProperty(): no property named " + propName);
+ throw MEDPresentationException("MEDPresentation::getStringProperty(): no property named " + propName);
}
}
-PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
+void
+MEDPresentation::setIntProperty(const std::string& propName, const int propValue)
{
- // TODO: improve to avoid getting dict at each call
+ _propertiesInt[propName] = propValue;
+}
- // All the calls below returns *borrowed* references
- PyObject* main_module = PyImport_AddModule((char*)"__main__");
- PyObject* global_dict = PyModule_GetDict(main_module);
- return PyDict_GetItemString(global_dict, python_var);
+int
+MEDPresentation::getIntProperty(const std::string& propName) const
+{
+ std::map<std::string, int>::const_iterator it = _propertiesInt.find(propName);
+ if (it != _propertiesInt.end()) {
+ return (*it).second;
+ }
+ else {
+ STDLOG("MEDPresentation::getIntProperty(): no property named " + propName);
+ throw MEDPresentationException("MEDPresentation::getIntProperty(): no property named " + propName);
+ }
}
-std::string MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType)
+ void
+ MEDPresentation::dumpIntProperties() const
+ {
+ std::map<std::string, int>::const_iterator it = _propertiesInt.begin();
+ STDLOG("@@@ Dumping INT properties");
+ for(; it != _propertiesInt.end(); ++it)
+ {
+ std::ostringstream oss;
+ oss << (*it).first << " -> " << (*it).second;
+ STDLOG(oss.str());
+ }
+ }
+
+ void
+ MEDPresentation::dumpStringProperties() const
+ {
+ std::map<std::string, std::string>::const_iterator it = _propertiesStr.begin();
+ STDLOG("@@@ Dumping STR properties");
+ for(; it != _propertiesStr.end(); ++it)
+ {
+ std::ostringstream oss;
+ oss << (*it).first << " -> " << (*it).second;
+ STDLOG(oss.str());
+ }
+ }
+
+ void
+ MEDPresentation::internalGeneratePipeline()
+ {
+ MEDPyLockWrapper lock;
+ pushAndExecPyLine( "import pvsimple as pvs;");
+ }
+
+
+PyObject*
+MEDPresentation::getPythonObjectFromMain(const char* python_var) const
+{
+ if (! _globalDict)
+ {
+ // All the calls below returns *borrowed* references
+ PyObject* main_module = PyImport_AddModule((char*)"__main__");
+ _globalDict = PyModule_GetDict(main_module);
+ }
+ return PyDict_GetItemString(_globalDict, python_var);
+}
+
+std::string
+MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
{
switch(fieldType)
{
case MEDCoupling::ON_NODES:
return "POINTS";
default:
- std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
+ STDLOG("MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?");
return "";
}
}
std::string
-MEDPresentation::getRenderViewCommand(MEDCALC::MEDPresentationViewMode viewMode)
-{
- std::string cmd = std::string("pvs._DisableFirstRenderCameraReset();");
- if (viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
- cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
- } else if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
- cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
- cmd += std::string("pvs.active_objects.source and pvs.Hide(view=__view1);");
- cmd += std::string("pvs.Render();");
- } else if (viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
- cmd += std::string("__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');");
- cmd += std::string("__view1 = pvs.CreateView('RenderView');");
- } else if (viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
- cmd += std::string("__view1 = pvs.CreateView('RenderView');");
+MEDPresentation::getRenderViewVar() const
+{
+ std::ostringstream oss;
+ oss << "__view" << _renderViewPyId;
+ return oss.str();
+}
+
+std::string
+MEDPresentation::getRenderViewCommand() const
+{
+ std::ostringstream oss, oss2;
+
+ std::string view(getRenderViewVar());
+ oss2 << "pvs._DisableFirstRenderCameraReset();" << std::endl;
+ if (_viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
+ // this might potentially re-assign to an existing view variable, but this is OK, we
+ // normally reassign exaclty the same RenderView object.
+ oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl;
+ } else if (_viewMode == MEDCALC::VIEW_MODE_REPLACE) {
+ // same as above
+ oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl;
+ oss2 << "pvs.active_objects.source and pvs.Hide(view=" << view << ");" << std::endl;
+ oss2 << "pvs.Render();" << std::endl;
+ } else if (_viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
+ oss2 << "__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');" << std::endl;
+ oss2 << view << " = pvs.CreateView('RenderView');" << std::endl;
+ } else if (_viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
+ oss2 << view << " = pvs.CreateView('RenderView');" << std::endl;
}
- cmd += std::string("__view1.ResetCamera();");
- return cmd;
+ return oss2.str();
}
std::string
-MEDPresentation::getColorMapCommand(MEDCALC::MEDPresentationColorMap colorMap)
+MEDPresentation::getResetCameraCommand() const
{
- switch(colorMap) {
- case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "Blue to Red Rainbow";
- case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "Cool to Warm";
+ return getRenderViewVar() + ".ResetCamera();";
+}
+
+std::string
+MEDPresentation::getComponentSelectionCommand() const
+{
+ std::ostringstream oss, oss_l;
+ std::string ret;
+
+ if (_selectedComponentIndex != -1)
+ {
+ oss << _lutVar << ".VectorMode = 'Component';\n";
+ oss << _lutVar << ".VectorComponent = " << _selectedComponentIndex << ";";
+ }
+ else // Euclidean norm
+ {
+ oss << _lutVar << ".VectorMode = 'Magnitude';";
+ }
+ return oss.str();
+}
+
+std::string
+MEDPresentation::getColorMapCommand() const
+{
+ std::string ret;
+ switch (_colorMap) {
+ case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW:
+ ret = _lutVar + ".ApplyPreset('Blue to Red Rainbow',True);";
+ break;
+ case MEDCALC::COLOR_MAP_COOL_TO_WARM:
+ ret = _lutVar + ".ApplyPreset('Cool to Warm',True);";
+ break;
+ default:
+ STDLOG("MEDPresentation::getColorMapCommand(): invalid colormap!");
+ throw KERNEL::createSalomeException("MEDPresentation::getColorMapCommand(): invalid colormap!");
+ }
+ return ret;
+}
+std::string
+MEDPresentation::getRescaleCommand() const
+{
+ std::string ret;
+ switch(_sbRange)
+ {
+ case MEDCALC::SCALAR_BAR_ALL_TIMESTEPS:
+ ret = _dispVar + ".RescaleTransferFunctionToDataRangeOverTime();";
+ break;
+ case MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP:
+ ret = _dispVar + ".RescaleTransferFunctionToDataRange(False);";
+ break;
+ default:
+ STDLOG("MEDPresentation::getRescaleCommand(): invalid range!");
+ throw KERNEL::createSalomeException("MEDPresentation::getRescaleCommand(): invalid range!");
+ }
+ return ret;
+}
+
+int
+MEDPresentation::GeneratePythonId()
+{
+ static int INIT_ID = 0;
+ return INIT_ID++;
+}
+
+void
+MEDPresentation::activateView()
+{
+ MEDPyLockWrapper lock;
+ pushAndExecPyLine("pvs.SetActiveView(" + getRenderViewVar() + ");");
+}
+
+
+std::string
+MEDPresentation::paravisDump() const
+{
+ using namespace std;
+ ostringstream oss;
+ for (vector<string>::const_iterator it=_pythonCmds.begin(); it != _pythonCmds.end(); ++it)
+ {
+ oss << (*it);
+ oss << "\n";
+ }
+ return oss.str();
+}
+
+/**
+ * Query all available component names for the field associated with this presentation.
+ * Fills in all the corresponding string properties:
+ * - PROP_COMPONENT1
+ * - PROP_COMPONENT2
+ * etc...
+ * and the number of components.
+ */
+void
+MEDPresentation::fillAvailableFieldComponents()
+{
+ MEDPyLockWrapper lock; // GIL!
+ std::string typ;
+
+ if(_fieldType == "CELLS") {
+ typ = "CellData";
+ }
+ else if (_fieldType == "POINTS") {
+ typ = "PointData";
}
+ else {
+ std::string msg("Unsupported spatial discretisation: " + _fieldType);
+ STDLOG(msg);
+ throw KERNEL::createSalomeException(msg.c_str());
+ }
+
+ std::ostringstream oss;
+ oss << "__nbCompo = " << _srcObjVar << "." << typ << ".GetArray('" << _fieldName << "').GetNumberOfComponents();";
+ execPyLine(oss.str());
+ PyObject* p_obj = getPythonObjectFromMain("__nbCompo");
+ long nbCompo;
+ if (p_obj && PyInt_Check(p_obj))
+ nbCompo = PyInt_AS_LONG(p_obj);
+ else
+ {
+ STDLOG("Unexpected Python error");
+ throw KERNEL::createSalomeException("Unexpected Python error");
+ }
+ setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, nbCompo);
+ for (long i = 0; i<nbCompo; i++)
+ {
+ std::ostringstream oss2;
+ oss2 << "__compo = " << _srcObjVar << "." << typ << ".GetArray('" << _fieldName << "').GetComponentName(" << i << ");";
+ execPyLine(oss2.str());
+ PyObject* p_obj = getPythonObjectFromMain("__compo");
+ std::string compo;
+ if (p_obj && PyString_Check(p_obj))
+ compo = std::string(PyString_AsString(p_obj)); // pointing to internal Python memory, so make a copy!!
+ else
+ {
+ STDLOG("Unexpected Python error");
+ throw KERNEL::createSalomeException("Unexpected Python error");
+ }
+ std::ostringstream oss_p;
+ oss_p << MEDPresentation::PROP_COMPONENT << i;
+ setStringProperty(oss_p.str(), compo);
+ }
}
+