]> SALOME platform Git repositories - modules/med.git/blob - src/MEDCalc/cmp/MEDPresentation.cxx
Salome HOME
GUI callback: add MEDPresentation
[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 <PyInterp_Utils.h>
25 #include <iostream>
26
27 void MEDPresentation::generatePipeline()
28 {
29   // Might be more complicated in the future:
30
31   this->internalGeneratePipeline();
32 }
33
34 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
35 {
36   _pipeline.push_back(obj);
37   _display.push_back(disp);
38 }
39
40 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
41 {
42   // LIMITED!!! For now switch the first display element to Wireframe
43   /*
44   PyLockWrapper lock;
45   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
46   */
47
48   _properties[propName] = propValue;
49 }
50
51 const std::string
52 MEDPresentation::getProperty(const std::string& propName)
53 {
54   if (_properties.find(propName) != _properties.end()) {
55     return _properties[propName];
56   }
57   else {
58     std::cerr << "getProperty(): no property named " << propName << std::endl;
59     return std::string();
60   }
61 }
62
63 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
64 {
65   // TODO: improve to avoid getting dict at each call
66
67   // All the calls below returns *borrowed* references
68   PyObject* main_module = PyImport_AddModule((char*)"__main__");
69   PyObject* global_dict = PyModule_GetDict(main_module);
70   return PyDict_GetItemString(global_dict, python_var);
71 }
72
73 std::string MEDPresentation::getFieldTypeString()
74 {
75   ParaMEDMEM::TypeOfField typ = (ParaMEDMEM::TypeOfField)_fieldHandler->type;
76   switch(typ)
77   {
78     case ParaMEDMEM::ON_CELLS:
79       return "CELLS";
80     case ParaMEDMEM::ON_NODES:
81       return "NODES";
82     default:
83       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
84       return "";
85   }
86 }
87
88
89 void MEDPresentationScalarMap::internalGeneratePipeline()
90 {
91   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
92
93   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(_fieldHandler->meshid);
94   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
95
96   std::string fileName(dataSHandler->uri);
97   std::string fieldName(_fieldHandler->fieldname);
98   std::string fieldType = getFieldTypeString();
99
100   std::cout << "Generating pipeline for SCALAR MAP:" <<std::endl;
101   std::cout << "\tfileName: " <<  fileName << std::endl;
102   std::cout << "\tfiedName: " << fieldName << std::endl;
103   if (fileName.substr(0, 7) != std::string("file://"))
104     {
105       std::cerr << "\tData source is not a file! Can not proceed." << std::endl;
106       return;
107     }
108
109   fileName = fileName.substr(7, fileName.size());
110   std::cout << "\tfileName: " <<  fileName << std::endl;
111
112   {  // PyLock protected section
113     PyLockWrapper lock;
114
115     PyRun_SimpleString("print 'hello world'");
116     std::string cmd = std::string(
117         "import pvsimple as pvs;"
118         "__obj1 = pvs.MEDReader(FileName='") + fileName + std::string("');"
119         "__disp1 = pvs.Show(__obj1);"
120         "pvs.ColorBy(__disp1, ('") + fieldType + std::string("', '") + fieldName + std::string("'));"
121         "pvs.GetActiveViewOrCreate('RenderView').ResetCamera()");
122
123     std::cerr << "Python command:" << std::endl;
124     std::cerr << cmd << std::endl;
125     PyRun_SimpleString(cmd.c_str());
126     // Retrieve Python object for internal storage:
127     PyObject * obj = getPythonObjectFromMain("__obj1");
128     PyObject * disp = getPythonObjectFromMain("__disp1");
129     pushInternal(obj, disp);
130   }
131
132 }