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