Salome HOME
[MEDCalc]: deletion of an existing presentation
[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 "Basics_Utils.hxx"
26 #include "PyInterp_Utils.h"
27
28 #include <sstream>
29
30 MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const std::string& name)
31     : _fieldHandlerId(fieldHandlerId), _pipeline(0), _display(0), _properties(),
32       _renderViewPyId(GeneratePythonId())
33 {
34   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
35   MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
36   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(fieldHandler->meshid);
37   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
38
39   _fileName = dataSHandler->uri;
40   _fieldName = fieldHandler->fieldname;
41   _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
42
43   if (_fileName.substr(0, 7) != std::string("file://")) {
44     const char* msg = "Data source is not a file! Can not proceed.";
45     throw MEDPresentationException(msg);
46   }
47   _fileName = _fileName.substr(7, _fileName.size());
48
49   setProperty("name", name);
50 }
51
52 MEDPresentation::~MEDPresentation()
53 {
54   STDLOG("~MEDPresentation(): clear display");
55   {
56     PyLockWrapper lock;
57     std::ostringstream oss_o, oss_v, oss;
58     // Get top level object and display:
59     oss_o << "__obj" << _pipeline.front().first;
60     oss_v << "__view" << _renderViewPyId;
61     oss << "pvs.Hide(" << oss_o.str() <<  ", view=" << oss_v.str() << ");";
62     oss << "pvs.Render();";
63
64 //    std::cerr << oss.str() << std::endl;
65     PyRun_SimpleString(oss.str().c_str());
66   }
67 }
68
69 void
70 MEDPresentation::generatePipeline()
71 {
72   // Might be more complicated in the future:
73
74   this->internalGeneratePipeline();
75 }
76
77 void
78 MEDPresentation::pushPyObjects(PyObjectId obj, PyObjectId disp)
79 {
80   _pipeline.push_back(obj);
81   _display.push_back(disp);
82 }
83
84 void
85 MEDPresentation::pushAndExecPyLine(const std::string & lin)
86 {
87   // TODO: store internally for low level dump
88   PyLockWrapper lock;
89 //  std::cerr << lin << std::endl;
90   PyRun_SimpleString(lin.c_str());
91 }
92
93 void
94 MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
95 {
96   // LIMITED!!! For now switch the first display element to Wireframe
97   /*
98   PyLockWrapper lock;
99   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
100   */
101
102   _properties[propName] = propValue;
103 }
104
105 const std::string
106 MEDPresentation::getProperty(const std::string& propName) const
107 {
108   std::map<std::string, std::string>::const_iterator it = _properties.find(propName);
109   if (it != _properties.end()) {
110     return (*it).second;
111   }
112   else {
113     STDLOG("MEDPresentation::getProperty(): no property named " + propName);
114     return std::string();
115   }
116 }
117
118 PyObject*
119 MEDPresentation::getPythonObjectFromMain(const char* python_var) const
120 {
121   // TODO: improve to avoid getting dict at each call
122
123   // All the calls below returns *borrowed* references
124   PyObject* main_module = PyImport_AddModule((char*)"__main__");
125   PyObject* global_dict = PyModule_GetDict(main_module);
126   return PyDict_GetItemString(global_dict, python_var);
127 }
128
129 std::string
130 MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
131 {
132   switch(fieldType)
133   {
134     case MEDCoupling::ON_CELLS:
135       return "CELLS";
136     case MEDCoupling::ON_NODES:
137       return "POINTS";
138     default:
139       STDLOG("MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?");
140       return "";
141   }
142 }
143
144 std::string
145 MEDPresentation::getRenderViewCommand(MEDCALC::MEDPresentationViewMode viewMode) const
146 {
147   std::ostringstream oss, oss2;
148   oss << "__view" << _renderViewPyId;
149   std::string view(oss.str());
150   oss2 << "pvs._DisableFirstRenderCameraReset();";
151   if (viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
152       oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');";
153   } else if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
154       oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');";
155       oss2 << "pvs.active_objects.source and pvs.Hide(view=" << view << ");";
156       oss2 << "pvs.Render();";
157   } else if (viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
158       oss2 <<  "__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');";
159       oss2 << view << " = pvs.CreateView('RenderView');";
160   } else if (viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
161       oss2 << view << " = pvs.CreateView('RenderView');";
162   }
163   return oss2.str();
164 }
165
166 std::string
167 MEDPresentation::getResetCameraCommand() const
168 {
169   std::ostringstream oss;
170   oss << "__view" << _renderViewPyId << ".ResetCamera();";
171   return oss.str();
172 }
173
174 std::string
175 MEDPresentation::getColorMapCommand(MEDCALC::MEDPresentationColorMap colorMap) const
176 {
177   switch (colorMap) {
178   case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "Blue to Red Rainbow";
179   case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "Cool to Warm";
180   }
181 }
182
183 int
184 MEDPresentation::GeneratePythonId()
185 {
186   static int INIT_ID = 0;
187   return INIT_ID++;
188 }