]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_TimeAnimation.cxx
Salome HOME
Preferences for VISU module.
[modules/visu.git] / src / VISU_I / VISU_TimeAnimation.cxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_TimeAnimation.cxx
6 //  Author : Vitaly SMETANNIKOV
7 //  Module : VISU
8
9 #include "VISU_TimeAnimation.h"
10
11 #include "VISUConfig.hh"
12
13 #include "VISU_Result_i.hh"
14 #include "VISU_Prs3d_i.hh"
15 #include "VISU_Mesh_i.hh"
16 #include "VISU_ScalarMap_i.hh"
17 #include "VISU_IsoSurfaces_i.hh"
18 #include "VISU_DeformedShape_i.hh"
19 #include "VISU_CutPlanes_i.hh"
20 #include "VISU_Plot3D_i.hh"
21 #include "VISU_CutLines_i.hh"
22 #include "VISU_Vectors_i.hh"
23 #include "VISU_StreamLines_i.hh"
24 #include "VISU_ViewManager_i.hh"
25 #include "VISU_View_i.hh"
26
27 #include "VISU_ScalarBarActor.hxx"
28 #include "VISU_Actor.h"
29
30 #include "SalomeApp_Study.h"
31
32 #include "SVTK_ViewWindow.h"
33
34 #include "SALOME_Event.hxx"
35
36 #include "SUIT_ResourceMgr.h"
37 #include "SUIT_Application.h"
38 #include "SUIT_Session.h"
39 #include "SUIT_Study.h"
40
41 #include "SALOMEDSClient_AttributeComment.hxx"
42 #include "SALOMEDSClient_AttributeName.hxx"
43
44 #include <qpixmap.h>
45
46 using namespace std;
47
48
49 //************************************************************************
50 VISU_TimeAnimation::VISU_TimeAnimation (_PTR(Study) theStudy,
51                                         VISU::View3D_ptr theView3D)
52 {
53   myStudy = theStudy;
54   myIsActive = false;
55   myFrame = 0;
56   mySpeed = 1;
57   myProportional = false;
58   myView = 0;
59
60   if (!CORBA::is_nil(theView3D)) {
61     VISU::View3D_i* pView = dynamic_cast<VISU::View3D_i*>(GetServant(theView3D).in());
62     //QAD_StudyFrame* aStudyFrame = pView->GetStudyFrame();
63     //myView = VISU::GetViewFrame(aStudyFrame);
64     SUIT_ViewWindow* aVW = pView->myViewWindow;
65     myView = VISU::GetViewWindow(aVW);
66   }
67
68   myMaxVal = 0;
69   myMinVal = 0;
70   myTimeMin = 0;
71   myTimeMax = 0;
72   myLastError = "";
73   myCycling = false;
74
75   myAnimEntry = "";
76 }
77
78
79 //************************************************************************
80 VISU_TimeAnimation::~VISU_TimeAnimation()
81 {
82   for (int i = 0; i < getNbFields(); i++) {
83     clearData(myFieldsLst[i]);
84   }
85 }
86
87
88 //************************************************************************
89 void VISU_TimeAnimation::addField (_PTR(SObject) theField)
90 {
91   FieldData aNewData;
92   aNewData.myField = theField;
93   aNewData.myNbFrames = 0;
94   aNewData.myPrsType = VISU::TSCALARMAP;
95   aNewData.myOffset[0] = aNewData.myOffset[1] = aNewData.myOffset[2] = 0;
96   VISU::Storable::TRestoringMap aMap = getMapOfValue(aNewData.myField);
97   aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
98   myFieldsLst.append(aNewData);
99
100   //find Min/Max timestamps
101   if ((myTimeMin == 0) && (myTimeMax == 0)) {
102     _PTR(ChildIterator) anIter = myStudy->NewChildIterator(theField);
103     anIter->Next(); // First is reference on support
104     myTimeMin = getTimeValue(anIter->Value());
105     for(; anIter->More(); anIter->Next()) {
106       myTimeMax = getTimeValue(anIter->Value());
107     }
108   }
109 }
110
111 //************************************************************************
112 void VISU_TimeAnimation::addField (SALOMEDS::SObject_ptr theField)
113 {
114   FieldData aNewData;
115   _PTR(SObject) aField = VISU::GetClientSObject(theField, myStudy);
116   aNewData.myField = aField;
117   aNewData.myNbFrames = 0;
118   aNewData.myPrsType = VISU::TSCALARMAP;
119   VISU::Storable::TRestoringMap aMap = getMapOfValue(aNewData.myField);
120   aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
121   myFieldsLst.append(aNewData);
122
123   //find Min/Max timestamps
124   if ((myTimeMin == 0) && (myTimeMax == 0)) {
125     _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aField);
126     anIter->Next(); // First is reference on support
127     myTimeMin = getTimeValue(anIter->Value());
128     for(; anIter->More(); anIter->Next()) {
129       myTimeMax = getTimeValue(anIter->Value());
130     }
131   }
132 }
133
134
135 //************************************************************************
136 void VISU_TimeAnimation::clearData(FieldData& theData) {
137   theData.myTiming.clear();
138   vtkRenderer* aRen = myView->getRenderer();
139   if (!theData.myActors.empty()) {
140     for (int i = 0, iEnd = theData.myActors.size(); i < iEnd; i++) {
141       if (theData.myActors[i] != 0) {
142         theData.myActors[i]->RemoveFromRender(aRen);
143         theData.myActors[i]->Delete();
144       }
145     }
146     theData.myActors.clear();
147   }
148   if (!theData.myPrs.empty()) {
149     for (int i = 0, iEnd = theData.myPrs.size(); i < iEnd; i++)
150       if (theData.myPrs[i] != 0) {
151         theData.myPrs[i]->_remove_ref();
152       }
153     theData.myPrs.clear();
154   }
155   theData.myNbFrames = 0;
156   myView->update();
157 }
158
159
160 //************************************************************************
161 void VISU_TimeAnimation::generatePresentations(CORBA::Long theFieldNum) {
162   FieldData& aData = myFieldsLst[theFieldNum];
163
164   // Delete previous presentations
165   clearData(aData);
166
167   VISU::Result_i* pResult = createPresent(aData.myField);
168   VISU::Storable::TRestoringMap aMap = getMapOfValue(aData.myField);
169   aData.myNbFrames = aData.myNbTimes;
170     //VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
171
172   aData.myPrs.resize(aData.myNbTimes,NULL);
173   aData.myTiming.resize(aData.myNbTimes);
174
175   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aData.myField);
176   _PTR(SObject) aTimeStamp;
177   anIter->Next(); // First is reference on support
178   long i = 0;
179   double aMin = VTK_LARGE_FLOAT, aMax = -VTK_LARGE_FLOAT;
180   for (; anIter->More(); anIter->Next()) {
181     if (i == aData.myNbTimes) {
182       MESSAGE("There are extra timestamps in field");
183       return;
184     }
185     aTimeStamp = anIter->Value();
186     if (!aTimeStamp) continue;
187
188     aData.myTiming[i] = getTimeValue(aTimeStamp);
189     if (isRangeDefined()) {
190       if (aData.myTiming[i] < myMinVal) continue;
191       if (aData.myTiming[i] > myMaxVal) break;
192     }
193
194     VISU::Storable::TRestoringMap aTimeMap = getMapOfValue(aTimeStamp);
195     QString aMeshName = VISU::Storable::FindValue(aTimeMap,"myMeshName");
196     VISU::Entity anEntity = (VISU::Entity) VISU::Storable::FindValue(aTimeMap,"myEntityId").toInt();
197     QString aFieldName = VISU::Storable::FindValue(aTimeMap,"myFieldName");
198     int aTimeStampId = VISU::Storable::FindValue(aTimeMap,"myTimeStampId").toInt();
199
200     switch (aData.myPrsType) {
201     case VISU::TSCALARMAP: // ScalarMap
202       {
203         VISU::ScalarMap_i* aPresent = new VISU::ScalarMap_i(pResult, false);
204         aPresent->Create(aMeshName.latin1(), anEntity,
205                          aFieldName.latin1(), aTimeStampId);
206         //VISU::ScalarMap_var aTmp = aPresent->_this();
207         //aPresent->_remove_ref();
208         aData.myPrs[i] = aPresent;
209       }
210       break;
211
212     case VISU::TISOSURFACE: // Iso Surfaces
213       {
214         VISU::IsoSurfaces_i* aPresent = new VISU::IsoSurfaces_i(pResult, false);
215         aPresent->Create(aMeshName.latin1(), anEntity,
216                          aFieldName.latin1(), aTimeStampId);
217         //VISU::IsoSurfaces_var aTmp = aPresent->_this();
218         //aPresent->_remove_ref();
219         aData.myPrs[i] = aPresent;
220       }
221       break;
222
223     case VISU::TCUTPLANES: // Cut Planes
224       {
225         VISU::CutPlanes_i* aPresent = new VISU::CutPlanes_i(pResult, false);
226         aPresent->Create(aMeshName.latin1(), anEntity,
227                          aFieldName.latin1(), aTimeStampId);
228         //VISU::CutPlanes_var aTmp = aPresent->_this();
229         //aPresent->_remove_ref();
230         aData.myPrs[i] = aPresent;
231       }
232       break;
233
234     case VISU::TPLOT3D: // Cut Planes
235       {
236         VISU::Plot3D_i* aPresent = new VISU::Plot3D_i (pResult, false);
237         aPresent->Create(aMeshName.latin1(), anEntity,
238                          aFieldName.latin1(), aTimeStampId);
239         aData.myPrs[i] = aPresent;
240       }
241       break;
242
243     case VISU::TDEFORMEDSHAPE: // Deformed Shape
244       {
245         VISU::DeformedShape_i* aPresent = new VISU::DeformedShape_i(pResult, false);
246         aPresent->Create(aMeshName.latin1(), anEntity,
247                          aFieldName.latin1(), aTimeStampId);
248         //VISU::DeformedShape_var aTmp = aPresent->_this();
249         //aPresent->_remove_ref();
250         aData.myPrs[i] = aPresent;
251       }
252       break;
253
254     case VISU::TVECTORS: // Vectors
255       {
256         VISU::Vectors_i* aPresent = new VISU::Vectors_i(pResult, false);
257         aPresent->Create(aMeshName.latin1(), anEntity,
258                          aFieldName.latin1(), aTimeStampId);
259         //VISU::Vectors_var aTmp = aPresent->_this();
260         //aPresent->_remove_ref();
261         aData.myPrs[i] = aPresent;
262       }
263       break;
264
265     case VISU::TSTREAMLINES: // Stream Lines
266       {
267         VISU::StreamLines_i* aPresent = new VISU::StreamLines_i(pResult, false);
268         aPresent->Create(aMeshName.latin1(), anEntity,
269                          aFieldName.latin1(), aTimeStampId);
270         //VISU::StreamLines_var aTmp = aPresent->_this();
271         //aPresent->_remove_ref();
272         aData.myPrs[i] = aPresent;
273       }
274       break;
275     default:
276       MESSAGE("Not implemented for this presentation type: " << aData.myPrsType);
277       return;
278     }
279     if (aData.myPrs[i]->GetMin() < aMin) aMin = aData.myPrs[i]->GetMin();
280     if (aData.myPrs[i]->GetMax() > aMax) aMax = aData.myPrs[i]->GetMax();
281     i++;
282   }
283   aData.myNbFrames = i;
284
285   int rangeType = VISU::GetResourceMgr()->integerValue("VISU" , "scalar_range_type", 0);
286   if ( rangeType != 1 ) {
287     for (i = 0; i < aData.myNbFrames; i++) {
288       aData.myPrs[i]->SetRange(aMin, aMax);
289       aData.myPrs[i]->SetOffset(aData.myOffset);
290     }
291     if (aData.myPrsType == VISU::TISOSURFACE)
292       for (i = 0; i < aData.myNbFrames; i++)
293         if (VISU::IsoSurfaces_i* aPrs = dynamic_cast<VISU::IsoSurfaces_i*>(aData.myPrs[i]))
294            aPrs->SetSubRange(aMin, aMax);
295   }
296 }
297
298
299 //************************************************************************
300 CORBA::Boolean VISU_TimeAnimation::generateFrames() {
301   if (!myView) {
302     MESSAGE("Viewer is not defined for animation");
303     return false;
304   }
305
306   myLastError = QString("Frame(s) for ");
307   bool aNoError = true;
308
309   clearView();
310
311   for (int i = 0; i < getNbFields(); i++) {
312     FieldData& aData = myFieldsLst[i];
313     aData.myActors.resize(aData.myNbFrames,NULL);
314     for (long j = 0; j < aData.myNbFrames; j++) {
315       VISU_Actor* aActor = NULL;
316       try{
317         aData.myPrs[j]->SetOffset(aData.myOffset);
318         aActor = aData.myPrs[j]->CreateActor();
319         myView->AddActor(aActor);
320         if(j == 0)
321           aActor->VisibilityOn();
322         else
323           aActor->VisibilityOff();
324       }catch(...){ //catch(std::runtime_error& exc){
325         aNoError = false;
326         myLastError += QString("%1 ").arg(aData.myTiming[j]);
327       }
328       aData.myActors[j] = aActor;
329     }
330   }
331   myFrame = 0;
332   myLastError += QString(" timestamp(s) cannot be created.");
333   emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
334   myView->Repaint();
335   return aNoError;
336 }
337
338 //************************************************************************
339 void VISU_TimeAnimation::clearView() {
340   if (!myView) {
341     MESSAGE("Viewer is not defined for animation");
342     return;
343   }
344   vtkRenderer* aRen = myView->getRenderer();
345   for (int i = 0; i < getNbFields(); i++) {
346     FieldData& aData = myFieldsLst[i];
347     if (!aData.myActors.empty()) {
348       for (int i = 0, iEnd = aData.myActors.size(); i < iEnd; i++) {
349         if (aData.myActors[i] != 0) {
350           aData.myActors[i]->RemoveFromRender(aRen);
351           aData.myActors[i]->Delete();
352         }
353       }
354       aData.myActors.clear();
355     }
356   }
357   VISU::RepaintView(myView);
358 }
359
360 //************************************************************************
361 void VISU_TimeAnimation::stopAnimation() {
362   myIsActive = false;
363 }
364
365 //************************************************************************
366 void VISU_TimeAnimation::startAnimation() {
367   if (!myIsActive) {
368     myIsActive = true;
369     QThread::start();
370   }
371 }
372
373 //************************************************************************
374 void VISU_TimeAnimation::nextFrame() {
375   stopAnimation();
376   if (myFrame < (myFieldsLst[0].myNbFrames-1)) {
377     int i;
378     for (i = 0; i < getNbFields(); i++)
379       if (myFieldsLst[i].myActors[myFrame] != 0)
380         myFieldsLst[i].myActors[myFrame]->VisibilityOff();
381
382     myFrame++;
383     for (i = 0; i < getNbFields(); i++)
384       if (myFieldsLst[i].myActors[myFrame] != 0)
385         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
386
387     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
388     myView->Repaint();
389   }
390 }
391
392 //************************************************************************
393 void VISU_TimeAnimation::prevFrame() {
394   stopAnimation();
395   if (myFrame > 0) {
396     int i;
397     for (i = 0; i < getNbFields(); i++)
398       if (myFieldsLst[i].myActors[myFrame] != 0)
399         myFieldsLst[i].myActors[myFrame]->VisibilityOff();
400
401     myFrame--;
402     for (i = 0; i < getNbFields(); i++)
403       if (myFieldsLst[i].myActors[myFrame] != 0)
404         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
405
406     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
407     myView->Repaint();
408   }
409 }
410
411 //************************************************************************
412 void VISU_TimeAnimation::firstFrame() {
413   stopAnimation();
414   int i;
415   for (i = 0; i < getNbFields(); i++)
416     if(!myFieldsLst[i].myActors.empty())
417       if (myFieldsLst[i].myActors[myFrame] != 0)
418         myFieldsLst[i].myActors[myFrame]->VisibilityOff();
419   myFrame = 0;
420   for (i = 0; i < getNbFields(); i++)
421     if(!myFieldsLst[i].myActors.empty())
422       if (myFieldsLst[i].myActors[myFrame] != 0)
423         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
424   if(!myFieldsLst[0].myTiming.empty()){
425     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
426     myView->Repaint();
427   }
428 }
429
430 //************************************************************************
431 void VISU_TimeAnimation::lastFrame() {
432   stopAnimation();
433   int i;
434   for (i = 0; i < getNbFields(); i++)
435    if (myFieldsLst[i].myActors[myFrame] != 0)
436       myFieldsLst[i].myActors[myFrame]->VisibilityOff();
437
438   myFrame = myFieldsLst[0].myNbFrames-1;
439   for (i = 0; i < getNbFields(); i++)
440     if (myFieldsLst[i].myActors[myFrame] != 0)
441       myFieldsLst[i].myActors[myFrame]->VisibilityOn();
442
443   emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
444   myView->Repaint();
445 }
446
447
448 //************************************************************************
449 // For Batchmode using
450 void VISU_TimeAnimation::gotoFrame(CORBA::Long theFrame) {
451   if ((theFrame < 0) || (theFrame > (getNbFrames()-1)))
452     return;
453   stopAnimation();
454   qApp->lock();
455   qApp->syncX();
456   int i;
457   for (i = 0; i < getNbFields(); i++)
458     if (myFieldsLst[i].myActors[myFrame] != 0)
459       myFieldsLst[i].myActors[myFrame]->VisibilityOff();
460
461   myFrame = theFrame;
462   for (i = 0; i < getNbFields(); i++)
463     if (myFieldsLst[i].myActors[myFrame] != 0)
464       myFieldsLst[i].myActors[myFrame]->VisibilityOn();
465
466   emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
467   myView->Repaint();
468   qApp->flushX();
469   qApp->processEvents(3);
470   qApp->unlock();
471 }
472
473
474 //************************************************************************
475 VISU::ScalarMap_ptr VISU_TimeAnimation::getPresentation(CORBA::Long theField, CORBA::Long theFrame) {
476   if ((theField > getNbFields()) || (theField < 0))
477     return VISU::ScalarMap::_nil();
478   if ((theFrame < 0) || (theFrame > (myFieldsLst[theField].myNbFrames - 1)))
479     return VISU::ScalarMap::_nil();
480   return myFieldsLst[theField].myPrs[theFrame]->_this();
481 }
482
483
484 //************************************************************************
485 CORBA::Long VISU_TimeAnimation::getNbFrames() {
486   return (getNbFields() > 0)? myFieldsLst[0].myNbFrames : 0;
487 }
488
489
490 //************************************************************************
491 void VISU_TimeAnimation::run()
492 {
493   if (!myView) {
494     MESSAGE("Viewer is not defined for animation");
495     return;
496   }
497   double k = 1;
498   bool   isDumping = !myDumpPath.isEmpty();
499   double aOneVal = 1;
500   if (myFieldsLst[0].myNbFrames > 2)
501     aOneVal = myFieldsLst[0].myTiming[1] - myFieldsLst[0].myTiming[0];
502   qApp->lock();
503   while (myIsActive) {
504     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
505     for (int i = 0; i < getNbFields(); i++) {
506       FieldData& aData = myFieldsLst[i];
507       if (myFrame > 0) {
508         if (aData.myActors[myFrame-1] != 0)
509           aData.myActors[myFrame-1]->VisibilityOff();
510       } else {
511         if (aData.myActors[aData.myNbFrames-1] != 0)
512           aData.myActors[aData.myNbFrames-1]->VisibilityOff();
513       }
514       if (aData.myActors[myFrame] != 0) {
515         aData.myActors[myFrame]->VisibilityOn();
516       }
517     }
518     myView->Repaint(false);
519
520     int delay = 100;
521     if (isDumping) {
522       QPixmap px = QPixmap::grabWindow(myView->winId());
523       QString aFile(myDumpPath);
524       QString aName = QString("%1").arg(myFieldsLst[0].myTiming[myFrame]);
525       int aPos = -1;
526       while ((aPos = aName.find(".")) > -1 )
527         aName.replace(aPos, 1, "_");
528       aFile += aName;
529       aFile += ".jpeg";
530       px.save(aFile, "JPEG");
531     } else {
532       k = 1;
533       if (myProportional) {
534         switch (myFrame) {
535         case 0:
536           break;
537         case 1:
538           if (myFieldsLst[0].myNbFrames > 2)
539             k = (myFieldsLst[0].myTiming[myFrame+1] -
540                  myFieldsLst[0].myTiming[myFrame]) / aOneVal;
541           break;
542         default:
543           if (myFrame < (myFieldsLst[0].myNbFrames - 1))
544             k = (myFieldsLst[0].myTiming[myFrame+1] -
545                  myFieldsLst[0].myTiming[myFrame]) / aOneVal;
546         }
547       }
548       delay = (int)(1000. * k / mySpeed);
549     }
550     qApp->unlock();
551     msleep(delay);
552     qApp->lock();
553
554     if (!myIsActive) break;
555
556     myFrame++;
557     if (myFrame == myFieldsLst[0].myNbFrames) {
558       if (!myCycling) {
559         myIsActive = false;
560         myFrame--;
561         break;
562       } else
563         myFrame = 0;
564     }
565   }
566   emit stopped();
567   qApp->unlock();
568   QThread::exit();
569 }
570
571 //************************************************************************
572 VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
573 {
574   _PTR(SObject) aSObj = theField->GetFather();
575   aSObj = aSObj->GetFather();
576   aSObj = aSObj->GetFather();
577   CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
578   if (CORBA::is_nil(anObject)) return NULL;
579   return dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
580 }
581
582 //************************************************************************
583 VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (_PTR(SObject) theSObject)
584 {
585   VISU::Storable::TRestoringMap aMap;
586   if (theSObject) {
587     _PTR(GenericAttribute) anAttr;
588     if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
589       _PTR(AttributeComment) aComment (anAttr);
590       std::string aString = aComment->Value();
591       QString strIn (aString.c_str());
592       VISU::Storable::StrToMap(strIn, aMap);
593     }
594   }
595   return aMap;
596 }
597
598 //************************************************************************
599 double VISU_TimeAnimation::getTimeValue (_PTR(SObject) theTimeStamp)
600 {
601   _PTR(GenericAttribute) anAttr;
602   if (theTimeStamp->FindAttribute(anAttr, "AttributeName")) {
603     _PTR(AttributeName) aName (anAttr);
604     QString aNameString (aName->Value().c_str());
605     int time_len = aNameString.find(',');
606     if (time_len > -1)
607       return aNameString.left(time_len).toDouble();
608     else
609       return aNameString.toDouble();
610   }
611   return -1.0;
612 }
613
614 //************************************************************************
615 void VISU_TimeAnimation::setSpeed(CORBA::Long theSpeed)
616 {
617   mySpeed = (theSpeed<1)? 1 : theSpeed;
618 }
619
620 //************************************************************************
621 int VISU_TimeAnimation::myNBAnimations = 0;
622 QString VISU_TimeAnimation::GenerateName()
623 {
624   return VISU::GenerateName("Animation", myNBAnimations++);
625 }
626
627 //************************************************************************
628 std::string GetPresentationComment (VISU::VISUType thePrsType)
629 {
630   std::string aPrsCmt;
631   switch (thePrsType) {
632   case VISU::TSCALARMAP:
633     aPrsCmt = VISU::ScalarMap_i::myComment;
634     break;
635   case VISU::TISOSURFACE:
636     aPrsCmt = VISU::IsoSurfaces_i::myComment;
637     break;
638   case VISU::TCUTPLANES:
639     aPrsCmt = VISU::CutPlanes_i::myComment;
640     break;
641   case VISU::TPLOT3D:
642     aPrsCmt = VISU::Plot3D_i::myComment;
643     break;
644   case VISU::TDEFORMEDSHAPE:
645     aPrsCmt = VISU::DeformedShape_i::myComment;
646     break;
647   case VISU::TVECTORS:
648     aPrsCmt = VISU::Vectors_i::myComment;
649     break;
650   case VISU::TSTREAMLINES:
651     aPrsCmt = VISU::StreamLines_i::myComment;
652     break;
653   default:
654     aPrsCmt = "Unknown presentation";
655     break;
656   }
657   return aPrsCmt;
658 }
659
660 //************************************************************************
661 SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
662 {
663   if (myStudy->GetProperties()->IsLocked())
664     return SALOMEDS::SObject::_nil();
665
666   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
667   aStudyBuilder->NewCommand();  // There is a transaction
668   _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
669   std::string aSComponentEntry = aSComponent->GetID();
670
671   QString aComment;
672   aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
673                    VISU::TANIMATION,myMinVal,myMaxVal);
674
675   string anEntry = VISU::CreateAttributes(myStudy,aSComponentEntry.c_str(),"","",
676                                           GenerateName(),"",aComment,true);
677   myAnimEntry = anEntry.c_str();
678   _PTR(SObject) aAnimSObject = myStudy->FindObjectID(anEntry.c_str());
679
680   for (int i = 0; i < getNbFields(); i++) {
681     FieldData& aData = myFieldsLst[i];
682     _PTR(SObject) newObj = aStudyBuilder->NewObject(aAnimSObject);
683     aStudyBuilder->Addreference(newObj, aData.myField);
684     if (aData.myPrs.empty()) {
685       VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
686                              GetPresentationComment(aData.myPrsType).c_str(),"","",true);
687     } else {
688       ostringstream strOut;
689       aData.myPrs[0]->ToStream(strOut);
690       string aPrsComment = strOut.str();
691       VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
692                              aData.myPrs[0]->GetComment(),"",aPrsComment.c_str(),true);
693     }
694   }
695   aStudyBuilder->CommitCommand();
696
697   return VISU::GetSObject(aAnimSObject);
698 }
699
700 //************************************************************************
701 void VISU_TimeAnimation::saveAnimation()
702 {
703   if (myStudy->GetProperties()->IsLocked())  return;
704   if (myAnimEntry.isEmpty()) return;
705
706   _PTR(SObject) aAnimSObject = myStudy->FindObjectID(myAnimEntry.latin1());
707   if (!aAnimSObject) return;
708
709   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
710   aStudyBuilder->NewCommand();  // There is a transaction
711   _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
712   std::string aSComponentEntry = aSComponent->GetID();
713
714   QString aComment;
715   aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
716                    VISU::TANIMATION,myMinVal,myMaxVal);
717
718   _PTR(GenericAttribute) anAttr;
719   anAttr = aStudyBuilder->FindOrCreateAttribute(aAnimSObject, "AttributeComment");
720   _PTR(AttributeComment) aCmnt (anAttr);
721   aCmnt->SetValue(aComment.latin1());
722
723   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
724   int i;
725   for (i = 0, anIter->Init(); anIter->More(); anIter->Next(), i++) {
726     FieldData& aData = myFieldsLst[i];
727
728     _PTR(SObject) aRefObj = anIter->Value();
729     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
730     anPrsIter->Init();
731
732     string aPrsComment, aPrsNameTxt;
733     if (aData.myPrs.empty()) {
734       aPrsComment = "";
735       aPrsNameTxt = GetPresentationComment(aData.myPrsType);
736     } else {
737       ostringstream strOut;
738       aData.myPrs[0]->ToStream(strOut);
739       aPrsComment = strOut.str();
740
741       aPrsNameTxt = aData.myPrs[0]->GetComment();
742     }
743
744     if (anPrsIter->More()) {
745       _PTR(SObject) aPrsObj = anPrsIter->Value();
746       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeComment");
747       aCmnt = _PTR(AttributeComment)(anAttr);
748       aCmnt->SetValue(aPrsComment.c_str());
749
750       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeName");
751       _PTR(AttributeName) aPrsName (anAttr);
752       aPrsName->SetValue(aPrsNameTxt);
753
754     } else {
755       VISU::CreateAttributes(myStudy, aRefObj->GetID().c_str(),"","",
756                              aPrsNameTxt.c_str(),"",aPrsComment.c_str(),true);
757     }
758   }
759   aStudyBuilder->CommitCommand();
760 }
761
762 //************************************************************************
763 void VISU_TimeAnimation::restoreFromStudy(SALOMEDS::SObject_ptr theField)
764 {
765   _PTR(SObject) aAnimSObject = VISU::GetClientSObject(theField, myStudy);
766   restoreFromStudy(aAnimSObject);
767 }
768
769 void VISU_TimeAnimation::restoreFromStudy(_PTR(SObject) theField)
770 {
771   _PTR(SObject) aAnimSObject = theField;
772
773   VISU::Storable::TRestoringMap aMap;
774   _PTR(GenericAttribute) anAttr;
775   if (!aAnimSObject->FindAttribute(anAttr, "AttributeComment")) return;
776
777   _PTR(AttributeComment) aComment (anAttr);
778   string aComm = aComment->Value();
779   QString strIn (aComm.c_str());
780   VISU::Storable::StrToMap(strIn,aMap);
781   bool isExist;
782
783   myMinVal = VISU::Storable::FindValue(aMap,"myMinVal",&isExist).toDouble();
784   myMaxVal = VISU::Storable::FindValue(aMap,"myMaxVal",&isExist).toDouble();
785
786   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
787   for (anIter->Init(); anIter->More(); anIter->Next()) {
788     _PTR(SObject) aRefObj = anIter->Value();
789     _PTR(SObject) aFieldObj;
790     if (!aRefObj->ReferencedObject(aFieldObj) ) continue;
791     addField(aFieldObj);
792     FieldData& aData = getFieldData(getNbFields()-1);
793
794     // Get Presentation object
795     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
796     anPrsIter->Init();
797     if (!anPrsIter->More()) continue;
798     _PTR(SObject) aPrsObj = anPrsIter->Value();
799     if (!aPrsObj->FindAttribute(anAttr, "AttributeName")) continue;
800     _PTR(AttributeName) aName (anAttr);
801     string aStr = aName->Value();
802     QString strName (aStr.c_str());
803
804     if (strName == VISU::ScalarMap_i::myComment.c_str())
805       aData.myPrsType = VISU::TSCALARMAP;
806     else if (strName == VISU::IsoSurfaces_i::myComment.c_str())
807       aData.myPrsType = VISU::TISOSURFACE;
808     else if (strName == VISU::CutPlanes_i::myComment.c_str())
809       aData.myPrsType = VISU::TCUTPLANES;
810     else if (strName == VISU::Plot3D_i::myComment.c_str())
811       aData.myPrsType = VISU::TPLOT3D;
812     else if (strName == VISU::DeformedShape_i::myComment.c_str())
813       aData.myPrsType = VISU::TDEFORMEDSHAPE;
814     else if (strName == VISU::Vectors_i::myComment.c_str())
815       aData.myPrsType = VISU::TVECTORS;
816     else if (strName == VISU::StreamLines_i::myComment.c_str())
817       aData.myPrsType = VISU::TSTREAMLINES;
818     else
819       continue;
820     generatePresentations(getNbFields()-1);
821
822     if (!aPrsObj->FindAttribute(anAttr, "AttributeComment")) continue;
823     _PTR(AttributeComment) aPrsComment (anAttr);
824     string aPrsComm = aPrsComment->Value();
825     if (aPrsComm.length() > 0) {
826       QString strPrsIn (aPrsComm.c_str());
827       VISU::Storable::TRestoringMap aPrsMap;
828       VISU::Storable::StrToMap(strPrsIn,aPrsMap);
829
830       aData.myPrs[0]->Restore(aPrsMap);
831     }
832     aData.myPrs[0]->GetOffset(aData.myOffset);
833     for (int i = 1; i < aData.myNbFrames; i++) {
834       aData.myPrs[i]->SameAs(aData.myPrs[0]);
835     }
836   }
837   string aStr = aAnimSObject->GetID();
838   myAnimEntry = aStr.c_str();
839 }
840
841
842 //========================================================================
843 //========================================================================
844 //========================================================================
845 struct TNewAnimationEvent: public SALOME_Event
846 {
847   std::string myStudyName;
848   VISU::View3D_ptr myView3D;
849
850   typedef VISU_TimeAnimation* TResult;
851   TResult myResult;
852
853   TNewAnimationEvent (std::string theStudyName, VISU::View3D_ptr theView3D):
854     myStudyName(theStudyName),
855     myView3D(VISU::View3D::_duplicate(theView3D)),
856     myResult(NULL)
857   {}
858
859   virtual
860   void
861   Execute()
862   {
863     SUIT_Session* aSession = SUIT_Session::session();
864     QPtrList<SUIT_Application> anApplications = aSession->applications();
865     QPtrListIterator<SUIT_Application> anIter (anApplications);
866     while (SUIT_Application* anApp = anIter.current()) {
867       ++anIter;
868       if (SUIT_Study* aSStudy = anApp->activeStudy()) {
869         if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
870           if (_PTR(Study) aCStudy = aStudy->studyDS()) {
871             if (myStudyName == aCStudy->Name()) {
872               myResult = new VISU_TimeAnimation (aCStudy, myView3D);
873               break;
874             }
875           }
876         }
877       }
878     }
879   }
880 };
881
882 VISU_TimeAnimation_i::VISU_TimeAnimation_i (SALOMEDS::Study_ptr theStudy,
883                                             VISU::View3D_ptr theView3D)
884 {
885   std::string aStudyName = theStudy->Name();
886   myAnim = ProcessEvent(new TNewAnimationEvent (aStudyName, theView3D));
887 }
888
889 VISU_TimeAnimation_i::~VISU_TimeAnimation_i()
890 {
891   delete myAnim;
892 }
893
894 void VISU_TimeAnimation_i::addField (SALOMEDS::SObject_ptr theField)
895 {
896   myAnim->addField(theField);
897 }
898
899 CORBA::Boolean VISU_TimeAnimation_i::generateFrames()
900 {
901   return ProcessEvent(new TMemFunEvent<VISU_TimeAnimation,bool>
902                       (myAnim,&VISU_TimeAnimation::generateFrames));
903 }
904
905 void VISU_TimeAnimation_i::generatePresentations (CORBA::Long theFieldNum)
906 {
907   myAnim->generatePresentations(theFieldNum);
908 }
909
910 void VISU_TimeAnimation_i::clearView()
911 {
912   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
913                    (myAnim,&VISU_TimeAnimation::clearView));
914 }
915
916 void VISU_TimeAnimation_i::stopAnimation()
917 {
918   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
919                    (myAnim,&VISU_TimeAnimation::stopAnimation));
920 }
921
922 void VISU_TimeAnimation_i::startAnimation()
923 {
924   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
925                    (myAnim,&VISU_TimeAnimation::startAnimation));
926 }
927
928 void VISU_TimeAnimation_i::nextFrame()
929 {
930   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
931                    (myAnim,&VISU_TimeAnimation::nextFrame));
932 }
933
934 void VISU_TimeAnimation_i::prevFrame()
935 {
936   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
937                    (myAnim,&VISU_TimeAnimation::prevFrame));
938 }
939
940 void VISU_TimeAnimation_i::firstFrame()
941 {
942   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
943                    (myAnim,&VISU_TimeAnimation::firstFrame));
944 }
945
946 void VISU_TimeAnimation_i::lastFrame()
947 {
948   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
949                    (myAnim,&VISU_TimeAnimation::lastFrame));
950 }
951
952 void VISU_TimeAnimation_i::gotoFrame(CORBA::Long theFrame)
953 {
954   ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_TimeAnimation,CORBA::Long>
955                    (myAnim,&VISU_TimeAnimation::gotoFrame,theFrame));
956 }
957
958 CORBA::Long VISU_TimeAnimation_i::getNbFields()
959 {
960   return myAnim->getNbFields();
961 }
962
963 CORBA::Long VISU_TimeAnimation_i::getNbFrames()
964 {
965   return myAnim->getNbFrames();
966 }
967
968 CORBA::Boolean VISU_TimeAnimation_i::isRunning()
969 {
970   return myAnim->isRunning();
971 }
972
973 CORBA::Long VISU_TimeAnimation_i::getCurrentFrame()
974 {
975   return myAnim->getCurrentFrame();
976 }
977
978 VISU::ScalarMap_ptr VISU_TimeAnimation_i::getPresentation
979                     (CORBA::Long theField, CORBA::Long theFrame)
980 {
981   return myAnim->getPresentation(theField,theFrame);
982 }
983
984 void VISU_TimeAnimation_i::setPresentationType (CORBA::Long theFieldNum,
985                                                 VISU::VISUType theType)
986 {
987   myAnim->setPresentationType(theFieldNum,theType);
988 }
989
990 VISU::VISUType VISU_TimeAnimation_i::getPresentationType (CORBA::Long theFieldNum)
991 {
992   return myAnim->getPresentationType(theFieldNum);
993 }
994
995 void VISU_TimeAnimation_i::setSpeed(CORBA::Long theSpeed)
996 {
997   myAnim->setSpeed(theSpeed);
998 }
999
1000 CORBA::Long VISU_TimeAnimation_i::getSpeed()
1001 {
1002   return myAnim->getSpeed();
1003 }
1004
1005 CORBA::Boolean VISU_TimeAnimation_i::isProportional()
1006 {
1007   return myAnim->isProportional();
1008 }
1009
1010 void VISU_TimeAnimation_i::setAnimationRange (CORBA::Double theMin,
1011                                               CORBA::Double theMax)
1012 {
1013   myAnim->setAnimationRange(theMin,theMax);
1014 }
1015
1016 CORBA::Double VISU_TimeAnimation_i::getMinRange()
1017 {
1018   return myAnim->getMinRange();
1019 }
1020
1021 CORBA::Double VISU_TimeAnimation_i::getMaxRange()
1022 {
1023   return myAnim->getMaxRange();
1024 }
1025
1026 CORBA::Boolean VISU_TimeAnimation_i::isRangeDefined()
1027 {
1028   return myAnim->isRangeDefined();
1029 }
1030
1031 void VISU_TimeAnimation_i::dumpTo (const char* thePath)
1032 {
1033   myAnim->dumpTo(thePath);
1034 }
1035
1036 CORBA::Boolean VISU_TimeAnimation_i::isCycling()
1037 {
1038   return myAnim->isCycling();
1039 }
1040
1041 CORBA::Double VISU_TimeAnimation_i::getMinTime()
1042 {
1043   return myAnim->getMinTime();
1044 }
1045
1046 CORBA::Double VISU_TimeAnimation_i::getMaxTime()
1047 {
1048   return myAnim->getMaxTime();
1049 }
1050
1051 void VISU_TimeAnimation_i::setProportional (CORBA::Boolean theProp)
1052 {
1053   myAnim->setProportional(theProp);
1054 }
1055
1056 void VISU_TimeAnimation_i::setCycling (CORBA::Boolean theCycle)
1057 {
1058   myAnim->setCycling(theCycle);
1059 }
1060
1061 SALOMEDS::SObject_ptr VISU_TimeAnimation_i::publishInStudy()
1062 {
1063   return myAnim->publishInStudy();
1064 }
1065
1066 void VISU_TimeAnimation_i::restoreFromStudy(SALOMEDS::SObject_ptr theObj)
1067 {
1068   myAnim->restoreFromStudy(theObj);
1069 }
1070
1071 CORBA::Boolean VISU_TimeAnimation_i::isSavedInStudy()
1072 {
1073   return myAnim->isSavedInStudy();
1074 }
1075
1076 void VISU_TimeAnimation_i::saveAnimation()
1077 {
1078   myAnim->saveAnimation();
1079 }