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