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