1 // Copyright (C) 2011-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Authors: A Bruneton (CEA), C Aguerre (EdF)
21 #include "MEDPyLockWrapper.hxx"
22 #include "MEDFactoryClient.hxx"
23 #include "MEDPresentation.hxx"
24 #include "MEDPresentationException.hxx"
25 #include "MEDCouplingRefCountObject.hxx"
26 #include <SALOME_KernelServices.hxx>
28 #include <Basics_Utils.hxx>
32 const std::string MEDPresentation::PROP_NAME = "name";
33 const std::string MEDPresentation::PROP_NB_COMPONENTS = "nbComponents";
34 const std::string MEDPresentation::PROP_SELECTED_COMPONENT = "selectedComponent";
35 const std::string MEDPresentation::PROP_COMPONENT = "component_";
36 const std::string MEDPresentation::PROP_COLOR_MAP = "colorMap";
37 const std::string MEDPresentation::PROP_SCALAR_BAR_RANGE = "scalarBarRange";
39 MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const std::string& name,
40 const MEDCALC::MEDPresentationViewMode viewMode,
41 const MEDCALC::MEDPresentationColorMap colorMap,
42 const MEDCALC::MEDPresentationScalarBarRange sbRange)
43 : _fieldHandlerId(fieldHandlerId), _propertiesStr(),
44 //_pipeline(0), _display(0)
45 _selectedComponentIndex(-1),
49 _renderViewPyId(-1), // will be set by getRenderViewCommand()
52 MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
53 MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
54 MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(fieldHandler->meshid);
55 MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
57 _fileName = dataSHandler->uri;
58 _fieldName = fieldHandler->fieldname;
59 _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
61 if (_fileName.substr(0, 7) != std::string("file://")) {
62 const char* msg = "MEDPresentation(): Data source is not a file! Can not proceed.";
64 throw MEDPresentationException(msg);
66 _fileName = _fileName.substr(7, _fileName.size());
68 setStringProperty(MEDPresentation::PROP_NAME, name);
70 setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, 0);
71 setIntProperty(MEDPresentation::PROP_SELECTED_COMPONENT, 0);
73 setIntProperty(MEDPresentation::PROP_COLOR_MAP, static_cast<int>(colorMap));
74 setIntProperty(MEDPresentation::PROP_SCALAR_BAR_RANGE, static_cast<int>(sbRange));
77 int id = GeneratePythonId();
78 std::ostringstream oss_o, oss_d, oss_l, oss_s;
79 oss_o << "__obj" << id;
80 oss_s << "__srcObj" << id;
81 oss_d << "__disp" << id;
82 oss_l << "__lut" << id;
83 _objVar = oss_o.str();
84 _srcObjVar = oss_s.str();
85 _dispVar = oss_d.str();
86 _lutVar = oss_l.str();
89 MEDPresentation::~MEDPresentation()
91 STDLOG("~MEDPresentation(): clear display");
93 MEDPyLockWrapper lock;
94 std::ostringstream oss_v, oss;
95 oss_v << "__view" << _renderViewPyId;
96 oss << "pvs.Hide(" << _objVar << ", view=" << oss_v.str() << ");";
97 oss << "pvs.Render();";
99 PyRun_SimpleString(oss.str().c_str());
104 MEDPresentation::generatePipeline()
106 // Might be more complicated in the future:
108 this->internalGeneratePipeline();
112 //MEDPresentation::pushPyObjects(PyObjectId obj, PyObjectId disp)
114 // _pipeline.push_back(obj);
115 // _display.push_back(disp);
119 MEDPresentation::pushAndExecPyLine(const std::string & lin)
122 _pythonCmds.push_back(lin);
126 MEDPresentation::execPyLine(const std::string & lin)
128 MEDPyLockWrapper lock;
129 STDLOG("@@@@ MEDPresentation::execPyLine() about to exec >> " << lin);
130 if(PyRun_SimpleString(lin.c_str()))
132 std::ostringstream oss;
133 oss << "MEDPresentation::execPyLine(): following Python command failed!\n";
136 throw KERNEL::createSalomeException(oss.str().c_str());
141 MEDPresentation::setStringProperty(const std::string& propName, const std::string& propValue)
143 _propertiesStr[propName] = propValue;
147 MEDPresentation::getStringProperty(const std::string& propName) const
149 std::map<std::string, std::string>::const_iterator it = _propertiesStr.find(propName);
150 if (it != _propertiesStr.end()) {
154 STDLOG("MEDPresentation::getStringProperty(): no property named " + propName);
155 throw MEDPresentationException("MEDPresentation::getStringProperty(): no property named " + propName);
160 MEDPresentation::setIntProperty(const std::string& propName, const int propValue)
162 _propertiesInt[propName] = propValue;
166 MEDPresentation::getIntProperty(const std::string& propName) const
168 std::map<std::string, int>::const_iterator it = _propertiesInt.find(propName);
169 if (it != _propertiesInt.end()) {
173 STDLOG("MEDPresentation::getIntProperty(): no property named " + propName);
174 throw MEDPresentationException("MEDPresentation::getIntProperty(): no property named " + propName);
179 MEDPresentation::dumpIntProperties() const
181 std::map<std::string, int>::const_iterator it = _propertiesInt.begin();
182 STDLOG("@@@ Dumping INT properties");
183 for(; it != _propertiesInt.end(); ++it)
185 std::ostringstream oss;
186 oss << (*it).first << " -> " << (*it).second;
192 MEDPresentation::dumpStringProperties() const
194 std::map<std::string, std::string>::const_iterator it = _propertiesStr.begin();
195 STDLOG("@@@ Dumping STR properties");
196 for(; it != _propertiesStr.end(); ++it)
198 std::ostringstream oss;
199 oss << (*it).first << " -> " << (*it).second;
205 MEDPresentation::internalGeneratePipeline()
207 MEDPyLockWrapper lock;
208 pushAndExecPyLine( "import pvsimple as pvs;");
213 MEDPresentation::getPythonObjectFromMain(const char* python_var) const
217 // All the calls below returns *borrowed* references
218 PyObject* main_module = PyImport_AddModule((char*)"__main__");
219 _globalDict = PyModule_GetDict(main_module);
221 return PyDict_GetItemString(_globalDict, python_var);
225 MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
229 case MEDCoupling::ON_CELLS:
231 case MEDCoupling::ON_NODES:
234 STDLOG("MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?");
240 MEDPresentation::getRenderViewVar() const
242 std::ostringstream oss;
243 oss << "__view" << _renderViewPyId;
248 MEDPresentation::getRenderViewCommand() const
250 std::ostringstream oss, oss2;
252 std::string view(getRenderViewVar());
253 oss2 << "pvs._DisableFirstRenderCameraReset();" << std::endl;
254 if (_viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
255 // this might potentially re-assign to an existing view variable, but this is OK, we
256 // normally reassign exaclty the same RenderView object.
257 oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl;
258 } else if (_viewMode == MEDCALC::VIEW_MODE_REPLACE) {
260 oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl;
261 oss2 << "pvs.active_objects.source and pvs.Hide(view=" << view << ");" << std::endl;
262 oss2 << "pvs.Render();" << std::endl;
263 } else if (_viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
264 oss2 << "__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');" << std::endl;
265 oss2 << view << " = pvs.CreateView('RenderView');" << std::endl;
266 } else if (_viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
267 oss2 << view << " = pvs.CreateView('RenderView');" << std::endl;
273 MEDPresentation::getResetCameraCommand() const
275 return getRenderViewVar() + ".ResetCamera();";
279 MEDPresentation::getComponentSelectionCommand() const
281 std::ostringstream oss, oss_l;
284 if (_selectedComponentIndex != -1)
286 oss << _lutVar << ".VectorMode = 'Component';\n";
287 oss << _lutVar << ".VectorComponent = " << _selectedComponentIndex << ";";
289 else // Euclidean norm
291 oss << _lutVar << ".VectorMode = 'Magnitude';";
297 MEDPresentation::getColorMapCommand() const
301 case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW:
302 ret = _lutVar + ".ApplyPreset('Blue to Red Rainbow',True);";
304 case MEDCALC::COLOR_MAP_COOL_TO_WARM:
305 ret = _lutVar + ".ApplyPreset('Cool to Warm',True);";
308 STDLOG("MEDPresentation::getColorMapCommand(): invalid colormap!");
309 throw KERNEL::createSalomeException("MEDPresentation::getColorMapCommand(): invalid colormap!");
314 MEDPresentation::getRescaleCommand() const
319 case MEDCALC::SCALAR_BAR_ALL_TIMESTEPS:
320 ret = _dispVar + ".RescaleTransferFunctionToDataRangeOverTime();";
322 case MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP:
323 ret = _dispVar + ".RescaleTransferFunctionToDataRange(False);";
326 STDLOG("MEDPresentation::getRescaleCommand(): invalid range!");
327 throw KERNEL::createSalomeException("MEDPresentation::getRescaleCommand(): invalid range!");
333 MEDPresentation::GeneratePythonId()
335 static int INIT_ID = 0;
340 MEDPresentation::activateView()
342 MEDPyLockWrapper lock;
343 pushAndExecPyLine("pvs.SetActiveView(" + getRenderViewVar() + ");");
348 MEDPresentation::paravisDump() const
352 for (vector<string>::const_iterator it=_pythonCmds.begin(); it != _pythonCmds.end(); ++it)
361 * Query all available component names for the field associated with this presentation.
362 * Fills in all the corresponding string properties:
366 * and the number of components.
369 MEDPresentation::fillAvailableFieldComponents()
371 MEDPyLockWrapper lock; // GIL!
374 if(_fieldType == "CELLS") {
377 else if (_fieldType == "POINTS") {
381 std::string msg("Unsupported spatial discretisation: " + _fieldType);
383 throw KERNEL::createSalomeException(msg.c_str());
386 std::ostringstream oss;
387 oss << "__nbCompo = " << _srcObjVar << "." << typ << ".GetArray('" << _fieldName << "').GetNumberOfComponents();";
388 execPyLine(oss.str());
389 PyObject* p_obj = getPythonObjectFromMain("__nbCompo");
391 if (p_obj && PyInt_Check(p_obj))
392 nbCompo = PyInt_AS_LONG(p_obj);
395 STDLOG("Unexpected Python error");
396 throw KERNEL::createSalomeException("Unexpected Python error");
398 setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, nbCompo);
399 for (long i = 0; i<nbCompo; i++)
401 std::ostringstream oss2;
402 oss2 << "__compo = " << _srcObjVar << "." << typ << ".GetArray('" << _fieldName << "').GetComponentName(" << i << ");";
403 execPyLine(oss2.str());
404 PyObject* p_obj = getPythonObjectFromMain("__compo");
406 if (p_obj && PyString_Check(p_obj))
407 compo = std::string(PyString_AsString(p_obj)); // pointing to internal Python memory, so make a copy!!
410 STDLOG("Unexpected Python error");
411 throw KERNEL::createSalomeException("Unexpected Python error");
413 std::ostringstream oss_p;
414 oss_p << MEDPresentation::PROP_COMPONENT << i;
415 setStringProperty(oss_p.str(), compo);