]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_VTKUtils.cxx
Salome HOME
bos #29143 [CEA] Compute takes too much time in polyhedron per solid use case
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_VTKUtils.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_VTKUtils.cxx
25 // Author : Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_VTKUtils.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Filter.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMDS_Mesh.hxx"
34 #include "SMESH_Actor.h"
35 #include "SMESH_ActorProps.h"
36 #include "SMESH_ActorUtils.h"
37 #include "SMESH_CellLabelActor.h"
38 #include "SMESH_ControlsDef.hxx"
39 #include "SMESH_NodeLabelActor.h"
40 #include "SMESH_ObjectDef.h"
41
42 // SALOME GUI includes
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_ResourceMgr.h>
48
49 #include <SALOME_ListIO.hxx>
50
51 #include <SVTK_Selector.h>
52 #include <SVTK_ViewModel.h>
53 #include <SVTK_ViewWindow.h>
54
55 #include <VTKViewer_Algorithm.h>
56
57 #include <LightApp_SelectionMgr.h>
58 #include <SalomeApp_Application.h>
59 #include <SalomeApp_Study.h>
60
61 // SALOME KERNEL includes
62 #include <utilities.h>
63
64 // IDL includes
65 #include <SALOMEconfig.h>
66 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
67 #include CORBA_CLIENT_HEADER(SMESH_Group)
68
69 // VTK includes
70 #include <vtkMath.h>
71 #include <vtkRenderer.h>
72 #include <vtkActorCollection.h>
73 #include <vtkUnstructuredGrid.h>
74
75 // OCCT includes
76 #include <Standard_ErrorHandler.hxx>
77
78 namespace SMESH
79 {
80   typedef std::map<std::string,TVisualObjPtr> TVisualObjCont;
81   static TVisualObjCont VISUAL_OBJ_CONT;
82
83   //=============================================================================
84   /*!
85    * \brief Allocate some memory at construction and release it at destruction.
86    * Is used to be able to continue working after mesh generation or visualization
87    * break due to lack of memory
88    */
89   //=============================================================================
90
91   struct MemoryReserve
92   {
93     char* myBuf;
94     MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
95     void Free() { if (myBuf) { delete [] myBuf; myBuf = 0; }}
96     ~MemoryReserve() { Free(); }
97   };
98   static MemoryReserve* theVISU_MemoryReserve = new MemoryReserve;
99
100   //================================================================================
101   /*!
102    * \brief Remove VisualObj and its actor from all views
103    */
104   //================================================================================
105
106   void RemoveVisualObjectWithActors( const char* theEntry, bool fromAllViews )
107   {
108     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
109     if(!app)
110       return;
111     SalomeApp_Study* aStudy  = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
112     if(!aStudy)
113       return;
114     ViewManagerList aList;
115
116     if(fromAllViews) {
117       app->viewManagers(SVTK_Viewer::Type() , aList);
118     } else {
119       SUIT_ViewManager* aVM = app->getViewManager(SVTK_Viewer::Type(), true);
120       if(aVM)
121         aList.append(aVM);
122     }    
123     bool actorRemoved = false;
124     ViewManagerList::ConstIterator it = aList.begin();
125     SUIT_ViewManager* aViewManager = 0;
126     for( ; it!=aList.end();it++) {
127       aViewManager = *it;
128       QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
129       for ( int iV = 0; iV < views.count(); ++iV ) {
130         if ( SMESH_Actor* actor = FindActorByEntry( views[iV], theEntry)) {
131           if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
132             vtkWnd->RemoveActor(actor);
133             actorRemoved = true;
134           }
135           actor->Delete();
136         }
137       }
138     }
139     
140     TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(theEntry);
141     if(anIter != VISUAL_OBJ_CONT.end()) {
142       // for unknown reason, object destructor is not called, so clear object manually
143       anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
144       anIter->second->GetUnstructuredGrid()->SetPoints(0);
145     }
146     VISUAL_OBJ_CONT.erase(theEntry);
147
148     if(actorRemoved)
149       aStudy->setVisibilityState(theEntry, Qtx::HiddenState);
150   }
151   //================================================================================
152   /*!
153    * \brief Remove all VisualObjs and their actors from all views
154    */
155   //================================================================================
156
157   void RemoveAllObjectsWithActors()
158   {
159     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
160       ( SUIT_Session::session()->activeApplication() );
161     if (!app) return;
162     ViewManagerList viewMgrs = app->viewManagers();
163     for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
164       SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
165       if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type()) {
166         QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
167         for ( int iV = 0; iV < views.count(); ++iV ) {
168           if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
169             vtkRenderer *aRenderer = vtkWnd->getRenderer();
170             VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
171             vtkActorCollection *actors = aCopy.GetActors();
172             for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
173               // size of actors changes inside the loop
174               if (SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
175               {
176                 vtkWnd->RemoveActor(actor);
177                 actor->Delete();
178               }
179             }
180           }
181         }
182       }
183     }
184     TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
185     for ( ; anIter != VISUAL_OBJ_CONT.end(); ++anIter ) {
186       // for unknown reason, object destructor is not called, so clear object manually
187       anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
188       anIter->second->GetUnstructuredGrid()->SetPoints(0);
189     }
190     VISUAL_OBJ_CONT.clear();
191   }
192
193   //================================================================================
194   /*!
195    * \brief Remove all VisualObjs of a study
196    */
197   //================================================================================
198
199   void RemoveVisuData()
200   {
201     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
202       ( SUIT_Session::session()->activeApplication() );
203     if (!app) return;
204     ViewManagerList viewMgrs = app->viewManagers();
205     for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
206       SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
207       if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type() ) {
208         QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
209         for ( int iV = 0; iV < views.count(); ++iV ) {
210           if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
211             vtkRenderer *aRenderer = vtkWnd->getRenderer();
212             VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
213             vtkActorCollection *actors = aCopy.GetActors();
214             for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
215               // size of actors changes inside the loop
216               if(SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
217               {
218                 vtkWnd->RemoveActor(actor);
219                 actor->Delete();
220               }
221             }
222           }
223         }
224       }
225     }
226     TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
227     for ( ; anIter != VISUAL_OBJ_CONT.end(); ) {
228       // for unknown reason, object destructor is not called, so clear object manually
229       anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
230       anIter->second->GetUnstructuredGrid()->SetPoints(0);
231       VISUAL_OBJ_CONT.erase( anIter++ ); // anIter++ returns a copy of self before incrementing
232     }
233   }
234
235   //================================================================================
236   /*!
237    * \brief Remove/update actors while module activation
238    *  \param [in] wnd - window
239    *
240    * At module activation, groups and sub-meshes can be removed on engine side due
241    * to modification of meshed geometry, while their actors can remain.
242    * Here we remove/update SMESH_Actor's of changed objects. State (emptiness) of objects
243    * is defined by their icons in the Object Browser
244    */
245   //================================================================================
246
247   void UpdateActorsAfterUpdateStudy( SUIT_ViewWindow* theWindow )
248   {
249     const char* emptyIcon = "ICON_SMESH_TREE_MESH_WARN";
250     _PTR(Study) aStudy = SMESH::getStudy();
251
252     if ( SVTK_ViewWindow* aViewWindow = GetVtkViewWindow( theWindow ))
253     {
254       vtkRenderer *aRenderer = aViewWindow->getRenderer();
255       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
256       vtkActorCollection *aCollection = aCopy.GetActors();
257       aCollection->InitTraversal();
258       while ( vtkActor *actor = aCollection->GetNextActor() ) {
259         if ( SMESH_Actor *smeshActor = dynamic_cast<SMESH_Actor*>( actor ))
260         {
261           if ( !smeshActor->hasIO() )
262             continue;
263           Handle(SALOME_InteractiveObject) io = smeshActor->getIO();
264           if ( !io->hasEntry() )
265             continue;
266           _PTR(SObject) so = aStudy->FindObjectID( io->getEntry() );
267           if ( !so )
268             continue; // seems impossible
269
270           CORBA::Object_var obj = SMESH::SObjectToObject( so );
271           if ( CORBA::is_nil( obj )) // removed object
272           {
273             RemoveActor( theWindow, smeshActor );
274             continue;
275           }
276
277           bool toShow = smeshActor->GetVisibility();
278           _PTR(GenericAttribute) attr;
279           if ( toShow && so->FindAttribute( attr, "AttributePixMap" )) // check emptiness
280           {
281             _PTR(AttributePixMap) pixMap = attr;
282             toShow = ( pixMap->GetPixMap() != emptyIcon );
283           }
284           //smeshActor->Update();
285           UpdateView( theWindow, toShow ? eDisplay : eErase, io->getEntry() );
286         }
287       }
288     }
289     return;
290   }
291
292   //================================================================================
293   /*!
294    * \brief Remove/update actors while module activation
295    *
296    * At module activation, groups and sub-meshes can be removed on engine side due
297    * to modification of meshed geometry, while their actors can remain.
298    * Here we remove/update SMESH_Actor's of changed objects. State (emptiness) of objects
299    * is defined by their icons in the Object Browser
300    */
301   //================================================================================
302
303   void UpdateActorsAfterUpdateStudy()
304   {
305     SUIT_Study* study = SMESH::GetActiveStudy();
306     if ( SUIT_Desktop* desk = study->application()->desktop() )
307     {
308       QList<SUIT_ViewWindow*> wndList = desk->windows();
309       SUIT_ViewWindow* wnd;
310       foreach ( wnd, wndList )
311         SMESH::UpdateActorsAfterUpdateStudy(wnd);
312     }
313   }
314
315   //================================================================================
316   /*!
317    * \brief Notify the user on problems during visualization
318    */
319   //================================================================================
320
321   void OnVisuException()
322   {
323     try {
324       OCC_CATCH_SIGNALS;
325       // PAL16774 (Crash after display of many groups). Salome sometimes crashes just
326       // after or at showing this message, so we do an additional check of available memory
327 //       char* buf = new char[100*1024];
328 //       delete [] buf;
329       SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
330                                QObject::tr("SMESH_VISU_PROBLEM"));
331     } catch (...) {
332       // no more memory at all: last resort
333       MESSAGE_BEGIN ( "SMESHGUI_VTKUtils::OnVisuException(), exception even at showing a message!!!" <<
334                       std::endl << "Try to remove all visual data..." );
335       if (theVISU_MemoryReserve) {
336         delete theVISU_MemoryReserve;
337         theVISU_MemoryReserve = 0;
338       }
339       RemoveAllObjectsWithActors();
340       SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
341                                QObject::tr("SMESH_VISU_PROBLEM_CLEAR"));
342       MESSAGE_END ( "...done" );
343     }
344   }
345   //================================================================================
346   /*!
347    * \brief Returns an updated visual object
348    */
349   //================================================================================
350
351   TVisualObjPtr GetVisualObj(const char* theEntry, bool nulData){
352     TVisualObjPtr aVisualObj;
353     try{
354       OCC_CATCH_SIGNALS;
355       TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(theEntry);
356       if(anIter != VISUAL_OBJ_CONT.end()){
357         aVisualObj = anIter->second;
358       }else{
359         SalomeApp_Application* app =
360           dynamic_cast<SalomeApp_Application*>( SMESHGUI::activeStudy()->application() );
361         _PTR(Study) aStudy = SMESH::getStudy();
362         _PTR(SObject) aSObj = aStudy->FindObjectID(theEntry);
363         if(aSObj){
364           _PTR(GenericAttribute) anAttr;
365           if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
366             _PTR(AttributeIOR) anIOR = anAttr;
367             CORBA::String_var aVal = anIOR->Value().c_str();
368             CORBA::Object_var anObj = app->orb()->string_to_object( aVal.in() );
369             if(!CORBA::is_nil(anObj)){
370               //Try narrow to SMESH_Mesh interface
371               SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
372               if(!aMesh->_is_nil()){
373                 aVisualObj.reset(new SMESH_MeshObj(aMesh));
374                 TVisualObjCont::value_type aValue(theEntry,aVisualObj);
375                 VISUAL_OBJ_CONT.insert(aValue);
376               }
377               //Try narrow to SMESH_Group interface
378               SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
379               if(!aGroup->_is_nil()){
380                 _PTR(SObject) aFatherSObj = aSObj->GetFather();
381                 if(!aFatherSObj) return aVisualObj;
382                 aFatherSObj = aFatherSObj->GetFather();
383                 if(!aFatherSObj) return aVisualObj;
384                 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
385                 TVisualObjPtr aVisObj = GetVisualObj(anEntry.in());
386                 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
387                   aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
388                   TVisualObjCont::value_type aValue(theEntry,aVisualObj);
389                   VISUAL_OBJ_CONT.insert(aValue);
390                 }
391               }
392               //Try narrow to SMESH_subMesh interface
393               SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
394               if(!aSubMesh->_is_nil()){
395                 _PTR(SObject) aFatherSObj = aSObj->GetFather();
396                 if(!aFatherSObj) return aVisualObj;
397                 aFatherSObj = aFatherSObj->GetFather();
398                 if(!aFatherSObj) return aVisualObj;
399                 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
400                 TVisualObjPtr aVisObj = GetVisualObj(anEntry.in());
401                 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
402                   aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
403                   TVisualObjCont::value_type aValue(theEntry,aVisualObj);
404                   VISUAL_OBJ_CONT.insert(aValue);
405                 }
406               }
407             }
408           }
409         }
410       }
411     }catch(...){
412       INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
413       return TVisualObjPtr();
414     }
415     // Update object
416     bool objModified = false;
417     if ( aVisualObj ) {
418       try {
419         OCC_CATCH_SIGNALS;
420         if (nulData)
421           objModified = aVisualObj->NulData();
422         else
423           objModified = aVisualObj->Update();
424       }
425       catch (...) {
426         MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
427         RemoveVisualObjectWithActors( theEntry ); // remove this object
428         OnVisuException();
429         aVisualObj.reset();
430       }
431     }
432
433     if ( objModified ) {
434       // PAL16631. Measurements showed that to show aVisualObj in SHADING(default) mode,
435       // ~5 times more memory is used than it occupies.
436       // Warn the user if there is less free memory than 30 sizes of a grid
437       // TODO: estimate memory usage in other modes and take current mode into account
438       int freeMB = SMDS_Mesh::CheckMemory(true);
439       int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
440       //MESSAGE("SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB << ", usedMB=" <<usedMB);
441       if ( freeMB > 0 && usedMB * 5 > freeMB ) {
442         bool continu = false;
443         if ( usedMB * 3 > freeMB )
444           // don't even try to show
445           SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
446                                    QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
447         else
448           // there is a chance to succeed
449           continu = SUIT_MessageBox::warning
450             (SMESHGUI::desktop(),
451              QObject::tr("SMESH_WRN_WARNING"),
452              QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
453              SUIT_MessageBox::Yes | SUIT_MessageBox::No,
454              SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
455         if ( !continu ) {
456           // remove the corresponding actors from all views
457           RemoveVisualObjectWithActors( theEntry );
458           aVisualObj.reset();
459         }
460       }
461     }
462
463     return aVisualObj;
464   }
465
466
467   /*! Return active view window, if it instantiates SVTK_ViewWindow class,
468    *  otherwise find or create corresponding view window, make it active and return it.
469    *  \note Active VVTK_ViewWindow can be returned, because it inherits SVTK_ViewWindow.
470    */
471   SVTK_ViewWindow* GetViewWindow (const SalomeApp_Module* theModule,
472                                   bool createIfNotFound)
473   {
474     SalomeApp_Application* anApp;
475     if (theModule)
476       anApp = theModule->getApp();
477     else
478       anApp = dynamic_cast<SalomeApp_Application*>
479         (SUIT_Session::session()->activeApplication());
480
481     if (anApp) {
482       if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow()))
483         return aView;
484
485       SUIT_ViewManager* aViewManager =
486         anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
487       if (aViewManager) {
488         if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
489           if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
490             aViewWindow->raise();
491             aViewWindow->setFocus();
492             return aView;
493           }
494         }
495       }
496     }
497     return NULL;
498   }
499
500   SVTK_ViewWindow* FindVtkViewWindow (SUIT_ViewManager* theMgr,
501                                       SUIT_ViewWindow * theWindow)
502   {
503     if( !theMgr )
504       return NULL;
505
506     QVector<SUIT_ViewWindow*> views = theMgr->getViews();
507     if( views.contains( theWindow ) )
508       return GetVtkViewWindow( theWindow );
509     else
510       return NULL;
511   }
512
513   SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
514     return dynamic_cast<SVTK_ViewWindow*>(theWindow);
515   }
516
517 /*  SUIT_ViewWindow* GetActiveWindow()
518   {
519     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
520     if( !app )
521       return NULL;
522     SUIT_ViewManager* mgr = app->activeViewManager();
523     if( mgr )
524       return mgr->getActiveView();
525     else
526       return NULL;
527   }*/
528
529   SVTK_ViewWindow* GetCurrentVtkView(){
530     return GetVtkViewWindow( GetActiveWindow() );
531   }
532
533
534   void RepaintCurrentView()
535   {
536     if (SVTK_ViewWindow* wnd = GetCurrentVtkView())
537     {
538       try {
539         OCC_CATCH_SIGNALS;
540         wnd->getRenderer()->Render();
541         wnd->Repaint(false);
542       }
543       catch (...) {
544         MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
545         OnVisuException();
546       }
547     }
548   }
549
550   void RepaintViewWindow(SVTK_ViewWindow* theWindow)
551   {
552     try {
553       OCC_CATCH_SIGNALS;
554       theWindow->getRenderer()->Render();
555       theWindow->Repaint();
556     }
557     catch (...) {
558       MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
559       OnVisuException();
560     }
561   }
562
563   void RenderViewWindow(SVTK_ViewWindow* theWindow)
564   {
565     try {
566       OCC_CATCH_SIGNALS;
567       theWindow->getRenderer()->Render();
568       theWindow->Repaint();
569     }
570     catch (...) {
571       MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
572       OnVisuException();
573     }
574   }
575
576   void FitAll(){
577     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
578       try {
579         OCC_CATCH_SIGNALS;
580         wnd->onFitAll();
581         wnd->Repaint();
582       }
583       catch (...) {
584         MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
585         OnVisuException();
586       }
587     }
588   }
589
590
591   SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
592                                 const char* theEntry)
593   {
594     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
595       vtkRenderer *aRenderer = aViewWindow->getRenderer();
596       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
597       vtkActorCollection *aCollection = aCopy.GetActors();
598       aCollection->InitTraversal();
599       while(vtkActor *anAct = aCollection->GetNextActor()){
600         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
601           if(anActor->hasIO()){
602             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
603             if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
604               return anActor;
605             }
606           }
607         }
608       }
609     }
610     return NULL;
611   }
612
613
614   SMESH_Actor* FindActorByEntry(const char* theEntry){
615     return FindActorByEntry(GetActiveWindow(),theEntry);
616   }
617
618
619   SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
620     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
621     if( !app )
622       return NULL;
623
624     if(!CORBA::is_nil(theObject)){
625       _PTR(Study) aStudy = getStudy();
626       CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
627       _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
628       if(aSObject){
629         CORBA::String_var anEntry = aSObject->GetID().c_str();
630         return FindActorByEntry(anEntry.in());
631       }
632     }
633     return NULL;
634   }
635
636
637   SMESH_Actor* CreateActor(const char* theEntry,
638                            int theIsClear)
639   {
640     SMESH_Actor *anActor = NULL;
641     if(TVisualObjPtr aVisualObj = GetVisualObj(theEntry)){
642       _PTR(SObject) aSObj = getStudy()->FindObjectID(theEntry);
643       if(aSObj){
644         _PTR(GenericAttribute) anAttr;
645         if(aSObj->FindAttribute(anAttr,"AttributeName")){
646           _PTR(AttributeName) aName = anAttr;
647           std::string aNameVal = aName->Value();
648           anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
649         }
650
651         SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( aSObj ));
652         if(!CORBA::is_nil(aGroup) && anActor)
653         {
654           QColor c;
655           int deltaF, deltaV;
656           SMESH::GetColor( "SMESH", "fill_color", c, deltaF, "0,170,255|-100"  );
657           SMESH::GetColor( "SMESH", "volume_color", c, deltaV, "255,0,170|-100"  );
658           c = SMESH::GetColor( "SMESH", "default_grp_color", c );
659           SALOMEDS::Color aColor = aGroup->GetColor();
660           if( !( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 ))
661           {
662             aColor.R = c.redF();
663             aColor.G = c.greenF();
664             aColor.B = c.blueF();
665             aGroup->SetColor( aColor );
666           }
667           if( aGroup->GetType() == SMESH::NODE )
668             anActor->SetNodeColor( aColor.R, aColor.G, aColor.B );
669           else if( aGroup->GetType() == SMESH::EDGE )
670             anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B );
671           else if( aGroup->GetType() == SMESH::ELEM0D )
672             anActor->Set0DColor( aColor.R, aColor.G, aColor.B );
673           else if( aGroup->GetType() == SMESH::BALL )
674             anActor->SetBallColor( aColor.R, aColor.G, aColor.B );
675           else if( aGroup->GetType() == SMESH::VOLUME )
676             anActor->SetVolumeColor( aColor.R, aColor.G, aColor.B, deltaV );
677           else
678             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B, deltaF );
679         }
680       }
681     }
682     //MESSAGE("CreateActor " << anActor);
683     if( anActor )
684       if( SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI() )
685         aSMESHGUI->addActorAsObserver( anActor );
686     return anActor;
687   }
688
689
690   void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
691     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
692       try {
693         OCC_CATCH_SIGNALS;
694         //MESSAGE("DisplayActor " << theActor);
695         theActor->SetPointRepresentation(false);
696         vtkWnd->AddActor(theActor);
697         vtkWnd->Repaint();
698       }
699       catch (...) {
700         MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
701         OnVisuException();
702       }
703     }
704   }
705
706
707   void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor)
708   {
709     if ( SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)) {
710       //MESSAGE("RemoveActor " << theActor);
711       vtkWnd->RemoveActor(theActor);
712       if(theActor->hasIO()){
713         Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
714         if(anIO->hasEntry()){
715           std::string anEntry = anIO->getEntry();
716           VISUAL_OBJ_CONT.erase(anEntry);
717         }
718       }
719       theActor->Delete();
720       vtkWnd->Repaint();
721     }
722   }
723
724   //================================================================================
725   /*!
726    * \brief Return true if there are no SMESH actors in a view
727    */
728   //================================================================================
729
730   bool NoSmeshActors(SUIT_ViewWindow *theWnd)
731   {
732     SUIT_ViewWindow* aWnd = ( theWnd == nullptr ) ? GetActiveWindow() : theWnd;
733     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(aWnd)) {
734       vtkRenderer *aRenderer = aViewWindow->getRenderer();
735       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
736       vtkActorCollection *aCollection = aCopy.GetActors();
737       aCollection->InitTraversal();
738       while(vtkActor *anAct = aCollection->GetNextActor())
739         if(dynamic_cast<SMESH_Actor*>(anAct))
740           return false;
741     }
742     return true;
743   }
744
745   bool UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
746   {
747     //MESSAGE("UpdateView");
748     bool OK = false;
749     SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd);
750     if (!aViewWnd)
751       return OK;
752
753     SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd);
754     if (!vtkWnd)
755       return OK;
756
757     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
758     
759     if (!aStudy)
760       return OK;
761
762     SUIT_ResourceMgr* resMgr;
763     {
764       OK = true;
765       vtkRenderer *aRenderer = aViewWnd->getRenderer();
766       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
767       vtkActorCollection *aCollection = aCopy.GetActors();
768       aCollection->InitTraversal();
769
770       switch (theAction) {
771       case eDisplayAll: {
772         while (vtkActor *anAct = aCollection->GetNextActor()) {
773           if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
774             //MESSAGE("--- display " << anActor);
775             anActor->SetVisibility(true);
776
777             if(anActor->hasIO()){
778               Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
779               if(anIO->hasEntry()){
780                 aStudy->setVisibilityState(anIO->getEntry(), Qtx::ShownState);
781               }
782             }
783           }
784         }
785         break;
786       }
787       case eDisplayOnly:
788       case eEraseAll: {
789         //MESSAGE("---case eDisplayOnly");
790         while (vtkActor *anAct = aCollection->GetNextActor()) {
791           if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
792             //MESSAGE("--- erase " << anActor);
793             anActor->SetVisibility(false);
794           }
795         }
796         aStudy->setVisibilityStateForAll(Qtx::HiddenState);
797       }
798       // fall through
799       default: {
800         if (SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)) {
801           switch (theAction) {
802             case eDisplay:
803             case eDisplayOnly:
804               //MESSAGE("--- display " << anActor);
805               anActor->Update();
806               anActor->SetVisibility(true);
807               if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
808               aStudy->setVisibilityState(theEntry, Qtx::ShownState);
809               if (( theAction == eDisplayOnly ) &&
810                   ( resMgr = SMESHGUI::resourceMgr() ) &&
811                   ( resMgr->booleanValue( "SMESH", "fitall_on_displayonly", false )))
812                 FitAll(); // PAL23615
813               break;
814             case eErase:
815               //MESSAGE("--- erase " << anActor);
816               anActor->SetVisibility(false);
817               aStudy->setVisibilityState(theEntry, Qtx::HiddenState);
818               break;
819             default:;
820           }
821         } else {
822           switch (theAction) {
823           case eDisplay:
824           case eDisplayOnly:
825             {
826               //MESSAGE("---");
827               SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
828               TVisualObjPtr aVisualObj;
829               if ( (aVisualObj = GetVisualObj(theEntry)) && aVisualObj->IsValid())
830               {
831                 if ((anActor = CreateActor(theEntry,true))) {
832                   bool needFitAll = NoSmeshActors(theWnd); // fit for the first object only
833                   DisplayActor(theWnd,anActor);
834                   anActor->SetVisibility(true);
835                   aStudy->setVisibilityState(theEntry, Qtx::ShownState);
836                   // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
837                   if (( !needFitAll ) &&
838                       ( theAction == eDisplayOnly ) &&
839                       ( resMgr = SMESHGUI::resourceMgr() ))
840                   {
841                     needFitAll = resMgr->booleanValue( "SMESH", "fitall_on_displayonly", false );
842                   }
843                   if ( needFitAll )
844                     FitAll();
845                 }
846                 else {
847                   OK = false;
848                 }
849               }
850               break;
851             }
852           default:;
853           }
854         }
855       }
856       }
857     }
858     return OK;
859   }
860
861
862   bool UpdateView(EDisplaing theAction, const char* theEntry) {
863     //MESSAGE("UpdateView");
864     SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
865     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
866     if ( SUIT_ViewManager* vm = app->activeViewManager() )
867     {
868       SUIT_ViewWindow *aWnd = vm->getActiveView();
869       return UpdateView(aWnd,theAction,theEntry);
870     }
871     return false;
872   }
873
874   void UpdateView( bool withChildrenOfSelected )
875   {
876     if ( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()) {
877       LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
878       SALOME_ListIO selected; mgr->selectedObjects( selected );
879
880       if ( selected.Extent() == 0 ) {
881         vtkRenderer* aRenderer = aWnd->getRenderer();
882         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
883         vtkActorCollection *aCollection = aCopy.GetActors();
884         aCollection->InitTraversal();
885         while(vtkActor *anAct = aCollection->GetNextActor()){
886           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
887             if(anActor->hasIO())
888               if (!Update(anActor->getIO(),anActor->GetVisibility()))
889                 break; // avoid multiple warnings if visu failed
890           }
891         }
892       }
893       else
894       {
895         SALOME_ListIteratorOfListIO anIter( selected );
896         for( ; anIter.More(); anIter.Next())
897         {
898           Handle(SALOME_InteractiveObject) anIO = anIter.Value();
899           if ( !Update( anIO, true ))
900             break; // avoid multiple warnings if visu failed
901
902           if ( withChildrenOfSelected ) // update all visible children
903           {
904             QString aFatherID = anIO->getEntry();
905             vtkRenderer* aRenderer = aWnd->getRenderer();
906             VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
907             vtkActorCollection *aCollection = aCopy.GetActors();
908             aCollection->InitTraversal();
909             while ( vtkActor *anAct = aCollection->GetNextActor() ) {
910               if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct )) {
911                 if ( anActor->hasIO() && anActor->GetVisibility() )
912                 {
913                   QString aChildID = anActor->getIO()->getEntry();
914                   if ( aChildID.size() > aFatherID.size() &&
915                        aChildID.startsWith( aFatherID ))
916                     if ( ! Update( anActor->getIO(), true ))
917                       break;
918                 }
919               }
920             }
921           }
922         }
923       }
924       RepaintCurrentView();
925     }
926   }
927
928
929   bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
930   {
931     //MESSAGE("Update");
932     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(theIO->getEntry())) {
933       if ( theDisplay )
934         UpdateView(SMESH::eDisplay,theIO->getEntry());
935       return true;
936     }
937     return false;
938   }
939
940   bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
941   {
942     //MESSAGE("UpdateNulData");
943     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(theIO->getEntry(), true)) {
944       if ( theDisplay )
945         UpdateView(SMESH::eDisplay,theIO->getEntry());
946       return true;
947     }
948     return false;
949   }
950
951   void UpdateSelectionProp( SMESHGUI* theModule )
952   {
953     if( !theModule )
954       return;
955
956     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
957     if( !app )
958     {
959       MESSAGE( "UpdateSelectionProp: Application is null" );
960       return;
961     }
962
963     SUIT_ViewManager* vm = app->activeViewManager();
964     if( !vm )
965     {
966       MESSAGE( "UpdateSelectionProp: View manager is null" );
967       return;
968     }
969
970     QVector<SUIT_ViewWindow*> views = vm->getViews();
971
972     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
973     if( !mgr )
974     {
975       MESSAGE( "UpdateSelectionProp: Resource manager is null" );
976       return;
977     }
978
979     SMESH_ActorProps::props()->reset();
980
981     QColor
982       //aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
983       aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
984       aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
985
986     int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
987     // int aBallSize   = mgr->integerValue("SMESH", "ball_elem_size", 5);
988     int aLineWidth  = mgr->integerValue("SMESH", "element_width", 1);
989     int maxSize = aElem0DSize;
990     if (aElem0DSize > maxSize) maxSize = aElem0DSize;
991     if (aLineWidth > maxSize) maxSize = aLineWidth;
992     //  if (aBallSize > maxSize) maxSize = aBallSize;
993
994     double
995       SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
996       SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
997       SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
998
999     for ( int i=0, n=views.count(); i<n; i++ )
1000     {
1001       // update VTK viewer properties
1002       if ( SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] ))
1003       {
1004         // mesh element selection
1005         aVtkView->SetSelectionProp(aSelColor.red()/255.,
1006                                    aSelColor.green()/255.,
1007                                    aSelColor.blue()/255.);
1008         // tolerances
1009         aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
1010
1011         // pre-selection
1012         aVtkView->SetPreselectionProp(aPreColor.red()/255.,
1013                                       aPreColor.green()/255.,
1014                                       aPreColor.blue()/255.);
1015         // update actors
1016         vtkRenderer* aRenderer = aVtkView->getRenderer();
1017         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1018         vtkActorCollection *aCollection = aCopy.GetActors();
1019         aCollection->InitTraversal();
1020         while ( vtkActor *anAct = aCollection->GetNextActor() ) {
1021           if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct) ) {
1022             anActor->UpdateSelectionProps();
1023           }
1024         }
1025       }
1026     }
1027   }
1028
1029
1030   void UpdateFontProp( SMESHGUI* theModule )
1031   {
1032     if ( !theModule ) return;
1033
1034     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
1035     if ( !app ) return;
1036
1037     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
1038     if ( !mgr ) return;
1039     //
1040     double anRGBNd[3] = {1,1,1};
1041     SMESH::GetColor( "SMESH", "numbering_node_color", anRGBNd[0], anRGBNd[1], anRGBNd[2], QColor( 255, 255, 255 ) );
1042     int aSizeNd = 10;
1043     SMESH::LabelFont aFamilyNd = SMESH::FntTimes;
1044     bool aBoldNd    = true;
1045     bool anItalicNd = false;
1046     bool aShadowNd  = false;
1047
1048     if ( mgr->hasValue( "SMESH", "numbering_node_font" ) ) {
1049       QFont f = mgr->fontValue( "SMESH", "numbering_node_font" );
1050       if ( f.family()      == "Arial" )   aFamilyNd = SMESH::FntArial;
1051       else if ( f.family() == "Courier" ) aFamilyNd = SMESH::FntCourier;
1052       else if ( f.family() == "Times" )   aFamilyNd = SMESH::FntTimes;
1053       aBoldNd    = f.bold();
1054       anItalicNd = f.italic();
1055       aShadowNd  = f.overline();
1056       aSizeNd    = f.pointSize();
1057     }
1058     //
1059     double anRGBEl[3] = {0,1,0};
1060     SMESH::GetColor( "SMESH", "numbering_elem_color", anRGBEl[0], anRGBEl[1], anRGBEl[2], QColor( 0, 255, 0 ) );
1061     int aSizeEl = 12;
1062     SMESH::LabelFont aFamilyEl = SMESH::FntTimes;
1063     bool aBoldEl    = true;
1064     bool anItalicEl = false;
1065     bool aShadowEl  = false;
1066
1067     if ( mgr->hasValue( "SMESH", "numbering_elem_font" ) ) {
1068       QFont f = mgr->fontValue( "SMESH", "numbering_elem_font" );
1069
1070       if ( f.family()      == "Arial" )   aFamilyEl = SMESH::FntArial;
1071       else if ( f.family() == "Courier" ) aFamilyEl = SMESH::FntCourier;
1072       else if ( f.family() == "Times" )   aFamilyEl = SMESH::FntTimes;    
1073       aBoldEl    = f.bold();
1074       anItalicEl = f.italic();
1075       aShadowEl  = f.overline();
1076       aSizeEl    = f.pointSize();
1077     }
1078     //
1079     ViewManagerList vmList;
1080     app->viewManagers( SVTK_Viewer::Type(), vmList );
1081     foreach ( SUIT_ViewManager* vm, vmList ) {
1082       QVector<SUIT_ViewWindow*> views = vm->getViews();
1083       foreach ( SUIT_ViewWindow* vw, views ) {
1084         // update VTK viewer properties
1085         if ( SVTK_ViewWindow* aVtkView = GetVtkViewWindow( vw ) ) {
1086           // update actors
1087           vtkRenderer* aRenderer = aVtkView->getRenderer();
1088           VTK::ActorCollectionCopy aCopy( aRenderer->GetActors() );
1089           vtkActorCollection* aCollection = aCopy.GetActors();
1090           aCollection->InitTraversal();
1091           while ( vtkActor* anAct = aCollection->GetNextActor() ) {
1092             if ( SMESH_NodeLabelActor* anActor = dynamic_cast< SMESH_NodeLabelActor* >( anAct ) ) {
1093               anActor->SetFontProperties( aFamilyNd, aSizeNd, aBoldNd, anItalicNd, aShadowNd, anRGBNd[0], anRGBNd[1], anRGBNd[2] );
1094             }
1095             else if ( SMESH_CellLabelActor* anActor = dynamic_cast< SMESH_CellLabelActor* >( anAct ) ) {
1096               anActor->SetFontProperties( aFamilyEl, aSizeEl, aBoldEl, anItalicEl, aShadowEl, anRGBEl[0], anRGBEl[1], anRGBEl[2] );
1097             }
1098           }
1099           aVtkView->Repaint( false ); 
1100         }
1101       }
1102     }
1103   }
1104
1105   //----------------------------------------------------------------------------
1106   SVTK_Selector*
1107   GetSelector(SUIT_ViewWindow *theWindow)
1108   {
1109     if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
1110       return aWnd->GetSelector();
1111
1112     return NULL;
1113   }
1114
1115   void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
1116                  SVTK_Selector* theSelector)
1117   {
1118     if (theSelector)
1119       theSelector->SetFilter(theFilter);
1120   }
1121
1122   Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
1123   {
1124     return theSelector->GetFilter(theId);
1125   }
1126
1127   bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
1128   {
1129     return theSelector->IsFilterPresent(theId);
1130   }
1131
1132   void RemoveFilter(int theId, SVTK_Selector* theSelector)
1133   {
1134     theSelector->RemoveFilter(theId);
1135   }
1136
1137   void RemoveFilters(SVTK_Selector* theSelector)
1138   {
1139     for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
1140       theSelector->RemoveFilter( id );
1141   }
1142
1143   bool IsValid(SALOME_Actor* theActor, int theCellId,
1144                SVTK_Selector* theSelector)
1145   {
1146     return theSelector->IsValid(theActor,theCellId);
1147   }
1148
1149
1150   //----------------------------------------------------------------------------
1151   void SetPointRepresentation(bool theIsVisible)
1152   {
1153     if ( SVTK_ViewWindow* aViewWindow = GetCurrentVtkView() ) {
1154       vtkRenderer *aRenderer = aViewWindow->getRenderer();
1155       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1156       vtkActorCollection *aCollection = aCopy.GetActors();
1157       aCollection->InitTraversal();
1158       while(vtkActor *anAct = aCollection->GetNextActor()){
1159         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
1160           if(anActor->GetVisibility()){
1161             anActor->SetPointRepresentation(theIsVisible);
1162           }
1163         }
1164       }
1165       RepaintCurrentView();
1166     }
1167   }
1168
1169
1170   void SetPickable(SMESH_Actor* theActor)
1171   {
1172     if ( SVTK_ViewWindow* aWnd = GetCurrentVtkView() ) {
1173       int anIsAllPickable = (theActor == NULL);
1174       vtkRenderer *aRenderer = aWnd->getRenderer();
1175       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1176       vtkActorCollection *aCollection = aCopy.GetActors();
1177       aCollection->InitTraversal();
1178       while(vtkActor *anAct = aCollection->GetNextActor()){
1179         if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
1180           if(anActor->GetVisibility()){
1181             anActor->SetPickable(anIsAllPickable);
1182           }
1183         }
1184       }
1185       if(theActor)
1186         theActor->SetPickable(!anIsAllPickable);
1187       RepaintCurrentView();
1188     }
1189   }
1190
1191
1192   //----------------------------------------------------------------------------
1193   int GetNameOfSelectedNodes(SVTK_Selector*                          theSelector,
1194                              const Handle(SALOME_InteractiveObject)& theIO,
1195                              QString&                                theName)
1196   {
1197     theName = "";
1198     SVTK_TIndexedMapOfVtkId aMapIndex;
1199     theSelector->GetIndex(theIO,aMapIndex);
1200
1201     for(int i = 1; i <= aMapIndex.Extent(); i++)
1202       theName += QString(" %1").arg(aMapIndex(i));
1203
1204     return aMapIndex.Extent();
1205   }
1206
1207   int GetNameOfSelectedElements(SVTK_Selector* theSelector,
1208                                 const Handle(SALOME_InteractiveObject)& theIO,
1209                                 QString& theName)
1210   {
1211     theName = "";
1212     SVTK_TIndexedMapOfVtkId aMapIndex;
1213     theSelector->GetIndex(theIO,aMapIndex);
1214
1215     typedef std::set<vtkIdType> TIdContainer;
1216     TIdContainer anIdContainer;
1217     for( int i = 1; i <= aMapIndex.Extent(); i++)
1218       anIdContainer.insert(aMapIndex(i));
1219
1220     TIdContainer::const_iterator anIter = anIdContainer.begin();
1221     for( ; anIter != anIdContainer.end(); anIter++)
1222       theName += QString(" %1").arg(*anIter);
1223
1224     return aMapIndex.Extent();
1225   }
1226
1227
1228   int GetEdgeNodes(SVTK_Selector* theSelector,
1229                    const TVisualObjPtr& theVisualObject,
1230                    ::smIdType& theId1,
1231                    ::smIdType& theId2)
1232   {
1233     const SALOME_ListIO& selected = theSelector->StoredIObjects();
1234
1235     if ( selected.Extent() != 1 )
1236       return -1;
1237
1238     Handle(SALOME_InteractiveObject) anIO = selected.First();
1239     if ( anIO.IsNull() || !anIO->hasEntry() )
1240       return -1;
1241
1242     SVTK_TIndexedMapOfVtkId aMapIndex;
1243     theSelector->GetIndex( anIO, aMapIndex );
1244     if ( aMapIndex.Extent() != 2 )
1245       return -1;
1246
1247     ::smIdType anObjId = -1, anEdgeNum = -1;
1248     for ( ::smIdType i = 1; i <= aMapIndex.Extent(); i++ ) {
1249       ::smIdType aVal = aMapIndex( i );
1250       if ( aVal > 0 )
1251         anObjId = aVal;
1252       else
1253         anEdgeNum = abs( aVal ) - 1;
1254     }
1255
1256     if ( anObjId == -1 || anEdgeNum == -1 )
1257       return -1;
1258
1259     return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1260   }
1261
1262   //----------------------------------------------------------------------------
1263   int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1264                              const Handle(SALOME_InteractiveObject)& theIO,
1265                              QString& theName)
1266   {
1267     theName = "";
1268     if(theIO->hasEntry()){
1269       if(FindActorByEntry(theIO->getEntry())){
1270         TColStd_IndexedMapOfInteger aMapIndex;
1271         theMgr->GetIndexes(theIO,aMapIndex);
1272         for(int i = 1; i <= aMapIndex.Extent(); i++){
1273           theName += QString(" %1").arg(aMapIndex(i));
1274         }
1275         return aMapIndex.Extent();
1276       }
1277     }
1278     return -1;
1279   }
1280
1281   int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName)
1282   {
1283     theName = "";
1284     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1285     if(selected.Extent() == 1){
1286       Handle(SALOME_InteractiveObject) anIO = selected.First();
1287       return GetNameOfSelectedNodes(theMgr,anIO,theName);
1288     }
1289     return -1;
1290   }
1291
1292
1293   int GetNameOfSelectedElements(LightApp_SelectionMgr *                 theMgr,
1294                                 const Handle(SALOME_InteractiveObject)& theIO,
1295                                 QString&                                theName)
1296   {
1297     theName = "";
1298     if(theIO->hasEntry()){
1299       if(FindActorByEntry(theIO->getEntry())){
1300         TColStd_IndexedMapOfInteger aMapIndex;
1301         theMgr->GetIndexes(theIO,aMapIndex);
1302         typedef std::set<int> TIdContainer;
1303         TIdContainer anIdContainer;
1304         for( int i = 1; i <= aMapIndex.Extent(); i++)
1305           anIdContainer.insert(aMapIndex(i));
1306         TIdContainer::const_iterator anIter = anIdContainer.begin();
1307         for( ; anIter != anIdContainer.end(); anIter++){
1308           theName += QString(" %1").arg(*anIter);
1309         }
1310         return aMapIndex.Extent();
1311       }
1312     }
1313     return -1;
1314   }
1315
1316
1317   int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1318   {
1319     theName = "";
1320     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1321
1322     if( selected.Extent() == 1){
1323       Handle(SALOME_InteractiveObject) anIO = selected.First();
1324       return GetNameOfSelectedElements(theMgr,anIO,theName);
1325     }
1326     return -1;
1327   }
1328
1329   int GetSelected(LightApp_SelectionMgr*       theMgr,
1330                   TColStd_IndexedMapOfInteger& theMap,
1331                   const bool                   /*theIsElement*/)
1332   {
1333     theMap.Clear();
1334     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1335
1336     if ( selected.Extent() == 1 )
1337     {
1338       Handle(SALOME_InteractiveObject) anIO = selected.First();
1339       if ( anIO->hasEntry() ) {
1340         theMgr->GetIndexes( anIO, theMap );
1341       }
1342     }
1343     return theMap.Extent();
1344   }
1345
1346
1347   int GetEdgeNodes( LightApp_SelectionMgr* theMgr, ::smIdType& theId1, ::smIdType& theId2 )
1348   {
1349     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1350
1351     if ( selected.Extent() != 1 )
1352       return -1;
1353
1354     Handle(SALOME_InteractiveObject) anIO = selected.First();
1355     if ( anIO.IsNull() || !anIO->hasEntry() )
1356       return -1;
1357
1358     SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1359     if ( anActor == 0 )
1360       return -1;
1361
1362     TColStd_IndexedMapOfInteger aMapIndex;
1363     theMgr->GetIndexes( anIO, aMapIndex );
1364     if ( aMapIndex.Extent() != 2 )
1365       return -1;
1366
1367     ::smIdType anObjId = -1, anEdgeNum = -1;
1368     for ( ::smIdType i = 1; i <= aMapIndex.Extent(); i++ ) {
1369       ::smIdType aVal = aMapIndex( i );
1370       if ( aVal > 0 )
1371         anObjId = aVal;
1372       else
1373         anEdgeNum = abs( aVal );
1374     }
1375
1376     if ( anObjId == -1 || anEdgeNum == -1 )
1377       return -1;
1378
1379     return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1380   }
1381
1382   void SetControlsPrecision( const long theVal )
1383   {
1384     if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1385     {
1386       vtkRenderer *aRenderer = aWnd->getRenderer();
1387       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1388       vtkActorCollection *aCollection = aCopy.GetActors();
1389       aCollection->InitTraversal();
1390
1391       while ( vtkActor *anAct = aCollection->GetNextActor())
1392       {
1393         if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1394         {
1395           anActor->SetControlsPrecision( theVal );
1396           anActor->SetControlMode( anActor->GetControlMode() );
1397         }
1398       }
1399
1400     }
1401   }
1402
1403   //----------------------------------------------------------------------------
1404   // internal function
1405   void ComputeBoundsParam( double theBounds[6],
1406                            double theDirection[3],
1407                            double theMinPnt[3],
1408                            double& theMaxBoundPrj,
1409                            double& theMinBoundPrj )
1410   {
1411     //Enlarge bounds in order to avoid conflicts of precision
1412     for(int i = 0; i < 6; i += 2){
1413       static double EPS = 1.0E-3;
1414       double aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1415       theBounds[i] -= aDelta;
1416       theBounds[i+1] += aDelta;
1417     }
1418
1419     double aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1420                                   {theBounds[1],theBounds[2],theBounds[4]},
1421                                   {theBounds[0],theBounds[3],theBounds[4]},
1422                                   {theBounds[1],theBounds[3],theBounds[4]},
1423                                   {theBounds[0],theBounds[2],theBounds[5]},
1424                                   {theBounds[1],theBounds[2],theBounds[5]},
1425                                   {theBounds[0],theBounds[3],theBounds[5]},
1426                                   {theBounds[1],theBounds[3],theBounds[5]}};
1427
1428     int aMaxId = 0;
1429     theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1430     theMinBoundPrj = theMaxBoundPrj;
1431     for(int i = 1; i < 8; i++){
1432       double aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1433       if(theMaxBoundPrj < aTmp){
1434         theMaxBoundPrj = aTmp;
1435         aMaxId = i;
1436       }
1437       if(theMinBoundPrj > aTmp){
1438         theMinBoundPrj = aTmp;
1439       }
1440     }
1441     double *aMinPnt = aBoundPoints[aMaxId];
1442     theMinPnt[0] = aMinPnt[0];
1443     theMinPnt[1] = aMinPnt[1];
1444     theMinPnt[2] = aMinPnt[2];
1445   }
1446
1447   // internal function
1448   void DistanceToPosition( double theBounds[6],
1449                            double theDirection[3],
1450                            double theDist,
1451                            double thePos[3] )
1452   {
1453     double aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1454     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1455     double aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1456     thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1457     thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1458     thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1459   }
1460
1461   // internal function (currently unused, left just in case)
1462   void PositionToDistance( double theBounds[6],
1463                            double theDirection[3],
1464                            double thePos[3],
1465                            double& theDist )
1466   {
1467     double aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1468     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1469     double aPrj = vtkMath::Dot(theDirection,thePos);
1470     theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1471   }
1472
1473   bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1474                                        double theNormal[3],
1475                                        double theDist,
1476                                        double theBounds[6],
1477                                        double theOrigin[3] )
1478   {
1479     bool anIsOk = false;
1480     anIsOk = ComputeBounds( theActorList, theBounds );
1481
1482
1483     if( !anIsOk )
1484       return false;
1485
1486     DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1487     return true;
1488   }
1489
1490   bool ComputeBounds( std::list<vtkActor*> theActorList,
1491                       double theBounds[6])
1492   {
1493     bool anIsOk = false;
1494     theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1495     theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1496     std::list<vtkActor*>::iterator anIter = theActorList.begin();
1497     for( ; anIter != theActorList.end(); anIter++ ) {
1498       if( vtkActor* aVTKActor = *anIter ) {
1499         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1500           double aBounds[6];
1501           anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1502           theBounds[0] = std::min( theBounds[0], aBounds[0] );
1503           theBounds[1] = std::max( theBounds[1], aBounds[1] );
1504           theBounds[2] = std::min( theBounds[2], aBounds[2] );
1505           theBounds[3] = std::max( theBounds[3], aBounds[3] );
1506           theBounds[4] = std::min( theBounds[4], aBounds[4] );
1507           theBounds[5] = std::max( theBounds[5], aBounds[5] );
1508           anIsOk = true;
1509         }
1510       }
1511     }
1512     return anIsOk;
1513   }
1514
1515 #ifndef DISABLE_PLOT2DVIEWER
1516   //================================================================================
1517   /*!
1518    * \brief Find all SMESH_Actor's in the View Window.
1519    * If actor contains Plot2d_Histogram object remove it from each Plot2d Viewer.
1520    */
1521   //================================================================================
1522
1523   void ClearPlot2Viewers( SUIT_ViewWindow* theWindow )
1524   {
1525     if ( SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow) ) {
1526       vtkRenderer *aRenderer = aViewWindow->getRenderer();
1527       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1528       vtkActorCollection *aCollection = aCopy.GetActors();
1529       aCollection->InitTraversal();
1530       while(vtkActor *anAct = aCollection->GetNextActor()){
1531         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
1532           if(anActor->hasIO() && anActor->GetPlot2Histogram() ){
1533             ProcessIn2DViewers(anActor,RemoveFrom2dViewer);
1534           }
1535         }
1536       }
1537     }
1538   }
1539
1540 #endif
1541
1542 } // end of namespace SMESH