Salome HOME
Add python GIL Ensure / Release.
[modules/med.git] / src / MEDCalc / cmp / MEDPresentation.cxx
1 // Copyright (C) 2011-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Authors: A Bruneton (CEA), C Aguerre (EdF)
20
21 #include "MEDFactoryClient.hxx"
22 #include "MEDPresentation.hxx"
23 #include "MEDCouplingRefCountObject.hxx"
24 #include <iostream>
25
26 MEDPresentation::MEDPresentation(MEDCALC::FieldHandler* fieldHdl, std::string name)
27     : _fieldHandler(fieldHdl), _pipeline(0), _display(0), _properties()
28 {
29   setProperty("name", name);
30 }
31
32 void MEDPresentation::generatePipeline()
33 {
34   // Might be more complicated in the future:
35
36   this->internalGeneratePipeline();
37 }
38
39 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
40 {
41   _pipeline.push_back(obj);
42   _display.push_back(disp);
43 }
44
45 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
46 {
47   // LIMITED!!! For now switch the first display element to Wireframe
48   /*
49   PyLockWrapper lock;
50   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
51   */
52
53   _properties[propName] = propValue;
54 }
55
56 const std::string
57 MEDPresentation::getProperty(const std::string& propName)
58 {
59   if (_properties.find(propName) != _properties.end()) {
60     return _properties[propName];
61   }
62   else {
63     std::cerr << "getProperty(): no property named " << propName << std::endl;
64     return std::string();
65   }
66 }
67
68 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
69 {
70   // TODO: improve to avoid getting dict at each call
71
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);
76 }
77
78 std::string MEDPresentation::getFieldTypeString()
79 {
80   ParaMEDMEM::TypeOfField typ = (ParaMEDMEM::TypeOfField)_fieldHandler->type;
81   switch(typ)
82   {
83     case ParaMEDMEM::ON_CELLS:
84       return "CELLS";
85     case ParaMEDMEM::ON_NODES:
86       return "NODES";
87     default:
88       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
89       return "";
90   }
91 }
92
93
94 void MEDPresentationScalarMap::internalGeneratePipeline()
95 {
96   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
97
98   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(_fieldHandler->meshid);
99   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
100
101   std::string fileName(dataSHandler->uri);
102   std::string fieldName(_fieldHandler->fieldname);
103   std::string fieldType = getFieldTypeString();
104
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://"))
109     {
110       std::cerr << "\tData source is not a file! Can not proceed." << std::endl;
111       return;
112     }
113
114   fileName = fileName.substr(7, fileName.size());
115   std::cout << "\tfileName: " <<  fileName << std::endl;
116
117   PyGILState_STATE _gil_state = PyGILState_Ensure();
118
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()");
126
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);
134
135   PyGILState_Release(_gil_state);
136 }