]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Tools.cxx
Salome HOME
4606134bce3fa591e69b75e922ec4b656bd2a48e
[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       ErasePrs(theModule, anObj);
385
386       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(anObj);
387       if (!CORBA::is_nil(aRemovableObject)) {
388         aRemovableObject->RemoveFromStudy();
389       }
390     } else {
391       // Remove aSObject together with all its sub-objects
392       VISU::RemoveFromStudy(theSObject,
393                             false,  // remove not only IOR attribute, but Object With Children
394                             false); // not Destroy() sub-objects
395     }
396   }
397
398   void
399   DeletePrs3d(SalomeApp_Module* theModule,
400               VISU::Prs3d_i* thePrs,
401               const Handle(SALOME_InteractiveObject)& theIO)
402   {
403     if (!thePrs)
404       return;
405     if (CheckLock(GetCStudy(GetAppStudy(theModule))))
406       return;
407     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
408     CORBA::String_var anEntry = aSObject->GetID();
409     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
410     Remove(aSelectionMgr,theIO);
411     TViewWindows aViewWindows = GetViews(theModule);
412     for(int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++){
413       SVTK_ViewWindow* aView = aViewWindows[i];
414       if(VISU_Actor* anActor = FindActor(aView,anEntry.in())){
415         aView->RemoveActor(anActor);
416         anActor->Delete();
417       }
418     }
419     thePrs->RemoveFromStudy();
420   }
421
422   void
423   ErasePrs3d(const SalomeApp_Module* theModule,
424              VISU::Prs3d_i* thePrs)
425   {
426     if ( SVTK_ViewWindow* vw = GetViewWindow( theModule ) ){
427       VISU_Actor* anVISUActor = FindActor( vw, thePrs );
428       if (anVISUActor) {
429         anVISUActor->VisibilityOff();
430       }
431     }
432   }
433
434   //************************************************************
435   // Presentation management
436
437   void
438   ChangeRepresentation (const SalomeApp_Module* theModule,
439                         VISU::PresentationType  theType)
440   {
441     SUIT_ViewWindow* aView = GetActiveView(theModule, VTKViewer_Viewer::Type());
442     if (!aView) return;
443     SVTK_ViewWindow* vw  = (SVTK_ViewWindow*) aView;
444
445     Handle(SALOME_InteractiveObject) anIO;
446     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
447     if (CORBA::is_nil(anObject)) return;
448     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
449     if (!aServant.in()) return;
450
451     VISU::Prs3d_i* aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
452     if (aPrs3d) {
453       if (VISU_Actor* anActor = GetActor(aPrs3d, vw)) {
454         switch (theType) {
455         case VISU::SHRINK:
456           if (anActor->IsShrunk())
457             anActor->UnShrink();
458           else
459             anActor->SetShrink();
460           break;
461         default:
462           if (VISU::Mesh_i* aMesh = dynamic_cast<VISU::Mesh_i*>(aPrs3d)) {
463             aMesh->SetPresentationType(theType);
464             RecreateActor(theModule, aMesh);
465           } else {
466             anActor->SetRepresentation(theType);
467           }
468         }
469         vw->Repaint();
470       }
471     }
472   }
473
474   //************************************************************
475   // SObject type
476
477   bool
478   CheckTimeStamp(const SalomeApp_Module* theModule,
479                  _PTR(SObject)&          theSObject,
480                  Handle(SALOME_InteractiveObject)* theIO)
481   {
482     Handle(SALOME_InteractiveObject) anIO;
483     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
484     if (theIO)
485       *theIO = anIO;
486     if (!anIO.IsNull() && anIO->hasEntry()){
487       _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
488       theSObject = aStudy->FindObjectID(anIO->getEntry());
489       QString aValue = getValue(theSObject,"myType");
490       if (aValue.toInt() == int(VISU::TTIMESTAMP))
491         return true;
492     }
493     SUIT_MessageBox::warn1(GetDesktop(theModule),
494                            QObject::tr("WRN_VISU"),
495                            QObject::tr("WRN_NO_AVAILABLE_DATA"),
496                            QObject::tr("BUT_OK") );
497     return false;
498   }
499
500   VISU::Result_i*
501   CheckResult(const SalomeApp_Module* theModule,
502               _PTR(SObject)           theSource,
503               VISU::Result_var&       theResult)
504   {
505     _PTR(SObject) aSObj = theSource->GetFather();
506     if (!aSObj)
507       return NULL;
508
509     aSObj = aSObj->GetFather();
510     if (!aSObj)
511       return NULL;
512
513     aSObj = aSObj->GetFather();
514     if (!aSObj)
515       return NULL;
516
517     CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
518     if (CORBA::is_nil(anObject)) {
519       aSObj = aSObj->GetFather();
520       if (!aSObj)
521         return NULL;
522       anObject = VISU::ClientSObjectToObject(aSObj);
523     }
524
525     if (CORBA::is_nil(anObject))
526       return NULL;
527
528     theResult = VISU::Result::_narrow(anObject);
529     VISU::Result_i* pResult = dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
530     if (pResult == NULL)
531       SUIT_MessageBox::warn1(GetDesktop(theModule),
532                              QObject::tr("WRN_VISU"),
533                              QObject::tr("WRN_NO_AVAILABLE_DATA"),
534                              QObject::tr("BUT_OK"));
535     return pResult;
536   }
537
538   //************************************************************
539   // Views
540
541   SUIT_ViewWindow* GetActiveView(const SalomeApp_Module* theModule, QString theType)
542   {
543     if(SalomeApp_Application* anApp = theModule->getApp()){
544       if(SUIT_ViewManager* aViewManager = anApp->activeViewManager()){
545         if (!theType.isNull()) {
546           if (aViewManager->getType() != theType)
547             return 0;
548         }
549         return aViewManager->getActiveView();
550       }
551     }
552     return 0;
553   }
554
555   //************************************************************
556   // VTK View
557
558   TViewWindows
559   GetViews(const SalomeApp_Module* theModule)
560   {
561     TViewWindows aViewWindows;
562     if(SalomeApp_Application* anApp = theModule->getApp()){
563       ViewManagerList aViewManagerList;
564       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
565       QPtrListIterator<SUIT_ViewManager> anIter(aViewManagerList);
566       while(SUIT_ViewManager* aViewManager = anIter.current()){
567         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
568         for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
569           if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
570             if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
571               aViewWindows.push_back(aView);
572         }
573         ++anIter;
574       }
575     }
576     return aViewWindows;
577   }
578
579   SVTK_ViewWindow*
580   GetViewWindow(const SalomeApp_Module* theModule, const bool theCreate )
581   {
582     if(SalomeApp_Application* anApp = theModule->getApp()){
583       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate )){
584         if(SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()){
585           return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
586         }
587       }
588     }
589     return NULL;
590   }
591
592
593   SVTK_ViewWindow*
594   GetViewWindow()
595   {
596     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
597       (SUIT_Session::session()->activeApplication());
598     if (anApp) {
599       if (SUIT_ViewManager* aViewManager = anApp->activeViewManager()) {
600         if (aViewManager->getType() == SVTK_Viewer::Type()) {
601           if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
602             return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
603           }
604         }
605       }
606     }
607     return NULL;
608   }
609
610   VISU_Actor*
611   PublishInView(const SalomeApp_Module* theModule,
612                 VISU::Prs3d_i* thePrs)
613   {
614     VISU_Actor* aActor = NULL;
615     if(!thePrs)
616       return aActor;
617     if(SVTK_ViewWindow* aView = GetViewWindow(theModule)){
618       QApplication::setOverrideCursor( Qt::waitCursor );
619       try{
620         if(aActor = thePrs->CreateActor())
621           aView->AddActor(aActor);
622       }catch(std::exception& exc){
623         SUIT_MessageBox::warn1(GetDesktop(theModule),
624                                QObject::tr("WRN_VISU"),
625                                QObject::tr("ERR_CANT_CREATE_ACTOR"),
626                                QObject::tr("BUT_OK"));
627       }
628       QApplication::restoreOverrideCursor();
629     }
630     return aActor;
631   }
632
633   VISU_Actor*
634   UpdateViewer(const SalomeApp_Module* theModule,
635                VISU::Prs3d_i* thePrs,
636                bool theDispOnly)
637   {
638     SVTK_ViewWindow* vw = GetViewWindow( theModule );
639     if (!vw) return NULL;
640
641     vtkRenderer *aRen = vw->getRenderer();
642     vtkActorCollection *anActColl = aRen->GetActors();
643
644     vtkActor *anActor;
645     VISU_Actor* anVISUActor = NULL;
646     VISU_Actor* aResActor = NULL;
647     for(anActColl->InitTraversal(); (anActor = anActColl->GetNextActor()) != NULL; ){
648       if(!SALOME_Actor::SafeDownCast(anActor))
649         continue;
650       if(anActor->IsA("VISU_Actor")){
651         anVISUActor = VISU_Actor::SafeDownCast(anActor);
652         VISU::Prs3d_i* aPrs = anVISUActor->GetPrs3d();
653         if(aPrs == NULL) continue;
654         if (thePrs == aPrs) {
655           aResActor = anVISUActor->GetParent();
656           thePrs->UpdateActor(aResActor);
657           aResActor->VisibilityOn();
658
659         } else if (theDispOnly) {
660           anVISUActor->GetParent()->VisibilityOff();
661         } else {
662         }
663       } else if (theDispOnly && anActor->GetVisibility()) {
664         anActor->VisibilityOff();
665       } else {
666       }
667     }
668     if (aResActor)
669       return aResActor;
670
671     anVISUActor = PublishInView( theModule, thePrs );
672     return anVISUActor;
673   }
674
675   void
676   RepaintViewWindows (const SalomeApp_Module* theModule,
677                       const Handle(SALOME_InteractiveObject)& theIObject)
678   {
679     TViewWindows aViewWindows;
680     if (SalomeApp_Application* anApp = theModule->getApp()) {
681       ViewManagerList aViewManagerList;
682       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
683       QPtrListIterator<SUIT_ViewManager> anIter (aViewManagerList);
684       while (SUIT_ViewManager* aViewManager = anIter.current()) {
685         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
686         for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
687           if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
688             if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
689               if (vw->isVisible(theIObject)) {
690                 vw->getRenderer()->ResetCameraClippingRange();
691                 vw->Repaint();
692                 vw->highlight(theIObject, true, true);
693               }
694             }
695           }
696         }
697         ++anIter;
698       }
699     }
700   }
701
702   VISU_Actor*
703   FindActor(SVTK_ViewWindow* theViewWindow,
704             const char* theEntry)
705   {
706     using namespace VTK;
707     if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
708       if(vtkActorCollection* aCollection = aRenderer->GetActors()){
709         if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
710           return anActor->GetParent();
711         }
712       }
713     }
714     return NULL;
715   }
716
717   VISU_Actor*
718   FindActor(SVTK_ViewWindow* theViewWindow,
719             VISU::Prs3d_i* thePrs)
720   {
721     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
722     CORBA::String_var anEntry = aSObject->GetID();
723     return FindActor(theViewWindow,anEntry.in());
724   }
725
726   void
727   RecreateActor (const SalomeApp_Module* theModule,
728                  VISU::Prs3d_i* thePrs)
729   {
730     QApplication::setOverrideCursor(Qt::waitCursor);
731
732     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
733     CORBA::String_var anEntry = aSObject->GetID();
734
735     try {
736       thePrs->Update();
737
738       TViewWindows aViewWindows = GetViews(theModule);
739       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
740         SVTK_ViewWindow* aView = aViewWindows[i];
741         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
742           thePrs->UpdateActor(anActor);
743         }
744       }
745     } catch (std::runtime_error& ex) {
746       INFOS(ex.what());
747       QApplication::restoreOverrideCursor();
748       SUIT_MessageBox::warn1 (GetDesktop(theModule), QObject::tr("WRN_VISU"),
749                               QObject::tr("ERR_CANT_BUILD_PRESENTATION") + " " + QObject::tr(ex.what()),
750                               QObject::tr("BUT_OK"));
751
752       TViewWindows aViewWindows = GetViews(theModule);
753       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
754         SVTK_ViewWindow* aView = aViewWindows[i];
755         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
756           aView->RemoveActor(anActor);
757           anActor->Delete();
758         }
759       }
760       return;
761     }
762     QApplication::restoreOverrideCursor();
763   }
764
765   //************************************************************
766   // Plot2d View
767
768   SPlot2d_Viewer*
769   GetPlot2dViewer(const SalomeApp_Module* theModule, const bool theCreate)
770   {
771     if(SalomeApp_Application* anApp = theModule->getApp()){
772       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( Plot2d_Viewer::Type(), theCreate )){
773         return dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
774       }
775     }
776     return NULL;
777   }
778
779   // Internal function used by several public functions below
780   void
781   UpdateCurve(VISU::Curve_i* theCurve,
782               Plot2d_ViewFrame* aPlot,
783               SPlot2d_Curve* plotCurve,
784               int theDisplaying)
785   {
786     if ( theDisplaying == VISU::eErase ) {
787       if ( plotCurve )
788         aPlot->eraseCurve( plotCurve, false );
789     }
790     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
791       if ( plotCurve ) {
792         plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
793         //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
794         plotCurve->setVerTitle( theCurve->GetName() );
795         plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
796         plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
797         double* xList = 0;
798         double* yList = 0;
799         int     nbPoints = theCurve->GetData( xList, yList );
800         if ( nbPoints > 0 && xList && yList ) {
801           plotCurve->setData( xList, yList, nbPoints );
802         }
803         if ( !theCurve->IsAuto() ) {
804           plotCurve->setLine( (Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
805           plotCurve->setMarker( (Plot2d_Curve::MarkerType)theCurve->GetMarker() );
806           SALOMEDS::Color color = theCurve->GetColor();
807           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
808         }
809         plotCurve->setAutoAssign( theCurve->IsAuto() );
810         aPlot->displayCurve( plotCurve, false );
811       }
812       else {
813         Plot2d_Curve* crv = theCurve->CreatePresentation();
814         if ( crv ) {
815           aPlot->displayCurve( crv, false );
816           theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
817           theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
818           SALOMEDS::Color newColor;
819           newColor.R = crv->getColor().red()/255.;
820           newColor.G = crv->getColor().green()/255.;
821           newColor.B = crv->getColor().blue()/255.;
822           theCurve->SetColor( newColor );
823           crv->setAutoAssign( theCurve->IsAuto() );
824         }
825       }
826     }
827   }
828
829   void
830   PlotTable(const SalomeApp_Module* theModule,
831             VISU::Table_i* table,
832             int theDisplaying)
833   {
834     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true ); // create if necessary
835     if ( !aView )
836       return;
837     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
838     if ( !aPlot )
839       return;
840
841     if ( theDisplaying == VISU::eDisplayOnly )
842       aPlot->EraseAll();
843     QList<Plot2d_Curve> clist;
844     aPlot->getCurves( clist );
845     _PTR(Study) aStudy = GetCStudy( GetAppStudy( theModule ) );
846     _PTR(SObject) TableSO = aStudy->FindObjectID( table->GetEntry().latin1() );
847     if ( TableSO ) {
848       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
849       for ( ; Iter->More(); Iter->Next() ) {
850         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
851         if( !CORBA::is_nil( childObject ) ) {
852           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
853           if( !CORBA::is_nil( aCurve ) ) {
854             VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
855             SPlot2d_Curve* plotCurve = 0;
856             SPlot2d_Curve* tmpCurve;
857             for ( int i = 0; i < clist.count(); i++ ) {
858               tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
859               if (tmpCurve && tmpCurve->hasIO() &&
860                   !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
861                 plotCurve = tmpCurve;
862                 break;
863               }
864             }
865
866             UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
867
868             if ( theDisplaying == VISU::eErase && plotCurve ) {
869               clist.remove( plotCurve );
870             }
871           }
872         }
873       }
874       aPlot->Repaint();
875     }
876   }
877
878   void
879   PlotCurve(const SalomeApp_Module* theModule,
880             VISU::Curve_i* theCurve,
881             int theDisplaying)
882   {
883     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
884     if ( !aView )
885       return;
886     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
887     if ( !aPlot )
888       return;
889
890 //  if ( theDisplaying == VISU::eDisplayOnly )
891 //    aPlot->EraseAll();
892     QList<Plot2d_Curve> clist;
893     aPlot->getCurves( clist );
894     SPlot2d_Curve* plotCurve = 0;
895     SPlot2d_Curve* tmpCurve;
896     for (int i = 0; i < clist.count(); i++) {
897       tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
898       if (tmpCurve && tmpCurve->hasIO() &&
899           !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
900         plotCurve = tmpCurve;
901       } else if (theDisplaying == VISU::eDisplayOnly) {
902         aPlot->eraseCurve(clist.at(i));
903       }
904     }
905
906     UpdateCurve(theCurve, aPlot, plotCurve, theDisplaying);
907
908     aPlot->Repaint();
909   }
910
911   void
912   PlotContainer(const SalomeApp_Module* theModule,
913                 VISU::Container_i* container,
914                 int theDisplaying)
915   {
916     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
917     if ( !aView )
918       return;
919     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
920     if ( !aPlot )
921       return;
922
923     if ( theDisplaying == VISU::eDisplayOnly )
924       aPlot->EraseAll();
925     QList<Plot2d_Curve> clist;
926     aPlot->getCurves( clist );
927     if ( container->GetNbCurves() > 0 ) {
928       int nbCurves = container->GetNbCurves();
929       for ( int k = 1; k <= nbCurves; k++ ) {
930         VISU::Curve_i* theCurve = container->GetCurve( k );
931         if ( theCurve && theCurve->IsValid() ) {
932           SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>
933             (aView->getCurveByIO(new SALOME_InteractiveObject (theCurve->GetEntry(), "", "")));
934
935           UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
936
937           if ( plotCurve && theDisplaying == VISU::eErase ) {
938             clist.remove( plotCurve );
939           }
940         }
941       }
942     }
943     aPlot->Repaint();
944   }
945
946   void
947   CreatePlot(SalomeApp_Module* theModule,
948              _PTR(SObject) theTableSO)
949   {
950     if ( IsSObjectTable(theTableSO) ) {
951       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
952       CORBA::Object_var aContainer = GetVisuGen( theModule )->CreateContainer();
953       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
954         VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
955         VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
956
957         if ( pContainer && pTable ) {
958           for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
959             CORBA::Object_var aNewCurve = GetVisuGen( theModule )->CreateCurve( pTable->_this(), 1, i );
960             if( !CORBA::is_nil( aNewCurve ) ) {
961               VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
962               if ( pCrv ) {
963                 pContainer->AddCurve( pCrv->_this() );
964               }
965             }
966           }
967           PlotContainer( theModule, pContainer, VISU::eDisplay );
968         }
969       }
970     }
971   }
972
973   //************************************************************
974   // Others
975
976   void CreateMesh (const SalomeApp_Module* theModule,
977                     const Handle(SALOME_InteractiveObject)& theIO)
978   {
979     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
980     //if (CheckLock(aStudy))
981     //  return;
982
983     _PTR(SObject) aResultSObj = aStudy->FindObjectID(theIO->getEntry());
984
985     // Get VISU::Result
986     VISU::Result_var aResult;
987     VISU::Result_i* pResult = CheckResult(theModule, aResultSObj, aResult);
988     if (pResult == NULL)
989       return;
990
991     Storable::TRestoringMap aMap = getMapOfValue(aResultSObj);
992     bool isExist;
993     string aComment = Storable::FindValue(aMap,"myComment",&isExist).latin1();
994     if (!isExist)
995       return;
996
997     CORBA::Object_var aMesh;
998     string aMeshName = Storable::FindValue(aMap,"myMeshName").latin1();
999 #ifdef CHECKTIME
1000     Utils_Timer timer;
1001     timer.Start();
1002 #endif
1003     if (aComment == "ENTITY") {
1004       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myId").toInt();
1005       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity))
1006         aMesh = GetVisuGen(theModule)->MeshOnEntity(aResult,aMeshName.c_str(),anEntity);
1007     } else if (aComment == "FAMILY") {
1008       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myEntityId").toInt();
1009       string aFamilyName = Storable::FindValue(aMap,"myName").latin1();
1010       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity,aFamilyName.c_str()))
1011         aMesh = GetVisuGen(theModule)->FamilyMeshOnEntity
1012           (aResult,aMeshName.c_str(),anEntity,aFamilyName.c_str());
1013     } else if (aComment == "GROUP") {
1014       string aGroupName = Storable::FindValue(aMap,"myName").latin1();
1015       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),aGroupName.c_str()))
1016         aMesh = GetVisuGen(theModule)->GroupMesh(aResult,aMeshName.c_str(),aGroupName.c_str());
1017     }
1018 #ifdef CHECKTIME
1019     timer.Stop();
1020     MESSAGE("VisuGUI::CreateMesh() - CREATE MESH");
1021     timer.Show();
1022 #endif
1023
1024     QApplication::restoreOverrideCursor();
1025     VISU::Mesh_i* pPresent = NULL;
1026     if (!CORBA::is_nil(aMesh))
1027       pPresent = dynamic_cast<VISU::Mesh_i*>(VISU::GetServant(aMesh).in());
1028     if (pPresent == NULL) {
1029       SUIT_MessageBox::warn1 (GetDesktop(theModule),
1030                               QObject::tr("WRN_VISU"),
1031                               QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
1032                               QObject::tr("BUT_OK"));
1033       return;
1034     }
1035
1036     if (SVTK_ViewWindow* aView = GetViewWindow(theModule)) {
1037       try {
1038 #ifdef CHECKTIME
1039         Utils_Timer timer;
1040         timer.Start();
1041 #endif
1042         PublishInView(theModule, pPresent);
1043         aView->onFitAll();
1044 #ifdef CHECKTIME
1045         timer.Stop();
1046         MESSAGE("VisuGUI::CreateMesh() - DISPLAY MESH");
1047         timer.Show();
1048 #endif
1049         theModule->application()->putInfo(QObject::tr("INF_DONE"));
1050         // Make "Save" button active
1051         theModule->getApp()->updateActions();
1052       } catch (std::runtime_error& exc) {
1053         INFOS(exc.what());
1054         SUIT_MessageBox::warn1 (GetDesktop(theModule),
1055                                 QObject::tr("WRN_VISU"),
1056                                 QObject::tr("ERR_CANT_CREATE_ACTOR") + " " + QObject::tr(exc.what()),
1057                                 QObject::tr("BUT_OK"));
1058       }
1059     }
1060   }
1061
1062   // ========================================================================================
1063   // GetPrs3dList: find list of presentations for the given object
1064   // ========================================================================================
1065   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1066                                             const Handle(SALOME_InteractiveObject)& theIO)
1067   {
1068     std::vector<VISU::Prs3d_i*> aList;
1069     if (!theIO.IsNull() && theIO->hasEntry()) {
1070       _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1071       _PTR(SObject) aSObject = aCStudy->FindObjectID(theIO->getEntry());
1072       aList = GetPrs3dList(theModule, aSObject);
1073     }
1074     return aList;
1075   }
1076
1077   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1078                                             _PTR(SObject) theObject)
1079   {
1080     std::vector<VISU::Prs3d_i*> aList; int k = 0;
1081     if (!theObject)
1082       return aList;
1083
1084     _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1085
1086     CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(theObject);
1087     if (!CORBA::is_nil(aCORBAObject)) {
1088       VISU::Base_var aVisuObj = VISU::Base::_narrow(aCORBAObject);
1089       if (!CORBA::is_nil(aVisuObj)) {
1090         VISU::VISUType aType = aVisuObj->GetType();
1091         switch (aType) {
1092         case VISU::TSCALARMAP:
1093         case VISU::TISOSURFACE:
1094         case VISU::TDEFORMEDSHAPE:
1095         case VISU::TCUTPLANES:
1096         case VISU::TCUTLINES:
1097         case VISU::TVECTORS:
1098         case VISU::TSTREAMLINES:
1099         case VISU::TPLOT3D:
1100           {
1101             PortableServer::ServantBase_var aServant = VISU::GetServant(aCORBAObject);
1102             if (aServant.in()) {
1103               VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
1104               aList.resize(k+1);
1105               aList[k] = aPrsObject;
1106               k++;
1107             }
1108           }
1109           break;
1110         case VISU::TFIELD:
1111           {
1112             _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1113             _PTR(SObject) aTimeStamp;
1114             anIter->Next(); // First is reference on support
1115             for (; anIter->More(); anIter->Next()) {
1116               aTimeStamp = anIter->Value();
1117               if (!aTimeStamp) continue;
1118               std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1119               if (!aSubList.empty()) {
1120                 int n = aSubList.size();
1121                 aList.resize(k+n);
1122                 for (int i = 0; i < n; i++) {
1123                   aList[k] = aSubList[i];
1124                   k++;
1125                 }
1126               }
1127             }
1128           }
1129           break;
1130         }
1131       }
1132     } else {
1133       _PTR(GenericAttribute) anAttr;
1134       if (theObject->FindAttribute(anAttr, "AttributeComment")) {
1135         _PTR(AttributeComment) aComment (anAttr);
1136         string aComm = aComment->Value();
1137         QString strIn (aComm.c_str());
1138         VISU::Storable::TRestoringMap pMap;
1139         VISU::Storable::StrToMap(strIn, pMap);
1140         bool isExist;
1141         VISU::VISUType aType =
1142           (VISU::VISUType)VISU::Storable::FindValue(pMap,"myType",&isExist).toInt();
1143         if (isExist) {
1144           switch (aType) {
1145           case VISU::TFIELD:
1146             {
1147               _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1148               _PTR(SObject) aTimeStamp;
1149               anIter->Next(); // First is reference on support
1150               for (; anIter->More(); anIter->Next()) {
1151                 aTimeStamp = anIter->Value();
1152                 if (!aTimeStamp) continue;
1153                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
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];
1159                     k++;
1160                   }
1161                 }
1162               }
1163             }
1164             break;
1165           case VISU::TTIMESTAMP:
1166             {
1167               _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1168               _PTR(SObject) aPrs;
1169               for (; anIter->More(); anIter->Next()) {
1170                 aPrs = anIter->Value();
1171                 if (!aPrs) continue;
1172                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aPrs);
1173                 if (!aSubList.empty()) {
1174                   int n = aSubList.size();
1175                   aList.resize(k+n);
1176                   for (int i = 0; i < n; i++) {
1177                     aList[k] = aSubList[i];
1178                     k++;
1179                   }
1180                 }
1181               }
1182             }
1183             break;
1184           }
1185         }
1186       }
1187     }
1188     return aList;
1189   }
1190 }