Salome HOME
[MEDCalcl] PointSprite and VectorField presentations.
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationManager_i.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
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   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
77   if (citr == _presentations.end())
78     return NULL;
79   return (*citr).second;
80 }
81
82 void
83 MEDPresentationManager_i::setPresentationStringProperty(MEDPresentation::TypeID presentationID, const char* propName, const char* propValue)
84 {
85   MEDPresentation* pres = _getPresentation(presentationID);
86   if (pres)
87     pres->setStringProperty(propName, propValue);
88   else
89     throw KERNEL::createSalomeException("setPresentationStringProperty(): presentation not found!!");
90 }
91
92 char*
93 MEDPresentationManager_i::getPresentationStringProperty(MEDPresentation::TypeID presentationID, const char* propName)
94 {
95   MEDPresentation* pres = _getPresentation(presentationID);
96   if (pres) {
97     return (char*) pres->getStringProperty(propName).c_str();
98   }
99   else
100     throw KERNEL::createSalomeException("getPresentationStringProperty(): presentation not found!!");
101 }
102
103 void
104 MEDPresentationManager_i::setPresentationIntProperty(MEDPresentation::TypeID presentationID, const char* propName,
105                                                      const CORBA::Long propValue)
106 {
107   MEDPresentation* pres = _getPresentation(presentationID);
108   if (pres)
109     pres->setIntProperty(propName, propValue);
110   else
111     throw KERNEL::createSalomeException("setPresentationIntProperty(): presentation not found!!");
112 }
113
114 CORBA::Long
115 MEDPresentationManager_i::getPresentationIntProperty(MEDPresentation::TypeID presentationID, const char* propName)
116 {
117   MEDPresentation* pres = _getPresentation(presentationID);
118   if (pres) {
119     return (CORBA::Long) pres->getIntProperty(propName);
120   }
121   else
122     throw KERNEL::createSalomeException("getPresentationIntProperty(): presentation not found!!");
123
124 }
125
126 MEDPresentation::TypeID
127 MEDPresentationManager_i::makeMeshView(const MEDCALC::MeshViewParameters& params, const MEDCALC::ViewModeType viewMode)
128 {
129   return _makePresentation<MEDPresentationMeshView>(params, viewMode);
130 }
131
132 MEDPresentation::TypeID
133 MEDPresentationManager_i::makeScalarMap(const MEDCALC::ScalarMapParameters& params, const MEDCALC::ViewModeType viewMode)
134 {
135   return _makePresentation<MEDPresentationScalarMap>(params, viewMode);
136 }
137
138 MEDPresentation::TypeID
139 MEDPresentationManager_i::makeContour(const MEDCALC::ContourParameters& params, const MEDCALC::ViewModeType viewMode)
140 {
141   return _makePresentation<MEDPresentationContour>(params, viewMode);
142 }
143
144 MEDPresentation::TypeID
145 MEDPresentationManager_i::makeVectorField(const MEDCALC::VectorFieldParameters& params, const MEDCALC::ViewModeType viewMode)
146 {
147   return _makePresentation<MEDPresentationVectorField>(params, viewMode);
148 }
149
150 MEDPresentation::TypeID
151 MEDPresentationManager_i::makeSlices(const MEDCALC::SlicesParameters& params, const MEDCALC::ViewModeType viewMode)
152 {
153   return _makePresentation<MEDPresentationSlices>(params, viewMode);
154 }
155 //
156 //MEDPresentation::TypeID
157 //MEDPresentationManager_i::makeDeflectionShape(const MEDCALC::DeflectionShapeParameters& params)
158 //{
159 //  return _makePresentation<MEDPresentationDeflectionShape>(params);
160 //}
161
162 MEDPresentation::TypeID
163 MEDPresentationManager_i::makePointSprite(const MEDCALC::PointSpriteParameters& params, const MEDCALC::ViewModeType viewMode)
164 {
165   return _makePresentation<MEDPresentationPointSprite>(params, viewMode);
166 }
167
168 MEDCALC::MeshViewParameters
169 MEDPresentationManager_i::getMeshViewParameters(MEDPresentation::TypeID presentationID)
170 {
171   MEDCALC::MeshViewParameters p;
172   _getParameters<MEDPresentationMeshView>(presentationID, p);
173   return p;
174 }
175
176
177 MEDCALC::ScalarMapParameters*
178 MEDPresentationManager_i::getScalarMapParameters(MEDPresentation::TypeID presentationID)
179 {
180   MEDCALC::ScalarMapParameters* p = new MEDCALC::ScalarMapParameters();
181   _getParameters<MEDPresentationScalarMap>(presentationID, *p);
182   MEDCALC::ScalarMapParameters_var tmp(p);
183   return tmp._retn();
184 }
185
186 MEDCALC::ContourParameters
187 MEDPresentationManager_i::getContourParameters(MEDPresentation::TypeID presentationID)
188 {
189   MEDCALC::ContourParameters p;
190   _getParameters<MEDPresentationContour>(presentationID, p);
191   return p;
192 }
193
194 MEDCALC::SlicesParameters*
195 MEDPresentationManager_i::getSlicesParameters(MEDPresentation::TypeID presentationID)
196 {
197   MEDCALC::SlicesParameters* p = new MEDCALC::SlicesParameters();
198   _getParameters<MEDPresentationSlices>(presentationID, *p);
199   MEDCALC::SlicesParameters_var tmp(p);
200   return tmp._retn();
201 }
202
203 MEDCALC::VectorFieldParameters*
204 MEDPresentationManager_i::getVectorFieldParameters(MEDPresentation::TypeID presentationID)
205 {
206   MEDCALC::VectorFieldParameters* p = new MEDCALC::VectorFieldParameters();
207   _getParameters<MEDPresentationVectorField>(presentationID, *p);
208   MEDCALC::VectorFieldParameters_var tmp(p);
209   return tmp._retn();
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 void
222 MEDPresentationManager_i::updateMeshView(MEDPresentation::TypeID presentationID, const MEDCALC::MeshViewParameters& params)
223 {
224   return _updatePresentation<MEDPresentationMeshView>(presentationID, params);
225 }
226
227 void
228 MEDPresentationManager_i::updateScalarMap(MEDPresentation::TypeID presentationID, const MEDCALC::ScalarMapParameters& params)
229 {
230   return _updatePresentation<MEDPresentationScalarMap>(presentationID, params);
231 }
232
233 void
234 MEDPresentationManager_i::updateContour(MEDPresentation::TypeID presentationID, const MEDCALC::ContourParameters& params)
235 {
236   return _updatePresentation<MEDPresentationContour>(presentationID, params);
237 }
238
239 void
240 MEDPresentationManager_i::updateVectorField(MEDPresentation::TypeID presentationID, const MEDCALC::VectorFieldParameters& params)
241 {
242   return _updatePresentation<MEDPresentationVectorField>(presentationID, params);
243 }
244
245 void
246 MEDPresentationManager_i::updateSlices(MEDPresentation::TypeID presentationID, const MEDCALC::SlicesParameters& params)
247 {
248   return _updatePresentation<MEDPresentationSlices>(presentationID, params);
249 }
250 //
251 //void
252 //MEDPresentationManager_i::updateDeflectionShape(MEDPresentation::TypeID presentationID, const MEDCALC::DeflectionShapeParameters& params)
253 //{
254 //  return _updatePresentation<MEDPresentationDeflectionShape>(presentationID, params);
255 //}
256
257 void
258 MEDPresentationManager_i::updatePointSprite(MEDPresentation::TypeID presentationID, const MEDCALC::PointSpriteParameters& params)
259 {
260   return _updatePresentation<MEDPresentationPointSprite>(presentationID, params);
261 }
262
263 CORBA::Boolean
264 MEDPresentationManager_i::removePresentation(MEDPresentation::TypeID presentationID)
265 {
266   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
267   if (citr == _presentations.end()) {
268     std::cerr << "removePresentation(): presentation not found!!" << std::endl;
269     return false;
270   }
271   MEDPresentation* presentation = (*citr).second;
272   if (presentation)
273     delete presentation;
274   _presentations.erase(presentationID);
275
276   STDLOG("Presentation " << presentationID << " has been removed.");
277   return true;
278 }
279
280 CORBA::Boolean
281 MEDPresentationManager_i::activateView(MEDPresentation::TypeID presentationID)
282 {
283   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator citr = _presentations.find(presentationID);
284   if (citr == _presentations.end()) {
285     std::cerr << "activateView(): presentation not found!!" << std::endl;
286     return false;
287   }
288   MEDPresentation* presentation = (*citr).second;
289
290   presentation->activateView();
291   _activeViewPythonId = presentation->getPyViewID();
292   STDLOG("Activated view " << _activeViewPythonId);
293   return true;
294 }
295
296 CORBA::Long
297 MEDPresentationManager_i::getActiveViewPythonId()
298 {
299   //TODO: should be more elaborate to re-identify the active ParaView view when another
300   //mechanism than MED module has activated another view.
301   // Idea: 1/ call Python to current active view
302   //       2/ compare with all id(__viewX) from currently existing presentations
303   return _activeViewPythonId;
304 }
305
306
307 //MEDCALC::ViewModeType
308 //MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID)
309 //{
310 //  MEDPresentation* pres = _getPresentation(presentationID);
311 //  if (pres) {
312 //    return pres->getViewMode();
313 //  } else {
314 //    std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl;
315 //    return MEDCALC::VIEW_MODE_DEFAULT;
316 //  }
317 //}
318
319 char*
320 MEDPresentationManager_i::getParavisDump(MEDPresentation::TypeID presentationID)
321 {
322   MEDPresentation* pres = _getPresentation(presentationID);
323   if (pres) {
324     return (char*) pres->paravisDump().c_str();
325   }
326   else
327     throw KERNEL::createSalomeException("getParavisDump(): presentation not found!!");
328 }
329
330 MEDCALC::PresentationsList*
331 MEDPresentationManager_i::getAllPresentations()
332 {
333   MEDCALC::PresentationsList* presList = new MEDCALC::PresentationsList;
334   presList->length(_presentations.size());
335   std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator it;
336   int i;
337   for (i = 0, it = _presentations.begin(); it != _presentations.end(); ++it, ++i)
338     (*presList)[i] = it->first;
339   return presList;
340 }