Salome HOME
bbdf3290549321fb223ebee8d5e282a7d2f9098d
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationManager_i.cxx
1 // Copyright (C) 2011-2020  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 = new MEDCALC::MeshViewParameters();
173   _getParameters<MEDPresentationMeshView>(presentationID, *p);
174   MEDCALC::MeshViewParameters_var tmp(p);
175   return tmp._retn();
176 }
177
178
179 MEDCALC::ScalarMapParameters*
180 MEDPresentationManager_i::getScalarMapParameters(MEDPresentation::TypeID presentationID)
181 {
182   MEDCALC::ScalarMapParameters* p = new MEDCALC::ScalarMapParameters();
183   _getParameters<MEDPresentationScalarMap>(presentationID, *p);
184   MEDCALC::ScalarMapParameters_var tmp(p);
185   return tmp._retn();
186 }
187
188 MEDCALC::ContourParameters
189 MEDPresentationManager_i::getContourParameters(MEDPresentation::TypeID presentationID)
190 {
191   MEDCALC::ContourParameters* p = new MEDCALC::ContourParameters();
192   _getParameters<MEDPresentationContour>(presentationID, *p);
193   MEDCALC::ContourParameters_var tmp(p);
194   return tmp._retn();
195 }
196
197 MEDCALC::SlicesParameters*
198 MEDPresentationManager_i::getSlicesParameters(MEDPresentation::TypeID presentationID)
199 {
200   MEDCALC::SlicesParameters* p = new MEDCALC::SlicesParameters();
201   _getParameters<MEDPresentationSlices>(presentationID, *p);
202   MEDCALC::SlicesParameters_var tmp(p);
203   return tmp._retn();
204 }
205
206 MEDCALC::VectorFieldParameters
207 MEDPresentationManager_i::getVectorFieldParameters(MEDPresentation::TypeID presentationID)
208 {
209   MEDCALC::VectorFieldParameters* p = new MEDCALC::VectorFieldParameters();
210   _getParameters<MEDPresentationVectorField>(presentationID, *p);
211   MEDCALC::VectorFieldParameters_var tmp(p);
212   return tmp._retn();
213 }
214
215 MEDCALC::PointSpriteParameters*
216 MEDPresentationManager_i::getPointSpriteParameters(MEDPresentation::TypeID presentationID)
217 {
218   MEDCALC::PointSpriteParameters* p = new MEDCALC::PointSpriteParameters();
219   _getParameters<MEDPresentationPointSprite>(presentationID, *p);
220   MEDCALC::PointSpriteParameters_var tmp(p);
221   return tmp._retn();
222 }
223
224 MEDCALC::DeflectionShapeParameters
225 MEDPresentationManager_i::getDeflectionShapeParameters(MEDPresentation::TypeID presentationID)
226 {
227   MEDCALC::DeflectionShapeParameters* p = new MEDCALC::DeflectionShapeParameters();
228   _getParameters<MEDPresentationDeflectionShape>(presentationID, *p);
229   MEDCALC::DeflectionShapeParameters_var tmp(p);
230   return tmp._retn();
231 }
232
233
234 void
235 MEDPresentationManager_i::updateMeshView(MEDPresentation::TypeID presentationID, const MEDCALC::MeshViewParameters& params)
236 {
237   return _updatePresentation<MEDPresentationMeshView>(presentationID, params);
238 }
239
240 void
241 MEDPresentationManager_i::updateScalarMap(MEDPresentation::TypeID presentationID, const MEDCALC::ScalarMapParameters& params)
242 {
243   return _updatePresentation<MEDPresentationScalarMap>(presentationID, params);
244 }
245
246 void
247 MEDPresentationManager_i::updateContour(MEDPresentation::TypeID presentationID, const MEDCALC::ContourParameters& params)
248 {
249   return _updatePresentation<MEDPresentationContour>(presentationID, params);
250 }
251
252 void
253 MEDPresentationManager_i::updateVectorField(MEDPresentation::TypeID presentationID, const MEDCALC::VectorFieldParameters& params)
254 {
255   return _updatePresentation<MEDPresentationVectorField>(presentationID, params);
256 }
257
258 void
259 MEDPresentationManager_i::updateSlices(MEDPresentation::TypeID presentationID, const MEDCALC::SlicesParameters& params)
260 {
261   return _updatePresentation<MEDPresentationSlices>(presentationID, params);
262 }
263
264 void
265 MEDPresentationManager_i::updateDeflectionShape(MEDPresentation::TypeID presentationID, const MEDCALC::DeflectionShapeParameters& params)
266 {
267   return _updatePresentation<MEDPresentationDeflectionShape>(presentationID, params);
268 }
269
270 void
271 MEDPresentationManager_i::updatePointSprite(MEDPresentation::TypeID presentationID, const MEDCALC::PointSpriteParameters& params)
272 {
273   return _updatePresentation<MEDPresentationPointSprite>(presentationID, params);
274 }
275
276 CORBA::Boolean
277 MEDPresentationManager_i::removePresentation(MEDPresentation::TypeID presentationID)
278 {
279   STDLOG("Remove presentation " << presentationID);
280   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
281   if (citr == _presentations.end()) {
282     std::cerr << "removePresentation(): presentation not found!!" << std::endl;
283     return false;
284   }
285   MEDPresentation* presentation = (*citr).second;
286   if (presentation)
287     delete presentation;
288   _presentations.erase(presentationID);
289
290   STDLOG("Presentation " << presentationID << " has been removed.");
291   return true;
292 }
293
294 CORBA::Boolean
295 MEDPresentationManager_i::activateView(MEDPresentation::TypeID presentationID)
296 {
297   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
298   if (citr == _presentations.end()) {
299     std::cerr << "activateView(): presentation not found!!" << std::endl;
300     return false;
301   }
302   MEDPresentation* presentation = (*citr).second;
303
304   if (!presentation->activateView())
305     {
306       // The view has been recreated - transfer all presentations that were in the deleted view to this new one
307       int viewId = presentation->getPyViewID();
308       std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator citr = _presentations.begin();
309       for (; citr != _presentations.end(); ++citr)
310         {
311           if(citr->second->getPyViewID() == viewId)
312             citr->second->recreateViewSetup();
313         }
314     }
315   _activeViewPythonId = presentation->getPyViewID();
316   STDLOG("Activated view " << _activeViewPythonId);
317   return true;
318 }
319
320 CORBA::Long
321 MEDPresentationManager_i::getActiveViewPythonId()
322 {
323   //TODO: should be more elaborate to re-identify the active ParaView view when another
324   //mechanism than MED module has activated another view.
325   // Idea: 1/ call Python to current active view
326   //       2/ compare with all id(__viewX) from currently existing presentations
327   return _activeViewPythonId;
328 }
329
330
331 //MEDCALC::ViewModeType
332 //MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID)
333 //{
334 //  MEDPresentation* pres = _getPresentation(presentationID);
335 //  if (pres) {
336 //    return pres->getViewMode();
337 //  } else {
338 //    std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl;
339 //    return MEDCALC::VIEW_MODE_DEFAULT;
340 //  }
341 //}
342
343 char *
344 MEDPresentationManager_i::getParavisDump(MEDPresentation::TypeID presentationID)
345 {
346   MEDPresentation* pres = _getPresentation(presentationID);
347   if (pres) {
348     return CORBA::string_dup(pres->paravisDump().c_str());
349   }
350   else
351     throw KERNEL::createSalomeException("getParavisDump(): presentation not found!!");
352 }
353
354 MEDCALC::PresentationsList*
355 MEDPresentationManager_i::getAllPresentations()
356 {
357   MEDCALC::PresentationsList* presList = new MEDCALC::PresentationsList;
358   presList->length(_presentations.size());
359   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator it;
360   int i;
361   for (i = 0, it = _presentations.begin(); it != _presentations.end(); ++it, ++i)
362     (*presList)[i] = it->first;
363   return presList;
364 }
365
366 void
367 MEDPresentationManager_i::cleanUp()
368 {
369   STDLOG("Cleanup");
370   _activeViewPythonId = -1;
371   std::map<MEDPresentation::TypeID, MEDPresentation*>::iterator it;
372   for (it = _presentations.begin(); it != _presentations.end(); ++it)
373     delete(it->second);
374   _presentations.clear();
375 }