Salome HOME
6d82f2673c4217b68ee895c4d684602446183029
[modules/visu.git] / src / VISU_I / VISU_TimeAnimation.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : VISU_TimeAnimation.cxx
21 //  Author : Vitaly SMETANNIKOV
22 //  Module : VISU
23
24 #include "VISU_TimeAnimation.h"
25
26 #ifdef WNT
27 #include <windows.h>
28 #include <vfw.h>
29 #include <qmessagebox.h>
30 #endif
31
32 #include "VISUConfig.hh"
33
34 #include "VISU_Result_i.hh"
35 #include "VISU_Prs3d_i.hh"
36 #include "VISU_Mesh_i.hh"
37 #include "VISU_IsoSurfaces_i.hh"
38 #include "VISU_DeformedShape_i.hh"
39 #include "VISU_ScalarMapOnDeformedShape_i.hh"
40 #include "VISU_CutPlanes_i.hh"
41 #include "VISU_Plot3D_i.hh"
42 #include "VISU_CutLines_i.hh"
43 #include "VISU_Vectors_i.hh"
44 #include "VISU_StreamLines_i.hh"
45 #include "VISU_GaussPoints_i.hh"
46 #include "VISU_ViewManager_i.hh"
47 #include "VISU_View_i.hh"
48
49 #include "VISU_ScalarBarActor.hxx"
50 #include "VISU_Actor.h"
51
52 #include "SalomeApp_Study.h"
53
54 #include "SVTK_ViewWindow.h"
55
56 #include "SALOME_Event.hxx"
57
58 #include "SUIT_ResourceMgr.h"
59 #include "SUIT_Application.h"
60 #include "SUIT_Session.h"
61 #include "SUIT_Study.h"
62
63 #include "SALOMEDSClient_AttributeString.hxx"
64 #include "SALOMEDSClient_AttributeName.hxx"
65
66 #include "Utils_ExceptHandlers.hxx"
67
68 #include <qpixmap.h>
69 #include <qimage.h>
70 #include <qstrlist.h>
71 #include <qdir.h>
72
73 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
74 #define NO_CAS_CATCH
75 #endif
76
77 #include <Standard_Failure.hxx>
78
79 #ifdef NO_CAS_CATCH
80 #include <Standard_ErrorHandler.hxx>
81 #endif
82
83
84 //------------------------------------------------------------------------
85 namespace VISU 
86 {
87   //------------------------------------------------------------------------
88   class ExecutionState 
89   {
90     bool myIsActive;
91     QMutex myIsActiveMutex;
92   public:
93     ExecutionState(bool isActive = false)
94       : myIsActive(isActive) {}
95
96     bool IsActive() {
97       bool state;
98       myIsActiveMutex.lock();
99       state = myIsActive;
100       myIsActiveMutex.unlock();
101       return state;
102     }
103     bool SetActive(bool isActive) {
104       bool state;
105       myIsActiveMutex.lock();
106       state = myIsActive;
107       myIsActive = isActive;
108       myIsActiveMutex.unlock();
109       return state;
110     }
111   };
112
113
114   //------------------------------------------------------------------------
115   struct TCompositeMinMaxController : virtual TVTKMinMaxController
116   {
117     typedef ColoredPrs3d_i* TKey;
118     typedef std::map< TKey, VISU::PMinMaxController > TMinMaxContainer;
119     TMinMaxContainer myMinMaxContainer;    
120
121     void
122     AddController(ColoredPrs3d_i* theReference, 
123                   VISU::PMinMaxController theMinMaxController)
124     {
125       myMinMaxContainer[ TKey( theReference ) ] = theMinMaxController;
126     }
127
128     virtual
129     void
130     UpdateReference(ColoredPrs3d_i* theFromPrs3, ColoredPrs3d_i* theToPrs3d)
131     {
132       TMinMaxContainer::iterator anIter = myMinMaxContainer.find( TKey( theFromPrs3 ) );
133       if ( anIter != myMinMaxContainer.end() ) {
134         myMinMaxContainer.erase( anIter );
135         myMinMaxContainer[ TKey( theToPrs3d ) ] = VISU::CreateDefaultMinMaxController( theToPrs3d );      
136       }
137     }
138
139     virtual
140     vtkFloatingPointType
141     GetComponentMin(vtkIdType theCompID)
142     {
143       vtkFloatingPointType aMin = TMinMaxController::GetComponentMin(theCompID);
144       if ( !myMinMaxContainer.empty() ) {
145         TMinMaxContainer::const_iterator anIter = myMinMaxContainer.begin();
146         for(; anIter != myMinMaxContainer.end(); anIter++){
147           VISU::PMinMaxController aMinMaxController = anIter->second;
148           aMin = std::min(aMin, aMinMaxController->GetComponentMin(theCompID));
149         }
150       }
151       return aMin;
152     }
153
154     virtual
155     vtkFloatingPointType
156     GetComponentMax(vtkIdType theCompID)
157     {
158       vtkFloatingPointType aMax = TMinMaxController::GetComponentMax(theCompID);
159       if ( !myMinMaxContainer.empty() ) {
160         TMinMaxContainer::const_iterator anIter = myMinMaxContainer.begin();
161         for(; anIter != myMinMaxContainer.end(); anIter++){
162           VISU::PMinMaxController aMinMaxController = anIter->second;
163           aMax = std::max(aMax, aMinMaxController->GetComponentMax(theCompID));
164         }
165       }
166       return aMax;
167     }
168   };
169
170   typedef SALOME::GenericObjPtr<TCompositeMinMaxController> PCompositeMinMaxController;
171 }
172
173 //------------------------------------------------------------------------
174 VISU_TimeAnimation::VISU_TimeAnimation (_PTR(Study) theStudy,
175                                         VISU::View3D_ptr theView3D)
176 {
177   myStudy = theStudy;
178   myExecutionState = new VISU::ExecutionState(false);
179   myFrame = 0;
180   mySpeed = VISU::GetResourceMgr()->integerValue("VISU", "speed", 1);
181   myProportional = VISU::GetResourceMgr()->booleanValue("VISU", "use_proportional_timing", false);
182   myView = 0;
183
184   if (!CORBA::is_nil(theView3D)) {
185     VISU::View3D_i* pView = dynamic_cast<VISU::View3D_i*>(GetServant(theView3D).in());
186     SUIT_ViewWindow* aVW = pView->GetViewWindow();
187     myView = dynamic_cast<SVTK_ViewWindow*>(aVW);
188     connect( myView, SIGNAL( destroyed() ), this, SLOT( onViewDeleted() ) );
189   }
190
191   myAnimationMode = VISU::Animation::PARALLEL;
192   myTimeMinVal = 0;
193   myTimeMaxVal = 0;
194   myTimeMin = 0;
195   myTimeMax = 0;
196   myLastError = "";
197   myCycling = VISU::GetResourceMgr()->booleanValue("VISU", "cycled_animation", false);
198   myCleaningMemoryAtEachFrame = VISU::GetResourceMgr()->booleanValue("VISU", "clean_memory_at_each_frame", false);
199
200   myAnimEntry = "";
201
202   myDumpPath = "";
203   myAVIMaker = "jpeg2yuv";
204 }
205
206
207 //------------------------------------------------------------------------
208 VISU_TimeAnimation::~VISU_TimeAnimation()
209 {
210   if (!myView) {
211     MESSAGE("Viewer is not defined for animation");
212     return;
213   }
214
215   if (QThread::running() && !QThread::finished()) {
216     //myExecutionState->SetActive(false);
217     stopAnimation();
218     QThread::wait(500);
219     if (QThread::running() && !QThread::finished()) {
220       QThread::terminate();
221     }
222   }
223
224   for (int i = 0; i < getNbFields(); i++) {
225     clearData(myFieldsLst[i]);
226   }
227   clearFieldData();
228
229   delete myExecutionState;
230
231   myDumpPath = "";
232
233   /* Terminates the execution of the thread. 
234    * The thread may or may not be terminated immediately, 
235    * depending on the operating system's scheduling policies. 
236    *  
237    * Use QThread::wait() after terminate() for synchronous termination.
238    *
239    * When the thread is terminated, all threads waiting for the the thread to finish will be woken up.
240    * 
241    * Warning: This function is dangerous, and its use is discouraged. 
242    * The thread can be terminated at any point in its code path. 
243    * Threads can be terminated while modifying data. 
244    * There is no chance for the thread to cleanup after itself, 
245    * unlock any held mutexes, etc. In short, use this function only if absolutely necessary. 
246    */
247   //QThread::wait(100);
248   //QThread::terminate();
249   //QThread::wait(400);
250 }
251
252
253 //------------------------------------------------------------------------
254 bool VISU_TimeAnimation::addField (_PTR(SObject) theField)
255 {
256   if (!theField) return false;
257
258   FieldData aNewData;
259   aNewData.myField = theField;
260   aNewData.myNbFrames = 0;
261   aNewData.myPrsType = VISU::TSCALARMAP;
262   aNewData.myOffset[0] = aNewData.myOffset[1] = aNewData.myOffset[2] = 0;
263   VISU::Storable::TRestoringMap aMap = VISU::Storable::GetStorableMap(aNewData.myField);
264   if(VISU::Storable::FindValue(aMap,"myComment") != "FIELD")
265     return false;
266
267   aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
268
269   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
270     if ( aNewData.myNbTimes < 2 )
271       return false;
272     if ( !myFieldsLst.isEmpty() && myFieldsLst.first().myNbTimes != aNewData.myNbTimes )
273       return false;
274     if ( myFieldsLst.isEmpty() )
275       myFieldsAbsFrames.push_back(aNewData.myNbTimes);
276   }
277   else { // successive animation mode
278     if ( aNewData.myNbTimes < 1 )
279       return false;
280
281     long aNumCompCurr = VISU::Storable::FindValue(aMap, "myNumComponent").toLong();
282     if ( !myFieldsLst.isEmpty() ) {
283       VISU::Storable::TRestoringMap aFMap = VISU::Storable::GetStorableMap(myFieldsLst.first().myField);
284       long aNumComp = VISU::Storable::FindValue(aFMap, "myNumComponent").toLong();
285       if ( aNumCompCurr != aNumComp )
286         return false;
287     }
288
289     if ( !myFieldsLst.isEmpty() )
290       myFieldsAbsFrames.push_back(myFieldsAbsFrames.back() + aNewData.myNbTimes);
291     else
292       myFieldsAbsFrames.push_back(aNewData.myNbTimes);
293   }
294     
295   myFieldsLst.append(aNewData);
296   
297   //find Min/Max timestamps
298   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(theField);
299   anIter->Next(); // First is reference on support
300   if ( myFieldsLst.size() == 1 ) { // the first field
301     myTimeMin = getTimeValue(anIter->Value());
302     myTimeMax = getTimeValue(anIter->Value());
303   }
304   for(; anIter->More(); anIter->Next()) {
305     if ( myTimeMin > getTimeValue(anIter->Value()) )
306       myTimeMin = getTimeValue(anIter->Value());
307     if ( myTimeMax < getTimeValue(anIter->Value()) )
308       myTimeMax = getTimeValue(anIter->Value());
309   }
310   
311   return true;
312 }
313
314 //------------------------------------------------------------------------
315 bool VISU_TimeAnimation::addField (SALOMEDS::SObject_ptr theField)
316 {
317   SALOMEDS::SObject_var theFieldDup = SALOMEDS::SObject::_duplicate(theField);
318   _PTR(SObject) aField = VISU::GetClientSObject(theFieldDup, myStudy);
319   return addField(aField);
320 }
321
322
323 //------------------------------------------------------------------------
324 void VISU_TimeAnimation::_clearData(FieldData& theData) {
325   if (!myView) {
326     MESSAGE("Viewer is not defined for animation");
327     return;
328   }
329   theData.myTiming.clear();
330   vtkRenderer* aRen = myView->getRenderer();
331   if (!theData.myActors.empty()) {
332     for (int i = 0, iEnd = theData.myActors.size(); i < iEnd; i++) {
333       if (theData.myActors[i] != 0) {
334         theData.myActors[i]->RemoveFromRender(aRen);
335       }
336     }
337     theData.myActors.clear();
338   }
339   if (!theData.myPrs.empty()) {
340     for (int i = 0, iEnd = theData.myPrs.size(); i < iEnd; i++)
341       if (theData.myPrs[i] != 0) {
342         theData.myPrs[i]->_remove_ref();
343       }
344     theData.myPrs.clear();
345   }
346   theData.myNbFrames = 0;
347   myView->update();
348 }
349
350 //------------------------------------------------------------------------
351 void VISU_TimeAnimation::clearData(FieldData& theData) {
352   ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_TimeAnimation,FieldData&>
353                    (this,&VISU_TimeAnimation::_clearData,theData));
354 }
355
356 namespace
357 {
358   //------------------------------------------------------------------------
359   template<class TPrs3d>
360   void
361   GeneratePresentations(_PTR(Study) theStudy,
362                         FieldData& theData,
363                         VISU::Result_i* theResult,
364                         bool theIsRangeDefined,
365                         CORBA::Double theTimeMin,
366                         CORBA::Double theTimeMax,
367                         QValueList<long> theSequence)
368   {
369     _PTR(ChildIterator) anIter = theStudy->NewChildIterator(theData.myField);
370     anIter->Next(); // First is reference on support
371
372     long aSequenceLength = theSequence.count();
373     bool isSequenceDefined = aSequenceLength > 0;
374     if (isSequenceDefined)
375       theData.myPrs.resize(aSequenceLength,NULL);
376
377     long aFrameId = 0;
378     long aSequenceIndex = 1;
379     for(; anIter->More(); anIter->Next(), aSequenceIndex++){
380       if (aFrameId == theData.myNbTimes) {
381         MESSAGE("There are extra timestamps in field");
382         return;
383       }
384       _PTR(SObject) aTimeStamp = anIter->Value();
385       if(!aTimeStamp) 
386         continue;
387
388       long aSequenceId = -1;
389
390       theData.myTiming[aFrameId] = VISU_TimeAnimation::getTimeValue(aTimeStamp);
391       if (theIsRangeDefined) {
392         if (theData.myTiming[aFrameId] < theTimeMin) 
393           continue;
394         if (theData.myTiming[aFrameId] > theTimeMax) 
395           break;
396       }
397       else if (isSequenceDefined) {
398         aSequenceId = theSequence.findIndex( aSequenceIndex );
399         if( aSequenceId == -1 )
400           continue;
401       }
402
403       VISU::Storable::TRestoringMap aTimeMap = VISU::Storable::GetStorableMap(aTimeStamp);
404       QString aMeshName = VISU::Storable::FindValue(aTimeMap,"myMeshName");
405       VISU::Entity anEntity = (VISU::Entity) VISU::Storable::FindValue(aTimeMap,"myEntityId").toInt();
406       QString aFieldName = VISU::Storable::FindValue(aTimeMap,"myFieldName");
407       int aTimeStampId = VISU::Storable::FindValue(aTimeMap,"myTimeStampId").toInt();
408       
409       bool anIsCreated = false;
410       TPrs3d* aPresent = new TPrs3d(VISU::ColoredPrs3d_i::EDoNotPublish);
411       aPresent->SetCResult(theResult);
412       aPresent->SetMeshName(aMeshName.latin1());
413       aPresent->SetEntity(anEntity);
414       aPresent->SetFieldName(aFieldName.latin1());
415       aPresent->SetTimeStampNumber(aTimeStampId);
416       try{       
417 #ifdef NO_CAS_CATCH
418         OCC_CATCH_SIGNALS;
419 #endif
420         if(aPresent->Apply(false)){
421           /*
422             if(isSequenceDefined)
423             {
424               theData.myPrs[aSequenceId] = aPresent;
425               aFrameId++;
426             }
427             else
428           */
429           theData.myPrs[aFrameId++] = aPresent;
430           anIsCreated = true;
431         }
432       }catch(Standard_Failure) {
433         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
434         INFOS("Follow signal was occured :\n"<<aFail->GetMessageString());
435       }catch(std::exception& exc){
436         INFOS("Follow exception was occured :\n"<<exc.what());
437       }catch(...){
438         INFOS("Unknown exception was occured!");
439       }
440       if(!anIsCreated)
441         aPresent->_remove_ref();
442     }
443
444     theData.myNbFrames = aFrameId;
445
446     if (theData.myPrsType != VISU::TGAUSSPOINTS) {
447       for(long aFrameId = 0; aFrameId < theData.myNbFrames; aFrameId++) {
448         if (VISU::ScalarMap_i* aPrs = dynamic_cast<VISU::ScalarMap_i*>(theData.myPrs[aFrameId])){
449           aPrs->SetOffset(theData.myOffset);
450         }
451       }
452     }
453   }
454 }
455
456 double getMinFieldsValue( QValueList<FieldData>& theFieldsLst )
457 {
458   // for successive animation mode only
459   double aRes;
460   for (int i = 0; i < theFieldsLst.count(); i++) {
461     if ( theFieldsLst[i].myPrs[0] ) {
462       aRes = theFieldsLst[i].myPrs[0]->GetMin();
463       break;
464     }
465   }
466
467   for (int i = 1; i < theFieldsLst.count() && theFieldsLst[i].myPrs[0]; i++) {
468     if ( aRes > theFieldsLst[i].myPrs[0]->GetMin() )
469       aRes = theFieldsLst[i].myPrs[0]->GetMin();    
470   }
471   return aRes;
472 }
473
474 double getMaxFieldsValue( QValueList<FieldData>& theFieldsLst )
475 {
476   // for successive animation mode only
477   double aRes;
478   for (int i = 0; i < theFieldsLst.count(); i++) {
479     if ( theFieldsLst[i].myPrs[0] ) {
480       aRes = theFieldsLst[i].myPrs[0]->GetMax();
481       break;
482     }
483   }
484
485   for (int i = 1; i < theFieldsLst.count() && theFieldsLst[i].myPrs[0]; i++) {
486     if ( aRes < theFieldsLst[i].myPrs[0]->GetMax() )
487       aRes = theFieldsLst[i].myPrs[0]->GetMax();    
488   }
489   return aRes;
490 }
491
492 void VISU_TimeAnimation::generatePresentations(CORBA::Long theFieldNum) {
493   FieldData& aData = myFieldsLst[theFieldNum];
494
495   // Delete previous presentations
496   clearData(aData);
497
498   VISU::Result_i* aResult = createPresent(aData.myField);
499   VISU::Storable::TRestoringMap aMap = VISU::Storable::GetStorableMap(aData.myField);
500   aData.myNbFrames = aData.myNbTimes;
501   //VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
502
503   aData.myPrs.resize(aData.myNbTimes,NULL);
504   aData.myTiming.resize(aData.myNbTimes);
505
506   QValueList<long> aSequence;
507   if( isSequenceDefined() )
508   {
509     bool ok = getIndicesFromSequence( mySequence, aSequence );
510     if( !ok )
511       return;
512   }
513
514   using namespace VISU;
515   switch (aData.myPrsType) {
516   case VISU::TSCALARMAP:
517     GeneratePresentations<ScalarMap_i>(myStudy,
518                                        aData,
519                                        aResult,
520                                        isRangeDefined(),
521                                        myTimeMinVal,
522                                        myTimeMaxVal,
523                                        aSequence);
524     break;
525   case VISU::TISOSURFACES: // Iso Surfaces
526     GeneratePresentations<IsoSurfaces_i>(myStudy,
527                                          aData,
528                                          aResult,
529                                          isRangeDefined(),
530                                          myTimeMinVal,
531                                          myTimeMaxVal,
532                                          aSequence);
533     break;
534   case VISU::TCUTPLANES: // Cut Planes
535     GeneratePresentations<CutPlanes_i>(myStudy,
536                                        aData,
537                                        aResult,
538                                        isRangeDefined(),
539                                        myTimeMinVal,
540                                        myTimeMaxVal,
541                                        aSequence);
542     break;
543   case VISU::TCUTLINES: // Cut Lines
544     GeneratePresentations<CutLines_i>(myStudy,
545                                       aData,
546                                       aResult,
547                                       isRangeDefined(),
548                                       myTimeMinVal,
549                                       myTimeMaxVal,
550                                       aSequence);
551     break;
552   case VISU::TPLOT3D: // Plot3d
553     GeneratePresentations<Plot3D_i>(myStudy,
554                                     aData,
555                                     aResult,
556                                     isRangeDefined(),
557                                     myTimeMinVal,
558                                     myTimeMaxVal,
559                                     aSequence);
560     break;
561   case VISU::TDEFORMEDSHAPE: // Deformed Shape
562     GeneratePresentations<DeformedShape_i>(myStudy,
563                                            aData,
564                                            aResult,
565                                            isRangeDefined(),
566                                            myTimeMinVal,
567                                            myTimeMaxVal,
568                                            aSequence);
569     break;
570   case VISU::TVECTORS: // Vectors
571     GeneratePresentations<Vectors_i>(myStudy,
572                                      aData,
573                                      aResult,
574                                      isRangeDefined(),
575                                      myTimeMinVal,
576                                      myTimeMaxVal,
577                                      aSequence);
578     break;
579   case VISU::TSTREAMLINES: // Stream Lines
580     GeneratePresentations<StreamLines_i>(myStudy,
581                                          aData,
582                                          aResult,
583                                          isRangeDefined(),
584                                          myTimeMinVal,
585                                          myTimeMaxVal,
586                                          aSequence);
587     break;
588   case VISU::TGAUSSPOINTS: // Gauss Points
589     GeneratePresentations<GaussPoints_i>(myStudy,
590                                          aData,
591                                          aResult,
592                                          isRangeDefined(),
593                                          myTimeMinVal,
594                                          myTimeMaxVal,
595                                          aSequence);
596     break;
597   case VISU::TSCALARMAPONDEFORMEDSHAPE: // Scalar map on deformed shape
598     GeneratePresentations<ScalarMapOnDeformedShape_i>(myStudy,
599                                                       aData,
600                                                       aResult,
601                                                       isRangeDefined(),
602                                                       myTimeMinVal,
603                                                       myTimeMaxVal,
604                                                       aSequence);
605     break;
606   default:
607     MESSAGE("Not implemented for this presentation type: " << aData.myPrsType);
608     return;
609   }
610   
611   if ( myAnimationMode == VISU::Animation::SUCCESSIVE ) { // successive animation mode
612     if ( myFieldsAbsFrames.size() == getNbFields() ) 
613       myFieldsAbsFrames.clear();
614     if ( theFieldNum > 0 )
615       myFieldsAbsFrames.push_back(myFieldsAbsFrames.back() + aData.myNbFrames);      
616     else
617       myFieldsAbsFrames.push_back(aData.myNbFrames);
618
619     if (theFieldNum == getNbFields() - 1) {
620       if ( aData.myPrsType != VISU::TGAUSSPOINTS) {
621
622         // Initialize the MinMax controller
623         VISU::PCompositeMinMaxController aMinMaxController(new VISU::TCompositeMinMaxController());
624         if ( myAnimationMode == VISU::Animation::PARALLEL ) {
625           FieldData& aFieldData = getFieldData(theFieldNum);
626           VISU::ColoredPrs3d_i* aPrs3d = aFieldData.myPrs[0];
627           aMinMaxController->AddController( aPrs3d, VISU::CreateDefaultMinMaxController( aPrs3d ) );
628         } else {
629           for (int aFieldId = 0; aFieldId < getNbFields(); aFieldId++) {
630             FieldData& aFieldData = getFieldData(aFieldId);
631             VISU::ColoredPrs3d_i* aPrs3d = aFieldData.myPrs[0];
632             aMinMaxController->AddController( aPrs3d, VISU::CreateDefaultMinMaxController( aPrs3d ) );
633           }
634         }
635
636         double aMin = getMinFieldsValue(myFieldsLst);
637         double aMax = getMaxFieldsValue(myFieldsLst);
638
639         for (int aFieldId = 0; aFieldId < getNbFields(); aFieldId++) {
640           FieldData& aFieldData = getFieldData(aFieldId);
641           for (long aFrameId = 0; aFrameId < aFieldData.myNbFrames; aFrameId++) {
642             VISU::ColoredPrs3d_i* aPrs3d = aFieldData.myPrs[aFrameId];
643             aPrs3d->SetMinMaxController(aMinMaxController);
644             if (VISU::IsoSurfaces_i* anIsoSurfaces = dynamic_cast<VISU::IsoSurfaces_i*>(aPrs3d))
645               anIsoSurfaces->SetSubRange(aMin, aMax);
646           }
647         }
648       }
649     }
650   }
651 }
652
653
654 //------------------------------------------------------------------------
655 CORBA::Boolean VISU_TimeAnimation::_generateFrames() {
656   if (!myView) {
657     MESSAGE("Viewer is not defined for animation");
658     return false;
659   }
660
661   myLastError = QString("Frame(s) for ");
662   bool aNoError = true;
663
664   clearView();
665
666   for (int i = 0; i < getNbFields(); i++) {
667     FieldData& aData = myFieldsLst[i];
668     aData.myActors.resize(aData.myNbFrames,NULL);
669     for (long j = 0; j < aData.myNbFrames; j++) {
670       VISU_Actor* aActor = NULL;
671       try{
672         aData.myPrs[j]->SetOffset(aData.myOffset);
673         aActor = aData.myPrs[j]->CreateActor();
674         myView->AddActor(aActor);
675         bool condition = ( myAnimationMode == VISU::Animation::PARALLEL ) ? (j == 0) : (j == 0 && i == 0);
676         if(condition)
677           aActor->VisibilityOn();
678         else
679           aActor->VisibilityOff();
680       }catch(...){ //catch(std::runtime_error& exc){
681         aNoError = false;
682         myLastError += QString("%1 ").arg(aData.myTiming[j]);
683       }
684       aData.myActors[j] = aActor;
685     }
686   }
687   myFrame = 0;
688   myLastError += QString(" timestamp(s) cannot be created.");
689   ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
690                                                                             myFrame, myFieldsLst[0].myTiming[myFrame]));
691   myView->Repaint();
692   return aNoError;
693 }
694
695 //------------------------------------------------------------------------
696 CORBA::Boolean VISU_TimeAnimation::generateFrames()
697 {
698   return ProcessEvent(new TMemFunEvent<VISU_TimeAnimation,bool>
699                       (this,&VISU_TimeAnimation::_generateFrames));
700 }
701
702 //------------------------------------------------------------------------
703 void VISU_TimeAnimation::_clearView() {
704   if (!myView) {
705     MESSAGE("Viewer is not defined for animation");
706     return;
707   }
708   vtkRenderer* aRen = myView->getRenderer();
709   for (int i = 0; i < getNbFields(); i++) {
710     FieldData& aData = myFieldsLst[i];
711     if (!aData.myActors.empty()) {
712       for (int i = 0, iEnd = aData.myActors.size(); i < iEnd; i++) {
713         if (aData.myActors[i] != 0) {
714           aData.myActors[i]->RemoveFromRender(aRen);
715         }
716       }
717       aData.myActors.clear();
718     }
719   }
720   VISU::RepaintView(myView);
721 }
722
723 //------------------------------------------------------------------------
724 void VISU_TimeAnimation::clearView()
725 {
726   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
727                    (this,&VISU_TimeAnimation::_clearView));
728 }
729
730 //------------------------------------------------------------------------
731 void VISU_TimeAnimation::_visibilityOff(int num_field, int num_frame) {
732   if ( num_field < 0 || num_frame < 0 ) return;
733   FieldData& aData = myFieldsLst[num_field];
734   if ( aData.myActors.empty() ) return;
735   VISU_Actor* aActor = aData.myActors[num_frame];
736   if (! myCleaningMemoryAtEachFrame) {
737     //
738     // Usual behaviour : VisibilityOff()
739     // Problem : It don't clean the memory so if there is
740     //           a lot of frames, the memory grows dramatically
741     //
742     aActor->VisibilityOff();
743   } else {
744     //
745     // myCleaningMemoryAtEachFrame behaviour:
746     // Delete the actor and re-creation it with VisibilityOff()
747     // since it takes memory only at VisibilityOn()
748     //
749     // Delete the actor
750     aActor->RemoveFromRender(myView->getRenderer());
751     // Re-create the actor
752     aActor = aData.myPrs[num_frame]->CreateActor();
753     myView->AddActor(aActor);
754     aActor->VisibilityOff();
755     aData.myActors[num_frame] = aActor;
756   }
757 }
758
759 //------------------------------------------------------------------------
760 void VISU_TimeAnimation::visibilityOff(int num_field, int num_frame)
761 {
762   ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,int,int>
763                    (this,&VISU_TimeAnimation::_visibilityOff,num_field,num_frame));
764 }
765
766 //------------------------------------------------------------------------
767 void VISU_TimeAnimation::stopAnimation()
768 {
769   myExecutionState->SetActive(false);
770 }
771
772 //------------------------------------------------------------------------
773 void VISU_TimeAnimation::_startAnimation() {
774   if (!myExecutionState->IsActive()) {
775     myExecutionState->SetActive(true);
776     QThread::start();
777   }
778 }
779
780 //------------------------------------------------------------------------
781 void VISU_TimeAnimation::startAnimation()
782 {
783   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
784                    (this,&VISU_TimeAnimation::_startAnimation));
785 }
786
787 //------------------------------------------------------------------------
788 CORBA::Boolean VISU_TimeAnimation::isRunning()
789 {
790   return myExecutionState->IsActive();
791 }
792
793 //------------------------------------------------------------------------
794 void VISU_TimeAnimation::_nextFrame() {
795   if (!myView) {
796     MESSAGE("Viewer is not defined for animation");
797     return;
798   }
799   stopAnimation();
800   if (myFrame < getNbFrames() - 1 ) { //(myFieldsLst[0].myNbFrames-1)) {
801     int i;
802     std::pair<int,long> aPair;
803     int aFieldId;
804     long aFrameId;
805
806     if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
807       for (i = 0; i < getNbFields(); i++)
808         if (myFieldsLst[i].myActors[myFrame] != 0)
809           visibilityOff(i, myFrame);
810     }
811     else { //successive animation mode
812       aPair = getRelativeFrameNumber(myFrame);
813       aFieldId = aPair.first;
814       aFrameId = aPair.second;
815       if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
816         visibilityOff(aFieldId, aFrameId);
817     }
818
819     myFrame++;
820
821     if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
822       for (i = 0; i < getNbFields(); i++)
823         if (myFieldsLst[i].myActors[myFrame] != 0)
824           myFieldsLst[i].myActors[myFrame]->VisibilityOn();
825
826       ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
827                                                                                 myFrame, myFieldsLst[0].myTiming[myFrame]));
828     }
829     else { //successive animation mode
830       aPair = getRelativeFrameNumber(myFrame);
831       aFieldId = aPair.first;
832       aFrameId = aPair.second;
833       if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
834         myFieldsLst[aFieldId].myActors[aFrameId]->VisibilityOn();
835
836       ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
837                                                                                 myFrame, myFieldsLst[aFieldId].myTiming[aFrameId]));
838     }
839     myView->Repaint();
840   }
841 }
842
843 //------------------------------------------------------------------------
844 void VISU_TimeAnimation::nextFrame()
845 {
846   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
847                    (this,&VISU_TimeAnimation::_nextFrame));
848 }
849
850 //------------------------------------------------------------------------
851 void VISU_TimeAnimation::_prevFrame() {
852   if (!myView) {
853     MESSAGE("Viewer is not defined for animation");
854     return;
855   }
856   stopAnimation();
857   if (myFrame > 0) {
858     int i;
859     std::pair<int,long> aPair;
860     int aFieldId;
861     long aFrameId;
862
863     if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
864       for (i = 0; i < getNbFields(); i++)
865         if (myFieldsLst[i].myActors[myFrame] != 0)
866           visibilityOff(i, myFrame);
867     }
868     else { //successive animation mode
869       aPair = getRelativeFrameNumber(myFrame);
870       aFieldId = aPair.first;
871       aFrameId = aPair.second;
872       if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
873           visibilityOff(aFieldId, aFrameId);
874     }
875
876     myFrame--;
877
878     if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
879       for (i = 0; i < getNbFields(); i++)
880         if (myFieldsLst[i].myActors[myFrame] != 0)
881           myFieldsLst[i].myActors[myFrame]->VisibilityOn();
882
883       ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
884                                                                                 myFrame, myFieldsLst[0].myTiming[myFrame]));
885     }
886     else { //successive animation mode
887       aPair = getRelativeFrameNumber(myFrame);
888       aFieldId = aPair.first;
889       aFrameId = aPair.second;
890       if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
891           myFieldsLst[aFieldId].myActors[aFrameId]->VisibilityOn();
892
893       ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
894                                                                                 myFrame, myFieldsLst[aFieldId].myTiming[aFrameId]));
895     }
896     myView->Repaint();
897   }
898 }
899
900 //------------------------------------------------------------------------
901 void VISU_TimeAnimation::prevFrame()
902 {
903   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
904                    (this,&VISU_TimeAnimation::_prevFrame));
905 }
906
907 //------------------------------------------------------------------------
908 void VISU_TimeAnimation::_firstFrame() {
909   if (!myView) {
910     MESSAGE("Viewer is not defined for animation");
911     return;
912   }
913   stopAnimation();
914   int i;
915   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
916     for (i = 0; i < getNbFields(); i++)
917       if(!myFieldsLst[i].myActors.empty())
918         if (myFieldsLst[i].myActors[myFrame] != 0)
919           visibilityOff(i, myFrame);
920     }
921   else { //successive animation mode
922     std::pair<int,long> aPair = getRelativeFrameNumber(myFrame);
923     int aFieldId = aPair.first;
924     long aFrameId = aPair.second;
925     if(!myFieldsLst[aFieldId].myActors.empty())
926       if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
927         visibilityOff(aFieldId, aFrameId);
928   }
929   myFrame = 0;
930
931   int imax;
932   if ( myAnimationMode == VISU::Animation::PARALLEL ) // parallel animation mode 
933     imax = getNbFields();
934   else //successive animation mode
935     imax = 1;
936
937   for (i = 0; i < imax; i++)
938     if(!myFieldsLst[i].myActors.empty())
939       if (myFieldsLst[i].myActors[myFrame] != 0)
940         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
941
942   if(!myFieldsLst[0].myTiming.empty()){
943     ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
944                                                                               myFrame, myFieldsLst[0].myTiming[myFrame]));
945     myView->Repaint();
946   }
947 }
948
949 //------------------------------------------------------------------------
950 void VISU_TimeAnimation::firstFrame()
951 {
952   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
953                    (this,&VISU_TimeAnimation::_firstFrame));
954 }
955
956 //------------------------------------------------------------------------
957 void VISU_TimeAnimation::_lastFrame() {
958   if (!myView) {
959     MESSAGE("Viewer is not defined for animation");
960     return;
961   }
962   stopAnimation();
963   int i;
964   std::pair<int,long> aPair;
965   int aFieldId;
966   long aFrameId;
967
968   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
969     for (i = 0; i < getNbFields(); i++)
970       if (myFieldsLst[i].myActors[myFrame] != 0)
971         visibilityOff(i, myFrame);
972   }
973   else { //successive animation mode
974     aPair = getRelativeFrameNumber(myFrame);
975     aFieldId = aPair.first;
976     aFrameId = aPair.second;
977     if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
978       visibilityOff(aFieldId, aFrameId);
979   }
980
981   myFrame = getNbFrames() - 1; //myFieldsLst[0].myNbFrames-1;
982
983   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
984     for (i = 0; i < getNbFields(); i++)
985       if (myFieldsLst[i].myActors[myFrame] != 0)
986         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
987     
988     ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
989                                                                               myFrame, myFieldsLst[0].myTiming[myFrame]));
990   }
991   else { //successive animation mode
992     aPair = getRelativeFrameNumber(myFrame);
993     aFieldId = aPair.first;
994     aFrameId = aPair.second;
995     if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
996         myFieldsLst[aFieldId].myActors[aFrameId]->VisibilityOn();
997
998     ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
999                                                                               myFrame, myFieldsLst[aFieldId].myTiming[aFrameId]));
1000   }
1001
1002   myView->Repaint();
1003 }
1004
1005 //------------------------------------------------------------------------
1006 void VISU_TimeAnimation::lastFrame()
1007 {
1008   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
1009                    (this,&VISU_TimeAnimation::_lastFrame));
1010 }
1011
1012
1013 //------------------------------------------------------------------------
1014 // For Batchmode using
1015 void VISU_TimeAnimation::_gotoFrame(CORBA::Long theFrame) {
1016   if (!myView) {
1017     MESSAGE("Viewer is not defined for animation");
1018     return;
1019   }
1020   if ((theFrame < 0) || (theFrame > (getNbFrames()-1)))
1021     return;
1022   stopAnimation();
1023   int i;
1024   std::pair<int,long> aPair;
1025   int aFieldId;
1026   long aFrameId;
1027   
1028   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
1029     for (i = 0; i < getNbFields(); i++)
1030       if (myFieldsLst[i].myActors[myFrame] != 0)
1031         visibilityOff(i, myFrame);
1032   }
1033   else { //successive animation mode
1034     aPair = getRelativeFrameNumber(myFrame);
1035     aFieldId = aPair.first;
1036     aFrameId = aPair.second;
1037     if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
1038         visibilityOff(aFieldId, aFrameId);
1039   }
1040
1041   myFrame = theFrame;
1042
1043   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
1044     for (i = 0; i < getNbFields(); i++)
1045       if (myFieldsLst[i].myActors[myFrame] != 0)
1046         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
1047
1048     ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
1049                                                                               myFrame, myFieldsLst[0].myTiming[myFrame]));
1050   }
1051   else { //successive animation mode
1052     aPair = getRelativeFrameNumber(myFrame);
1053     aFieldId = aPair.first;
1054     aFrameId = aPair.second;
1055     if (myFieldsLst[aFieldId].myActors[aFrameId] != 0)
1056         myFieldsLst[aFieldId].myActors[aFrameId]->VisibilityOn();
1057
1058     ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>(this, &VISU_TimeAnimation::_emitFrameChanged,
1059                                                                               myFrame, myFieldsLst[aFieldId].myTiming[aFrameId]));
1060   }
1061
1062   myView->Repaint();
1063 }
1064
1065 //------------------------------------------------------------------------
1066 void VISU_TimeAnimation::gotoFrame(CORBA::Long theFrame)
1067 {
1068   ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_TimeAnimation,CORBA::Long>
1069                    (this,&VISU_TimeAnimation::_gotoFrame,theFrame));
1070 }
1071
1072 //------------------------------------------------------------------------
1073 void VISU_TimeAnimation::_emitFrameChanged(long theNewFrame, double theTime)
1074 {
1075   emit frameChanged(theNewFrame, theTime);
1076 }
1077
1078 //------------------------------------------------------------------------
1079 void VISU_TimeAnimation::_emitStopped()
1080 {
1081   emit stopped();
1082 }
1083
1084 //------------------------------------------------------------------------
1085 VISU::ColoredPrs3d_ptr VISU_TimeAnimation::getPresentation(CORBA::Long theField, CORBA::Long theFrame) {
1086   if ((theField > getNbFields()) || (theField < 0))
1087     return VISU::ColoredPrs3d::_nil();
1088   if ((theFrame < 0) || (theFrame > (myFieldsLst[theField].myNbFrames - 1)))
1089     return VISU::ColoredPrs3d::_nil();
1090   return myFieldsLst[theField].myPrs[theFrame]->_this();
1091 }
1092
1093
1094 //------------------------------------------------------------------------
1095 CORBA::Long VISU_TimeAnimation::getNbFrames() {
1096   if ( myAnimationMode == VISU::Animation::PARALLEL ) // parallel animation mode
1097     return (getNbFields() > 0)? myFieldsLst[0].myNbFrames : 0;
1098   else //successive animation mode
1099     return (getNbFields() > 0 && !myFieldsAbsFrames.empty()) ? myFieldsAbsFrames[myFieldsAbsFrames.size()-1] : 0;
1100 }
1101
1102 //------------------------------------------------------------------------
1103 long VISU_TimeAnimation::getAbsoluteFrameNumber(std::pair<int,long> theFieldTimeStamp)
1104 {
1105   long aRes = -1;
1106   if ( getNbFields() > 0 ) {
1107     int aFieldId = theFieldTimeStamp.first;
1108     long aFrameNum = theFieldTimeStamp.second + 1;
1109     if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
1110       if ( aFrameNum <= myFieldsAbsFrames[0] )
1111         aRes = aFrameNum;
1112     }
1113     else { //successive animation mode
1114       if ( aFieldId == 0 && aFrameNum <= myFieldsAbsFrames[aFieldId] )
1115         aRes = aFrameNum;
1116       else if ( aFieldId && aFrameNum <= myFieldsAbsFrames[aFieldId] - myFieldsAbsFrames[aFieldId-1] )
1117         aRes = myFieldsAbsFrames[aFieldId-1] + aFrameNum;
1118     }
1119   }
1120   return aRes - 1;
1121 }
1122
1123 //------------------------------------------------------------------------
1124 std::pair<int,long> VISU_TimeAnimation::getRelativeFrameNumber(long theFrame)
1125 {
1126   std::pair<int,long> aRes;
1127   if ( getNbFields() > 0 && theFrame < getNbFrames() ) {
1128     theFrame = theFrame + 1;
1129     if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
1130       aRes.first = 0;
1131       aRes.second = theFrame - 1;
1132     }
1133     else { //successive animation mode
1134       for (int i = 0, iEnd = myFieldsAbsFrames.size(); i < iEnd; i++)
1135         if ( myFieldsAbsFrames[i] >= theFrame ) {
1136           aRes.first = i;
1137           if ( i == 0 )
1138             aRes.second = theFrame - 1;
1139           else
1140             aRes.second = theFrame-myFieldsAbsFrames[i-1] - 1;
1141           break;
1142         }
1143     }
1144   }
1145   return aRes;
1146 }
1147
1148 //------------------------------------------------------------------------
1149 void VISU_TimeAnimation::parallelAnimation( bool& theIsDumping, QValueList<int>& theIndexList )
1150 {
1151   double k = 1;
1152   double aOneVal = 1;
1153   FieldData& aFirstFieldData = myFieldsLst[0];
1154   if (aFirstFieldData.myNbFrames > 2)
1155     aOneVal = ( myTimeMax - myTimeMin ) / getNbFrames();
1156   int aNbFiles = 0;
1157
1158   bool aHasNextFrame = aFirstFieldData.myField && aFirstFieldData.myNbFrames > 0;
1159   while (aHasNextFrame && myExecutionState->IsActive()) {
1160     ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>
1161                      (this, &VISU_TimeAnimation::_emitFrameChanged,
1162                       myFrame, aFirstFieldData.myTiming[myFrame]));
1163     if (myExecutionState->IsActive()) {
1164       for (int i = 0; i < getNbFields(); i++) {
1165         FieldData& aData = myFieldsLst[i];
1166         if (aData.myNbFrames == 0)
1167           continue;
1168         if (myFrame > 0) {
1169           if (aData.myActors[myFrame-1] != 0)
1170             visibilityOff(i, myFrame-1);
1171         } else {
1172           if (aData.myActors[aData.myNbFrames-1] != 0)
1173             visibilityOff(i, aData.myNbFrames-1);
1174         }
1175         if (aData.myActors[myFrame] != 0) {
1176           ProcessVoidEvent(new TVoidMemFunEvent<VISU_Actor>(aData.myActors[myFrame],
1177                                                             &VISU_Actor::VisibilityOn));
1178         }
1179       }
1180       bool repainArg = false;
1181       ProcessVoidEvent(new TVoidMemFun1ArgEvent<SVTK_ViewWindow,bool>(myView,
1182                                                                       &SVTK_ViewWindow::Repaint,
1183                                                                       repainArg));
1184     }
1185
1186     k = 1;
1187     if (myProportional) {
1188       switch (myFrame) {
1189       case 0:
1190         break;
1191       case 1:
1192         if (aFirstFieldData.myNbFrames > 2)
1193           k = (aFirstFieldData.myTiming[myFrame+1] -
1194                aFirstFieldData.myTiming[myFrame]) / aOneVal;
1195         break;
1196       default:
1197         if (myFrame < (aFirstFieldData.myNbFrames - 1))
1198           k = (aFirstFieldData.myTiming[myFrame+1] -
1199                aFirstFieldData.myTiming[myFrame]) / aOneVal;
1200       }
1201     }
1202     int delay = (int)(1000. * k / mySpeed);
1203     theIsDumping = !myDumpPath.isEmpty();
1204     if (delay < 1 && theIsDumping) {
1205       // We must unlock mutex for some time before grabbing to allow view updating
1206       delay = 1;
1207     }
1208     msleep(delay);
1209     if (!myExecutionState->IsActive()) 
1210       return;
1211
1212     if (theIsDumping) {
1213       // We must unlock mutex for some time before grabbing to allow view updating
1214       msleep(delay);
1215       if (!myExecutionState->IsActive()) 
1216         return;
1217
1218       if (!(aFirstFieldData.myField)) // break, if field was deleted.
1219         break;
1220
1221       saveImages( 0, aOneVal, aNbFiles, theIndexList );
1222     }
1223
1224     if (!myExecutionState->IsActive()) 
1225       break;
1226
1227     myFrame++;
1228     if (myFrame == aFirstFieldData.myNbFrames) {
1229       if (!myCycling) {
1230         aHasNextFrame = false;
1231         myFrame--;
1232       }
1233       else
1234         myFrame = 0;
1235     }
1236   } // while (aHasNextFrame && myExecutionState->IsActive())
1237 }
1238
1239 //------------------------------------------------------------------------
1240 void VISU_TimeAnimation::successiveAnimation( bool& theIsDumping, QValueList<int>& theIndexList )
1241 {
1242   if (myFrame >= getNbFrames() - 1)
1243   {
1244     myExecutionState->SetActive(false);
1245     return;
1246   }
1247
1248   double k = 1;
1249   double aOneVal = 1;
1250   FieldData& aFirstFieldData = myFieldsLst[0];
1251   if (aFirstFieldData.myNbFrames > 2)
1252     aOneVal = ( myTimeMax - myTimeMin ) / getNbFrames();
1253   int aNbFiles = 0;
1254   long aFrame = myFrame;
1255
1256   bool aHasNextFrame = true;
1257   while (aHasNextFrame && myExecutionState->IsActive())
1258   {
1259     for (int aFieldId = 0;
1260          (aFieldId < getNbFields()) && (myFieldsLst[aFieldId].myField);
1261          aFieldId++, aFrame = 0)
1262     {
1263       if (!myExecutionState->IsActive()) break;
1264
1265       FieldData& aData = myFieldsLst[aFieldId];
1266       if ( !aData.myPrs[0] ) continue;
1267       for (; aFrame < aData.myNbFrames && myExecutionState->IsActive(); aFrame++, myFrame++)
1268       {
1269         ProcessVoidEvent(new TVoidMemFun2ArgEvent<VISU_TimeAnimation,long,double>
1270                          (this, &VISU_TimeAnimation::_emitFrameChanged,
1271                           myFrame, myFieldsLst[aFieldId].myTiming[aFrame]));
1272
1273         if (myExecutionState->IsActive()) {
1274           if (aFrame > 0) {
1275             if (aData.myActors[aFrame-1] != 0)
1276               visibilityOff(aFieldId, aFrame-1);
1277           } else if ( myFrame > 0) {
1278             if (myFieldsLst[aFieldId-1].myActors[myFieldsLst[aFieldId-1].myNbFrames-1] != 0)
1279               visibilityOff(aFieldId-1, myFieldsLst[aFieldId-1].myNbFrames-1);
1280           } else if ( myCycling ) {
1281             if (myFieldsLst[getNbFields()-1].myActors[myFieldsLst[getNbFields()-1].myNbFrames-1] != 0)
1282               visibilityOff(getNbFields()-1, myFieldsLst[getNbFields()-1].myNbFrames-1);
1283           } else {
1284             if (aData.myActors[aData.myNbFrames-1] != 0)
1285               visibilityOff(aFieldId, aData.myNbFrames-1);
1286           }
1287           if (aData.myActors[aFrame] != 0) {
1288             ProcessVoidEvent(new TVoidMemFunEvent<VISU_Actor>(aData.myActors[aFrame],
1289                                                               &VISU_Actor::VisibilityOn));
1290           }
1291
1292           bool repainArg = false;
1293           ProcessVoidEvent(new TVoidMemFun1ArgEvent<SVTK_ViewWindow,bool>(myView,
1294                                                                           &SVTK_ViewWindow::Repaint,
1295                                                                           repainArg));
1296         }
1297
1298         k = 1;
1299         if (myProportional) {
1300           switch (aFrame) {
1301           case 0:
1302             break;
1303           case 1:
1304             if (aFirstFieldData.myNbFrames > 2)
1305               k = (aFirstFieldData.myTiming[aFrame+1] -
1306                    aFirstFieldData.myTiming[aFrame]) / aOneVal;
1307             break;
1308           default:
1309             if (aFrame < (aFirstFieldData.myNbFrames - 1))
1310               k = (aFirstFieldData.myTiming[aFrame+1] -
1311                    aFirstFieldData.myTiming[aFrame]) / aOneVal;
1312           }
1313         }
1314         int delay = (int)(1000. * k / mySpeed);
1315         theIsDumping = !myDumpPath.isEmpty();
1316         if (delay < 1 && theIsDumping) {
1317           // We must unlock mutex for some time before grabbing to allow view updating
1318           delay = 1;
1319         }
1320         msleep(delay);
1321
1322         if (!myExecutionState->IsActive()) return;
1323
1324         if (theIsDumping) {
1325           // We must unlock mutex for some time before grabbing to allow view updating
1326           msleep(delay);
1327           if (!myExecutionState->IsActive()) return;
1328
1329           if (!(myFieldsLst[aFieldId].myField)) // break, if field was deleted.
1330             break;
1331
1332           saveImages( aFieldId, aOneVal, aNbFiles, theIndexList );
1333         }
1334       } // for (; aFrame < aData.myNbFrames && myExecutionState->IsActive(); aFrame++, myFrame++)
1335     } // for (int aFieldId = 0;
1336
1337     if (!myCycling) {
1338       aHasNextFrame = false;
1339       myFrame--;
1340     }
1341     else {
1342       myFrame = 0;
1343       aFrame = myFrame;
1344     }
1345   } // while (aHasNextFrame && myExecutionState->IsActive())
1346 }
1347
1348 //------------------------------------------------------------------------
1349 void VISU_TimeAnimation::saveImages( int theFieldId, 
1350                                      double& theOneVal, int& theNbFiles, 
1351                                      QValueList<int>& theIndexList )
1352 {
1353   if (myDumpFormat.compare("AVI") != 0) {
1354     QString aFile(myDumpPath);
1355
1356     QString aName;
1357     if ( myAnimationMode == VISU::Animation::PARALLEL ) // parallel animation mode
1358       aName = QString("%1").arg(myFieldsLst[theFieldId].myTiming[myFrame]);
1359     else // successive animation mode
1360       aName = QString("%1").arg(myFieldsLst[theFieldId].myTiming[getRelativeFrameNumber(myFrame).second]);
1361
1362     int aPos = -1;
1363     while ((aPos = aName.find(".")) > -1 )
1364       aName.replace(aPos, 1, "_");
1365     aFile += aName;
1366     aFile += ".";
1367     aFile += myDumpFormat.lower();
1368     ProcessVoidEvent(new TVoidMemFunEvent<SVTK_ViewWindow>
1369                      (myView,&SVTK_ViewWindow::RefreshDumpImage)); // IPAL13602
1370     ProcessEvent(new TMemFun2ArgEvent<SUIT_ViewWindow,bool,const QString&,const QString&>
1371                  (myView,&SUIT_ViewWindow::dumpViewToFormat,aFile,myDumpFormat));
1372   } else {
1373     QFileInfo aFileInfo(myDumpPath);
1374     QString aDirPath = aFileInfo.dirPath(true);
1375     QString aBaseName = aFileInfo.fileName();
1376     
1377     switch (myFrame) {
1378     case 0: 
1379       break;
1380     case 1:
1381       myFileIndex += 5;
1382       break;
1383     default:
1384       if (myProportional) {
1385         FieldData& aFirstFieldData = myFieldsLst[0];
1386         double p = (aFirstFieldData.myTiming[myFrame] -
1387                     aFirstFieldData.myTiming[myFrame-1]) / theOneVal;
1388         myFileIndex += (long) (5*p);
1389       } else {
1390         myFileIndex += 5;
1391       }
1392     }
1393     
1394     QString aFile = aDirPath + QDir::separator() + aBaseName;
1395     aFile += "_";
1396     aFile += QString("%1").arg(myFileIndex).rightJustify(8, '0');
1397     aFile += ".jpeg";
1398     
1399     /* check image size is divisable 16
1400        myView->dumpViewToFormat(aFile,"JPEG");
1401     */
1402     SUIT_ViewWindow* aView = myView;
1403     ProcessVoidEvent(new TVoidMemFunEvent<SVTK_ViewWindow>(myView,&SVTK_ViewWindow::RefreshDumpImage)); // IPAL13602
1404     QImage img = ProcessEvent(new TMemFunEvent<SUIT_ViewWindow,QImage>(aView,&SUIT_ViewWindow::dumpView));
1405     if (!img.isNull()) {
1406       int width = img.width(); width = (width/16)*16;
1407       int height = img.height(); height = (height/16)*16;
1408       QImage copy = img.copy(0, 0, width, height);
1409       if (copy.save(aFile, "JPEG")) {
1410         theIndexList.append(myFileIndex);
1411         theNbFiles++;
1412       }
1413     }
1414   }
1415 }
1416
1417 //------------------------------------------------------------------------
1418 void VISU_TimeAnimation::run()
1419 {
1420   if (!myView) {
1421     MESSAGE("Viewer is not defined for animation");
1422     return;
1423   }
1424
1425   bool isDumping = !myDumpPath.isEmpty();
1426   myFileIndex = 0;
1427   QValueList<int> anIndexList;
1428
1429   if ( myAnimationMode == VISU::Animation::PARALLEL ) // parallel animation mode
1430     parallelAnimation( isDumping, anIndexList );
1431   else //successive animation mode
1432     successiveAnimation( isDumping, anIndexList );
1433
1434   // make AVI file if need
1435   if (isDumping && myDumpFormat.compare("AVI") == 0 && myExecutionState->IsActive()) {
1436     double aFPS = 17.3 * mySpeed;
1437
1438     QFileInfo aFileInfo(myDumpPath);
1439     QString aDirPath = aFileInfo.dirPath(true);
1440     QString aBaseName = aFileInfo.fileName();
1441
1442     // add missing files
1443     if (anIndexList.count() > 1) {
1444       QString aFFile = aDirPath + QDir::separator() + aBaseName;
1445       aFFile += QString("_%1.jpeg");
1446       int aStartIndex = anIndexList[0], anEndIndex;
1447       for (int i = 1; i < anIndexList.count(); i++) {
1448         anEndIndex = anIndexList[i];
1449         QString aCurFile = aFFile.arg(QString::number(aStartIndex).rightJustify(8, '0'));
1450         QStringList aCommands;
1451         for (int j = aStartIndex+1; j < anEndIndex; j++) {
1452           QString aFile = aFFile.arg(QString::number(j).rightJustify(8, '0'));
1453           aCommands.append(QString("ln -s %1 %2").arg(aCurFile).arg(aFile));
1454         }
1455         system(aCommands.join(" ; \\\n").latin1());
1456         aStartIndex = anEndIndex;
1457       }
1458     }
1459
1460     // make AVI file
1461     QString aPattern = aDirPath + QDir::separator() + aBaseName;
1462     aPattern += "_\%08d.jpeg";
1463
1464     QString aCmd = myAVIMaker;
1465     aCmd += " -I p";
1466     aCmd += " -v 0";
1467     aCmd += QString(" -f %1").arg(aFPS);
1468     // aCmd += QString(" -n %1").arg(aNbFiles);
1469     aCmd += QString(" -n %1").arg(myFileIndex+1);
1470     aCmd += QString(" -j %1").arg(aPattern);
1471     aCmd += " | yuv2lav";
1472     aCmd += QString(" -o %1").arg(myDumpPath);
1473     system(aCmd.latin1());
1474
1475     // remove temporary jpeg files
1476     aCmd = "( ";
1477     aCmd += QString("cd %1").arg(aDirPath);
1478     aCmd += "; ls";
1479     aCmd += QString(" | egrep '%1_[0-9]*.jpeg'").arg(aBaseName);
1480     aCmd += " | xargs rm";
1481     aCmd += " )";
1482     system(aCmd.latin1());
1483   }
1484
1485   if (myExecutionState->IsActive())
1486     ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>(this,&VISU_TimeAnimation::_emitStopped));
1487   myExecutionState->SetActive(false);
1488 }
1489
1490 //------------------------------------------------------------------------
1491 VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
1492 {
1493   _PTR(SObject) aSObj = theField->GetFather();
1494   aSObj = aSObj->GetFather();
1495   aSObj = aSObj->GetFather();
1496   CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
1497   if (CORBA::is_nil(anObject)) return NULL;
1498   return dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
1499 }
1500
1501 //------------------------------------------------------------------------
1502 double VISU_TimeAnimation::getTimeValue (_PTR(SObject) theTimeStamp)
1503 {
1504   _PTR(GenericAttribute) anAttr;
1505   if (theTimeStamp->FindAttribute(anAttr, "AttributeName")) {
1506     _PTR(AttributeName) aName (anAttr);
1507     QString aNameString (aName->Value().c_str());
1508     int time_len = aNameString.find(',');
1509     if (time_len > -1)
1510       return aNameString.left(time_len).toDouble();
1511     else
1512       return aNameString.toDouble();
1513   }
1514   return -1.0;
1515 }
1516
1517 //------------------------------------------------------------------------
1518 void VISU_TimeAnimation::setSpeed(CORBA::Long theSpeed)
1519 {
1520   mySpeed = (theSpeed<1)? 1 : theSpeed;
1521 }
1522
1523 //------------------------------------------------------------------------
1524 void VISU_TimeAnimation::setAnimationSequence(const char* theSequence)
1525 {
1526   mySequence = QString( theSequence );
1527 }
1528
1529 //------------------------------------------------------------------------
1530 char* VISU_TimeAnimation::getAnimationSequence()
1531 {
1532   return (char*)mySequence.latin1();
1533 }
1534
1535 //------------------------------------------------------------------------
1536 CORBA::Boolean VISU_TimeAnimation::isSequenceDefined()
1537 {
1538   return !mySequence.isEmpty();
1539 }
1540
1541 //------------------------------------------------------------------------
1542 bool VISU_TimeAnimation::getIndicesFromSequence( QString theSequence, QValueList<long>& theIndices )
1543 {
1544   bool isCorrect = true;
1545
1546   theIndices.clear();
1547
1548   QStringList aList = QStringList::split( ",", theSequence );
1549   QStringList::iterator it = aList.begin();
1550   QStringList::iterator itEnd = aList.end();
1551   for( ; it != itEnd; ++it )
1552   {
1553     if( !isCorrect )
1554       break;
1555
1556     isCorrect = false;
1557
1558     QString aString = *it;
1559     if( aString.isEmpty() )
1560       continue;
1561
1562     bool ok = false;
1563     int aSingleIndex = aString.toLong( &ok );
1564     if( ok )
1565     {
1566       theIndices.append( aSingleIndex );
1567       isCorrect = true;
1568     }
1569     else if( aString.contains( '-' ) == 1 )
1570     {
1571       QString aLeftIndexStr = aString.section( '-', 0, 0 );
1572       QString aRightIndexStr = aString.section( '-', -1 );
1573
1574       ok = false;
1575       int aLeftIndex = aLeftIndexStr.toLong( &ok );
1576       if( !ok )
1577         continue;
1578
1579       ok = false;
1580       int aRightIndex = aRightIndexStr.toLong( &ok );
1581       if( !ok )
1582         continue;
1583
1584       if( aLeftIndex >= aRightIndex )
1585         continue;
1586
1587       for( int i = aLeftIndex; i <= aRightIndex; i++ )
1588         theIndices.append( i );
1589
1590       isCorrect = true;
1591     }
1592   }
1593
1594   return isCorrect;
1595 }
1596
1597 //------------------------------------------------------------------------
1598 std::string VISU_TimeAnimation::setDumpFormat(const char* theFormat)
1599 {
1600   myDumpFormat = theFormat;
1601   QStrList aDumpFormats = QImageIO::outputFormats();
1602   if (myDumpFormat.isEmpty() || 
1603       (aDumpFormats.find(theFormat) < 0 && myDumpFormat.compare("AVI") != 0)) {
1604     if (aDumpFormats.find("JPEG"))
1605       myDumpFormat = "JPEG";
1606     else
1607       myDumpFormat = aDumpFormats.at(0);
1608   }
1609   return myDumpFormat.latin1();
1610 }
1611
1612 //------------------------------------------------------------------------
1613 bool VISU_TimeAnimation::checkAVIMaker() const
1614 {
1615   QStrList aDumpFormats = QImageIO::outputFormats();
1616   if (aDumpFormats.find("JPEG") < 0) return false;
1617
1618   QString aCmd("which ");
1619   aCmd += myAVIMaker;
1620   aCmd += " >& /dev/null";
1621   int iErr = system(aCmd.latin1());
1622   return (iErr == 0);
1623 }
1624
1625 //************************************************************************
1626 int VISU_TimeAnimation::myNBAnimations = 0;
1627 QString VISU_TimeAnimation::GenerateName()
1628 {
1629   return VISU::GenerateName("Animation", myNBAnimations++);
1630 }
1631
1632 //------------------------------------------------------------------------
1633 std::string GetPresentationComment (VISU::VISUType thePrsType)
1634 {
1635   std::string aPrsCmt;
1636   switch (thePrsType) {
1637   case VISU::TSCALARMAP:
1638     aPrsCmt = VISU::ScalarMap_i::myComment;
1639     break;
1640   case VISU::TISOSURFACES:
1641     aPrsCmt = VISU::IsoSurfaces_i::myComment;
1642     break;
1643   case VISU::TCUTPLANES:
1644     aPrsCmt = VISU::CutPlanes_i::myComment;
1645     break;
1646   case VISU::TCUTLINES:
1647     aPrsCmt = VISU::CutLines_i::myComment;
1648     break;
1649   case VISU::TPLOT3D:
1650     aPrsCmt = VISU::Plot3D_i::myComment;
1651     break;
1652   case VISU::TDEFORMEDSHAPE:
1653     aPrsCmt = VISU::DeformedShape_i::myComment;
1654     break;
1655   case VISU::TVECTORS:
1656     aPrsCmt = VISU::Vectors_i::myComment;
1657     break;
1658   case VISU::TSTREAMLINES:
1659     aPrsCmt = VISU::StreamLines_i::myComment;
1660     break;
1661   case VISU::TGAUSSPOINTS:
1662     aPrsCmt = VISU::GaussPoints_i::myComment;
1663     break;
1664   case VISU::TSCALARMAPONDEFORMEDSHAPE:
1665     aPrsCmt = VISU::ScalarMapOnDeformedShape_i::myComment;
1666     break;
1667   default:
1668     aPrsCmt = "Unknown presentation";
1669     break;
1670   }
1671   return aPrsCmt;
1672 }
1673
1674 //------------------------------------------------------------------------
1675 SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
1676 {
1677   if (myStudy->GetProperties()->IsLocked())
1678     return SALOMEDS::SObject::_nil();
1679
1680   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
1681   aStudyBuilder->NewCommand();  // There is a transaction
1682   _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
1683   std::string aSComponentEntry = aSComponent->GetID();
1684
1685   QString aComment;
1686   aComment.sprintf("myComment=ANIMATION;myTimeMinVal=%g;myTimeMaxVal=%g;mySequence=%s;myMode=%d",
1687                    myTimeMinVal,
1688                    myTimeMaxVal,
1689                    mySequence.latin1(),
1690                    myAnimationMode);
1691
1692   string anEntry = VISU::CreateAttributes(myStudy,
1693                                           aSComponentEntry.c_str(),
1694                                           VISU::NO_ICON,
1695                                           VISU::NO_IOR,
1696                                           GenerateName(),
1697                                           VISU::NO_PERFSITENT_REF,
1698                                           aComment,
1699                                           true);
1700   myAnimEntry = anEntry.c_str();
1701   _PTR(SObject) aAnimSObject = myStudy->FindObjectID(anEntry.c_str());
1702
1703   for (int i = 0; i < getNbFields(); i++) {
1704     FieldData& aData = myFieldsLst[i];
1705     if (aData.myPrs.empty()) {
1706       generatePresentations(i);
1707     }
1708     if ( !aData.myPrs.empty() ) {
1709       _PTR(SObject) newObj = aStudyBuilder->NewObject(aAnimSObject);
1710       aStudyBuilder->Addreference(newObj, aData.myField);
1711       
1712       ostringstream strOut;
1713       aData.myPrs[0]->ToStream(strOut);
1714       string aPrsComment = strOut.str();
1715       string aPrsMyComment = aData.myPrs[0]->GetComment();
1716       if(aPrsMyComment == "PRSMERGER")
1717         aPrsMyComment = "SCALARMAP";
1718       VISU::CreateAttributes(myStudy, 
1719                              newObj->GetID().c_str(),
1720                              VISU::NO_ICON,
1721                              VISU::NO_IOR,
1722                              aPrsMyComment.c_str(),
1723                              VISU::NO_PERFSITENT_REF,
1724                              aPrsComment.c_str(),
1725                              true);
1726     }
1727   }
1728   aStudyBuilder->CommitCommand();
1729
1730   return VISU::GetSObject(aAnimSObject);
1731 }
1732
1733 //------------------------------------------------------------------------
1734 void VISU_TimeAnimation::saveAnimation()
1735 {
1736   if (myStudy->GetProperties()->IsLocked()) return;
1737   if (myAnimEntry.isEmpty()) return;
1738
1739   _PTR(SObject) aAnimSObject = myStudy->FindObjectID(myAnimEntry.latin1());
1740   if (!aAnimSObject) return;
1741
1742   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
1743   aStudyBuilder->NewCommand();  // There is a transaction
1744   _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
1745   std::string aSComponentEntry = aSComponent->GetID();
1746
1747   QString aComment;
1748   aComment.sprintf("myComment=ANIMATION;myTimeMinVal=%g;myTimeMaxVal=%g;mySequence=%s;myMode=%d",
1749                    myTimeMinVal,
1750                    myTimeMaxVal,
1751                    mySequence.latin1(),
1752                    myAnimationMode);
1753
1754   _PTR(GenericAttribute) anAttr;
1755   anAttr = aStudyBuilder->FindOrCreateAttribute(aAnimSObject, "AttributeString");
1756   _PTR(AttributeString) aCmnt (anAttr);
1757   aCmnt->SetValue(aComment.latin1());
1758
1759   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
1760   int i = 0, nbf = getNbFields();
1761   for (anIter->Init(); anIter->More(); anIter->Next(), i++) {
1762     if (i >= nbf) break; // it must not be
1763     FieldData& aData = myFieldsLst[i];
1764
1765     // Get presentation name and comment
1766     if (aData.myPrs.empty()) {
1767       generatePresentations(i);
1768     }
1769     ostringstream strOut;
1770     aData.myPrs[0]->ToStream(strOut);
1771     string aPrsComment = strOut.str();
1772     string aPrsNameTxt = aData.myPrs[0]->GetComment();
1773     if(aPrsNameTxt == "PRSMERGER")
1774       aPrsNameTxt = "SCALARMAP";
1775     // Save in study
1776     _PTR(SObject) aRefObj = anIter->Value();
1777     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
1778     anPrsIter->Init();
1779
1780     if (anPrsIter->More()) {
1781       _PTR(SObject) aPrsObj = anPrsIter->Value();
1782       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeString");
1783       aCmnt = _PTR(AttributeString)(anAttr);
1784       aCmnt->SetValue(aPrsComment.c_str());
1785
1786       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeName");
1787       _PTR(AttributeName) aPrsName (anAttr);
1788       aPrsName->SetValue(aPrsNameTxt);
1789
1790     } else {
1791       VISU::CreateAttributes(myStudy, 
1792                              aRefObj->GetID().c_str(),
1793                              VISU::NO_ICON,
1794                              VISU::NO_IOR,
1795                              aPrsNameTxt.c_str(),
1796                              VISU::NO_PERFSITENT_REF,
1797                              aPrsComment.c_str(),
1798                              true);
1799     }
1800   }
1801   aStudyBuilder->CommitCommand();
1802 }
1803
1804 //------------------------------------------------------------------------
1805 void VISU_TimeAnimation::restoreFromStudy(SALOMEDS::SObject_ptr theField)
1806 {
1807   _PTR(SObject) aAnimSObject = VISU::GetClientSObject(theField, myStudy);
1808   restoreFromStudy(aAnimSObject);
1809 }
1810
1811 void VISU_TimeAnimation::restoreFromStudy(_PTR(SObject) theField)
1812 {
1813   _PTR(SObject) aAnimSObject = theField;
1814
1815   VISU::Storable::TRestoringMap aMap = VISU::Storable::GetStorableMap(aAnimSObject);
1816   if (aMap.empty()) 
1817     return;
1818
1819   bool isExist;
1820   myTimeMinVal = VISU::Storable::FindValue(aMap,"myTimeMinVal",&isExist).toDouble();
1821   myTimeMaxVal = VISU::Storable::FindValue(aMap,"myTimeMaxVal",&isExist).toDouble();
1822   mySequence = VISU::Storable::FindValue(aMap,"mySequence",&isExist);
1823   myAnimationMode = VISU::Animation::AnimationMode(VISU::Storable::FindValue(aMap,"myMode",&isExist).toInt());
1824
1825   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
1826   for (anIter->Init(); anIter->More(); anIter->Next()) {
1827     _PTR(SObject) aRefObj = anIter->Value();
1828     _PTR(SObject) aFieldObj;
1829     if (!aRefObj->ReferencedObject(aFieldObj) ) 
1830       continue;
1831
1832     addField(aFieldObj);
1833     if ( isRangeDefined() || isSequenceDefined() ) 
1834       myFieldsAbsFrames.pop_back();
1835
1836     FieldData& aData = getFieldData(getNbFields()-1);
1837     
1838     // Get Presentation object
1839     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
1840     anPrsIter->Init();
1841     if (!anPrsIter->More()) 
1842       continue;
1843     _PTR(SObject) aPrsObj = anPrsIter->Value();
1844     _PTR(GenericAttribute) anAttr;
1845     if (!aPrsObj->FindAttribute(anAttr, "AttributeName")) 
1846       continue;
1847     _PTR(AttributeName) aName (anAttr);
1848     string aStr = aName->Value();
1849     QString strName (aStr.c_str());
1850
1851     if (strName == VISU::ScalarMap_i::myComment.c_str())
1852       aData.myPrsType = VISU::TSCALARMAP;
1853     else if (strName == VISU::IsoSurfaces_i::myComment.c_str())
1854       aData.myPrsType = VISU::TISOSURFACES;
1855     else if (strName == VISU::CutPlanes_i::myComment.c_str())
1856       aData.myPrsType = VISU::TCUTPLANES;
1857     else if (strName == VISU::CutLines_i::myComment.c_str())
1858       aData.myPrsType = VISU::TCUTLINES;
1859     else if (strName == VISU::Plot3D_i::myComment.c_str())
1860       aData.myPrsType = VISU::TPLOT3D;
1861     else if (strName == VISU::DeformedShape_i::myComment.c_str())
1862       aData.myPrsType = VISU::TDEFORMEDSHAPE;
1863     else if (strName == VISU::Vectors_i::myComment.c_str())
1864       aData.myPrsType = VISU::TVECTORS;
1865     else if (strName == VISU::StreamLines_i::myComment.c_str())
1866       aData.myPrsType = VISU::TSTREAMLINES;
1867     else if (strName == VISU::GaussPoints_i::myComment.c_str())
1868       aData.myPrsType = VISU::TGAUSSPOINTS;
1869     else if (strName == VISU::ScalarMapOnDeformedShape_i::myComment.c_str())
1870       aData.myPrsType = VISU::TSCALARMAPONDEFORMEDSHAPE;
1871     else
1872       continue;
1873     generatePresentations(getNbFields()-1);
1874
1875     VISU::Storable::TRestoringMap aPrsMap = VISU::Storable::GetStorableMap(aPrsObj);
1876     if (aPrsMap.empty())
1877       continue;
1878     if (aData.myPrs[0]) {
1879       aData.myPrs[0]->Restore(VISU::GetSObject(aData.myField), aPrsMap);
1880       aData.myPrs[0]->GetOffset(aData.myOffset);
1881     }
1882     for (int i = 1; i < aData.myNbFrames; i++) {
1883       if (!aData.myPrs[0])
1884         continue;
1885       bool anIsFixedRange = false;
1886       if (aData.myPrsType != VISU::TGAUSSPOINTS) {
1887         if (VISU::ScalarMap_i* aPrs = dynamic_cast<VISU::ScalarMap_i*>(aData.myPrs[i]))
1888           anIsFixedRange = aPrs->IsRangeFixed();
1889       }
1890       aData.myPrs[i]->SameAs(aData.myPrs[0]);
1891     }
1892   }
1893   string aStr = aAnimSObject->GetID();
1894   myAnimEntry = aStr.c_str();
1895 }
1896
1897 void VISU_TimeAnimation::onViewDeleted()
1898 {
1899   myView = 0;
1900 }
1901
1902 void VISU_TimeAnimation::ApplyProperties(CORBA::Long theFieldNum, VISU::ColoredPrs3d_ptr thePrs)
1903   throw (SALOME::SALOME_Exception)
1904 {
1905   Unexpect aCatch(SALOME_SalomeException);
1906
1907   VISU::ColoredPrs3d_i* aPrs_i = dynamic_cast<VISU::ColoredPrs3d_i*>(GetServant(thePrs).in());
1908
1909   if ( !aPrs_i ) 
1910     THROW_SALOME_CORBA_EXCEPTION("Error : invalid dynamic cast of the given presentation to VISU::ColoredPrs3d_i",
1911                                  SALOME::INTERNAL_ERROR);
1912   
1913   if ( myAnimationMode == VISU::Animation::PARALLEL ) { // parallel animation mode
1914     FieldData& aData = myFieldsLst[theFieldNum];
1915     
1916     if ( aData.myPrs.empty() )
1917       THROW_SALOME_CORBA_EXCEPTION("Error : presentations for the given field is not yet created!",
1918                                    SALOME::INTERNAL_ERROR);
1919     
1920     if ( aPrs_i->GetCResult() != aData.myPrs[0]->GetCResult() )
1921       THROW_SALOME_CORBA_EXCEPTION("Error : the MED file is not the same!",
1922                                    SALOME::INTERNAL_ERROR);
1923     
1924     for (int i = 0; i < aData.myNbFrames; i++) {
1925       bool anIsFixedRange = false;
1926       if (aData.myPrsType != VISU::TGAUSSPOINTS) {
1927         if (VISU::ScalarMap_i* aPrs = dynamic_cast<VISU::ScalarMap_i*>(aData.myPrs[i]))
1928           anIsFixedRange = aPrs->IsRangeFixed();
1929       }
1930       aData.myPrs[i]->SameAs(aPrs_i);
1931     }
1932   }
1933   else if ( myAnimationMode == VISU::Animation::SUCCESSIVE ) { // successive animation mode
1934     for (int f = 0; f < getNbFields(); f++) {
1935       FieldData& aData = myFieldsLst[f];
1936       
1937       if ( aData.myPrs.empty() )
1938         THROW_SALOME_CORBA_EXCEPTION("Error : presentations for the given field is not yet created!",
1939                                      SALOME::INTERNAL_ERROR);
1940       
1941       for (int i = 0; i < aData.myNbFrames; i++) {
1942         aData.myPrs[i]->SameAs(aPrs_i);
1943       }
1944     }
1945   }
1946 }
1947
1948 //========================================================================
1949 //========================================================================
1950 //========================================================================
1951 struct TNewAnimationEvent: public SALOME_Event
1952 {
1953   std::string myStudyName;
1954   VISU::View3D_ptr myView3D;
1955
1956   typedef VISU_TimeAnimation* TResult;
1957   TResult myResult;
1958
1959   TNewAnimationEvent (std::string theStudyName, VISU::View3D_ptr theView3D):
1960     myStudyName(theStudyName),
1961     myView3D(VISU::View3D::_duplicate(theView3D)),
1962     myResult(NULL)
1963   {}
1964
1965   virtual
1966   void
1967   Execute()
1968   {
1969     SUIT_Session* aSession = SUIT_Session::session();
1970     QPtrList<SUIT_Application> anApplications = aSession->applications();
1971     QPtrListIterator<SUIT_Application> anIter (anApplications);
1972     while (SUIT_Application* anApp = anIter.current()) {
1973       ++anIter;
1974       if (SUIT_Study* aSStudy = anApp->activeStudy()) {
1975         if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
1976           if (_PTR(Study) aCStudy = aStudy->studyDS()) {
1977             if (myStudyName == aCStudy->Name()) {
1978               myResult = new VISU_TimeAnimation (aCStudy, myView3D);
1979               break;
1980             }
1981           }
1982         }
1983       }
1984     }
1985   }
1986 };
1987
1988 VISU_TimeAnimation_i::VISU_TimeAnimation_i (SALOMEDS::Study_ptr theStudy,
1989                                             VISU::View3D_ptr theView3D)
1990 {
1991   std::string aStudyName = theStudy->Name();
1992   myAnim = ProcessEvent(new TNewAnimationEvent (aStudyName, theView3D));
1993 }
1994
1995 VISU_TimeAnimation_i::~VISU_TimeAnimation_i()
1996 {
1997   delete myAnim;
1998 }
1999
2000 bool VISU_TimeAnimation_i::addField (SALOMEDS::SObject_ptr theField)
2001 {
2002   return myAnim->addField(theField);
2003 }
2004
2005 void VISU_TimeAnimation_i::clearFields ()
2006 {
2007   for (int i = 0; i < myAnim->getNbFields(); i++) {
2008     myAnim->clearData(myAnim->getFieldData(i));
2009   }
2010   myAnim->clearFieldData();
2011 }
2012
2013 CORBA::Boolean VISU_TimeAnimation_i::generateFrames()
2014 {
2015   //return ProcessEvent(new TMemFunEvent<VISU_TimeAnimation,bool>
2016   //                    (myAnim,&VISU_TimeAnimation::generateFrames));
2017   return myAnim->generateFrames();
2018 }
2019
2020 void VISU_TimeAnimation_i::generatePresentations (CORBA::Long theFieldNum)
2021 {
2022   myAnim->generatePresentations(theFieldNum);
2023 }
2024
2025 void VISU_TimeAnimation_i::clearView()
2026 {
2027   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2028   //                 (myAnim,&VISU_TimeAnimation::clearView));
2029   myAnim->clearView();
2030 }
2031
2032 void VISU_TimeAnimation_i::stopAnimation()
2033 {
2034   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2035   //                 (myAnim,&VISU_TimeAnimation::stopAnimation));
2036   myAnim->stopAnimation();
2037 }
2038
2039 void VISU_TimeAnimation_i::startAnimation()
2040 {
2041   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2042   //                 (myAnim,&VISU_TimeAnimation::startAnimation));
2043   myAnim->startAnimation();
2044 }
2045
2046 void VISU_TimeAnimation_i::nextFrame()
2047 {
2048   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2049   //                 (myAnim,&VISU_TimeAnimation::nextFrame));
2050   myAnim->nextFrame();
2051 }
2052
2053 void VISU_TimeAnimation_i::prevFrame()
2054 {
2055   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2056   //                 (myAnim,&VISU_TimeAnimation::prevFrame));
2057   myAnim->prevFrame();
2058 }
2059
2060 void VISU_TimeAnimation_i::firstFrame()
2061 {
2062   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2063   //                 (myAnim,&VISU_TimeAnimation::firstFrame));
2064   myAnim->firstFrame();
2065 }
2066
2067 void VISU_TimeAnimation_i::lastFrame()
2068 {
2069   //ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
2070   //                 (myAnim,&VISU_TimeAnimation::lastFrame));
2071   myAnim->lastFrame();
2072 }
2073
2074 void VISU_TimeAnimation_i::gotoFrame(CORBA::Long theFrame)
2075 {
2076   //ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_TimeAnimation,CORBA::Long>
2077   //                 (myAnim,&VISU_TimeAnimation::gotoFrame,theFrame));
2078   myAnim->gotoFrame(theFrame);
2079 }
2080
2081 CORBA::Long VISU_TimeAnimation_i::getNbFields()
2082 {
2083   return myAnim->getNbFields();
2084 }
2085
2086 CORBA::Long VISU_TimeAnimation_i::getNbFrames()
2087 {
2088   return myAnim->getNbFrames();
2089 }
2090
2091 CORBA::Boolean VISU_TimeAnimation_i::isRunning()
2092 {
2093   return myAnim->isRunning();
2094 }
2095
2096 CORBA::Long VISU_TimeAnimation_i::getCurrentFrame()
2097 {
2098   return myAnim->getCurrentFrame();
2099 }
2100
2101 VISU::ColoredPrs3d_ptr VISU_TimeAnimation_i::getPresentation
2102                     (CORBA::Long theField, CORBA::Long theFrame)
2103 {
2104   return myAnim->getPresentation(theField,theFrame);
2105 }
2106
2107 void VISU_TimeAnimation_i::setPresentationType (CORBA::Long theFieldNum,
2108                                                 VISU::VISUType theType)
2109 {
2110   myAnim->setPresentationType(theFieldNum,theType);
2111 }
2112
2113 VISU::VISUType VISU_TimeAnimation_i::getPresentationType (CORBA::Long theFieldNum)
2114 {
2115   return myAnim->getPresentationType(theFieldNum);
2116 }
2117
2118 void VISU_TimeAnimation_i::setSpeed(CORBA::Long theSpeed)
2119 {
2120   myAnim->setSpeed(theSpeed);
2121 }
2122
2123 CORBA::Long VISU_TimeAnimation_i::getSpeed()
2124 {
2125   return myAnim->getSpeed();
2126 }
2127
2128 CORBA::Boolean VISU_TimeAnimation_i::isProportional()
2129 {
2130   return myAnim->isProportional();
2131 }
2132
2133 void VISU_TimeAnimation_i::setAnimationRange (CORBA::Double theMin,
2134                                               CORBA::Double theMax)
2135 {
2136   myAnim->setAnimationRange(theMin,theMax);
2137 }
2138
2139 CORBA::Double VISU_TimeAnimation_i::getMinRange()
2140 {
2141   return myAnim->getMinRange();
2142 }
2143
2144 CORBA::Double VISU_TimeAnimation_i::getMaxRange()
2145 {
2146   return myAnim->getMaxRange();
2147 }
2148
2149 CORBA::Boolean VISU_TimeAnimation_i::isRangeDefined()
2150 {
2151   return myAnim->isRangeDefined();
2152 }
2153
2154 void VISU_TimeAnimation_i::setAnimationSequence (const char* theSequence)
2155 {
2156   myAnim->setAnimationSequence(theSequence);
2157 }
2158
2159 char* VISU_TimeAnimation_i::getAnimationSequence()
2160 {
2161   return myAnim->getAnimationSequence();
2162 }
2163
2164 CORBA::Boolean VISU_TimeAnimation_i::isSequenceDefined()
2165 {
2166   return myAnim->isSequenceDefined();
2167 }
2168
2169 void VISU_TimeAnimation_i::dumpTo (const char* thePath)
2170 {
2171   myAnim->dumpTo(thePath);
2172 }
2173
2174 char* VISU_TimeAnimation_i::setDumpFormat (const char* theFormat)
2175 {
2176   string aDumpFormat = myAnim->setDumpFormat(theFormat);
2177   return CORBA::string_dup(aDumpFormat.c_str());
2178 }
2179
2180 CORBA::Boolean VISU_TimeAnimation_i::isCycling()
2181 {
2182   return myAnim->isCycling();
2183 }
2184
2185 CORBA::Boolean VISU_TimeAnimation_i::isCleaningMemoryAtEachFrame(){
2186   return myAnim->isCleaningMemoryAtEachFrame();
2187 }
2188
2189 CORBA::Double VISU_TimeAnimation_i::getMinTime()
2190 {
2191   return myAnim->getMinTime();
2192 }
2193
2194 CORBA::Double VISU_TimeAnimation_i::getMaxTime()
2195 {
2196   return myAnim->getMaxTime();
2197 }
2198
2199 void VISU_TimeAnimation_i::setProportional (CORBA::Boolean theProp)
2200 {
2201   myAnim->setProportional(theProp);
2202 }
2203
2204 void VISU_TimeAnimation_i::setCycling (CORBA::Boolean theCycle)
2205 {
2206   myAnim->setCycling(theCycle);
2207 }
2208
2209 void VISU_TimeAnimation_i::setCleaningMemoryAtEachFrame(CORBA::Boolean theCycle){
2210   myAnim->setCleaningMemoryAtEachFrame(theCycle);
2211 }
2212
2213 SALOMEDS::SObject_ptr VISU_TimeAnimation_i::publishInStudy()
2214 {
2215   return myAnim->publishInStudy();
2216 }
2217
2218 void VISU_TimeAnimation_i::restoreFromStudy(SALOMEDS::SObject_ptr theObj)
2219 {
2220   myAnim->restoreFromStudy(theObj);
2221 }
2222
2223 CORBA::Boolean VISU_TimeAnimation_i::isSavedInStudy()
2224 {
2225   return myAnim->isSavedInStudy();
2226 }
2227
2228 void VISU_TimeAnimation_i::saveAnimation()
2229 {
2230   myAnim->saveAnimation();
2231 }
2232
2233 void VISU_TimeAnimation_i::setAnimationMode(VISU::Animation::AnimationMode theMode)
2234 {
2235   myAnim->setAnimationMode(theMode);
2236 }
2237
2238 VISU::Animation::AnimationMode VISU_TimeAnimation_i::getAnimationMode()
2239 {
2240   return VISU::Animation::AnimationMode(myAnim->getAnimationMode());
2241 }
2242
2243 void VISU_TimeAnimation_i::ApplyProperties(CORBA::Long theFieldNum, VISU::ColoredPrs3d_ptr thePrs)
2244   throw (SALOME::SALOME_Exception)
2245 {
2246   myAnim->ApplyProperties(theFieldNum, thePrs);
2247 }