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"
26 MEDPresentation::MEDPresentation(MEDCALC::FieldHandler* fieldHdl, std::string name)
27 : _fieldHandler(fieldHdl), _pipeline(0), _display(0), _properties()
29 setProperty("name", name);
32 void MEDPresentation::generatePipeline()
34 // Might be more complicated in the future:
36 this->internalGeneratePipeline();
39 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
41 _pipeline.push_back(obj);
42 _display.push_back(disp);
45 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
47 // LIMITED!!! For now switch the first display element to Wireframe
50 PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
53 _properties[propName] = propValue;
57 MEDPresentation::getProperty(const std::string& propName)
59 if (_properties.find(propName) != _properties.end()) {
60 return _properties[propName];
63 std::cerr << "getProperty(): no property named " << propName << std::endl;
68 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
70 // TODO: improve to avoid getting dict at each call
72 // All the calls below returns *borrowed* references
73 PyObject* main_module = PyImport_AddModule((char*)"__main__");
74 PyObject* global_dict = PyModule_GetDict(main_module);
75 return PyDict_GetItemString(global_dict, python_var);
78 std::string MEDPresentation::getFieldTypeString()
80 MEDCoupling::TypeOfField typ = (MEDCoupling::TypeOfField)_fieldHandler->type;
83 case MEDCoupling::ON_CELLS:
85 case MEDCoupling::ON_NODES:
88 std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
94 void MEDPresentationScalarMap::internalGeneratePipeline()
96 MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
98 MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(_fieldHandler->meshid);
99 MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
101 std::string fileName(dataSHandler->uri);
102 std::string fieldName(_fieldHandler->fieldname);
103 std::string fieldType = getFieldTypeString();
105 std::cout << "Generating pipeline for SCALAR MAP:" <<std::endl;
106 std::cout << "\tfileName: " << fileName << std::endl;
107 std::cout << "\tfiedName: " << fieldName << std::endl;
108 if (fileName.substr(0, 7) != std::string("file://"))
110 std::cerr << "\tData source is not a file! Can not proceed." << std::endl;
114 fileName = fileName.substr(7, fileName.size());
115 std::cout << "\tfileName: " << fileName << std::endl;
117 PyGILState_STATE _gil_state = PyGILState_Ensure();
119 PyRun_SimpleString("print 'hello world'");
120 std::string cmd = std::string(
121 "import pvsimple as pvs;"
122 "__obj1 = pvs.MEDReader(FileName='") + fileName + std::string("');"
123 "__disp1 = pvs.Show(__obj1);"
124 "pvs.ColorBy(__disp1, ('") + fieldType + std::string("', '") + fieldName + std::string("'));"
125 "pvs.GetActiveViewOrCreate('RenderView').ResetCamera()");
127 std::cerr << "Python command:" << std::endl;
128 std::cerr << cmd << std::endl;
129 PyRun_SimpleString(cmd.c_str());
130 // Retrieve Python object for internal storage:
131 PyObject * obj = getPythonObjectFromMain("__obj1");
132 PyObject * disp = getPythonObjectFromMain("__disp1");
133 pushInternal(obj, disp);
135 PyGILState_Release(_gil_state);