Salome HOME
Copyright update 2022
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationManager_i.cxx
1 // Copyright (C) 2011-2022  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 // sphinx doc: begin of makePlot3D
197 MEDPresentation::TypeID
198 MEDPresentationManager_i::makePlot3D(const MEDCALC::Plot3DParameters& params, const MEDCALC::ViewModeType viewMode)
199 {
200   return _makePresentation<MEDPresentationPlot3D>(params, viewMode);
201 }
202 // sphinx doc: end of makePlot3D
203
204 MEDPresentation::TypeID
205 MEDPresentationManager_i::makeStreamLines(const MEDCALC::StreamLinesParameters& params, const MEDCALC::ViewModeType viewMode)
206 {
207   return _makePresentation<MEDPresentationStreamLines>(params, viewMode);
208 }
209
210 MEDPresentation::TypeID
211 MEDPresentationManager_i::makeCutSegment(const MEDCALC::CutSegmentParameters& params, const MEDCALC::ViewModeType viewMode)
212 {
213   return _makePresentation<MEDPresentationCutSegment>(params, viewMode);
214 }
215
216 MEDCALC::MeshViewParameters
217 MEDPresentationManager_i::getMeshViewParameters(MEDPresentation::TypeID presentationID)
218 {
219   MEDCALC::MeshViewParameters* p = new MEDCALC::MeshViewParameters();
220   _getParameters<MEDPresentationMeshView>(presentationID, *p);
221   MEDCALC::MeshViewParameters_var tmp(p);
222   return tmp._retn();
223 }
224
225
226 MEDCALC::ScalarMapParameters*
227 MEDPresentationManager_i::getScalarMapParameters(MEDPresentation::TypeID presentationID)
228 {
229   MEDCALC::ScalarMapParameters* p = new MEDCALC::ScalarMapParameters();
230   _getParameters<MEDPresentationScalarMap>(presentationID, *p);
231   MEDCALC::ScalarMapParameters_var tmp(p);
232   return tmp._retn();
233 }
234
235 MEDCALC::ContourParameters*
236 MEDPresentationManager_i::getContourParameters(MEDPresentation::TypeID presentationID)
237 {
238   MEDCALC::ContourParameters* p = new MEDCALC::ContourParameters();
239   _getParameters<MEDPresentationContour>(presentationID, *p);
240   MEDCALC::ContourParameters_var tmp(p);
241   return tmp._retn();
242 }
243
244 MEDCALC::SlicesParameters*
245 MEDPresentationManager_i::getSlicesParameters(MEDPresentation::TypeID presentationID)
246 {
247   MEDCALC::SlicesParameters* p = new MEDCALC::SlicesParameters();
248   _getParameters<MEDPresentationSlices>(presentationID, *p);
249   MEDCALC::SlicesParameters_var tmp(p);
250   return tmp._retn();
251 }
252
253 MEDCALC::VectorFieldParameters*
254 MEDPresentationManager_i::getVectorFieldParameters(MEDPresentation::TypeID presentationID)
255 {
256   MEDCALC::VectorFieldParameters* p = new MEDCALC::VectorFieldParameters;
257   _getParameters<MEDPresentationVectorField>(presentationID, *p);
258   MEDCALC::VectorFieldParameters_var tmp(p);
259   return tmp._retn();
260 }
261
262 MEDCALC::PointSpriteParameters*
263 MEDPresentationManager_i::getPointSpriteParameters(MEDPresentation::TypeID presentationID)
264 {
265   MEDCALC::PointSpriteParameters* p = new MEDCALC::PointSpriteParameters();
266   _getParameters<MEDPresentationPointSprite>(presentationID, *p);
267   MEDCALC::PointSpriteParameters_var tmp(p);
268   return tmp._retn();
269 }
270
271 // sphinx doc: begin of getPlot3DParameters
272 MEDCALC::Plot3DParameters*
273 MEDPresentationManager_i::getPlot3DParameters(MEDPresentation::TypeID presentationID)
274 {
275   MEDCALC::Plot3DParameters* p = new MEDCALC::Plot3DParameters();
276   _getParameters<MEDPresentationPlot3D>(presentationID, *p);
277   MEDCALC::Plot3DParameters_var tmp(p);
278   return tmp._retn();
279 }
280 // sphinx doc: end of getPlot3DParameters
281
282 MEDCALC::StreamLinesParameters*
283 MEDPresentationManager_i::getStreamLinesParameters(MEDPresentation::TypeID presentationID)
284 {
285   MEDCALC::StreamLinesParameters* p = new MEDCALC::StreamLinesParameters();
286   _getParameters<MEDPresentationStreamLines>(presentationID, *p);
287   MEDCALC::StreamLinesParameters_var tmp(p);
288   return tmp._retn();
289 }
290
291 MEDCALC::CutSegmentParameters*
292 MEDPresentationManager_i::getCutSegmentParameters(MEDPresentation::TypeID presentationID)
293 {
294   MEDCALC::CutSegmentParameters* p = new MEDCALC::CutSegmentParameters();
295   _getParameters<MEDPresentationCutSegment>(presentationID, *p);
296   MEDCALC::CutSegmentParameters_var tmp(p);
297   return tmp._retn();
298 }
299
300 MEDCALC::DeflectionShapeParameters*
301 MEDPresentationManager_i::getDeflectionShapeParameters(MEDPresentation::TypeID presentationID)
302 {
303   MEDCALC::DeflectionShapeParameters* p = new MEDCALC::DeflectionShapeParameters();
304   _getParameters<MEDPresentationDeflectionShape>(presentationID, *p);
305   MEDCALC::DeflectionShapeParameters_var tmp(p);
306   return tmp._retn();
307 }
308
309
310 void
311 MEDPresentationManager_i::updateMeshView(MEDPresentation::TypeID presentationID, const MEDCALC::MeshViewParameters& params)
312 {
313   return _updatePresentation<MEDPresentationMeshView>(presentationID, params);
314 }
315
316 void
317 MEDPresentationManager_i::updateScalarMap(MEDPresentation::TypeID presentationID, const MEDCALC::ScalarMapParameters& params)
318 {
319   return _updatePresentation<MEDPresentationScalarMap>(presentationID, params);
320 }
321
322 void
323 MEDPresentationManager_i::updateContour(MEDPresentation::TypeID presentationID, const MEDCALC::ContourParameters& params)
324 {
325   return _updatePresentation<MEDPresentationContour>(presentationID, params);
326 }
327
328 void
329 MEDPresentationManager_i::updateVectorField(MEDPresentation::TypeID presentationID, const MEDCALC::VectorFieldParameters& params)
330 {
331   return _updatePresentation<MEDPresentationVectorField>(presentationID, params);
332 }
333
334 void
335 MEDPresentationManager_i::updateSlices(MEDPresentation::TypeID presentationID, const MEDCALC::SlicesParameters& params)
336 {
337   return _updatePresentation<MEDPresentationSlices>(presentationID, params);
338 }
339
340 void
341 MEDPresentationManager_i::updateDeflectionShape(MEDPresentation::TypeID presentationID, const MEDCALC::DeflectionShapeParameters& params)
342 {
343   return _updatePresentation<MEDPresentationDeflectionShape>(presentationID, params);
344 }
345
346 void
347 MEDPresentationManager_i::updatePointSprite(MEDPresentation::TypeID presentationID, const MEDCALC::PointSpriteParameters& params)
348 {
349   return _updatePresentation<MEDPresentationPointSprite>(presentationID, params);
350 }
351
352 // sphinx doc: begin of updatePlot3D
353 void
354 MEDPresentationManager_i::updatePlot3D(MEDPresentation::TypeID presentationID, const MEDCALC::Plot3DParameters& params)
355 {
356   return _updatePresentation<MEDPresentationPlot3D>(presentationID, params);
357 }
358 // sphinx doc: end of updatePlot3D
359
360 void
361 MEDPresentationManager_i::updateStreamLines(MEDPresentation::TypeID presentationID, const MEDCALC::StreamLinesParameters& params)
362 {
363   return _updatePresentation<MEDPresentationStreamLines>(presentationID, params);
364 }
365
366 void
367 MEDPresentationManager_i::updateCutSegment(MEDPresentation::TypeID presentationID, const MEDCALC::CutSegmentParameters& params)
368 {
369   return _updatePresentation<MEDPresentationCutSegment>(presentationID, params);
370 }
371
372 CORBA::Boolean
373 MEDPresentationManager_i::removePresentation(MEDPresentation::TypeID presentationID)
374 {
375   STDLOG("Remove presentation " << presentationID);
376   MEDPresentation* presentation = _getPresentation(presentationID);
377   
378   if (!presentation)
379     return false;
380   
381   delete presentation;
382   _presentations.erase(presentationID);
383
384   STDLOG("Presentation " << presentationID << " has been removed.");
385   return true;
386 }
387
388 CORBA::Boolean
389 MEDPresentationManager_i::activateView(MEDPresentation::TypeID presentationID)
390 {
391   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
392   if (citr == _presentations.end()) {
393     std::cerr << "activateView(): presentation not found!!" << std::endl;
394     return false;
395   }
396   MEDPresentation* presentation = (*citr).second;
397
398   if (!presentation->activateView())
399     {
400       // The view has been recreated - transfer all presentations that were in the deleted view to this new one
401       int viewId = presentation->getPyViewID();
402       std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator citr = _presentations.begin();
403       for (; citr != _presentations.end(); ++citr)
404         {
405           if(citr->second->getPyViewID() == viewId)
406             citr->second->recreateViewSetup();
407         }
408     }
409   _activeViewPythonId = presentation->getPyViewID();
410   STDLOG("Activated view " << _activeViewPythonId);
411   return true;
412 }
413
414 CORBA::Long
415 MEDPresentationManager_i::getActiveViewPythonId()
416 {
417   //TODO: should be more elaborate to re-identify the active ParaView view when another
418   //mechanism than MED module has activated another view.
419   // Idea: 1/ call Python to current active view
420   //       2/ compare with all id(__viewX) from currently existing presentations
421   return _activeViewPythonId;
422 }
423
424
425 //MEDCALC::ViewModeType
426 //MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID)
427 //{
428 //  MEDPresentation* pres = _getPresentation(presentationID);
429 //  if (pres) {
430 //    return pres->getViewMode();
431 //  } else {
432 //    std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl;
433 //    return MEDCALC::VIEW_MODE_DEFAULT;
434 //  }
435 //}
436
437 char *
438 MEDPresentationManager_i::getParavisDump(MEDPresentation::TypeID presentationID)
439 {
440   MEDPresentation* pres = _getPresentation(presentationID);
441   if (pres) {
442     return CORBA::string_dup(pres->paravisDump().c_str());
443   }
444   else
445     throw KERNEL::createSalomeException("getParavisDump(): presentation not found!!");
446 }
447
448 MEDCALC::PresentationsList*
449 MEDPresentationManager_i::getAllPresentations()
450 {
451   MEDCALC::PresentationsList* presList = new MEDCALC::PresentationsList;
452   presList->length(_presentations.size());
453   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator it;
454   int i;
455   for (i = 0, it = _presentations.begin(); it != _presentations.end(); ++it, ++i)
456     (*presList)[i] = it->first;
457   return presList;
458 }
459
460 MEDCALC::PresentationVisibility 
461 MEDPresentationManager_i::stateInActiveView(MEDPresentation::TypeID presentationID)
462 {
463   MEDPresentation* pres = _getPresentation(presentationID);
464   if (pres)
465     return pres->presentationStateInActiveView();
466   else
467     throw KERNEL::createSalomeException("stateInActiveView(): presentation not found!!");
468 }
469  
470
471 void
472 MEDPresentationManager_i::cleanUp()
473 {
474   STDLOG("Cleanup");
475   _activeViewPythonId = -1;
476   std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator it;
477   for (it = _presentations.begin(); it != _presentations.end(); ++it)
478     delete(it->second);
479   _presentations.clear();
480 }