1 // Copyright (C) 2011-2015 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 "MEDCouplingRefCountObject.hxx"
24 #include <PyInterp_Utils.h>
27 MEDPresentation::MEDPresentation(MEDCALC::FieldHandler* fieldHdl, std::string name)
28 : _fieldHandler(fieldHdl), _pipeline(0), _display(0), _properties()
30 setProperty("name", name);
33 void MEDPresentation::generatePipeline()
35 // Might be more complicated in the future:
37 this->internalGeneratePipeline();
40 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
42 _pipeline.push_back(obj);
43 _display.push_back(disp);
46 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
48 // LIMITED!!! For now switch the first display element to Wireframe
51 PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
54 _properties[propName] = propValue;
58 MEDPresentation::getProperty(const std::string& propName)
60 if (_properties.find(propName) != _properties.end()) {
61 return _properties[propName];
64 std::cerr << "getProperty(): no property named " << propName << std::endl;
69 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
71 // TODO: improve to avoid getting dict at each call
73 // All the calls below returns *borrowed* references
74 PyObject* main_module = PyImport_AddModule((char*)"__main__");
75 PyObject* global_dict = PyModule_GetDict(main_module);
76 return PyDict_GetItemString(global_dict, python_var);
79 std::string MEDPresentation::getFieldTypeString()
81 ParaMEDMEM::TypeOfField typ = (ParaMEDMEM::TypeOfField)_fieldHandler->type;
84 case ParaMEDMEM::ON_CELLS:
86 case ParaMEDMEM::ON_NODES:
89 std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
95 void MEDPresentationScalarMap::internalGeneratePipeline()
97 MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
99 MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(_fieldHandler->meshid);
100 MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
102 std::string fileName(dataSHandler->uri);
103 std::string fieldName(_fieldHandler->fieldname);
104 std::string fieldType = getFieldTypeString();
106 std::cout << "Generating pipeline for SCALAR MAP:" <<std::endl;
107 std::cout << "\tfileName: " << fileName << std::endl;
108 std::cout << "\tfiedName: " << fieldName << std::endl;
109 if (fileName.substr(0, 7) != std::string("file://"))
111 std::cerr << "\tData source is not a file! Can not proceed." << std::endl;
115 fileName = fileName.substr(7, fileName.size());
116 std::cout << "\tfileName: " << fileName << std::endl;
118 { // PyLock protected section
121 PyRun_SimpleString("print 'hello world'");
122 std::string cmd = std::string(
123 "import pvsimple as pvs;"
124 "__obj1 = pvs.MEDReader(FileName='") + fileName + std::string("');"
125 "__disp1 = pvs.Show(__obj1);"
126 "pvs.ColorBy(__disp1, ('") + fieldType + std::string("', '") + fieldName + std::string("'));"
127 "pvs.GetActiveViewOrCreate('RenderView').ResetCamera()");
129 std::cerr << "Python command:" << std::endl;
130 std::cerr << cmd << std::endl;
131 PyRun_SimpleString(cmd.c_str());
132 // Retrieve Python object for internal storage:
133 PyObject * obj = getPythonObjectFromMain("__obj1");
134 PyObject * disp = getPythonObjectFromMain("__disp1");
135 pushInternal(obj, disp);