]> SALOME platform Git repositories - modules/med.git/blob - src/MEDCalc/cmp/MEDPresentation.cxx
Salome HOME
First scalar map implementation. No GUI (tree browser) update.
[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 char * propName, const char * 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 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
49 {
50   // TODO: improve to avoid getting dict at each call
51
52   // All the calls below returns *borrowed* references
53   PyObject* main_module = PyImport_AddModule((char*)"__main__");
54   PyObject* global_dict = PyModule_GetDict(main_module);
55   return PyDict_GetItemString(global_dict, python_var);
56 }
57
58 std::string MEDPresentation::getFieldTypeString()
59 {
60   ParaMEDMEM::TypeOfField typ = (ParaMEDMEM::TypeOfField)_fieldHandler->type;
61   switch(typ)
62   {
63     case ParaMEDMEM::ON_CELLS:
64       return "CELLS";
65     case ParaMEDMEM::ON_NODES:
66       return "NODES";
67     default:
68       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
69       return "";
70   }
71 }
72
73
74 void MEDPresentationScalarMap::internalGeneratePipeline()
75 {
76   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
77
78   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(_fieldHandler->meshid);
79   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
80
81   std::string fileName(dataSHandler->uri);
82   std::string fieldName(_fieldHandler->fieldname);
83   std::string fieldType = getFieldTypeString();
84
85   std::cout << "Generating pipeline for SCALAR MAP:" <<std::endl;
86   std::cout << "\tfileName: " <<  fileName << std::endl;
87   std::cout << "\tfiedName: " << fieldName << std::endl;
88   if (fileName.substr(0, 7) != std::string("file://"))
89     {
90       std::cerr << "\tData source is not a file! Can not proceed." << std::endl;
91       return;
92     }
93
94   fileName = fileName.substr(7, fileName.size());
95   std::cout << "\tfileName: " <<  fileName << std::endl;
96
97   {  // PyLock protected section
98     PyLockWrapper lock;
99
100     PyRun_SimpleString("print 'hello world'");
101     std::string cmd = std::string(
102         "import pvsimple as pvs;"
103         "__obj1 = pvs.MEDReader(FileName='") + fileName + std::string("');"
104         "__disp1 = pvs.Show(__obj1);"
105         "pvs.ColorBy(__disp1, ('") + fieldType + std::string("', '") + fieldName + std::string("'));"
106         "pvs.GetActiveViewOrCreate('RenderView').ResetCamera()");
107
108     std::cerr << "Python command:" << std::endl;
109     std::cerr << cmd << std::endl;
110     PyRun_SimpleString(cmd.c_str());
111     // Retrieve Python object for internal storage:
112     PyObject * obj = getPythonObjectFromMain("__obj1");
113     PyObject * disp = getPythonObjectFromMain("__disp1");
114     pushInternal(obj, disp);
115   }
116
117 }