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