Salome HOME
50ed29bb97016431b83089f97123c083871a767b
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationManager_i.cxx
1 // Copyright (C) 2011-2017  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
20 #include "MEDPresentationManager_i.hxx"
21 #include "MEDFactoryClient.hxx"
22 #include "Basics_Utils.hxx"
23
24 // presentations
25 #include "MEDPresentationMeshView.hxx"
26 #include "MEDPresentationScalarMap.hxx"
27 #include "MEDPresentationContour.hxx"
28 #include "MEDPresentationSlices.hxx"
29 #include "MEDPresentationVectorField.hxx"
30 #include "MEDPresentationDeflectionShape.hxx"
31 #include "MEDPresentationPointSprite.hxx"
32
33 #include <SALOME_KernelServices.hxx>
34
35 #include <iostream>
36 #include <sstream>
37
38 MEDPresentationManager_i* MEDPresentationManager_i::_instance = NULL;
39
40 MEDPresentationManager_i*
41 MEDPresentationManager_i::getInstance() {
42   if ( _instance == NULL )
43     _instance = new MEDPresentationManager_i();
44   return _instance;
45 }
46
47 MEDPresentationManager_i::MEDPresentationManager_i() :
48     _presentations(),
49     _activeViewPythonId(-1)
50 {
51 }
52
53 MEDPresentationManager_i::~MEDPresentationManager_i()
54 {
55   /*
56   std::vector<MEDPresentation*>::iterator itr = this->_presentations.begin();
57   for ( ; itr != this->_presentations.end(); ++itr) {
58     delete *itr;
59     *itr = NULL;
60   }
61   this->_presentations.clear();
62   */
63 }
64
65 MEDPresentation::TypeID
66 MEDPresentationManager_i::GenerateID()
67 {
68   static MEDPresentation::TypeID START_ID = -1;
69   START_ID++;
70   return START_ID;
71 }
72
73 MEDPresentation*
74 MEDPresentationManager_i::_getPresentation(MEDPresentation::TypeID presentationID) const
75 {
76   STDLOG("Get presentation " << presentationID);
77   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
78   if (citr == _presentations.end())
79     return NULL;
80   return (*citr).second;
81 }
82
83 void
84 MEDPresentationManager_i::setPresentationStringProperty(MEDPresentation::TypeID presentationID, const char* propName, const char* propValue)
85 {
86   MEDPresentation* pres = _getPresentation(presentationID);
87   if (pres)
88     pres->setStringProperty(propName, propValue);
89   else
90     throw KERNEL::createSalomeException("setPresentationStringProperty(): presentation not found!!");
91 }
92
93 char*
94 MEDPresentationManager_i::getPresentationStringProperty(MEDPresentation::TypeID presentationID, const char* propName)
95 {
96   MEDPresentation* pres = _getPresentation(presentationID);
97   if (pres) {
98     return CORBA::string_dup(pres->getStringProperty(propName).c_str());
99   }
100   else
101     throw KERNEL::createSalomeException("getPresentationStringProperty(): presentation not found!!");
102 }
103
104 void
105 MEDPresentationManager_i::setPresentationIntProperty(MEDPresentation::TypeID presentationID, const char* propName,
106                                                      const CORBA::Long propValue)
107 {
108   MEDPresentation* pres = _getPresentation(presentationID);
109   if (pres)
110     pres->setIntProperty(propName, propValue);
111   else
112     throw KERNEL::createSalomeException("setPresentationIntProperty(): presentation not found!!");
113 }
114
115 CORBA::Long
116 MEDPresentationManager_i::getPresentationIntProperty(MEDPresentation::TypeID presentationID, const char* propName)
117 {
118   MEDPresentation* pres = _getPresentation(presentationID);
119   if (pres) {
120     return (CORBA::Long) pres->getIntProperty(propName);
121   }
122   else
123     throw KERNEL::createSalomeException("getPresentationIntProperty(): presentation not found!!");
124
125 }
126
127 MEDPresentation::TypeID
128 MEDPresentationManager_i::makeMeshView(const MEDCALC::MeshViewParameters& params, const MEDCALC::ViewModeType viewMode)
129 {
130   return _makePresentation<MEDPresentationMeshView>(params, viewMode);
131 }
132
133 MEDPresentation::TypeID
134 MEDPresentationManager_i::makeScalarMap(const MEDCALC::ScalarMapParameters& params, const MEDCALC::ViewModeType viewMode)
135 {
136   return _makePresentation<MEDPresentationScalarMap>(params, viewMode);
137 }
138
139 MEDPresentation::TypeID
140 MEDPresentationManager_i::makeContour(const MEDCALC::ContourParameters& params, const MEDCALC::ViewModeType viewMode)
141 {
142   return _makePresentation<MEDPresentationContour>(params, viewMode);
143 }
144
145 MEDPresentation::TypeID
146 MEDPresentationManager_i::makeVectorField(const MEDCALC::VectorFieldParameters& params, const MEDCALC::ViewModeType viewMode)
147 {
148   return _makePresentation<MEDPresentationVectorField>(params, viewMode);
149 }
150
151 MEDPresentation::TypeID
152 MEDPresentationManager_i::makeSlices(const MEDCALC::SlicesParameters& params, const MEDCALC::ViewModeType viewMode)
153 {
154   return _makePresentation<MEDPresentationSlices>(params, viewMode);
155 }
156
157 MEDPresentation::TypeID
158 MEDPresentationManager_i::makeDeflectionShape(const MEDCALC::DeflectionShapeParameters& params, const MEDCALC::ViewModeType viewMode)
159 {
160   return _makePresentation<MEDPresentationDeflectionShape>(params, viewMode);
161 }
162
163 MEDPresentation::TypeID
164 MEDPresentationManager_i::makePointSprite(const MEDCALC::PointSpriteParameters& params, const MEDCALC::ViewModeType viewMode)
165 {
166   return _makePresentation<MEDPresentationPointSprite>(params, viewMode);
167 }
168
169 MEDCALC::MeshViewParameters
170 MEDPresentationManager_i::getMeshViewParameters(MEDPresentation::TypeID presentationID)
171 {
172   MEDCALC::MeshViewParameters p;
173   _getParameters<MEDPresentationMeshView>(presentationID, p);
174   return p;
175 }
176
177
178 MEDCALC::ScalarMapParameters*
179 MEDPresentationManager_i::getScalarMapParameters(MEDPresentation::TypeID presentationID)
180 {
181   MEDCALC::ScalarMapParameters* p = new MEDCALC::ScalarMapParameters();
182   _getParameters<MEDPresentationScalarMap>(presentationID, *p);
183   MEDCALC::ScalarMapParameters_var tmp(p);
184   return tmp._retn();
185 }
186
187 MEDCALC::ContourParameters
188 MEDPresentationManager_i::getContourParameters(MEDPresentation::TypeID presentationID)
189 {
190   MEDCALC::ContourParameters p;
191   _getParameters<MEDPresentationContour>(presentationID, p);
192   return p;
193 }
194
195 MEDCALC::SlicesParameters*
196 MEDPresentationManager_i::getSlicesParameters(MEDPresentation::TypeID presentationID)
197 {
198   MEDCALC::SlicesParameters* p = new MEDCALC::SlicesParameters();
199   _getParameters<MEDPresentationSlices>(presentationID, *p);
200   MEDCALC::SlicesParameters_var tmp(p);
201   return tmp._retn();
202 }
203
204 MEDCALC::VectorFieldParameters
205 MEDPresentationManager_i::getVectorFieldParameters(MEDPresentation::TypeID presentationID)
206 {
207   MEDCALC::VectorFieldParameters p;
208   _getParameters<MEDPresentationVectorField>(presentationID, p);
209   return p;
210 }
211
212 MEDCALC::PointSpriteParameters*
213 MEDPresentationManager_i::getPointSpriteParameters(MEDPresentation::TypeID presentationID)
214 {
215   MEDCALC::PointSpriteParameters* p = new MEDCALC::PointSpriteParameters();
216   _getParameters<MEDPresentationPointSprite>(presentationID, *p);
217   MEDCALC::PointSpriteParameters_var tmp(p);
218   return tmp._retn();
219 }
220
221 MEDCALC::DeflectionShapeParameters
222 MEDPresentationManager_i::getDeflectionShapeParameters(MEDPresentation::TypeID presentationID)
223 {
224   MEDCALC::DeflectionShapeParameters p;
225   _getParameters<MEDPresentationDeflectionShape>(presentationID, p);
226   return p;
227 }
228
229
230 void
231 MEDPresentationManager_i::updateMeshView(MEDPresentation::TypeID presentationID, const MEDCALC::MeshViewParameters& params)
232 {
233   return _updatePresentation<MEDPresentationMeshView>(presentationID, params);
234 }
235
236 void
237 MEDPresentationManager_i::updateScalarMap(MEDPresentation::TypeID presentationID, const MEDCALC::ScalarMapParameters& params)
238 {
239   return _updatePresentation<MEDPresentationScalarMap>(presentationID, params);
240 }
241
242 void
243 MEDPresentationManager_i::updateContour(MEDPresentation::TypeID presentationID, const MEDCALC::ContourParameters& params)
244 {
245   return _updatePresentation<MEDPresentationContour>(presentationID, params);
246 }
247
248 void
249 MEDPresentationManager_i::updateVectorField(MEDPresentation::TypeID presentationID, const MEDCALC::VectorFieldParameters& params)
250 {
251   return _updatePresentation<MEDPresentationVectorField>(presentationID, params);
252 }
253
254 void
255 MEDPresentationManager_i::updateSlices(MEDPresentation::TypeID presentationID, const MEDCALC::SlicesParameters& params)
256 {
257   return _updatePresentation<MEDPresentationSlices>(presentationID, params);
258 }
259
260 void
261 MEDPresentationManager_i::updateDeflectionShape(MEDPresentation::TypeID presentationID, const MEDCALC::DeflectionShapeParameters& params)
262 {
263   return _updatePresentation<MEDPresentationDeflectionShape>(presentationID, params);
264 }
265
266 void
267 MEDPresentationManager_i::updatePointSprite(MEDPresentation::TypeID presentationID, const MEDCALC::PointSpriteParameters& params)
268 {
269   return _updatePresentation<MEDPresentationPointSprite>(presentationID, params);
270 }
271
272 CORBA::Boolean
273 MEDPresentationManager_i::removePresentation(MEDPresentation::TypeID presentationID)
274 {
275   STDLOG("Remove presentation " << presentationID);
276   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
277   if (citr == _presentations.end()) {
278     std::cerr << "removePresentation(): presentation not found!!" << std::endl;
279     return false;
280   }
281   MEDPresentation* presentation = (*citr).second;
282   if (presentation)
283     delete presentation;
284   _presentations.erase(presentationID);
285
286   STDLOG("Presentation " << presentationID << " has been removed.");
287   return true;
288 }
289
290 CORBA::Boolean
291 MEDPresentationManager_i::activateView(MEDPresentation::TypeID presentationID)
292 {
293   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
294   if (citr == _presentations.end()) {
295     std::cerr << "activateView(): presentation not found!!" << std::endl;
296     return false;
297   }
298   MEDPresentation* presentation = (*citr).second;
299
300   if (!presentation->activateView())
301     {
302       // The view has been recreated - transfer all presentations that were in the deleted view to this new one
303       int viewId = presentation->getPyViewID();
304       std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator citr = _presentations.begin();
305       for (; citr != _presentations.end(); ++citr)
306         {
307           if(citr->second->getPyViewID() == viewId)
308             citr->second->recreateViewSetup();
309         }
310     }
311   _activeViewPythonId = presentation->getPyViewID();
312   STDLOG("Activated view " << _activeViewPythonId);
313   return true;
314 }
315
316 CORBA::Long
317 MEDPresentationManager_i::getActiveViewPythonId()
318 {
319   //TODO: should be more elaborate to re-identify the active ParaView view when another
320   //mechanism than MED module has activated another view.
321   // Idea: 1/ call Python to current active view
322   //       2/ compare with all id(__viewX) from currently existing presentations
323   return _activeViewPythonId;
324 }
325
326
327 //MEDCALC::ViewModeType
328 //MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID)
329 //{
330 //  MEDPresentation* pres = _getPresentation(presentationID);
331 //  if (pres) {
332 //    return pres->getViewMode();
333 //  } else {
334 //    std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl;
335 //    return MEDCALC::VIEW_MODE_DEFAULT;
336 //  }
337 //}
338
339 char *
340 MEDPresentationManager_i::getParavisDump(MEDPresentation::TypeID presentationID)
341 {
342   MEDPresentation* pres = _getPresentation(presentationID);
343   if (pres) {
344     return CORBA::string_dup(pres->paravisDump().c_str());
345   }
346   else
347     throw KERNEL::createSalomeException("getParavisDump(): presentation not found!!");
348 }
349
350 MEDCALC::PresentationsList*
351 MEDPresentationManager_i::getAllPresentations()
352 {
353   MEDCALC::PresentationsList* presList = new MEDCALC::PresentationsList;
354   presList->length(_presentations.size());
355   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator it;
356   int i;
357   for (i = 0, it = _presentations.begin(); it != _presentations.end(); ++it, ++i)
358     (*presList)[i] = it->first;
359   return presList;
360 }
361
362 void
363 MEDPresentationManager_i::cleanUp()
364 {
365   STDLOG("Cleanup");
366   _activeViewPythonId = -1;
367   std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator it;
368   for (it = _presentations.begin(); it != _presentations.end(); ++it)
369     delete(it->second);
370   _presentations.clear();
371 }