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