Salome HOME
Merge with branch V2_2_0_VISU_improvement
[modules/visu.git] / src / VISUGUI / VisuGUI_Tools.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : VisuGUI_Tools.cxx
25 //  Author : Sergey Anikin
26 //  Module : VISU
27
28
29 #include "VisuGUI_Tools.h"
30
31 #include "VISU_Gen_i.hh"
32 #include "VISU_Prs3d_i.hh"
33 #include "VISU_Result_i.hh"
34 #include "VISU_Table_i.hh"
35 #include "VISU_Mesh_i.hh"
36 #include "VISU_ViewManager_i.hh"
37
38 #include "VISU_Actor.h"
39
40 #include "SalomeApp_Module.h"
41 #include "SalomeApp_Study.h"
42 #include "SalomeApp_Application.h"
43 #include "SalomeApp_SelectionMgr.h"
44
45 #include "SALOME_ListIO.hxx"
46 #include "SALOME_ListIteratorOfListIO.hxx"
47
48 #include "SVTK_ViewWindow.h"
49 #include "SVTK_ViewModel.h"
50 #include "SVTK_Functor.h"
51
52 #include "VTKViewer_Algorithm.h"
53
54 #include "SPlot2d_ViewModel.h"
55 #include "Plot2d_ViewFrame.h"
56
57 #include "SUIT_Session.h"
58 #include "SUIT_MessageBox.h"
59
60 #include <vtkRenderer.h>
61 #include <vtkActorCollection.h>
62
63
64 //=============================================================================
65 namespace VISU
66 {
67   SUIT_Desktop*
68   GetDesktop(const CAM_Module* theModule)
69   {
70     return theModule->application()->desktop();
71   }
72
73   SalomeApp_SelectionMgr*
74   GetSelectionMgr(const SalomeApp_Module* theModule)
75   {
76     return theModule->getApp()->selectionMgr();
77   }
78
79   SalomeApp_Study*
80   GetAppStudy(const CAM_Module* theModule)
81   {
82     return
83       dynamic_cast<SalomeApp_Study*>(theModule->application()->activeStudy());
84   }
85
86   _PTR(Study)
87   GetCStudy(const SalomeApp_Study* theStudy)
88   {
89     return theStudy->studyDS();
90   }
91
92   SALOMEDS::StudyManager_var
93   GetStudyManager()
94   {
95     static SALOMEDS::StudyManager_var aStudyManager;
96     if(CORBA::is_nil(aStudyManager)){
97       SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
98       CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
99       aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
100     }
101     return aStudyManager;
102   }
103
104   bool
105   IsStudyLocked( _PTR(Study) theStudy )
106   {
107     if( theStudy )
108       return theStudy->GetProperties()->IsLocked();
109     return true;
110   }
111
112   bool
113   CheckLock( _PTR(Study) theStudy )
114   {
115     if(IsStudyLocked(theStudy))
116       throw std::runtime_error(QObject::tr("WRN_STUDY_LOCKED").latin1());
117     return false;
118   }
119
120   VISU_Gen_i*
121   GetVisuGen(const CAM_Module* theModule)
122   {
123     static VISU_Gen_i* aGen = NULL;
124     if(!aGen){
125       SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
126       Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
127       VISU_Gen_var aVISU = VISU_Gen::_narrow(aComponent);
128       if(!CORBA::is_nil(aVISU)){
129         aGen = VISU_Gen_i::GetVisuGenImpl();
130         aGen->SetCurrentStudy(GetDSStudy(GetCStudy(GetAppStudy(theModule))));
131       }
132     }
133     if(!aGen)
134       throw std::runtime_error(QObject::tr("ERR_CANT_FIND_VISU_COMPONENT").latin1());
135     return aGen;
136   }
137
138   SALOME_MED::MED_Gen_var
139   GetMEDEngine()
140   {
141     static SALOME_MED::MED_Gen_var aGen;
142     if(CORBA::is_nil(aGen)){
143       SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
144       Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","MED");
145       aGen = SALOME_MED::MED_Gen::_narrow(aComponent);
146     }
147     if(CORBA::is_nil(aGen))
148       throw std::runtime_error(QObject::tr("ERR_CANT_FIND_MED_COMPONENT").latin1());
149     return aGen;
150   }
151
152   VISU::Storable::TRestoringMap
153   getMapOfValue(SALOMEDS::SObject_var theSObject)
154   {
155     VISU::Storable::TRestoringMap aMap;
156     if(!theSObject->_is_nil()){
157       SALOMEDS::GenericAttribute_var anAttr;
158       if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
159         SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
160         CORBA::String_var aValue = aComment->Value();
161         QString aString(aValue.in());
162         VISU::Storable::StrToMap(aString,aMap);
163       }
164     }
165     return aMap;
166   }
167
168   QString
169   getValue(SALOMEDS::SObject_var theSObject,
170            QString theKey)
171   {
172     QString aStr("");
173     VISU::Storable::TRestoringMap aMap = getMapOfValue(theSObject);
174     if(!aMap.empty())
175       aStr = VISU::Storable::FindValue(aMap,theKey.latin1());
176     return aStr;
177   }
178
179   VISU::Storable::TRestoringMap getMapOfValue (_PTR(SObject) theSObject)
180   {
181     VISU::Storable::TRestoringMap aMap;
182     if (theSObject) {
183       _PTR(GenericAttribute) anAttr;
184       if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
185         _PTR(AttributeComment) aComment (anAttr);
186         std::string aValue = aComment->Value();
187         QString aString (aValue.c_str());
188         VISU::Storable::StrToMap(aString, aMap);
189       }
190     }
191     return aMap;
192   }
193
194   QString getValue (_PTR(SObject) theSObject, QString theKey)
195   {
196     QString aStr("");
197     VISU::Storable::TRestoringMap aMap = getMapOfValue(theSObject);
198     if (!aMap.empty())
199       aStr = VISU::Storable::FindValue(aMap, theKey.latin1());
200     return aStr;
201   }
202
203   //************************************************************
204   // Selection
205   CORBA::Object_var
206   GetSelectedObj(const SalomeApp_Study* theStudy,
207                  const QString& theEntry,
208                  VISU::Storable::TRestoringMap* theMap)
209   {
210     if ( !theStudy || theEntry.isEmpty() )
211       return CORBA::Object::_nil();
212
213     SALOMEDS::Study_var aStudy = GetDSStudy(GetCStudy( theStudy ));
214     SALOMEDS::SObject_var aSObject = aStudy->FindObjectID( theEntry.latin1() );
215     if(!aSObject->_is_nil()){
216       SALOMEDS::GenericAttribute_var anAttr;
217       if(theMap && aSObject->FindAttribute(anAttr,"AttributeComment")){
218         SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
219         std::string aValue = aComment->Value();
220         QString aString(aValue.c_str());
221         VISU::Storable::StrToMap(aString,*theMap);
222       }
223       return VISU::SObjectToObject(aSObject);
224     }
225     return CORBA::Object::_nil();
226   }
227
228   CORBA::Object_var
229   GetSelectedObj(const SalomeApp_Module* theModule,
230                  Handle(SALOME_InteractiveObject)* theIO,
231                  VISU::Storable::TRestoringMap* theMap)
232   {
233     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
234     SALOME_ListIO aListIO;
235     aSelectionMgr->selectedObjects(aListIO);
236     SALOME_ListIteratorOfListIO anIter(aListIO);
237     if(anIter.More()){
238       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
239       if(theIO)
240         *theIO = anIO;
241       if(anIO->hasEntry())
242         return GetSelectedObj( GetAppStudy(theModule), anIO->getEntry(), theMap );
243     }
244     return CORBA::Object::_nil();
245   }
246
247   void
248   Add(SalomeApp_SelectionMgr* theSelectionMgr,
249       const Handle(SALOME_InteractiveObject)& theIO)
250   {
251     SALOME_ListIO aListIO;
252     theSelectionMgr->selectedObjects(aListIO);
253     aListIO.Append(theIO);
254     theSelectionMgr->setSelectedObjects(aListIO);
255   }
256
257   void
258   Remove(SalomeApp_SelectionMgr* theSelectionMgr,
259          const Handle(SALOME_InteractiveObject)& theIO)
260   {
261     if (theIO.IsNull()) return;
262     SALOME_ListIO aListIO, aNewListIO;
263     theSelectionMgr->selectedObjects(aListIO);
264     SALOME_ListIteratorOfListIO anIter (aListIO);
265     for (; anIter.More(); anIter.Next()) {
266       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
267       if (!anIO->isSame(theIO)) {
268         aNewListIO.Append(theIO);
269       }
270     }
271     theSelectionMgr->setSelectedObjects(aNewListIO);
272   }
273
274   bool
275   IsRemovableSelected (const SalomeApp_Module* theModule)
276   {
277     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
278     SALOME_ListIO aListIO;
279     aSelectionMgr->selectedObjects(aListIO);
280
281     if (aListIO.Extent() < 1)
282       return false;
283
284     SALOME_ListIteratorOfListIO anIter (aListIO);
285     for (; anIter.More(); anIter.Next()) {
286       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
287       if (anIO->hasEntry()) {
288         _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
289         _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
290         VISU::Storable::TRestoringMap pMap;
291         if (aSObject) {
292           CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(aSObject);
293           if (!CORBA::is_nil(aCORBAObject)) {
294             VISU::RemovableObject_var aRemovableObj = VISU::RemovableObject::_narrow(aCORBAObject);
295             if (CORBA::is_nil(aRemovableObj)) {
296               // Not removable CORBA object
297               return false;
298             }
299           } else {
300             // Can be removed, if lays directly under VISU
301             // (first sub-level) or is a child of such an object
302             string aNAME, aVisuNAME = GetVisuGen(theModule)->ComponentDataType();
303             _PTR(GenericAttribute) anAttr;
304             _PTR(AttributeComment) aComment;
305
306             bool isUnderVISU = false;
307             _PTR(SObject) aFatherSObject = aSObject->GetFather();
308             if (aFatherSObject->FindAttribute(anAttr, "AttributeComment")) {
309               _PTR(AttributeComment) aComment (anAttr);
310               aNAME = aComment->Value();
311               if (aNAME == aVisuNAME) {
312                 isUnderVISU = true;
313               }
314             }
315             if (!isUnderVISU) {
316               // Not directly under VISU component, check father
317               aCORBAObject = VISU::ClientSObjectToObject(aFatherSObject);
318               if (!CORBA::is_nil(aCORBAObject)) {
319                 // Father has IOR
320                 return false;
321               }
322
323               isUnderVISU = false;
324               aFatherSObject = aFatherSObject->GetFather();
325               if (aFatherSObject->FindAttribute(anAttr, "AttributeComment")) {
326                 _PTR(AttributeComment) aComment (anAttr);
327                 aNAME = aComment->Value();
328                 if (aNAME == aVisuNAME) {
329                   isUnderVISU = true;
330                 }
331               }
332               if (!isUnderVISU) {
333                 // Father is not directly under VISU component
334                 return false;
335               }
336             }
337           }
338         }
339       }
340     }
341     return true;
342   }
343
344   //************************************************************
345   // Display/Erase
346
347   void
348   ErasePrs (const SalomeApp_Module* theModule,
349             CORBA::Object_ptr theObject, bool theUpdate)
350   {
351     if (!CORBA::is_nil(theObject)) {
352       VISU::Base_var aBase = VISU::Base::_narrow(theObject);
353       if (CORBA::is_nil(aBase)) return;
354       VISU::VISUType aType = aBase->GetType();
355       switch (aType) {
356       case VISU::TCURVE:
357         {
358           if (VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aBase).in()))
359             PlotCurve(theModule, aCurve, VISU::eErase );
360           break;
361         }
362       case VISU::TCONTAINER:
363         {
364           if (VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aBase).in()))
365             PlotContainer(theModule, aContainer, VISU::eErase );
366           break;
367         }
368       case VISU::TTABLE:
369         {
370           if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aBase).in()))
371             PlotTable(theModule, aTable, VISU::eErase );
372           break;
373         }
374       default:
375         {
376           if (VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aBase).in())) {
377             ErasePrs3d(theModule, aPrsObject);
378             if (theUpdate) {
379               if (SVTK_ViewWindow* vw = GetViewWindow(theModule))
380                 vw->Repaint();
381             }
382           }
383         }
384       } // switch (aType)
385     }
386   }
387
388   void
389   DeleteSObject (SalomeApp_Module* theModule,
390                  SALOMEDS::Study_ptr theStudy,
391                  SALOMEDS::SObject_ptr theSObject)
392   {
393     SALOMEDS::ChildIterator_var aChildIter = theStudy->NewChildIterator(theSObject);
394     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
395       SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
396       CORBA::Object_var aChildObj = VISU::SObjectToObject(aChildSObject);
397       ErasePrs(theModule, aChildObj);
398     }
399
400     CORBA::Object_var anObj = VISU::SObjectToObject(theSObject);
401     if (!CORBA::is_nil(anObj)) {
402       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(anObj);
403       if (!CORBA::is_nil(aRemovableObject)) {
404         aRemovableObject->RemoveFromStudy();
405       }
406     } else {
407       // Remove aSObject together with all its sub-objects
408       VISU::RemoveFromStudy(theSObject,
409                             false,  // remove not only IOR attribute, but Object With Children
410                             false); // not Destroy() sub-objects
411     }
412   }
413
414   void
415   DeletePrs3d(SalomeApp_Module* theModule,
416               VISU::Prs3d_i* thePrs,
417               const Handle(SALOME_InteractiveObject)& theIO)
418   {
419     if(!thePrs)
420       return;
421     if(CheckLock(GetCStudy(GetAppStudy(theModule))))
422       return;
423     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
424     CORBA::String_var anEntry = aSObject->GetID();
425     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
426     Remove(aSelectionMgr,theIO);
427     TViewWindows aViewWindows = GetViews(theModule);
428     for(int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++){
429       SVTK_ViewWindow* aView = aViewWindows[i];
430       if(VISU_Actor* anActor = FindActor(aView,anEntry.in())){
431         aView->RemoveActor(anActor);
432         anActor->Delete();
433       }
434     }
435     thePrs->RemoveFromStudy();
436     theModule->updateObjBrowser(); //update Object browser
437   }
438
439   void
440   ErasePrs3d(const SalomeApp_Module* theModule,
441              VISU::Prs3d_i* thePrs)
442   {
443     if ( SVTK_ViewWindow* vw = GetViewWindow( theModule ) ){
444       VISU_Actor* anVISUActor = FindActor( vw, thePrs );
445       if (anVISUActor) {
446         anVISUActor->VisibilityOff();
447       }
448     }
449   }
450
451   //************************************************************
452   // Presentation management
453
454   void
455   ChangeRepresentation (const SalomeApp_Module* theModule,
456                         VISU::PresentationType  theType)
457   {
458     SUIT_ViewWindow* aView = GetActiveView(theModule, VTKViewer_Viewer::Type());
459     if (!aView) return;
460     SVTK_ViewWindow* vw  = (SVTK_ViewWindow*) aView;
461
462     Handle(SALOME_InteractiveObject) anIO;
463     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
464     if (CORBA::is_nil(anObject)) return;
465     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
466     if (!aServant.in()) return;
467
468     VISU::Prs3d_i* aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
469     if (aPrs3d) {
470       if (VISU_Actor* anActor = GetActor(aPrs3d, vw)) {
471         switch (theType) {
472         case VISU::SHRINK:
473           if (anActor->IsShrunk())
474             anActor->UnShrink();
475           else
476             anActor->SetShrink();
477           break;
478         default:
479           if (VISU::Mesh_i* aMesh = dynamic_cast<VISU::Mesh_i*>(aPrs3d)) {
480             aMesh->SetPresentationType(theType);
481             RecreateActor(theModule, aMesh);
482           } else {
483             anActor->SetRepresentation(theType);
484           }
485         }
486         vw->Repaint();
487       }
488     }
489   }
490
491   //************************************************************
492   // SObject type
493
494   bool
495   CheckTimeStamp(const SalomeApp_Module* theModule,
496                  SALOMEDS::SObject_var& theSObject,
497                  Handle(SALOME_InteractiveObject)* theIO)
498   {
499     Handle(SALOME_InteractiveObject) anIO;
500     CORBA::Object_var anObject = GetSelectedObj(theModule,&anIO);
501     if(theIO)
502       *theIO = anIO;
503     if(!anIO.IsNull() && anIO->hasEntry()){
504       SALOMEDS::Study_var aStudy = GetDSStudy(GetCStudy(GetAppStudy(theModule)));
505       theSObject = aStudy->FindObjectID(anIO->getEntry());
506       QString aValue = getValue(theSObject,"myType");
507       if(aValue.toInt() == int(VISU::TTIMESTAMP))
508         return true;
509     }
510     SUIT_MessageBox::warn1(GetDesktop(theModule),
511                            QObject::tr("WRN_VISU"),
512                            QObject::tr("WRN_NO_AVAILABLE_DATA"),
513                            QObject::tr("BUT_OK") );
514     return false;
515   }
516
517
518   VISU::Result_i*
519   CheckResult(const SalomeApp_Module* theModule,
520               SALOMEDS::SObject_var theSource,
521               VISU::Result_var& theResult)
522   {
523     SALOMEDS::SObject_var aSObj;
524
525     aSObj = theSource->GetFather();
526     if (CORBA::is_nil(aSObj))
527       return NULL;
528
529     aSObj = aSObj->GetFather();
530     if (CORBA::is_nil(aSObj))
531       return NULL;
532
533     aSObj = aSObj->GetFather();
534     if (CORBA::is_nil(aSObj))
535       return NULL;
536
537     CORBA::Object_var anObject = VISU::SObjectToObject(aSObj);
538     if (CORBA::is_nil(anObject)) {
539       aSObj = aSObj->GetFather();
540       if (CORBA::is_nil(aSObj))
541         return NULL;
542       anObject = VISU::SObjectToObject(aSObj);
543     }
544
545     if (CORBA::is_nil(anObject))
546       return NULL;
547
548     theResult = VISU::Result::_narrow(anObject);
549     VISU::Result_i* pResult = dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
550     if(pResult == NULL)
551       SUIT_MessageBox::warn1(GetDesktop(theModule),
552                              QObject::tr("WRN_VISU"),
553                              QObject::tr("WRN_NO_AVAILABLE_DATA"),
554                              QObject::tr("BUT_OK"));
555     return pResult;
556   }
557
558   //************************************************************
559   // Views
560
561   SUIT_ViewWindow* GetActiveView(const SalomeApp_Module* theModule, QString theType)
562   {
563     if(SalomeApp_Application* anApp = theModule->getApp()){
564       if(SUIT_ViewManager* aViewManager = anApp->activeViewManager()){
565         if (!theType.isNull()) {
566           if (aViewManager->getType() != theType)
567             return 0;
568         }
569         return aViewManager->getActiveView();
570       }
571     }
572     return 0;
573   }
574
575   //************************************************************
576   // VTK View
577
578   TViewWindows
579   GetViews(const SalomeApp_Module* theModule)
580   {
581     TViewWindows aViewWindows;
582     if(SalomeApp_Application* anApp = theModule->getApp()){
583       ViewManagerList aViewManagerList;
584       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
585       QPtrListIterator<SUIT_ViewManager> anIter(aViewManagerList);
586       while(SUIT_ViewManager* aViewManager = anIter.current()){
587         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
588         for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
589           if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
590             if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
591               aViewWindows.push_back(aView);
592         }
593         ++anIter;
594       }
595     }
596     return aViewWindows;
597   }
598
599   SVTK_ViewWindow*
600   GetViewWindow(const SalomeApp_Module* theModule, const bool theCreate )
601   {
602     if(SalomeApp_Application* anApp = theModule->getApp()){
603       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate )){
604         if(SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()){
605           return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
606         }
607       }
608     }
609     return NULL;
610   }
611
612
613   SVTK_ViewWindow*
614   GetViewWindow()
615   {
616     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
617       (SUIT_Session::session()->activeApplication());
618     if (anApp) {
619       if (SUIT_ViewManager* aViewManager = anApp->activeViewManager()) {
620         if (aViewManager->getType() == SVTK_Viewer::Type()) {
621           if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
622             return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
623           }
624         }
625       }
626     }
627     return NULL;
628   }
629
630   VISU_Actor* 
631   PublishInView(const SalomeApp_Module* theModule,
632                 VISU::Prs3d_i* thePrs)
633   {
634     VISU_Actor* aActor = NULL;
635     if(!thePrs)
636       return aActor;
637     if(SVTK_ViewWindow* aView = GetViewWindow(theModule)){
638       QApplication::setOverrideCursor( Qt::waitCursor );
639       if(aActor = thePrs->CreateActor()){
640         aView->AddActor(aActor);
641       }
642       QApplication::restoreOverrideCursor();
643     }
644     return aActor;
645   }
646
647   VISU_Actor* 
648   UpdateViewer(const SalomeApp_Module* theModule,
649                VISU::Prs3d_i* thePrs, 
650                bool theDispOnly)
651   {
652     SVTK_ViewWindow* vw = GetViewWindow( theModule );
653     if (!vw) return NULL;
654
655     vtkRenderer *aRen = vw->getRenderer();
656     vtkActorCollection *anActColl = aRen->GetActors();
657
658     vtkActor *anActor;
659     VISU_Actor* anVISUActor = NULL;
660     VISU_Actor* aResActor = NULL;
661     for(anActColl->InitTraversal(); (anActor = anActColl->GetNextActor()) != NULL; ){
662       if(!SALOME_Actor::SafeDownCast(anActor))
663         continue;
664       if(anActor->IsA("VISU_Actor")){ 
665         anVISUActor = VISU_Actor::SafeDownCast(anActor);
666         VISU::Prs3d_i* aPrs = anVISUActor->GetPrs3d();      
667         if(aPrs == NULL) continue;
668         if (thePrs == aPrs) {
669           aResActor = anVISUActor->GetParent();
670           thePrs->UpdateActor(aResActor);
671           aResActor->VisibilityOn();
672           
673         } else if (theDispOnly) {
674           anVISUActor->GetParent()->VisibilityOff();
675         } else {
676         }
677       } else if (theDispOnly && anActor->GetVisibility()) {
678         anActor->VisibilityOff();
679       } else {
680       } 
681     }
682     if (aResActor) 
683       return aResActor;
684
685     anVISUActor = PublishInView( theModule, thePrs );
686     return anVISUActor;
687   }
688
689   VISU_Actor*
690   FindActor(SVTK_ViewWindow* theViewWindow,
691             const char* theEntry)
692   {
693     using namespace VTK;
694     if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
695       if(vtkActorCollection* aCollection = aRenderer->GetActors()){
696         if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
697           return anActor->GetParent();
698         }
699       }
700     }
701     return NULL;
702   }
703
704   VISU_Actor*
705   FindActor(SVTK_ViewWindow* theViewWindow,
706             VISU::Prs3d_i* thePrs)
707   {
708     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
709     CORBA::String_var anEntry = aSObject->GetID();
710     return FindActor(theViewWindow,anEntry.in());
711   }
712
713   void
714   RecreateActor (const SalomeApp_Module* theModule,
715                  VISU::Prs3d_i* thePrs)
716   {
717     QApplication::setOverrideCursor(Qt::waitCursor);
718
719     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
720     CORBA::String_var anEntry = aSObject->GetID();
721
722     try {
723       thePrs->Update();
724
725       TViewWindows aViewWindows = GetViews(theModule);
726       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
727         SVTK_ViewWindow* aView = aViewWindows[i];
728         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
729           thePrs->UpdateActor(anActor);
730         }
731       }
732     } catch (std::runtime_error& ex) {
733       INFOS(ex.what());
734       QApplication::restoreOverrideCursor();
735       SUIT_MessageBox::warn1 (GetDesktop(theModule), QObject::tr("WRN_VISU"), 
736                               QObject::tr("ERR_CANT_BUILD_PRESENTATION") + " " + QObject::tr(ex.what()), 
737                               QObject::tr("BUT_OK"));
738
739       TViewWindows aViewWindows = GetViews(theModule);
740       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
741         SVTK_ViewWindow* aView = aViewWindows[i];
742         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
743           aView->RemoveActor(anActor);
744           anActor->Delete();
745         }
746       }
747       return;
748     }
749     QApplication::restoreOverrideCursor();
750   }
751
752   //************************************************************
753   // Plot2d View
754
755   SPlot2d_Viewer*
756   GetPlot2dViewer(const SalomeApp_Module* theModule, const bool theCreate)
757   {
758     if(SalomeApp_Application* anApp = theModule->getApp()){
759       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( Plot2d_Viewer::Type(), theCreate )){
760         return dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
761       }
762     }
763     return NULL;
764   }
765
766   // Internal function used by several public functions below
767   void
768   UpdateCurve(VISU::Curve_i* theCurve,
769               Plot2d_ViewFrame* aPlot,
770               SPlot2d_Curve* plotCurve,
771               int theDisplaying)
772   {
773     if ( theDisplaying == VISU::eErase ) {
774       if ( plotCurve )
775         aPlot->eraseCurve( plotCurve, false );
776     }
777     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
778       if ( plotCurve ) {
779         plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
780         //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
781         plotCurve->setVerTitle( theCurve->GetName() );
782         plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
783         plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
784         double* xList = 0;
785         double* yList = 0;
786         int     nbPoints = theCurve->GetData( xList, yList );
787         if ( nbPoints > 0 && xList && yList ) {
788           plotCurve->setData( xList, yList, nbPoints );
789         }
790         if ( !theCurve->IsAuto() ) {
791           plotCurve->setLine( (Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
792           plotCurve->setMarker( (Plot2d_Curve::MarkerType)theCurve->GetMarker() ); 
793           SALOMEDS::Color color = theCurve->GetColor();
794           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
795         }
796         plotCurve->setAutoAssign( theCurve->IsAuto() );
797         aPlot->displayCurve( plotCurve, false );
798       }
799       else {
800         Plot2d_Curve* crv = theCurve->CreatePresentation();
801         if ( crv ) {
802           aPlot->displayCurve( crv, false );
803           theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
804           theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
805           SALOMEDS::Color newColor;
806           newColor.R = crv->getColor().red()/255.;
807           newColor.G = crv->getColor().green()/255.;
808           newColor.B = crv->getColor().blue()/255.;
809           theCurve->SetColor( newColor );
810           crv->setAutoAssign( theCurve->IsAuto() );
811         }
812       }
813     }
814   }
815
816   void                                 
817   PlotTable(const SalomeApp_Module* theModule, 
818             VISU::Table_i* table, 
819             int theDisplaying)
820   {
821     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true ); // create if necessary
822     if ( !aView )
823       return;
824     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
825     if ( !aPlot )
826       return;
827
828     if ( theDisplaying == VISU::eDisplayOnly ) 
829       aPlot->EraseAll();
830     QList<Plot2d_Curve> clist;
831     aPlot->getCurves( clist );
832     _PTR(Study) aStudy = GetCStudy( GetAppStudy( theModule ) );
833     _PTR(SObject) TableSO = aStudy->FindObjectID( table->GetEntry().latin1() );
834     if ( TableSO ) {
835       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
836       for ( ; Iter->More(); Iter->Next() ) {
837         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
838         if( !CORBA::is_nil( childObject ) ) {
839           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
840           if( !CORBA::is_nil( aCurve ) ) {
841             VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
842             SPlot2d_Curve* plotCurve = 0;
843             SPlot2d_Curve* tmpCurve;
844             for ( int i = 0; i < clist.count(); i++ ) {
845               tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
846               if (tmpCurve && tmpCurve->hasIO() &&
847                   !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
848                 plotCurve = tmpCurve;
849                 break;
850               }
851             }
852
853             UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
854
855             if ( theDisplaying == VISU::eErase && plotCurve ) {
856               clist.remove( plotCurve );
857             }
858           }
859         }
860       }
861       aPlot->Repaint();
862     }
863   }
864
865   void
866   PlotCurve(const SalomeApp_Module* theModule, 
867             VISU::Curve_i* theCurve, 
868             int theDisplaying)
869   {
870     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
871     if ( !aView )
872       return;
873     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
874     if ( !aPlot )
875       return;
876
877 //  if ( theDisplaying == VISU::eDisplayOnly ) 
878 //    aPlot->EraseAll();
879     QList<Plot2d_Curve> clist;
880     aPlot->getCurves( clist );
881     SPlot2d_Curve* plotCurve = 0;
882     SPlot2d_Curve* tmpCurve;
883     for (int i = 0; i < clist.count(); i++) {
884       tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
885       if (tmpCurve && tmpCurve->hasIO() &&
886           !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
887         plotCurve = tmpCurve;
888       } else if (theDisplaying == VISU::eDisplayOnly) {
889         aPlot->eraseCurve(clist.at(i));
890       }
891     }
892
893     UpdateCurve(theCurve, aPlot, plotCurve, theDisplaying);
894
895     aPlot->Repaint();
896   }
897
898   void
899   PlotContainer(const SalomeApp_Module* theModule, 
900                 VISU::Container_i* container, 
901                 int theDisplaying)
902   {
903     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
904     if ( !aView )
905       return;
906     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
907     if ( !aPlot )
908       return;
909
910     if ( theDisplaying == VISU::eDisplayOnly ) 
911       aPlot->EraseAll();
912     QList<Plot2d_Curve> clist;
913     aPlot->getCurves( clist );
914     if ( container->GetNbCurves() > 0 ) {
915       int nbCurves = container->GetNbCurves();
916       for ( int k = 1; k <= nbCurves; k++ ) {
917         VISU::Curve_i* theCurve = container->GetCurve( k );
918         if ( theCurve && theCurve->IsValid() ) {
919           SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>
920             (aView->getCurveByIO(new SALOME_InteractiveObject (theCurve->GetEntry(), "", "")));
921           
922           UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
923
924           if ( plotCurve && theDisplaying == VISU::eErase ) {
925             clist.remove( plotCurve );
926           }
927         }
928       }
929     }
930     aPlot->Repaint();
931   }
932
933   void
934   CreatePlot(SalomeApp_Module* theModule,
935              _PTR(SObject) theTableSO)
936   {
937     if ( IsSObjectTable(theTableSO) ) {
938       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
939       CORBA::Object_var aContainer = GetVisuGen( theModule )->CreateContainer();
940       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
941         VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
942         VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
943       
944         if ( pContainer && pTable ) {
945           for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
946             CORBA::Object_var aNewCurve = GetVisuGen( theModule )->CreateCurve( pTable->_this(), 1, i );
947             if( !CORBA::is_nil( aNewCurve ) ) {
948               VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
949               if ( pCrv ) {
950                 pContainer->AddCurve( pCrv->_this() );
951               }
952             }
953           }
954           theModule->updateObjBrowser();
955           PlotContainer( theModule, pContainer, VISU::eDisplay );
956         }
957       }
958     }
959   }
960
961   //************************************************************
962   // Others
963
964   void CreateMesh (const SalomeApp_Module* theModule,
965                    const Handle(SALOME_InteractiveObject)& theIO)
966   {
967     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
968     //if (CheckLock(aStudy))
969     //  return;
970
971     SALOMEDS::SObject_var aResultSObj =
972       GetDSStudy(aStudy)->FindObjectID(theIO->getEntry());
973
974     // Get VISU::Result
975     VISU::Result_var aResult;
976     VISU::Result_i* pResult = CheckResult(theModule, aResultSObj, aResult);
977     if (pResult == NULL)
978       return;
979
980     Storable::TRestoringMap aMap = getMapOfValue(aResultSObj);
981     bool isExist;
982     string aComment = Storable::FindValue(aMap,"myComment",&isExist).latin1();
983     if (!isExist)
984       return;
985
986     CORBA::Object_var aMesh;
987     string aMeshName = Storable::FindValue(aMap,"myMeshName").latin1();
988 #ifdef CHECKTIME
989     Utils_Timer timer;
990     timer.Start();
991 #endif
992     if (aComment == "ENTITY") {
993       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myId").toInt();
994       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity))
995         aMesh = GetVisuGen(theModule)->MeshOnEntity(aResult,aMeshName.c_str(),anEntity);
996     } else if (aComment == "FAMILY") {
997       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myEntityId").toInt();
998       string aFamilyName = Storable::FindValue(aMap,"myName").latin1();
999       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity,aFamilyName.c_str()))
1000         aMesh = GetVisuGen(theModule)->FamilyMeshOnEntity
1001           (aResult,aMeshName.c_str(),anEntity,aFamilyName.c_str());
1002     } else if (aComment == "GROUP") {
1003       string aGroupName = Storable::FindValue(aMap,"myName").latin1();
1004       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),aGroupName.c_str()))
1005         aMesh = GetVisuGen(theModule)->GroupMesh(aResult,aMeshName.c_str(),aGroupName.c_str());
1006     }
1007 #ifdef CHECKTIME
1008     timer.Stop();
1009     MESSAGE("VisuGUI::CreateMesh() - CREATE MESH");
1010     timer.Show();
1011 #endif
1012
1013     QApplication::restoreOverrideCursor();
1014     VISU::Mesh_i* pPresent = NULL;
1015     if (!CORBA::is_nil(aMesh))
1016       pPresent = dynamic_cast<VISU::Mesh_i*>(VISU::GetServant(aMesh).in());
1017     if (pPresent == NULL) {
1018       SUIT_MessageBox::warn1 (GetDesktop(theModule),
1019                               QObject::tr("WRN_VISU"),
1020                               QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
1021                               QObject::tr("BUT_OK"));
1022       return;
1023     }
1024
1025     if (SVTK_ViewWindow* aView = GetViewWindow(theModule)) {
1026       try {
1027 #ifdef CHECKTIME
1028         Utils_Timer timer;
1029         timer.Start();
1030 #endif
1031         PublishInView(theModule, pPresent);
1032         aView->onFitAll();
1033 #ifdef CHECKTIME
1034         timer.Stop();
1035         MESSAGE("VisuGUI::CreateMesh() - DISPLAY MESH");
1036         timer.Show();
1037 #endif
1038         theModule->application()->putInfo(QObject::tr("INF_DONE"));
1039       } catch (std::runtime_error& exc) {
1040         INFOS(exc.what());
1041         SUIT_MessageBox::warn1 (GetDesktop(theModule),
1042                                 QObject::tr("WRN_VISU"),
1043                                 QObject::tr("ERR_CANT_CREATE_ACTOR") + " " + QObject::tr(exc.what()),
1044                                 QObject::tr("BUT_OK"));
1045       }
1046     }
1047   }
1048
1049   // ========================================================================================
1050   // GetPrs3dList: find list of presentations for the given object
1051   // ========================================================================================
1052   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1053                                             const Handle(SALOME_InteractiveObject)& theIO)
1054   {
1055     std::vector<VISU::Prs3d_i*> aList;
1056     if (!theIO.IsNull() && theIO->hasEntry()) {
1057       _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1058       SALOMEDS::Study_var aStudy = GetDSStudy(aCStudy);
1059       SALOMEDS::SObject_var aSObject = aStudy->FindObjectID(theIO->getEntry());
1060       aList = GetPrs3dList(theModule, aSObject);
1061     }
1062     return aList;
1063   }
1064
1065   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1066                                             SALOMEDS::SObject_ptr theObject)
1067   {
1068     _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1069     SALOMEDS::Study_var aStudy = GetDSStudy(aCStudy);
1070
1071     std::vector<VISU::Prs3d_i*> aList; int k = 0;
1072     if (!theObject->_is_nil()) {
1073       CORBA::Object_var aCORBAObject = VISU::SObjectToObject(theObject);
1074       if (!CORBA::is_nil(aCORBAObject)) {
1075         VISU::Base_var aVisuObj = VISU::Base::_narrow(aCORBAObject);
1076         if (!CORBA::is_nil(aVisuObj)) {
1077           VISU::VISUType aType = aVisuObj->GetType();
1078           switch (aType) {
1079           case VISU::TSCALARMAP:
1080           case VISU::TISOSURFACE:
1081           case VISU::TDEFORMEDSHAPE:
1082           case VISU::TCUTPLANES:
1083           case VISU::TCUTLINES:
1084           case VISU::TVECTORS:
1085           case VISU::TSTREAMLINES:
1086           case VISU::TPLOT3D:
1087             {
1088               PortableServer::ServantBase_var aServant = VISU::GetServant(aCORBAObject);
1089               if (aServant.in()) {
1090                 VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
1091                 aList.resize(k+1); aList[k] = aPrsObject; k++;
1092               }
1093             }
1094             break;
1095           case VISU::TFIELD:
1096             {
1097               SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theObject);
1098               SALOMEDS::SObject_var aTimeStamp;
1099               anIter->Next(); // First is reference on support
1100               for(;anIter->More();anIter->Next()) {
1101                 aTimeStamp = anIter->Value();
1102                 if (aTimeStamp->_is_nil()) continue;
1103                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1104                 if (!aSubList.empty()) {
1105                   int n = aSubList.size();
1106                   aList.resize(k+n);
1107                   for (int i = 0; i < n; i++)
1108                     { aList[k] = aSubList[i]; k++; }
1109                 }
1110               }
1111             }
1112             break;
1113           }
1114         }
1115       } else {
1116         SALOMEDS::GenericAttribute_var anAttr;
1117         if (theObject->FindAttribute(anAttr, "AttributeComment")) {
1118           SALOMEDS::AttributeComment_var aComment =
1119             SALOMEDS::AttributeComment::_narrow(anAttr);
1120           string aComm = aComment->Value();
1121           QString strIn( aComm.c_str() );
1122           VISU::Storable::TRestoringMap pMap;
1123           VISU::Storable::StrToMap( strIn, pMap );
1124           bool isExist;
1125           VISU::VISUType aType = (VISU::VISUType)VISU::Storable::FindValue(pMap,"myType",&isExist).toInt();
1126           if (isExist) {
1127             switch (aType) {
1128             case VISU::TFIELD:
1129               {
1130                 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theObject);
1131                 SALOMEDS::SObject_var aTimeStamp;
1132                 anIter->Next(); // First is reference on support
1133                 for (;anIter->More();anIter->Next()) {
1134                   aTimeStamp = anIter->Value();
1135                   if (aTimeStamp->_is_nil()) continue;
1136                   std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1137                   if (!aSubList.empty()) {
1138                     int n = aSubList.size();
1139                     aList.resize(k+n);
1140                     for (int i = 0; i < n; i++)
1141                       { aList[k] = aSubList[i]; k++; }
1142                   }
1143                 }
1144               }
1145               break;
1146             case VISU::TTIMESTAMP:
1147               {
1148                 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theObject);
1149                 SALOMEDS::SObject_var aPrs;
1150                 for (;anIter->More();anIter->Next()) {
1151                   aPrs = anIter->Value();
1152                   if (aPrs->_is_nil()) continue;
1153                   std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aPrs);
1154                   if (!aSubList.empty()) {
1155                     int n = aSubList.size();
1156                     aList.resize(k+n);
1157                     for (int i = 0; i < n; i++)
1158                       { aList[k] = aSubList[i]; k++; }
1159                   }
1160                 }
1161               }
1162               break;
1163             }
1164           }
1165         }
1166       }
1167     }
1168     return aList;
1169   }
1170 }