Salome HOME
bos #20430 [CEA 20428] FIELDS : improvement of simplified visualisations
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationManager_i.cxx
1 // Copyright (C) 2011-2021  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 #include "MEDPresentationPlot3D.hxx"
33 #include "MEDPresentationStreamLines.hxx"
34 #include "MEDPresentationCutSegment.hxx"
35
36 #include <SALOME_KernelServices.hxx>
37
38 #include <iostream>
39 #include <sstream>
40
41 MEDPresentationManager_i* MEDPresentationManager_i::_instance = NULL;
42
43 MEDPresentationManager_i*
44 MEDPresentationManager_i::getInstance() {
45   if ( _instance == NULL )
46     _instance = new MEDPresentationManager_i();
47   return _instance;
48 }
49
50 MEDPresentationManager_i::MEDPresentationManager_i() :
51     _presentations(),
52     _activeViewPythonId(-1)
53 {
54 }
55
56 MEDPresentationManager_i::~MEDPresentationManager_i()
57 {
58   /*
59   std::vector<MEDPresentation*>::iterator itr = this->_presentations.begin();
60   for ( ; itr != this->_presentations.end(); ++itr) {
61     delete *itr;
62     *itr = NULL;
63   }
64   this->_presentations.clear();
65   */
66 }
67
68 MEDPresentation::TypeID
69 MEDPresentationManager_i::GenerateID()
70 {
71   static MEDPresentation::TypeID START_ID = -1;
72   START_ID++;
73   return START_ID;
74 }
75
76 MEDPresentation*
77 MEDPresentationManager_i::_getPresentation(MEDPresentation::TypeID presentationID) const
78 {
79   STDLOG("Get presentation " << presentationID);
80   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
81   if (citr == _presentations.end())
82     return nullptr;
83   return (*citr).second;
84 }
85
86 void
87 MEDPresentationManager_i::setPresentationStringProperty(MEDPresentation::TypeID presentationID, const char* propName, const char* propValue)
88 {
89   MEDPresentation* pres = _getPresentation(presentationID);
90   if (pres)
91     pres->setStringProperty(propName, propValue);
92   else
93     throw KERNEL::createSalomeException("setPresentationStringProperty(): presentation not found!!");
94 }
95
96 char*
97 MEDPresentationManager_i::getPresentationStringProperty(MEDPresentation::TypeID presentationID, const char* propName)
98 {
99   MEDPresentation* pres = _getPresentation(presentationID);
100   if (pres) {
101     return CORBA::string_dup(pres->getStringProperty(propName).c_str());
102   }
103   else
104     throw KERNEL::createSalomeException("getPresentationStringProperty(): presentation not found!!");
105 }
106
107 void
108 MEDPresentationManager_i::setPresentationIntProperty(MEDPresentation::TypeID presentationID, const char* propName,
109                                                      const CORBA::Long propValue)
110 {
111   MEDPresentation* pres = _getPresentation(presentationID);
112   if (pres)
113     pres->setIntProperty(propName, propValue);
114   else
115     throw KERNEL::createSalomeException("setPresentationIntProperty(): presentation not found!!");
116 }
117
118 CORBA::Long
119 MEDPresentationManager_i::getPresentationIntProperty(MEDPresentation::TypeID presentationID, const char* propName)
120 {
121   MEDPresentation* pres = _getPresentation(presentationID);
122   if (pres) {
123     return (CORBA::Long) pres->getIntProperty(propName);
124   }
125   else
126     throw KERNEL::createSalomeException("getPresentationIntProperty(): presentation not found!!");
127
128 }
129
130 void
131 MEDPresentationManager_i::setPresentationDoubleProperty(MEDPresentation::TypeID presentationID, const char* propName,
132   const CORBA::Double propValue)
133 {
134   MEDPresentation* pres = _getPresentation(presentationID);
135   if (pres)
136     pres->setDoubleProperty(propName, propValue);
137   else
138     throw KERNEL::createSalomeException("setPresentationDoubleProperty(): presentation not found!!");
139 }
140
141 CORBA::Double
142 MEDPresentationManager_i::getPresentationDoubleProperty(MEDPresentation::TypeID presentationID, const char* propName)
143 {
144   MEDPresentation* pres = _getPresentation(presentationID);
145   if (pres) {
146     return (CORBA::Double) pres->getDoubleProperty(propName);
147   }
148   else
149     throw KERNEL::createSalomeException("getPresentationIntProperty(): presentation not found!!");
150
151 }
152
153
154 MEDPresentation::TypeID
155 MEDPresentationManager_i::makeMeshView(const MEDCALC::MeshViewParameters& params, const MEDCALC::ViewModeType viewMode)
156 {
157   return _makePresentation<MEDPresentationMeshView>(params, viewMode);
158 }
159
160 MEDPresentation::TypeID
161 MEDPresentationManager_i::makeScalarMap(const MEDCALC::ScalarMapParameters& params, const MEDCALC::ViewModeType viewMode)
162 {
163   return _makePresentation<MEDPresentationScalarMap>(params, viewMode);
164 }
165
166 MEDPresentation::TypeID
167 MEDPresentationManager_i::makeContour(const MEDCALC::ContourParameters& params, const MEDCALC::ViewModeType viewMode)
168 {
169   return _makePresentation<MEDPresentationContour>(params, viewMode);
170 }
171
172 MEDPresentation::TypeID
173 MEDPresentationManager_i::makeVectorField(const MEDCALC::VectorFieldParameters& params, const MEDCALC::ViewModeType viewMode)
174 {
175   return _makePresentation<MEDPresentationVectorField>(params, viewMode);
176 }
177
178 MEDPresentation::TypeID
179 MEDPresentationManager_i::makeSlices(const MEDCALC::SlicesParameters& params, const MEDCALC::ViewModeType viewMode)
180 {
181   return _makePresentation<MEDPresentationSlices>(params, viewMode);
182 }
183
184 MEDPresentation::TypeID
185 MEDPresentationManager_i::makeDeflectionShape(const MEDCALC::DeflectionShapeParameters& params, const MEDCALC::ViewModeType viewMode)
186 {
187   return _makePresentation<MEDPresentationDeflectionShape>(params, viewMode);
188 }
189
190 MEDPresentation::TypeID
191 MEDPresentationManager_i::makePointSprite(const MEDCALC::PointSpriteParameters& params, const MEDCALC::ViewModeType viewMode)
192 {
193   return _makePresentation<MEDPresentationPointSprite>(params, viewMode);
194 }
195
196 MEDPresentation::TypeID
197 MEDPresentationManager_i::makePlot3D(const MEDCALC::Plot3DParameters& params, const MEDCALC::ViewModeType viewMode)
198 {
199   return _makePresentation<MEDPresentationPlot3D>(params, viewMode);
200 }
201
202 MEDPresentation::TypeID
203 MEDPresentationManager_i::makeStreamLines(const MEDCALC::StreamLinesParameters& params, const MEDCALC::ViewModeType viewMode)
204 {
205   return _makePresentation<MEDPresentationStreamLines>(params, viewMode);
206 }
207
208 MEDPresentation::TypeID
209 MEDPresentationManager_i::makeCutSegment(const MEDCALC::CutSegmentParameters& params, const MEDCALC::ViewModeType viewMode)
210 {
211   return _makePresentation<MEDPresentationCutSegment>(params, viewMode);
212 }
213
214 MEDCALC::MeshViewParameters
215 MEDPresentationManager_i::getMeshViewParameters(MEDPresentation::TypeID presentationID)
216 {
217   MEDCALC::MeshViewParameters* p = new MEDCALC::MeshViewParameters();
218   _getParameters<MEDPresentationMeshView>(presentationID, *p);
219   MEDCALC::MeshViewParameters_var tmp(p);
220   return tmp._retn();
221 }
222
223
224 MEDCALC::ScalarMapParameters*
225 MEDPresentationManager_i::getScalarMapParameters(MEDPresentation::TypeID presentationID)
226 {
227   MEDCALC::ScalarMapParameters* p = new MEDCALC::ScalarMapParameters();
228   _getParameters<MEDPresentationScalarMap>(presentationID, *p);
229   MEDCALC::ScalarMapParameters_var tmp(p);
230   return tmp._retn();
231 }
232
233 MEDCALC::ContourParameters*
234 MEDPresentationManager_i::getContourParameters(MEDPresentation::TypeID presentationID)
235 {
236   MEDCALC::ContourParameters* p = new MEDCALC::ContourParameters();
237   _getParameters<MEDPresentationContour>(presentationID, *p);
238   MEDCALC::ContourParameters_var tmp(p);
239   return tmp._retn();
240 }
241
242 MEDCALC::SlicesParameters*
243 MEDPresentationManager_i::getSlicesParameters(MEDPresentation::TypeID presentationID)
244 {
245   MEDCALC::SlicesParameters* p = new MEDCALC::SlicesParameters();
246   _getParameters<MEDPresentationSlices>(presentationID, *p);
247   MEDCALC::SlicesParameters_var tmp(p);
248   return tmp._retn();
249 }
250
251 MEDCALC::VectorFieldParameters*
252 MEDPresentationManager_i::getVectorFieldParameters(MEDPresentation::TypeID presentationID)
253 {
254   MEDCALC::VectorFieldParameters* p = new MEDCALC::VectorFieldParameters;
255   _getParameters<MEDPresentationVectorField>(presentationID, *p);
256   MEDCALC::VectorFieldParameters_var tmp(p);
257   return tmp._retn();
258 }
259
260 MEDCALC::PointSpriteParameters*
261 MEDPresentationManager_i::getPointSpriteParameters(MEDPresentation::TypeID presentationID)
262 {
263   MEDCALC::PointSpriteParameters* p = new MEDCALC::PointSpriteParameters();
264   _getParameters<MEDPresentationPointSprite>(presentationID, *p);
265   MEDCALC::PointSpriteParameters_var tmp(p);
266   return tmp._retn();
267 }
268
269 MEDCALC::Plot3DParameters*
270 MEDPresentationManager_i::getPlot3DParameters(MEDPresentation::TypeID presentationID)
271 {
272   MEDCALC::Plot3DParameters* p = new MEDCALC::Plot3DParameters();
273   _getParameters<MEDPresentationPlot3D>(presentationID, *p);
274   MEDCALC::Plot3DParameters_var tmp(p);
275   return tmp._retn();
276 }
277
278 MEDCALC::StreamLinesParameters*
279 MEDPresentationManager_i::getStreamLinesParameters(MEDPresentation::TypeID presentationID)
280 {
281   MEDCALC::StreamLinesParameters* p = new MEDCALC::StreamLinesParameters();
282   _getParameters<MEDPresentationStreamLines>(presentationID, *p);
283   MEDCALC::StreamLinesParameters_var tmp(p);
284   return tmp._retn();
285 }
286
287 MEDCALC::CutSegmentParameters*
288 MEDPresentationManager_i::getCutSegmentParameters(MEDPresentation::TypeID presentationID)
289 {
290   MEDCALC::CutSegmentParameters* p = new MEDCALC::CutSegmentParameters();
291   _getParameters<MEDPresentationCutSegment>(presentationID, *p);
292   MEDCALC::CutSegmentParameters_var tmp(p);
293   return tmp._retn();
294 }
295
296 MEDCALC::DeflectionShapeParameters*
297 MEDPresentationManager_i::getDeflectionShapeParameters(MEDPresentation::TypeID presentationID)
298 {
299   MEDCALC::DeflectionShapeParameters* p = new MEDCALC::DeflectionShapeParameters();
300   _getParameters<MEDPresentationDeflectionShape>(presentationID, *p);
301   MEDCALC::DeflectionShapeParameters_var tmp(p);
302   return tmp._retn();
303 }
304
305
306 void
307 MEDPresentationManager_i::updateMeshView(MEDPresentation::TypeID presentationID, const MEDCALC::MeshViewParameters& params)
308 {
309   return _updatePresentation<MEDPresentationMeshView>(presentationID, params);
310 }
311
312 void
313 MEDPresentationManager_i::updateScalarMap(MEDPresentation::TypeID presentationID, const MEDCALC::ScalarMapParameters& params)
314 {
315   return _updatePresentation<MEDPresentationScalarMap>(presentationID, params);
316 }
317
318 void
319 MEDPresentationManager_i::updateContour(MEDPresentation::TypeID presentationID, const MEDCALC::ContourParameters& params)
320 {
321   return _updatePresentation<MEDPresentationContour>(presentationID, params);
322 }
323
324 void
325 MEDPresentationManager_i::updateVectorField(MEDPresentation::TypeID presentationID, const MEDCALC::VectorFieldParameters& params)
326 {
327   return _updatePresentation<MEDPresentationVectorField>(presentationID, params);
328 }
329
330 void
331 MEDPresentationManager_i::updateSlices(MEDPresentation::TypeID presentationID, const MEDCALC::SlicesParameters& params)
332 {
333   return _updatePresentation<MEDPresentationSlices>(presentationID, params);
334 }
335
336 void
337 MEDPresentationManager_i::updateDeflectionShape(MEDPresentation::TypeID presentationID, const MEDCALC::DeflectionShapeParameters& params)
338 {
339   return _updatePresentation<MEDPresentationDeflectionShape>(presentationID, params);
340 }
341
342 void
343 MEDPresentationManager_i::updatePointSprite(MEDPresentation::TypeID presentationID, const MEDCALC::PointSpriteParameters& params)
344 {
345   return _updatePresentation<MEDPresentationPointSprite>(presentationID, params);
346 }
347
348 void
349 MEDPresentationManager_i::updatePlot3D(MEDPresentation::TypeID presentationID, const MEDCALC::Plot3DParameters& params)
350 {
351   return _updatePresentation<MEDPresentationPlot3D>(presentationID, params);
352 }
353
354 void
355 MEDPresentationManager_i::updateStreamLines(MEDPresentation::TypeID presentationID, const MEDCALC::StreamLinesParameters& params)
356 {
357   return _updatePresentation<MEDPresentationStreamLines>(presentationID, params);
358 }
359
360 void
361 MEDPresentationManager_i::updateCutSegment(MEDPresentation::TypeID presentationID, const MEDCALC::CutSegmentParameters& params)
362 {
363   return _updatePresentation<MEDPresentationCutSegment>(presentationID, params);
364 }
365
366 CORBA::Boolean
367 MEDPresentationManager_i::removePresentation(MEDPresentation::TypeID presentationID)
368 {
369   STDLOG("Remove presentation " << presentationID);
370   MEDPresentation* presentation = _getPresentation(presentationID);
371   
372   if (!presentation)
373     return false;
374   
375   delete presentation;
376   _presentations.erase(presentationID);
377
378   STDLOG("Presentation " << presentationID << " has been removed.");
379   return true;
380 }
381
382 CORBA::Boolean
383 MEDPresentationManager_i::activateView(MEDPresentation::TypeID presentationID)
384 {
385   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
386   if (citr == _presentations.end()) {
387     std::cerr << "activateView(): presentation not found!!" << std::endl;
388     return false;
389   }
390   MEDPresentation* presentation = (*citr).second;
391
392   if (!presentation->activateView())
393     {
394       // The view has been recreated - transfer all presentations that were in the deleted view to this new one
395       int viewId = presentation->getPyViewID();
396       std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator citr = _presentations.begin();
397       for (; citr != _presentations.end(); ++citr)
398         {
399           if(citr->second->getPyViewID() == viewId)
400             citr->second->recreateViewSetup();
401         }
402     }
403   _activeViewPythonId = presentation->getPyViewID();
404   STDLOG("Activated view " << _activeViewPythonId);
405   return true;
406 }
407
408 CORBA::Long
409 MEDPresentationManager_i::getActiveViewPythonId()
410 {
411   //TODO: should be more elaborate to re-identify the active ParaView view when another
412   //mechanism than MED module has activated another view.
413   // Idea: 1/ call Python to current active view
414   //       2/ compare with all id(__viewX) from currently existing presentations
415   return _activeViewPythonId;
416 }
417
418
419 //MEDCALC::ViewModeType
420 //MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID)
421 //{
422 //  MEDPresentation* pres = _getPresentation(presentationID);
423 //  if (pres) {
424 //    return pres->getViewMode();
425 //  } else {
426 //    std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl;
427 //    return MEDCALC::VIEW_MODE_DEFAULT;
428 //  }
429 //}
430
431 char *
432 MEDPresentationManager_i::getParavisDump(MEDPresentation::TypeID presentationID)
433 {
434   MEDPresentation* pres = _getPresentation(presentationID);
435   if (pres) {
436     return CORBA::string_dup(pres->paravisDump().c_str());
437   }
438   else
439     throw KERNEL::createSalomeException("getParavisDump(): presentation not found!!");
440 }
441
442 MEDCALC::PresentationsList*
443 MEDPresentationManager_i::getAllPresentations()
444 {
445   MEDCALC::PresentationsList* presList = new MEDCALC::PresentationsList;
446   presList->length(_presentations.size());
447   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator it;
448   int i;
449   for (i = 0, it = _presentations.begin(); it != _presentations.end(); ++it, ++i)
450     (*presList)[i] = it->first;
451   return presList;
452 }
453
454 MEDCALC::PresentationVisibility 
455 MEDPresentationManager_i::stateInActiveView(MEDPresentation::TypeID presentationID)
456 {
457   MEDPresentation* pres = _getPresentation(presentationID);
458   if (pres)
459     return pres->presentationStateInActiveView();
460   else
461     throw KERNEL::createSalomeException("stateInActiveView(): presentation not found!!");
462 }
463  
464
465 void
466 MEDPresentationManager_i::cleanUp()
467 {
468   STDLOG("Cleanup");
469   _activeViewPythonId = -1;
470   std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator it;
471   for (it = _presentations.begin(); it != _presentations.end(); ++it)
472     delete(it->second);
473   _presentations.clear();
474 }