Salome HOME
untabify
[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->SetVisibility(true);
723               if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
724               break;
725             case eErase:
726                 //MESSAGE("--- erase " << anActor);
727               anActor->SetVisibility(false);
728               break;
729           }
730         } else {
731           switch (theAction) {
732           case eDisplay:
733           case eDisplayOnly:
734             {
735                 //MESSAGE("---");
736               SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
737               _PTR(Study) aDocument = aStudy->studyDS();
738               // Pass non-visual objects (hypotheses, etc.), return true in this case
739               CORBA::Long anId = aDocument->StudyId();
740               TVisualObjPtr aVisualObj;
741               if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
742               {
743                 if ((anActor = CreateActor(aDocument,theEntry,true))) {
744                   bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
745                   DisplayActor(theWnd,anActor);
746                   // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
747                   if (needFitAll) FitAll();
748                 } else {
749                   OK = false;
750                 }
751               }
752               break;
753             }
754           }
755         }
756       }
757       }
758     }
759     return OK;
760   }
761
762
763   bool UpdateView(EDisplaing theAction, const char* theEntry){
764         //MESSAGE("UpdateView");
765     SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
766     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
767     SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
768     return UpdateView(aWnd,theAction,theEntry);
769   }
770
771   void UpdateView(){
772     if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
773       LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
774       SALOME_ListIO selected; mgr->selectedObjects( selected );
775
776       if( selected.Extent() == 0){
777         vtkRenderer* aRenderer = aWnd->getRenderer();
778         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
779         vtkActorCollection *aCollection = aCopy.GetActors();
780         aCollection->InitTraversal();
781         while(vtkActor *anAct = aCollection->GetNextActor()){
782           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
783             if(anActor->hasIO())
784               if (!Update(anActor->getIO(),anActor->GetVisibility()))
785                 break; // avoid multiple warinings if visu failed
786           }
787         }
788       }else{
789         SALOME_ListIteratorOfListIO anIter( selected );
790         for( ; anIter.More(); anIter.Next()){
791           Handle(SALOME_InteractiveObject) anIO = anIter.Value();
792           if ( !Update(anIO,true) )
793             break; // avoid multiple warinings if visu failed
794         }
795       }
796       RepaintCurrentView();
797     }
798   }
799
800
801   bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
802   {
803         MESSAGE("Update");
804     _PTR(Study) aStudy = GetActiveStudyDocument();
805     CORBA::Long anId = aStudy->StudyId();
806     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
807       if ( theDisplay )
808         UpdateView(SMESH::eDisplay,theIO->getEntry());
809       return true;
810     }
811     return false;
812   }
813
814   bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
815   {
816         MESSAGE("UpdateNulData");
817     _PTR(Study) aStudy = GetActiveStudyDocument();
818     CORBA::Long anId = aStudy->StudyId();
819     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry(), true)) {
820       if ( theDisplay )
821         UpdateView(SMESH::eDisplay,theIO->getEntry());
822       return true;
823     }
824     return false;
825   }
826
827   void UpdateSelectionProp( SMESHGUI* theModule ) {
828     if( !theModule )
829       return;
830
831     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
832     if( !app )
833     {
834       MESSAGE( "UpdateSelectionProp: Application is null" );
835       return;
836     }
837
838     SUIT_ViewManager* vm = app->activeViewManager();
839     if( !vm )
840     {
841       MESSAGE( "UpdateSelectionProp: View manager is null" );
842       return;
843     }
844
845     QVector<SUIT_ViewWindow*> views = vm->getViews();
846
847     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
848     if( !mgr )
849     {
850       MESSAGE( "UpdateSelectionProp: Resource manager is null" );
851       return;
852     }
853
854     QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
855            aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
856            aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
857
858     int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
859         PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
860
861     // adjust highlight_width to the width of mesh entities
862     int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
863     int aLineWidth  = mgr->integerValue("SMESH", "element_width", 1);
864     int maxSize = aElem0DSize;
865     if (aElem0DSize > maxSize) maxSize = aElem0DSize;
866     if (aLineWidth > maxSize) maxSize = aLineWidth;
867     if (PW < maxSize + 2) PW = maxSize + 2;
868
869     double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
870            SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
871            SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
872
873     for ( int i=0, n=views.count(); i<n; i++ ){
874       // update VTK viewer properties
875       if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
876         // mesh element selection
877         aVtkView->SetSelectionProp(aSelColor.red()/255.,
878                                    aSelColor.green()/255.,
879                                    aSelColor.blue()/255.,
880                                    SW );
881         // tolerances
882         aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
883
884         // pre-selection
885         aVtkView->SetPreselectionProp(aPreColor.red()/255.,
886                                       aPreColor.green()/255.,
887                                       aPreColor.blue()/255.,
888                                       PW);
889         // update actors
890         vtkRenderer* aRenderer = aVtkView->getRenderer();
891         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
892         vtkActorCollection *aCollection = aCopy.GetActors();
893         aCollection->InitTraversal();
894         while(vtkActor *anAct = aCollection->GetNextActor()){
895           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
896             anActor->SetHighlightColor(aHiColor.red()/255.,
897                                        aHiColor.green()/255.,
898                                        aHiColor.blue()/255.);
899             anActor->SetPreHighlightColor(aPreColor.red()/255.,
900                                           aPreColor.green()/255.,
901                                           aPreColor.blue()/255.);
902           }
903         }
904       }
905     }
906   }
907
908
909   //----------------------------------------------------------------------------
910   SVTK_Selector*
911   GetSelector(SUIT_ViewWindow *theWindow)
912   {
913     if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
914       return aWnd->GetSelector();
915
916     return NULL;
917   }
918
919   void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
920                  SVTK_Selector* theSelector)
921   {
922     if (theSelector)
923       theSelector->SetFilter(theFilter);
924   }
925
926   Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
927   {
928     return theSelector->GetFilter(theId);
929   }
930
931   bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
932   {
933     return theSelector->IsFilterPresent(theId);
934   }
935
936   void RemoveFilter(int theId, SVTK_Selector* theSelector)
937   {
938     theSelector->RemoveFilter(theId);
939   }
940
941   void RemoveFilters(SVTK_Selector* theSelector)
942   {
943     for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
944       theSelector->RemoveFilter( id );
945   }
946
947   bool IsValid(SALOME_Actor* theActor, int theCellId,
948                SVTK_Selector* theSelector)
949   {
950     return theSelector->IsValid(theActor,theCellId);
951   }
952
953
954   //----------------------------------------------------------------------------
955   void SetPointRepresentation(bool theIsVisible){
956     if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
957       vtkRenderer *aRenderer = aViewWindow->getRenderer();
958       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
959       vtkActorCollection *aCollection = aCopy.GetActors();
960       aCollection->InitTraversal();
961       while(vtkActor *anAct = aCollection->GetNextActor()){
962         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
963           if(anActor->GetVisibility()){
964             anActor->SetPointRepresentation(theIsVisible);
965           }
966         }
967       }
968       RepaintCurrentView();
969     }
970   }
971
972
973   void SetPickable(SMESH_Actor* theActor){
974     if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
975       int anIsAllPickable = (theActor == NULL);
976       vtkRenderer *aRenderer = aWnd->getRenderer();
977       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
978       vtkActorCollection *aCollection = aCopy.GetActors();
979       aCollection->InitTraversal();
980       while(vtkActor *anAct = aCollection->GetNextActor()){
981         if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
982           if(anActor->GetVisibility()){
983             anActor->SetPickable(anIsAllPickable);
984           }
985         }
986       }
987       if(theActor)
988         theActor->SetPickable(!anIsAllPickable);
989       RepaintCurrentView();
990     }
991   }
992
993
994   //----------------------------------------------------------------------------
995   int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
996                              const Handle(SALOME_InteractiveObject)& theIO,
997                              QString& theName)
998   {
999     theName = "";
1000     TColStd_IndexedMapOfInteger aMapIndex;
1001     theSelector->GetIndex(theIO,aMapIndex);
1002
1003     for(int i = 1; i <= aMapIndex.Extent(); i++)
1004       theName += QString(" %1").arg(aMapIndex(i));
1005
1006     return aMapIndex.Extent();
1007   }
1008
1009   int GetNameOfSelectedElements(SVTK_Selector* theSelector,
1010                                 const Handle(SALOME_InteractiveObject)& theIO,
1011                                 QString& theName)
1012   {
1013     theName = "";
1014     TColStd_IndexedMapOfInteger aMapIndex;
1015     theSelector->GetIndex(theIO,aMapIndex);
1016
1017     typedef std::set<int> TIdContainer;
1018     TIdContainer anIdContainer;
1019     for( int i = 1; i <= aMapIndex.Extent(); i++)
1020       anIdContainer.insert(aMapIndex(i));
1021
1022     TIdContainer::const_iterator anIter = anIdContainer.begin();
1023     for( ; anIter != anIdContainer.end(); anIter++)
1024       theName += QString(" %1").arg(*anIter);
1025
1026     return aMapIndex.Extent();
1027   }
1028
1029
1030   int GetEdgeNodes(SVTK_Selector* theSelector,
1031                    const TVisualObjPtr& theVisualObject,
1032                    int& theId1,
1033                    int& theId2)
1034   {
1035     const SALOME_ListIO& selected = theSelector->StoredIObjects();
1036
1037     if ( selected.Extent() != 1 )
1038       return -1;
1039
1040     Handle(SALOME_InteractiveObject) anIO = selected.First();
1041     if ( anIO.IsNull() || !anIO->hasEntry() )
1042       return -1;
1043
1044     TColStd_IndexedMapOfInteger aMapIndex;
1045     theSelector->GetIndex( anIO, aMapIndex );
1046     if ( aMapIndex.Extent() != 2 )
1047       return -1;
1048
1049     int anObjId = -1, anEdgeNum = -1;
1050     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1051       int aVal = aMapIndex( i );
1052       if ( aVal > 0 )
1053         anObjId = aVal;
1054       else
1055         anEdgeNum = abs( aVal ) - 1;
1056     }
1057
1058     if ( anObjId == -1 || anEdgeNum == -1 )
1059       return -1;
1060
1061     return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1062   }
1063
1064   //----------------------------------------------------------------------------
1065   int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1066                              const Handle(SALOME_InteractiveObject)& theIO,
1067                              QString& theName)
1068   {
1069     theName = "";
1070     if(theIO->hasEntry()){
1071       if(FindActorByEntry(theIO->getEntry())){
1072         TColStd_IndexedMapOfInteger aMapIndex;
1073         theMgr->GetIndexes(theIO,aMapIndex);
1074         for(int i = 1; i <= aMapIndex.Extent(); i++){
1075           theName += QString(" %1").arg(aMapIndex(i));
1076         }
1077         return aMapIndex.Extent();
1078       }
1079     }
1080     return -1;
1081   }
1082
1083   int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1084     theName = "";
1085     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1086     if(selected.Extent() == 1){
1087       Handle(SALOME_InteractiveObject) anIO = selected.First();
1088       return GetNameOfSelectedNodes(theMgr,anIO,theName);
1089     }
1090     return -1;
1091   }
1092
1093
1094   int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr,
1095                                 const Handle(SALOME_InteractiveObject)& theIO,
1096                                 QString& theName)
1097   {
1098     theName = "";
1099     if(theIO->hasEntry()){
1100       if(FindActorByEntry(theIO->getEntry())){
1101         TColStd_IndexedMapOfInteger aMapIndex;
1102         theMgr->GetIndexes(theIO,aMapIndex);
1103         typedef std::set<int> TIdContainer;
1104         TIdContainer anIdContainer;
1105         for( int i = 1; i <= aMapIndex.Extent(); i++)
1106           anIdContainer.insert(aMapIndex(i));
1107         TIdContainer::const_iterator anIter = anIdContainer.begin();
1108         for( ; anIter != anIdContainer.end(); anIter++){
1109           theName += QString(" %1").arg(*anIter);
1110         }
1111         return aMapIndex.Extent();
1112       }
1113     }
1114     return -1;
1115   }
1116
1117
1118   int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1119   {
1120     theName = "";
1121     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1122
1123     if( selected.Extent() == 1){
1124       Handle(SALOME_InteractiveObject) anIO = selected.First();
1125       return GetNameOfSelectedElements(theMgr,anIO,theName);
1126     }
1127     return -1;
1128   }
1129
1130   int GetSelected(LightApp_SelectionMgr*       theMgr,
1131                   TColStd_IndexedMapOfInteger& theMap,
1132                   const bool                   theIsElement)
1133   {
1134     theMap.Clear();
1135     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1136
1137     if ( selected.Extent() == 1 )
1138     {
1139       Handle(SALOME_InteractiveObject) anIO = selected.First();
1140       if ( anIO->hasEntry() ) {
1141         theMgr->GetIndexes( anIO, theMap );
1142       }
1143     }
1144     return theMap.Extent();
1145   }
1146
1147
1148   int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1149   {
1150     SALOME_ListIO selected; theMgr->selectedObjects( selected );
1151
1152     if ( selected.Extent() != 1 )
1153       return -1;
1154
1155     Handle(SALOME_InteractiveObject) anIO = selected.First();
1156     if ( anIO.IsNull() || !anIO->hasEntry() )
1157       return -1;
1158
1159     SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1160     if ( anActor == 0 )
1161       return -1;
1162
1163     TColStd_IndexedMapOfInteger aMapIndex;
1164     theMgr->GetIndexes( anIO, aMapIndex );
1165     if ( aMapIndex.Extent() != 2 )
1166       return -1;
1167
1168     int anObjId = -1, anEdgeNum = -1;
1169     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1170       int aVal = aMapIndex( i );
1171       if ( aVal > 0 )
1172         anObjId = aVal;
1173       else
1174         anEdgeNum = abs( aVal );
1175     }
1176
1177     if ( anObjId == -1 || anEdgeNum == -1 )
1178       return -1;
1179
1180     return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1181   }
1182
1183   void SetControlsPrecision( const long theVal )
1184   {
1185     if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1186     {
1187       vtkRenderer *aRenderer = aWnd->getRenderer();
1188       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1189       vtkActorCollection *aCollection = aCopy.GetActors();
1190       aCollection->InitTraversal();
1191
1192       while ( vtkActor *anAct = aCollection->GetNextActor())
1193       {
1194         if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1195         {
1196           anActor->SetControlsPrecision( theVal );
1197           anActor->SetControlMode( anActor->GetControlMode() );
1198         }
1199       }
1200
1201     }
1202   }
1203
1204   //----------------------------------------------------------------------------
1205   // internal function
1206   void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
1207                            vtkFloatingPointType theDirection[3],
1208                            vtkFloatingPointType theMinPnt[3],
1209                            vtkFloatingPointType& theMaxBoundPrj,
1210                            vtkFloatingPointType& theMinBoundPrj )
1211   {
1212     //Enlarge bounds in order to avoid conflicts of precision
1213     for(int i = 0; i < 6; i += 2){
1214       static double EPS = 1.0E-3;
1215       vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1216       theBounds[i] -= aDelta;
1217       theBounds[i+1] += aDelta;
1218     }
1219
1220     vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1221                                                 {theBounds[1],theBounds[2],theBounds[4]},
1222                                                 {theBounds[0],theBounds[3],theBounds[4]},
1223                                                 {theBounds[1],theBounds[3],theBounds[4]},
1224                                                 {theBounds[0],theBounds[2],theBounds[5]},
1225                                                 {theBounds[1],theBounds[2],theBounds[5]}, 
1226                                                 {theBounds[0],theBounds[3],theBounds[5]}, 
1227                                                 {theBounds[1],theBounds[3],theBounds[5]}};
1228
1229     int aMaxId = 0, aMinId = aMaxId;
1230     theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1231     theMinBoundPrj = theMaxBoundPrj;
1232     for(int i = 1; i < 8; i++){
1233       vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1234       if(theMaxBoundPrj < aTmp){
1235         theMaxBoundPrj = aTmp;
1236         aMaxId = i;
1237       }
1238       if(theMinBoundPrj > aTmp){
1239         theMinBoundPrj = aTmp;
1240         aMinId = i;
1241       }
1242     }
1243     vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
1244     theMinPnt[0] = aMinPnt[0];
1245     theMinPnt[1] = aMinPnt[1];
1246     theMinPnt[2] = aMinPnt[2];
1247   }
1248
1249   // internal function
1250   void DistanceToPosition( vtkFloatingPointType theBounds[6],
1251                            vtkFloatingPointType theDirection[3],
1252                            vtkFloatingPointType theDist,
1253                            vtkFloatingPointType thePos[3] )
1254   {
1255     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1256     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1257     vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1258     thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1259     thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1260     thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1261   }
1262
1263   // internal function (currently unused, left just in case)
1264   void PositionToDistance( vtkFloatingPointType theBounds[6],
1265                            vtkFloatingPointType theDirection[3],
1266                            vtkFloatingPointType thePos[3],
1267                            vtkFloatingPointType& theDist )
1268   {
1269     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1270     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1271     vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
1272     theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1273   }
1274
1275   bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1276                                        vtkFloatingPointType theNormal[3],
1277                                        vtkFloatingPointType theDist,
1278                                        vtkFloatingPointType theBounds[6],
1279                                        vtkFloatingPointType theOrigin[3] )
1280   {
1281     bool anIsOk = false;
1282     theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1283     theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1284     std::list<vtkActor*>::iterator anIter = theActorList.begin();
1285     for( ; anIter != theActorList.end(); anIter++ ) {
1286       if( vtkActor* aVTKActor = *anIter ) {
1287         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1288           vtkFloatingPointType aBounds[6];
1289           anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1290           theBounds[0] = std::min( theBounds[0], aBounds[0] );
1291           theBounds[1] = std::max( theBounds[1], aBounds[1] );
1292           theBounds[2] = std::min( theBounds[2], aBounds[2] );
1293           theBounds[3] = std::max( theBounds[3], aBounds[3] );
1294           theBounds[4] = std::min( theBounds[4], aBounds[4] );
1295           theBounds[5] = std::max( theBounds[5], aBounds[5] );
1296           anIsOk = true;
1297         }
1298       }
1299     }
1300
1301     if( !anIsOk )
1302       return false;
1303
1304     DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1305     return true;
1306   }
1307
1308 #ifndef DISABLE_PLOT2DVIEWER
1309   //================================================================================
1310   /*!
1311    * \brief Find all SMESH_Actor's in the View Window.
1312    * If actor constains Plot2d_Histogram object remove it from each Plot2d Viewer.
1313    */
1314   //================================================================================
1315
1316   void ClearPlot2Viewers( SUIT_ViewWindow* theWindow ) {
1317     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
1318       vtkRenderer *aRenderer = aViewWindow->getRenderer();
1319       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1320       vtkActorCollection *aCollection = aCopy.GetActors();
1321       aCollection->InitTraversal();
1322       while(vtkActor *anAct = aCollection->GetNextActor()){
1323         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
1324           if(anActor->hasIO() && anActor->GetPlot2Histogram() ){
1325             ProcessIn2DViewers(anActor,RemoveFrom2dViewer);
1326           }
1327         }
1328       }
1329     }
1330   }
1331   
1332 #endif
1333
1334 } // end of namespace SMESH