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