Salome HOME
Add presentation in datasource tree
[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 MEDPresentation::MEDPresentation(MEDCALC::FieldHandler* fieldHdl, std::string name)
28     : _fieldHandler(fieldHdl), _pipeline(0), _display(0), _properties()
29 {
30   setProperty("name", name);
31 }
32
33 void MEDPresentation::generatePipeline()
34 {
35   // Might be more complicated in the future:
36
37   this->internalGeneratePipeline();
38 }
39
40 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
41 {
42   _pipeline.push_back(obj);
43   _display.push_back(disp);
44 }
45
46 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
47 {
48   // LIMITED!!! For now switch the first display element to Wireframe
49   /*
50   PyLockWrapper lock;
51   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
52   */
53
54   _properties[propName] = propValue;
55 }
56
57 const std::string
58 MEDPresentation::getProperty(const std::string& propName)
59 {
60   if (_properties.find(propName) != _properties.end()) {
61     return _properties[propName];
62   }
63   else {
64     std::cerr << "getProperty(): no property named " << propName << std::endl;
65     return std::string();
66   }
67 }
68
69 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
70 {
71   // TODO: improve to avoid getting dict at each call
72
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);
77 }
78
79 std::string MEDPresentation::getFieldTypeString()
80 {
81   ParaMEDMEM::TypeOfField typ = (ParaMEDMEM::TypeOfField)_fieldHandler->type;
82   switch(typ)
83   {
84     case ParaMEDMEM::ON_CELLS:
85       return "CELLS";
86     case ParaMEDMEM::ON_NODES:
87       return "NODES";
88     default:
89       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
90       return "";
91   }
92 }
93
94
95 void MEDPresentationScalarMap::internalGeneratePipeline()
96 {
97   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
98
99   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(_fieldHandler->meshid);
100   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
101
102   std::string fileName(dataSHandler->uri);
103   std::string fieldName(_fieldHandler->fieldname);
104   std::string fieldType = getFieldTypeString();
105
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://"))
110     {
111       std::cerr << "\tData source is not a file! Can not proceed." << std::endl;
112       return;
113     }
114
115   fileName = fileName.substr(7, fileName.size());
116   std::cout << "\tfileName: " <<  fileName << std::endl;
117
118   {  // PyLock protected section
119     PyLockWrapper lock;
120
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()");
128
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);
136   }
137
138 }