Salome HOME
Merge branch 'master' into abn/qtesting
[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, const 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 MEDPresentation::~MEDPresentation()
49 {
50   std::cout << "###TODO#### ~MEDPresentation: clear pipeline\n";
51   std::cout << "###TODO#### ~MEDPresentation: clear display\n";
52 }
53
54 void
55 MEDPresentation::generatePipeline()
56 {
57   // Might be more complicated in the future:
58
59   this->internalGeneratePipeline();
60 }
61
62 void
63 MEDPresentation::pushInternal(PyObject* obj, PyObject* disp)
64 {
65   _pipeline.push_back(obj);
66   _display.push_back(disp);
67 }
68
69 void
70 MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
71 {
72   // LIMITED!!! For now switch the first display element to Wireframe
73   /*
74   PyLockWrapper lock;
75   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
76   */
77
78   _properties[propName] = propValue;
79 }
80
81 const std::string
82 MEDPresentation::getProperty(const std::string& propName) const
83 {
84   std::map<std::string, std::string>::const_iterator it = _properties.find(propName);
85   if (it != _properties.end()) {
86     return (*it).second;
87   }
88   else {
89     std::cerr << "getProperty(): no property named " << propName << std::endl;
90     return std::string();
91   }
92 }
93
94 PyObject*
95 MEDPresentation::getPythonObjectFromMain(const char* python_var) const
96 {
97   // TODO: improve to avoid getting dict at each call
98
99   // All the calls below returns *borrowed* references
100   PyObject* main_module = PyImport_AddModule((char*)"__main__");
101   PyObject* global_dict = PyModule_GetDict(main_module);
102   return PyDict_GetItemString(global_dict, python_var);
103 }
104
105 std::string
106 MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
107 {
108   switch(fieldType)
109   {
110     case MEDCoupling::ON_CELLS:
111       return "CELLS";
112     case MEDCoupling::ON_NODES:
113       return "POINTS";
114     default:
115       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
116       return "";
117   }
118 }
119
120 std::string
121 MEDPresentation::getRenderViewCommand(MEDCALC::MEDPresentationViewMode viewMode) const
122 {
123   std::string cmd = std::string("pvs._DisableFirstRenderCameraReset();");
124   if (viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
125     cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
126   } else if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
127     cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
128     cmd += std::string("pvs.active_objects.source and pvs.Hide(view=__view1);");
129     cmd += std::string("pvs.Render();");
130   } else if (viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
131     cmd += std::string("__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');");
132     cmd += std::string("__view1 = pvs.CreateView('RenderView');");
133   } else if (viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
134     cmd += std::string("__view1 = pvs.CreateView('RenderView');");
135   }
136   return cmd;
137 }
138
139 std::string
140 MEDPresentation::getResetCameraCommand() const
141 {
142   return std::string("__view1.ResetCamera();");
143 }
144
145
146 std::string
147 MEDPresentation::getColorMapCommand(MEDCALC::MEDPresentationColorMap colorMap) const
148 {
149   switch (colorMap) {
150   case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "Blue to Red Rainbow";
151   case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "Cool to Warm";
152   }
153 }