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