Salome HOME
merge from branch BR_SMDS_MEMIMP 29 nov 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,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,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,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, bool nulData){
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         //MESSAGE("GetVisualObj");
335         if (nulData)
336                 objModified = aVisualObj->NulData();
337         else
338           objModified = aVisualObj->Update();
339       }
340       catch (...) {
341 #ifdef _DEBUG_
342         MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
343 #endif
344         RemoveVisualObjectWithActors( theEntry ); // remove this object
345         OnVisuException();
346         aVisualObj.reset();
347       }
348     }
349
350     if ( objModified ) {
351       // PAL16631. Mesurements showed that to show aVisualObj in SHADING(default) mode,
352       // ~10 times more memory is used than it occupies.
353       // Warn the user if there is less free memory than 30 sizes of a grid
354       // TODO: estimate memory usage in other modes and take current mode into account
355       int freeMB = SMDS_Mesh::CheckMemory(true);
356       int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
357       if ( freeMB > 0 && usedMB * 30 > freeMB ) {
358 #ifdef _DEBUG_
359         MESSAGE ( "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
360                << ", usedMB=" << usedMB );
361 #endif
362 //        bool continu = false;
363 //        if ( usedMB * 10 > freeMB )
364 //          // even dont try to show
365 //          SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
366 //                                   QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
367 //        else
368 //          // there is a chance to succeed
369 //          continu = SUIT_MessageBox::warning
370 //            (SMESHGUI::desktop(),
371 //             QObject::tr("SMESH_WRN_WARNING"),
372 //             QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
373 //             SUIT_MessageBox::Yes | SUIT_MessageBox::No,
374 //             SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
375 //        if ( !continu ) {
376 //          // remove the corresponding actors from all views
377 //          RemoveVisualObjectWithActors( theEntry );
378 //          aVisualObj.reset();
379 //        }
380       }
381     }
382
383     return aVisualObj;
384   }
385
386
387   /*! Return active view window, if it instantiates SVTK_ViewWindow class,
388    *  overwise find or create corresponding view window, make it active and return it.
389    *  \note Active VVTK_ViewWindow can be returned, because it inherits SVTK_ViewWindow.
390    */
391   SVTK_ViewWindow* GetViewWindow (const SalomeApp_Module* theModule,
392                                   bool createIfNotFound)
393   {
394     SalomeApp_Application* anApp;
395     if (theModule)
396       anApp = theModule->getApp();
397     else
398       anApp = dynamic_cast<SalomeApp_Application*>
399         (SUIT_Session::session()->activeApplication());
400
401     if (anApp) {
402       if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow()))
403         return aView;
404
405       SUIT_ViewManager* aViewManager =
406         anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
407       if (aViewManager) {
408         if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
409           if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
410             aViewWindow->raise();
411             aViewWindow->setFocus();
412             return aView;
413           }
414         }
415       }
416     }
417     return NULL;
418   }
419
420   SVTK_ViewWindow* FindVtkViewWindow (SUIT_ViewManager* theMgr,
421                                       SUIT_ViewWindow * theWindow)
422   {
423     if( !theMgr )
424       return NULL;
425
426     QVector<SUIT_ViewWindow*> views = theMgr->getViews();
427     if( views.contains( theWindow ) )
428       return GetVtkViewWindow( theWindow );
429     else
430       return NULL;
431   }
432
433   SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
434     return dynamic_cast<SVTK_ViewWindow*>(theWindow);
435   }
436
437 /*  SUIT_ViewWindow* GetActiveWindow()
438   {
439     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
440     if( !app )
441       return NULL;
442     SUIT_ViewManager* mgr = app->activeViewManager();
443     if( mgr )
444       return mgr->getActiveView();
445     else
446       return NULL;
447   }*/
448
449   SVTK_ViewWindow* GetCurrentVtkView(){
450     return GetVtkViewWindow( GetActiveWindow() );
451   }
452
453
454   void RepaintCurrentView()
455   {
456     if (SVTK_ViewWindow* wnd = GetCurrentVtkView())
457     {
458       try {
459 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
460         OCC_CATCH_SIGNALS;
461 #endif
462         wnd->getRenderer()->Render();
463         wnd->Repaint(false);
464       }
465       catch (...) {
466 #ifdef _DEBUG_
467         MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
468 #endif
469         OnVisuException();
470       }
471     }
472   }
473
474   void RepaintViewWindow(SVTK_ViewWindow* theWindow)
475   {
476     try {
477 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
478       OCC_CATCH_SIGNALS;
479 #endif
480       theWindow->getRenderer()->Render();
481       theWindow->Repaint();
482     }
483     catch (...) {
484 #ifdef _DEBUG_
485       MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
486 #endif
487       OnVisuException();
488     }
489   }
490
491   void RenderViewWindow(SVTK_ViewWindow* theWindow)
492   {
493     try {
494 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
495       OCC_CATCH_SIGNALS;
496 #endif
497       theWindow->getRenderer()->Render();
498       theWindow->Repaint();
499     }
500     catch (...) {
501 #ifdef _DEBUG_
502       MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
503 #endif
504       OnVisuException();
505     }
506   }
507
508   void FitAll(){
509     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
510       try {
511 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
512         OCC_CATCH_SIGNALS;
513 #endif
514         wnd->onFitAll();
515         wnd->Repaint();
516       }
517       catch (...) {
518 #ifdef _DEBUG_
519         MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
520 #endif
521         OnVisuException();
522       }
523     }
524   }
525
526
527   SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
528                                 const char* theEntry)
529   {
530     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
531       vtkRenderer *aRenderer = aViewWindow->getRenderer();
532       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
533       vtkActorCollection *aCollection = aCopy.GetActors();
534       aCollection->InitTraversal();
535       while(vtkActor *anAct = aCollection->GetNextActor()){
536         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
537           if(anActor->hasIO()){
538             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
539             if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
540               return anActor;
541             }
542           }
543         }
544       }
545     }
546     return NULL;
547   }
548
549
550   SMESH_Actor* FindActorByEntry(const char* theEntry){
551     return FindActorByEntry(GetActiveWindow(),theEntry);
552   }
553
554
555   SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
556     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
557     if( !app )
558       return NULL;
559
560     if(!CORBA::is_nil(theObject)){
561       _PTR(Study) aStudy = GetActiveStudyDocument();
562       CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
563       _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
564       if(aSObject){
565         CORBA::String_var anEntry = aSObject->GetID().c_str();
566         return FindActorByEntry(anEntry.in());
567       }
568     }
569     return NULL;
570   }
571
572
573   SMESH_Actor* CreateActor(_PTR(Study) theStudy,
574                            const char* theEntry,
575                            int theIsClear)
576   {
577     SMESH_Actor *anActor = NULL;
578     CORBA::Long anId = theStudy->StudyId();
579     if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
580       _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
581       if(aSObj){
582         _PTR(GenericAttribute) anAttr;
583         if(aSObj->FindAttribute(anAttr,"AttributeName")){
584           _PTR(AttributeName) aName = anAttr;
585           std::string aNameVal = aName->Value();
586           anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
587         }
588
589         SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( aSObj ));
590         if(!CORBA::is_nil(aGroup))
591         {
592           SALOMEDS::Color aColor = aGroup->GetColor();
593           if( !( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 ) )
594           {
595             int r = 0, g = 0, b = 0;
596             SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
597             aColor.R = (float)r / 255.0;
598             aColor.G = (float)g / 255.0;
599             aColor.B = (float)b / 255.0;
600             aGroup->SetColor( aColor );
601           }
602           if( aGroup->GetType() == SMESH::NODE )
603             anActor->SetNodeColor( aColor.R, aColor.G, aColor.B );
604           else if( aGroup->GetType() == SMESH::EDGE )
605             anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B );
606           else if( aGroup->GetType() == SMESH::ELEM0D )
607             anActor->Set0DColor( aColor.R, aColor.G, aColor.B );
608           else
609             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B );
610         }
611       }
612     }
613     MESSAGE("CreateActor " << anActor);
614     if( anActor )
615       if( SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI() )
616         aSMESHGUI->addActorAsObserver( anActor );
617     return anActor;
618   }
619
620
621   void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
622     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
623       try {
624 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
625         OCC_CATCH_SIGNALS;
626 #endif
627         MESSAGE("DisplayActor " << theActor);
628         vtkWnd->AddActor(theActor);
629         vtkWnd->Repaint();
630       }
631       catch (...) {
632 #ifdef _DEBUG_
633         MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
634 #endif
635         OnVisuException();
636       }
637     }
638   }
639
640
641   void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
642     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
643         MESSAGE("RemoveActor " << theActor);
644       vtkWnd->RemoveActor(theActor);
645       if(theActor->hasIO()){
646         Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
647         if(anIO->hasEntry()){
648           std::string anEntry = anIO->getEntry();
649           SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
650           int aStudyId = aStudy->id();
651           TVisualObjCont::key_type aKey(aStudyId,anEntry);
652           VISUAL_OBJ_CONT.erase(aKey);
653         }
654       }
655       theActor->Delete();
656       vtkWnd->Repaint();
657     }
658   }
659
660   //================================================================================
661   /*!
662    * \brief Return true if there are no SMESH actors in a view
663    */
664   //================================================================================
665
666   bool noSmeshActors(SUIT_ViewWindow *theWnd)
667   {
668     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWnd)) {
669       vtkRenderer *aRenderer = aViewWindow->getRenderer();
670       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
671       vtkActorCollection *aCollection = aCopy.GetActors();
672       aCollection->InitTraversal();
673       while(vtkActor *anAct = aCollection->GetNextActor())
674         if(dynamic_cast<SMESH_Actor*>(anAct))
675           return false;
676     }
677     return true;
678   }
679
680   bool UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
681   {
682         //MESSAGE("UpdateView");
683     bool OK = false;
684     SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd);
685     if (!aViewWnd)
686       return OK;
687
688     {
689       OK = true;
690       vtkRenderer *aRenderer = aViewWnd->getRenderer();
691       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
692       vtkActorCollection *aCollection = aCopy.GetActors();
693       aCollection->InitTraversal();
694
695       switch (theAction) {
696       case eDisplayAll: {
697         while (vtkActor *anAct = aCollection->GetNextActor()) {
698           if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
699                 MESSAGE("--- display " << anActor);
700             anActor->SetVisibility(true);
701           }
702         }
703         break;
704       }
705       case eDisplayOnly:
706       case eEraseAll: {
707         //MESSAGE("---case eDisplayOnly");
708         while (vtkActor *anAct = aCollection->GetNextActor()) {
709           if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
710                 //MESSAGE("--- erase " << anActor);
711             anActor->SetVisibility(false);
712           }
713         }
714       }
715       default: {
716         if (SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)) {
717           switch (theAction) {
718             case eDisplay:
719             case eDisplayOnly:
720                 //MESSAGE("--- display " << anActor);
721               anActor->SetVisibility(true);
722               if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
723               break;
724             case eErase:
725                 //MESSAGE("--- erase " << anActor);
726               anActor->SetVisibility(false);
727               break;
728           }
729         } else {
730           switch (theAction) {
731           case eDisplay:
732           case eDisplayOnly:
733             {
734                 //MESSAGE("---");
735               SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
736               _PTR(Study) aDocument = aStudy->studyDS();
737               // Pass non-visual objects (hypotheses, etc.), return true in this case
738               CORBA::Long anId = aDocument->StudyId();
739               TVisualObjPtr aVisualObj;
740               if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
741               {
742                 if ((anActor = CreateActor(aDocument,theEntry,true))) {
743                   bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
744                   DisplayActor(theWnd,anActor);
745                   // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
746                   if (needFitAll) FitAll();
747                 } else {
748                   OK = false;
749                 }
750               }
751               break;
752             }
753           }
754         }
755       }
756       }
757     }
758     return OK;
759   }
760
761
762   bool UpdateView(EDisplaing theAction, const char* theEntry){
763         //MESSAGE("UpdateView");
764     SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
765     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
766     SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
767     return UpdateView(aWnd,theAction,theEntry);
768   }
769
770   void UpdateView(){
771     if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
772       LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
773       SALOME_ListIO selected; mgr->selectedObjects( selected );
774
775       if( selected.Extent() == 0){
776         vtkRenderer* aRenderer = aWnd->getRenderer();
777         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
778         vtkActorCollection *aCollection = aCopy.GetActors();
779         aCollection->InitTraversal();
780         while(vtkActor *anAct = aCollection->GetNextActor()){
781           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
782             if(anActor->hasIO())
783               if (!Update(anActor->getIO(),anActor->GetVisibility()))
784                 break; // avoid multiple warinings if visu failed
785           }
786         }
787       }else{
788         SALOME_ListIteratorOfListIO anIter( selected );
789         for( ; anIter.More(); anIter.Next()){
790           Handle(SALOME_InteractiveObject) anIO = anIter.Value();
791           if ( !Update(anIO,true) )
792             break; // avoid multiple warinings if visu failed
793         }
794       }
795       RepaintCurrentView();
796     }
797   }
798
799
800   bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
801   {
802         MESSAGE("Update");
803     _PTR(Study) aStudy = GetActiveStudyDocument();
804     CORBA::Long anId = aStudy->StudyId();
805     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
806       if ( theDisplay )
807         UpdateView(SMESH::eDisplay,theIO->getEntry());
808       return true;
809     }
810     return false;
811   }
812
813   bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
814   {
815         MESSAGE("UpdateNulData");
816     _PTR(Study) aStudy = GetActiveStudyDocument();
817     CORBA::Long anId = aStudy->StudyId();
818     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry(), true)) {
819       if ( theDisplay )
820         UpdateView(SMESH::eDisplay,theIO->getEntry());
821       return true;
822     }
823     return false;
824   }
825
826   void UpdateSelectionProp( SMESHGUI* theModule ) {
827     if( !theModule )
828       return;
829
830     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
831     if( !app )
832     {
833       MESSAGE( "UpdateSelectionProp: Application is null" );
834       return;
835     }
836
837     SUIT_ViewManager* vm = app->activeViewManager();
838     if( !vm )
839     {
840       MESSAGE( "UpdateSelectionProp: View manager is null" );
841       return;
842     }
843
844     QVector<SUIT_ViewWindow*> views = vm->getViews();
845
846     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
847     if( !mgr )
848     {
849       MESSAGE( "UpdateSelectionProp: Resource manager is null" );
850       return;
851     }
852
853     QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
854            aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
855            aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
856
857     int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
858         PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
859
860     // adjust highlight_width to the width of mesh entities
861     int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
862     int aLineWidth  = mgr->integerValue("SMESH", "element_width", 1);
863     int maxSize = aElem0DSize;
864     if (aElem0DSize > maxSize) maxSize = aElem0DSize;
865     if (aLineWidth > maxSize) maxSize = aLineWidth;
866     if (PW < maxSize + 2) PW = maxSize + 2;
867
868     double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
869            SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
870            SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
871
872     for ( int i=0, n=views.count(); i<n; i++ ){
873       // update VTK viewer properties
874       if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
875         // mesh element selection
876         aVtkView->SetSelectionProp(aSelColor.red()/255.,
877                                    aSelColor.green()/255.,
878                                    aSelColor.blue()/255.,
879                                    SW );
880         // tolerances
881         aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
882
883         // pre-selection
884         aVtkView->SetPreselectionProp(aPreColor.red()/255.,
885                                       aPreColor.green()/255.,
886                                       aPreColor.blue()/255.,
887                                       PW);
888         // update actors
889         vtkRenderer* aRenderer = aVtkView->getRenderer();
890         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
891         vtkActorCollection *aCollection = aCopy.GetActors();
892         aCollection->InitTraversal();
893         while(vtkActor *anAct = aCollection->GetNextActor()){
894           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
895             anActor->SetHighlightColor(aHiColor.red()/255.,
896                                        aHiColor.green()/255.,
897                                        aHiColor.blue()/255.);
898             anActor->SetPreHighlightColor(aPreColor.red()/255.,
899                                           aPreColor.green()/255.,
900                                           aPreColor.blue()/255.);
901           }
902         }
903       }
904     }
905   }
906
907
908   //----------------------------------------------------------------------------
909   SVTK_Selector*
910   GetSelector(SUIT_ViewWindow *theWindow)
911   {
912     if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
913       return aWnd->GetSelector();
914
915     return NULL;
916   }
917
918   void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
919                  SVTK_Selector* theSelector)
920   {
921     if (theSelector)
922       theSelector->SetFilter(theFilter);
923   }
924
925   Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
926   {
927     return theSelector->GetFilter(theId);
928   }
929
930   bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
931   {
932     return theSelector->IsFilterPresent(theId);
933   }
934
935   void RemoveFilter(int theId, SVTK_Selector* theSelector)
936   {
937     theSelector->RemoveFilter(theId);
938   }
939
940   void RemoveFilters(SVTK_Selector* theSelector)
941   {
942     for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
943       theSelector->RemoveFilter( id );
944   }
945
946   bool IsValid(SALOME_Actor* theActor, int theCellId,
947                SVTK_Selector* theSelector)
948   {
949     return theSelector->IsValid(theActor,theCellId);
950   }
951
952
953   //----------------------------------------------------------------------------
954   void SetPointRepresentation(bool theIsVisible){
955     if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
956       vtkRenderer *aRenderer = aViewWindow->getRenderer();
957       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
958       vtkActorCollection *aCollection = aCopy.GetActors();
959       aCollection->InitTraversal();
960       while(vtkActor *anAct = aCollection->GetNextActor()){
961         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
962           if(anActor->GetVisibility()){
963             anActor->SetPointRepresentation(theIsVisible);
964           }
965         }
966       }
967       RepaintCurrentView();
968     }
969   }
970
971
972   void SetPickable(SMESH_Actor* theActor){
973     if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
974       int anIsAllPickable = (theActor == NULL);
975       vtkRenderer *aRenderer = aWnd->getRenderer();
976       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
977       vtkActorCollection *aCollection = aCopy.GetActors();
978       aCollection->InitTraversal();
979       while(vtkActor *anAct = aCollection->GetNextActor()){
980         if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
981           if(anActor->GetVisibility()){
982             anActor->SetPickable(anIsAllPickable);
983           }
984         }
985       }
986       if(theActor)
987         theActor->SetPickable(!anIsAllPickable);
988       RepaintCurrentView();
989     }
990   }
991
992
993   //----------------------------------------------------------------------------
994   int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
995                              const Handle(SALOME_InteractiveObject)& theIO,
996                              QString& theName)
997   {
998     theName = "";
999     TColStd_IndexedMapOfInteger aMapIndex;
1000     theSelector->GetIndex(theIO,aMapIndex);
1001
1002     for(int i = 1; i <= aMapIndex.Extent(); i++)
1003       theName += QString(" %1").arg(aMapIndex(i));
1004
1005     return aMapIndex.Extent();
1006   }
1007
1008   int GetNameOfSelectedElements(SVTK_Selector* theSelector,
1009                                 const Handle(SALOME_InteractiveObject)& theIO,
1010                                 QString& theName)
1011   {
1012     theName = "";
1013     TColStd_IndexedMapOfInteger aMapIndex;
1014     theSelector->GetIndex(theIO,aMapIndex);
1015
1016     typedef std::set<int> TIdContainer;
1017     TIdContainer anIdContainer;
1018     for( int i = 1; i <= aMapIndex.Extent(); i++)
1019       anIdContainer.insert(aMapIndex(i));
1020
1021     TIdContainer::const_iterator anIter = anIdContainer.begin();
1022     for( ; anIter != anIdContainer.end(); anIter++)
1023       theName += QString(" %1").arg(*anIter);
1024
1025     return aMapIndex.Extent();
1026   }
1027
1028
1029   int GetEdgeNodes(SVTK_Selector* theSelector,
1030                    const TVisualObjPtr& theVisualObject,
1031                    int& theId1,
1032                    int& theId2)
1033   {
1034     const SALOME_ListIO& selected = theSelector->StoredIObjects();
1035
1036     if ( selected.Extent() != 1 )
1037       return -1;
1038
1039     Handle(SALOME_InteractiveObject) anIO = selected.First();
1040     if ( anIO.IsNull() || !anIO->hasEntry() )
1041       return -1;
1042
1043     TColStd_IndexedMapOfInteger aMapIndex;
1044     theSelector->GetIndex( anIO, aMapIndex );
1045     if ( aMapIndex.Extent() != 2 )
1046       return -1;
1047
1048     int anObjId = -1, anEdgeNum = -1;
1049     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1050       int aVal = aMapIndex( i );
1051       if ( aVal > 0 )
1052         anObjId = aVal;
1053       else
1054         anEdgeNum = abs( aVal ) - 1;
1055     }
1056
1057     if ( anObjId == -1 || anEdgeNum == -1 )
1058       return -1;
1059
1060     return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1061   }
1062
1063   //----------------------------------------------------------------------------
1064   int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1065                              const Handle(SALOME_InteractiveObject)& theIO,
1066                              QString& theName)
1067   {
1068     theName = "";
1069     if(theIO->hasEntry()){
1070       if(FindActorByEntry(theIO->getEntry())){
1071         TColStd_IndexedMapOfInteger aMapIndex;
1072         theMgr->GetIndexes(theIO,aMapIndex);
1073         for(int i = 1; i <= aMapIndex.Extent(); i++){
1074           theName += QString(" %1").arg(aMapIndex(i));
1075         }
1076         return aMapIndex.Extent();
1077       }
1078     }
1079     return -1;
1080   }
1081
1082   int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1083     theName = "";
1084     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1085     if(selected.Extent() == 1){
1086       Handle(SALOME_InteractiveObject) anIO = selected.First();
1087       return GetNameOfSelectedNodes(theMgr,anIO,theName);
1088     }
1089     return -1;
1090   }
1091
1092
1093   int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr,
1094                                 const Handle(SALOME_InteractiveObject)& theIO,
1095                                 QString& theName)
1096   {
1097     theName = "";
1098     if(theIO->hasEntry()){
1099       if(FindActorByEntry(theIO->getEntry())){
1100         TColStd_IndexedMapOfInteger aMapIndex;
1101         theMgr->GetIndexes(theIO,aMapIndex);
1102         typedef std::set<int> TIdContainer;
1103         TIdContainer anIdContainer;
1104         for( int i = 1; i <= aMapIndex.Extent(); i++)
1105           anIdContainer.insert(aMapIndex(i));
1106         TIdContainer::const_iterator anIter = anIdContainer.begin();
1107         for( ; anIter != anIdContainer.end(); anIter++){
1108           theName += QString(" %1").arg(*anIter);
1109         }
1110         return aMapIndex.Extent();
1111       }
1112     }
1113     return -1;
1114   }
1115
1116
1117   int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1118   {
1119     theName = "";
1120     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1121
1122     if( selected.Extent() == 1){
1123       Handle(SALOME_InteractiveObject) anIO = selected.First();
1124       return GetNameOfSelectedElements(theMgr,anIO,theName);
1125     }
1126     return -1;
1127   }
1128
1129   int GetSelected(LightApp_SelectionMgr*       theMgr,
1130                   TColStd_IndexedMapOfInteger& theMap,
1131                   const bool                   theIsElement)
1132   {
1133     theMap.Clear();
1134     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1135
1136     if ( selected.Extent() == 1 )
1137     {
1138       Handle(SALOME_InteractiveObject) anIO = selected.First();
1139       if ( anIO->hasEntry() ) {
1140         theMgr->GetIndexes( anIO, theMap );
1141       }
1142     }
1143     return theMap.Extent();
1144   }
1145
1146
1147   int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1148   {
1149     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1150
1151     if ( selected.Extent() != 1 )
1152       return -1;
1153
1154     Handle(SALOME_InteractiveObject) anIO = selected.First();
1155     if ( anIO.IsNull() || !anIO->hasEntry() )
1156       return -1;
1157
1158     SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1159     if ( anActor == 0 )
1160       return -1;
1161
1162     TColStd_IndexedMapOfInteger aMapIndex;
1163     theMgr->GetIndexes( anIO, aMapIndex );
1164     if ( aMapIndex.Extent() != 2 )
1165       return -1;
1166
1167     int anObjId = -1, anEdgeNum = -1;
1168     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1169       int aVal = aMapIndex( i );
1170       if ( aVal > 0 )
1171         anObjId = aVal;
1172       else
1173         anEdgeNum = abs( aVal );
1174     }
1175
1176     if ( anObjId == -1 || anEdgeNum == -1 )
1177       return -1;
1178
1179     return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1180   }
1181
1182   void SetControlsPrecision( const long theVal )
1183   {
1184     if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1185     {
1186       vtkRenderer *aRenderer = aWnd->getRenderer();
1187       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1188       vtkActorCollection *aCollection = aCopy.GetActors();
1189       aCollection->InitTraversal();
1190
1191       while ( vtkActor *anAct = aCollection->GetNextActor())
1192       {
1193         if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1194         {
1195           anActor->SetControlsPrecision( theVal );
1196           anActor->SetControlMode( anActor->GetControlMode() );
1197         }
1198       }
1199
1200     }
1201   }
1202
1203   //----------------------------------------------------------------------------
1204   // internal function
1205   void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
1206                            vtkFloatingPointType theDirection[3],
1207                            vtkFloatingPointType theMinPnt[3],
1208                            vtkFloatingPointType& theMaxBoundPrj,
1209                            vtkFloatingPointType& theMinBoundPrj )
1210   {
1211     //Enlarge bounds in order to avoid conflicts of precision
1212     for(int i = 0; i < 6; i += 2){
1213       static double EPS = 1.0E-3;
1214       vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1215       theBounds[i] -= aDelta;
1216       theBounds[i+1] += aDelta;
1217     }
1218
1219     vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1220                                                 {theBounds[1],theBounds[2],theBounds[4]},
1221                                                 {theBounds[0],theBounds[3],theBounds[4]},
1222                                                 {theBounds[1],theBounds[3],theBounds[4]},
1223                                                 {theBounds[0],theBounds[2],theBounds[5]},
1224                                                 {theBounds[1],theBounds[2],theBounds[5]}, 
1225                                                 {theBounds[0],theBounds[3],theBounds[5]}, 
1226                                                 {theBounds[1],theBounds[3],theBounds[5]}};
1227
1228     int aMaxId = 0, aMinId = aMaxId;
1229     theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1230     theMinBoundPrj = theMaxBoundPrj;
1231     for(int i = 1; i < 8; i++){
1232       vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1233       if(theMaxBoundPrj < aTmp){
1234         theMaxBoundPrj = aTmp;
1235         aMaxId = i;
1236       }
1237       if(theMinBoundPrj > aTmp){
1238         theMinBoundPrj = aTmp;
1239         aMinId = i;
1240       }
1241     }
1242     vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
1243     theMinPnt[0] = aMinPnt[0];
1244     theMinPnt[1] = aMinPnt[1];
1245     theMinPnt[2] = aMinPnt[2];
1246   }
1247
1248   // internal function
1249   void DistanceToPosition( vtkFloatingPointType theBounds[6],
1250                            vtkFloatingPointType theDirection[3],
1251                            vtkFloatingPointType theDist,
1252                            vtkFloatingPointType thePos[3] )
1253   {
1254     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1255     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1256     vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1257     thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1258     thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1259     thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1260   }
1261
1262   // internal function (currently unused, left just in case)
1263   void PositionToDistance( vtkFloatingPointType theBounds[6],
1264                            vtkFloatingPointType theDirection[3],
1265                            vtkFloatingPointType thePos[3],
1266                            vtkFloatingPointType& theDist )
1267   {
1268     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1269     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1270     vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
1271     theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1272   }
1273
1274   bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1275                                        vtkFloatingPointType theNormal[3],
1276                                        vtkFloatingPointType theDist,
1277                                        vtkFloatingPointType theBounds[6],
1278                                        vtkFloatingPointType theOrigin[3] )
1279   {
1280     bool anIsOk = false;
1281     theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1282     theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1283     std::list<vtkActor*>::iterator anIter = theActorList.begin();
1284     for( ; anIter != theActorList.end(); anIter++ ) {
1285       if( vtkActor* aVTKActor = *anIter ) {
1286         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1287           vtkFloatingPointType aBounds[6];
1288           anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1289           theBounds[0] = std::min( theBounds[0], aBounds[0] );
1290           theBounds[1] = std::max( theBounds[1], aBounds[1] );
1291           theBounds[2] = std::min( theBounds[2], aBounds[2] );
1292           theBounds[3] = std::max( theBounds[3], aBounds[3] );
1293           theBounds[4] = std::min( theBounds[4], aBounds[4] );
1294           theBounds[5] = std::max( theBounds[5], aBounds[5] );
1295           anIsOk = true;
1296         }
1297       }
1298     }
1299
1300     if( !anIsOk )
1301       return false;
1302
1303     DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1304     return true;
1305   }
1306 } // end of namespace SMESH