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 "Basics_Utils.hxx"
26 #include "PyInterp_Utils.h"
30 MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const std::string& name)
31 : _fieldHandlerId(fieldHandlerId), _pipeline(0), _display(0), _properties(),
32 _renderViewPyId(GeneratePythonId())
34 MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
35 MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
36 MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(fieldHandler->meshid);
37 MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
39 _fileName = dataSHandler->uri;
40 _fieldName = fieldHandler->fieldname;
41 _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
43 if (_fileName.substr(0, 7) != std::string("file://")) {
44 const char* msg = "Data source is not a file! Can not proceed.";
45 throw MEDPresentationException(msg);
47 _fileName = _fileName.substr(7, _fileName.size());
49 setProperty("name", name);
52 MEDPresentation::~MEDPresentation()
54 STDLOG("~MEDPresentation(): clear display");
57 std::ostringstream oss_o, oss_v, oss;
58 // Get top level object and display:
59 oss_o << "__obj" << _pipeline.front().first;
60 oss_v << "__view" << _renderViewPyId;
61 oss << "pvs.Hide(" << oss_o.str() << ", view=" << oss_v.str() << ");";
62 oss << "pvs.Render();";
64 // std::cerr << oss.str() << std::endl;
65 PyRun_SimpleString(oss.str().c_str());
70 MEDPresentation::generatePipeline()
72 // Might be more complicated in the future:
74 this->internalGeneratePipeline();
78 MEDPresentation::pushPyObjects(PyObjectId obj, PyObjectId disp)
80 _pipeline.push_back(obj);
81 _display.push_back(disp);
85 MEDPresentation::pushAndExecPyLine(const std::string & lin)
87 // TODO: store internally for low level dump
89 // std::cerr << lin << std::endl;
90 PyRun_SimpleString(lin.c_str());
94 MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
96 // LIMITED!!! For now switch the first display element to Wireframe
99 PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
102 _properties[propName] = propValue;
106 MEDPresentation::getProperty(const std::string& propName) const
108 std::map<std::string, std::string>::const_iterator it = _properties.find(propName);
109 if (it != _properties.end()) {
113 STDLOG("MEDPresentation::getProperty(): no property named " + propName);
114 return std::string();
119 MEDPresentation::getPythonObjectFromMain(const char* python_var) const
121 // TODO: improve to avoid getting dict at each call
123 // All the calls below returns *borrowed* references
124 PyObject* main_module = PyImport_AddModule((char*)"__main__");
125 PyObject* global_dict = PyModule_GetDict(main_module);
126 return PyDict_GetItemString(global_dict, python_var);
130 MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
134 case MEDCoupling::ON_CELLS:
136 case MEDCoupling::ON_NODES:
139 STDLOG("MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?");
145 MEDPresentation::getRenderViewCommand(MEDCALC::MEDPresentationViewMode viewMode) const
147 std::ostringstream oss, oss2;
148 oss << "__view" << _renderViewPyId;
149 std::string view(oss.str());
150 oss2 << "pvs._DisableFirstRenderCameraReset();";
151 if (viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
152 oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');";
153 } else if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
154 oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');";
155 oss2 << "pvs.active_objects.source and pvs.Hide(view=" << view << ");";
156 oss2 << "pvs.Render();";
157 } else if (viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
158 oss2 << "__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');";
159 oss2 << view << " = pvs.CreateView('RenderView');";
160 } else if (viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
161 oss2 << view << " = pvs.CreateView('RenderView');";
167 MEDPresentation::getResetCameraCommand() const
169 std::ostringstream oss;
170 oss << "__view" << _renderViewPyId << ".ResetCamera();";
175 MEDPresentation::getColorMapCommand(MEDCALC::MEDPresentationColorMap colorMap) const
178 case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "Blue to Red Rainbow";
179 case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "Cool to Warm";
184 MEDPresentation::GeneratePythonId()
186 static int INIT_ID = 0;
191 MEDPresentation::activateView() const
194 std::ostringstream oss;
195 oss << "pvs.SetActiveView(__view" << _renderViewPyId << ");";
196 std::cerr << oss.str() << std::endl;
197 PyRun_SimpleString(oss.str().c_str());