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 "MEDFactoryClient.hxx"
22 #include "MEDPresentation.hxx"
23 #include "MEDPresentationException.hxx"
24 #include "MEDCouplingRefCountObject.hxx"
25 #include <SALOME_KernelServices.hxx>
27 #include <Basics_Utils.hxx>
28 #include <PyInterp_Utils.h>
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 : _fieldHandlerId(fieldHandlerId), _propertiesStr(),
43 //_pipeline(0), _display(0)
44 _selectedComponentIndex(-1),
47 _renderViewPyId(GeneratePythonId()),
48 _objId(_renderViewPyId), _dispId(_renderViewPyId), _lutId(_renderViewPyId),
51 MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
52 MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
53 MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(fieldHandler->meshid);
54 MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
56 _fileName = dataSHandler->uri;
57 _fieldName = fieldHandler->fieldname;
58 _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
60 if (_fileName.substr(0, 7) != std::string("file://")) {
61 const char* msg = "Data source is not a file! Can not proceed.";
62 throw MEDPresentationException(msg);
64 _fileName = _fileName.substr(7, _fileName.size());
66 setStringProperty(MEDPresentation::PROP_NAME, name);
68 setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, 0);
69 setIntProperty(MEDPresentation::PROP_SELECTED_COMPONENT, 0);
71 setIntProperty(MEDPresentation::PROP_COLOR_MAP, static_cast<int>(MEDCALC::COLOR_MAP_DEFAULT));
72 setIntProperty(MEDPresentation::PROP_SCALAR_BAR_RANGE, static_cast<int>(MEDCALC::SCALAR_BAR_RANGE_DEFAULT));
75 MEDPresentation::~MEDPresentation()
77 STDLOG("~MEDPresentation(): clear display");
80 std::ostringstream oss_o, oss_v, oss;
81 oss_o << "__obj" << _objId;
82 oss_v << "__view" << _renderViewPyId;
83 oss << "pvs.Hide(" << oss_o.str() << ", view=" << oss_v.str() << ");";
84 oss << "pvs.Render();";
86 PyRun_SimpleString(oss.str().c_str());
91 MEDPresentation::generatePipeline()
93 // Might be more complicated in the future:
95 this->internalGeneratePipeline();
99 //MEDPresentation::pushPyObjects(PyObjectId obj, PyObjectId disp)
101 // _pipeline.push_back(obj);
102 // _display.push_back(disp);
106 MEDPresentation::pushAndExecPyLine(const std::string & lin)
109 // std::cerr << lin << std::endl;
110 if(PyRun_SimpleString(lin.c_str()))
112 std::ostringstream oss;
113 oss << "MEDPresentation::pushAndExecPyLine(): following Python command failed!\n";
116 throw KERNEL::createSalomeException(oss.str().c_str());
118 _pythonCmds.push_back(lin);
122 MEDPresentation::setStringProperty(const std::string& propName, const std::string& propValue)
124 _propertiesStr[propName] = propValue;
128 MEDPresentation::getStringProperty(const std::string& propName) const
130 std::map<std::string, std::string>::const_iterator it = _propertiesStr.find(propName);
131 if (it != _propertiesStr.end()) {
135 throw MEDPresentationException("MEDPresentation::getStringProperty(): no property named " + propName);
140 MEDPresentation::setIntProperty(const std::string& propName, const int propValue)
142 _propertiesInt[propName] = propValue;
146 MEDPresentation::getIntProperty(const std::string& propName) const
148 std::map<std::string, int>::const_iterator it = _propertiesInt.find(propName);
149 if (it != _propertiesInt.end()) {
153 throw MEDPresentationException("MEDPresentation::getIntProperty(): no property named " + propName);
158 MEDPresentation::dumpIntProperties() const
160 std::map<std::string, int>::const_iterator it = _propertiesInt.begin();
161 STDLOG("@@@ Dumping INT properties");
162 for(; it != _propertiesInt.end(); ++it)
164 std::ostringstream oss;
165 oss << (*it).first << " -> " << (*it).second;
171 MEDPresentation::dumpStringProperties() const
173 std::map<std::string, std::string>::const_iterator it = _propertiesStr.begin();
174 STDLOG("@@@ Dumping STR properties");
175 for(; it != _propertiesStr.end(); ++it)
177 std::ostringstream oss;
178 oss << (*it).first << " -> " << (*it).second;
185 MEDPresentation::getPythonObjectFromMain(const char* python_var) const
189 // All the calls below returns *borrowed* references
190 PyObject* main_module = PyImport_AddModule((char*)"__main__");
191 _globalDict = PyModule_GetDict(main_module);
193 return PyDict_GetItemString(_globalDict, python_var);
197 MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
201 case MEDCoupling::ON_CELLS:
203 case MEDCoupling::ON_NODES:
206 STDLOG("MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?");
212 MEDPresentation::getRenderViewCommand() const
214 std::ostringstream oss, oss2;
215 oss << "__view" << _renderViewPyId;
216 std::string view(oss.str());
217 oss2 << "pvs._DisableFirstRenderCameraReset();" << std::endl;
218 if (_viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
219 oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl;
220 } else if (_viewMode == MEDCALC::VIEW_MODE_REPLACE) {
221 oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl;
222 oss2 << "pvs.active_objects.source and pvs.Hide(view=" << view << ");" << std::endl;
223 oss2 << "pvs.Render();" << std::endl;
224 } else if (_viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
225 oss2 << "__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');" << std::endl;
226 oss2 << view << " = pvs.CreateView('RenderView');" << std::endl;
227 } else if (_viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
228 oss2 << view << " = pvs.CreateView('RenderView');" << std::endl;
234 MEDPresentation::getResetCameraCommand() const
236 std::ostringstream oss;
237 oss << "__view" << _renderViewPyId << ".ResetCamera();";
242 MEDPresentation::getComponentSelectionCommand() const
244 std::ostringstream oss, oss_l;
245 oss_l << "__lut" << _lutId; std::string lut(oss_l.str());
247 if (_selectedComponentIndex != -1)
249 oss << lut << ".VectorMode = 'Component';\n";
250 oss << lut << ".VectorComponent = " << _selectedComponentIndex << ";";
252 else // Euclidean norm
254 oss << lut << ".VectorMode = 'Magnitude';";
260 MEDPresentation::getColorMapCommand() const
262 std::ostringstream oss, oss_l;
263 oss_l << "__lut" << _lutId; std::string lut(oss_l.str());
265 case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW:
266 oss << lut << ".ApplyPreset('Blue to Red Rainbow',True);";
268 case MEDCALC::COLOR_MAP_COOL_TO_WARM:
269 oss << lut << ".ApplyPreset('Cool to Warm',True);";
272 throw KERNEL::createSalomeException("MEDPresentation::getColorMapCommand(): invalid colormap!");
277 MEDPresentation::getRescaleCommand() const
279 std::ostringstream oss, oss_d;
280 oss_d << "__disp" << _dispId; std::string disp(oss_d.str());
283 case MEDCALC::SCALAR_BAR_ALL_TIMESTEPS:
284 oss << disp << ".RescaleTransferFunctionToDataRangeOverTime();";
286 case MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP:
287 oss << disp << ".RescaleTransferFunctionToDataRange(False);";
290 throw KERNEL::createSalomeException("MEDPresentation::getRescaleCommand(): invalid range!");
296 MEDPresentation::GeneratePythonId()
298 static int INIT_ID = 0;
303 MEDPresentation::activateView()
307 std::ostringstream oss;
308 oss << "pvs.SetActiveView(__view" << _renderViewPyId << ");";
309 pushAndExecPyLine(oss.str());
314 MEDPresentation::paravisDump() const
318 for (vector<string>::const_iterator it=_pythonCmds.begin(); it != _pythonCmds.end(); ++it)
327 * Query all available component names for the field associated with this presentation.
328 * Fills in all the corresponding string properties:
332 * and the number of components.
335 MEDPresentation::fillAvailableFieldComponents()
337 PyLockWrapper lock; // GIL!
339 std::ostringstream oss_o;
340 oss_o << "__obj" << _objId; std::string obj(oss_o.str());
343 if(_fieldType == "CELLS") {
346 else if (_fieldType == "POINTS") {
350 std::string msg("Unsupported spatial discretisation: " + _fieldType);
351 throw KERNEL::createSalomeException(msg.c_str());
354 std::ostringstream oss;
355 oss << "__nbCompo = " << obj << "." << typ << ".GetArray('" << _fieldName << "').GetNumberOfComponents();";
356 PyRun_SimpleString(oss.str().c_str());
357 PyObject* p_obj = getPythonObjectFromMain("__nbCompo");
359 if (p_obj && PyInt_Check(p_obj))
360 nbCompo = PyInt_AS_LONG(p_obj);
362 throw KERNEL::createSalomeException("Unexpected Python error");
363 setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, nbCompo);
364 for (long i = 0; i<nbCompo; i++)
366 std::ostringstream oss2;
367 oss2 << "__compo = " << obj << "." << typ << ".GetArray('" << _fieldName << "').GetComponentName(" << i << ");";
368 PyRun_SimpleString(oss2.str().c_str());
369 PyObject* p_obj = getPythonObjectFromMain("__compo");
371 if (p_obj && PyString_Check(p_obj))
372 compo = std::string(PyString_AsString(p_obj)); // pointing to internal Python memory, so make a copy!!
374 throw KERNEL::createSalomeException("Unexpected Python error");
375 std::ostringstream oss_p;
376 oss_p << MEDPresentation::PROP_COMPONENT << i;
377 setStringProperty(oss_p.str(), compo);