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