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