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