Salome HOME
fix medcoupling api changes
[modules/med.git] / src / MEDCalc / cmp / MEDPresentation.cxx
1 // Copyright (C) 2011-2016  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 "MEDPresentationException.hxx"
24 #include "MEDCouplingRefCountObject.hxx"
25 #include <iostream>
26
27 MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, std::string name)
28     : _fieldHandlerId(fieldHandlerId), _pipeline(0), _display(0), _properties()
29 {
30   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
31   MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
32   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(fieldHandler->meshid);
33   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
34
35   _fileName = dataSHandler->uri;
36   _fieldName = fieldHandler->fieldname;
37   _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
38
39   if (_fileName.substr(0, 7) != std::string("file://")) {
40     const char* msg = "Data source is not a file! Can not proceed.";
41     throw MEDPresentationException(msg);
42   }
43   _fileName = _fileName.substr(7, _fileName.size());
44
45   setProperty("name", name);
46 }
47
48 void MEDPresentation::generatePipeline()
49 {
50   // Might be more complicated in the future:
51
52   this->internalGeneratePipeline();
53 }
54
55 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
56 {
57   _pipeline.push_back(obj);
58   _display.push_back(disp);
59 }
60
61 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
62 {
63   // LIMITED!!! For now switch the first display element to Wireframe
64   /*
65   PyLockWrapper lock;
66   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
67   */
68
69   _properties[propName] = propValue;
70 }
71
72 const std::string
73 MEDPresentation::getProperty(const std::string& propName)
74 {
75   if (_properties.find(propName) != _properties.end()) {
76     return _properties[propName];
77   }
78   else {
79     std::cerr << "getProperty(): no property named " << propName << std::endl;
80     return std::string();
81   }
82 }
83
84 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var)
85 {
86   // TODO: improve to avoid getting dict at each call
87
88   // All the calls below returns *borrowed* references
89   PyObject* main_module = PyImport_AddModule((char*)"__main__");
90   PyObject* global_dict = PyModule_GetDict(main_module);
91   return PyDict_GetItemString(global_dict, python_var);
92 }
93
94 std::string MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType)
95 {
96   switch(fieldType)
97   {
98     case MEDCoupling::ON_CELLS:
99       return "CELLS";
100     case MEDCoupling::ON_NODES:
101       return "POINTS";
102     default:
103       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
104       return "";
105   }
106 }