Salome HOME
ac064ec25a41bd753af8242a86d2464219513e69
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_VTKUtils.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 //  Lesser General Public License for more details.
13 //
14 //  You should have received a copy of the GNU Lesser General Public
15 //  License along with this library; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19
20
21 #include "SMESHGUI_VTKUtils.h"
22 #include "SMESHGUI_Utils.h"
23 #include "SMESHGUI_Filter.h"
24
25 #include <vtkRenderer.h>
26 #include <vtkActorCollection.h>
27
28 #include <TColStd_IndexedMapOfInteger.hxx>
29
30 #include <SUIT_Desktop.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_Study.h>
33
34 #include "SalomeApp_SelectionMgr.h"
35
36 #include "SVTK_Selector.h"
37 #include "SVTK_ViewModel.h"
38 #include "SVTK_ViewWindow.h"
39 #include "SVTK_RenderWindow.h"
40 #include "SVTK_InteractorStyle.h"
41 #include "SVTK_RenderWindowInteractor.h"
42
43 #include "utilities.h"
44
45 #include "SALOMEconfig.h"
46 #include CORBA_CLIENT_HEADER(SMESH_Gen)
47 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
48 #include CORBA_CLIENT_HEADER(SMESH_Group)
49 #include CORBA_CLIENT_HEADER(SMESH_Hypothesis)
50
51 #include "SMESHGUI.h"
52 #include "SMESH_Actor.h"
53 #include "SMESH_ObjectDef.h"
54
55 #include <SalomeApp_Application.h>
56 #include <SalomeApp_SelectionMgr.h>
57 #include <SalomeApp_Study.h>
58
59 #include <SALOMEDSClient_Study.hxx>
60 #include <SALOMEDSClient_SObject.hxx>
61
62 #include <SALOME_ListIO.hxx>
63 #include <SALOME_ListIteratorOfListIO.hxx>
64
65 #include <set>
66 using namespace std;
67
68 namespace SMESH{
69
70   typedef map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
71   static TVisualObjCont VISUAL_OBJ_CONT;
72
73   TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry){
74     TVisualObjPtr aVisualObj;
75     try{
76       TVisualObjCont::key_type aKey(theStudyId,theEntry);
77       TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
78       if(anIter != VISUAL_OBJ_CONT.end()){
79         aVisualObj = anIter->second;
80       }else{
81         SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SMESHGUI::activeStudy()->application() );
82         _PTR(Study) aStudy = SMESHGUI::activeStudy()->studyDS();
83         _PTR(SObject) aSObj = aStudy->FindObjectID(theEntry);
84         if(aSObj){
85           _PTR(GenericAttribute) anAttr;
86           if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
87             _PTR(AttributeIOR) anIOR = anAttr;
88             CORBA::String_var aVal = anIOR->Value().c_str();
89             CORBA::Object_var anObj = app->orb()->string_to_object( aVal.in() );
90             if(!CORBA::is_nil(anObj)){
91               //Try narrow to SMESH_Mesh interafce
92               SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
93               if(!aMesh->_is_nil()){
94                 aVisualObj.reset(new SMESH_MeshObj(aMesh));
95                 aVisualObj->Update();
96                 TVisualObjCont::value_type aValue(aKey,aVisualObj); 
97                 VISUAL_OBJ_CONT.insert(aValue);
98                 return aVisualObj;
99               }
100               //Try narrow to SMESH_Group interafce
101               SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
102               if(!aGroup->_is_nil()){
103                 _PTR(SObject) aFatherSObj = aSObj->GetFather();
104                 if(!aFatherSObj) return aVisualObj;
105                 aFatherSObj = aFatherSObj->GetFather();
106                 if(!aFatherSObj) return aVisualObj;
107                 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
108                 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
109                 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
110                   aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
111                   aVisualObj->Update();
112                   TVisualObjCont::value_type aValue(aKey,aVisualObj);
113                   VISUAL_OBJ_CONT.insert(aValue);
114                   return aVisualObj;
115                 }
116               }
117               //Try narrow to SMESH_subMesh interafce
118               SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
119               if(!aSubMesh->_is_nil()){
120                 _PTR(SObject) aFatherSObj = aSObj->GetFather();
121                 if(!aFatherSObj) return aVisualObj;
122                 aFatherSObj = aFatherSObj->GetFather();
123                 if(!aFatherSObj) return aVisualObj;
124                 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
125                 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
126                 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
127                   aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
128                   aVisualObj->Update();
129                   TVisualObjCont::value_type aValue(aKey,aVisualObj);
130                   VISUAL_OBJ_CONT.insert(aValue);
131                   return aVisualObj;
132                 }
133               }
134             }
135           }
136         }
137       }
138     }catch(...){
139       INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
140     }
141     return aVisualObj;
142   }
143
144
145   SVTK_ViewWindow*
146   GetViewWindow(const SalomeApp_Module* theModule)
147   {
148     if(SalomeApp_Application* anApp = theModule->getApp())
149       return dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow());
150   }
151
152   SVTK_ViewWindow* FindVtkViewWindow( SUIT_ViewManager* theMgr,
153                                            SUIT_ViewWindow* theWindow )
154   {
155     if( !theMgr )
156       return NULL;
157
158     QPtrVector<SUIT_ViewWindow> views = theMgr->getViews();
159     if( views.containsRef( theWindow ) )
160       return GetVtkViewWindow( theWindow );
161     else
162       return NULL;
163   }
164
165
166   SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
167     return dynamic_cast<SVTK_ViewWindow*>(theWindow);
168   }
169
170
171 /*  SUIT_ViewWindow* GetActiveWindow()
172   {
173     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
174     if( !app )
175       return NULL;
176     SUIT_ViewManager* mgr = app->activeViewManager();
177     if( mgr )
178       return mgr->getActiveView();
179     else
180       return NULL;
181   }*/
182
183   SVTK_ViewWindow* GetCurrentVtkView(){
184     return GetVtkViewWindow( GetActiveWindow() );
185   }
186
187   void RepaintViewWindow(SVTK_ViewWindow* theWindow)
188   {
189     theWindow->Repaint();
190   }
191
192   void RenderViewWindow(SVTK_ViewWindow* theWindow)
193   {
194     theWindow->getRenderer()->Render();
195     theWindow->Repaint();
196   }
197
198   SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
199                                 const char* theEntry)
200   {
201     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
202       vtkRenderer *aRenderer = aViewWindow->getRenderer();
203       vtkActorCollection *aCollection = aRenderer->GetActors();
204       aCollection->InitTraversal();
205       while(vtkActor *anAct = aCollection->GetNextActor()){
206         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
207           if(anActor->hasIO()){
208             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
209             if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
210               return anActor;
211             }
212           }
213         }
214       }
215     }
216     return NULL;
217   }
218
219
220   SMESH_Actor* FindActorByEntry(const char* theEntry){
221     return FindActorByEntry(GetActiveWindow(),theEntry);
222   }
223
224
225   SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
226     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
227     if( !app )
228       return NULL;
229
230     if(!CORBA::is_nil(theObject)){
231       _PTR(Study) aStudy = GetActiveStudyDocument();
232       CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
233       _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
234       if(aSObject){
235         CORBA::String_var anEntry = aSObject->GetID().c_str();
236         return FindActorByEntry(anEntry.in());
237       }
238     }
239     return NULL;
240   }
241
242
243   SMESH_Actor* CreateActor(_PTR(Study) theStudy,
244                            const char* theEntry,
245                            int theIsClear)
246   {
247     SMESH_Actor *anActor = NULL;
248     CORBA::Long anId = theStudy->StudyId();
249     if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
250       _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
251       if(aSObj){
252         _PTR(GenericAttribute) anAttr;
253         if(aSObj->FindAttribute(anAttr,"AttributeName")){
254           _PTR(AttributeName) aName = anAttr;
255           std::string aNameVal = aName->Value();
256           anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
257         }
258       }
259     }
260     return anActor;
261   }
262
263
264   void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
265     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
266       vtkWnd->AddActor(theActor);
267       vtkWnd->Repaint();
268     }
269   }
270
271
272   void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
273     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
274       vtkWnd->RemoveActor(theActor);
275       if(theActor->hasIO()){
276         Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
277         if(anIO->hasEntry()){
278           std::string anEntry = anIO->getEntry();
279           SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
280           int aStudyId = aStudy->id();
281           TVisualObjCont::key_type aKey(aStudyId,anEntry);
282           VISUAL_OBJ_CONT.erase(aKey);
283         }
284       }
285       theActor->Delete();
286       vtkWnd->Repaint();
287     }
288   }
289
290
291   void FitAll(){
292     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
293       wnd->onFitAll();
294       wnd->Repaint();
295     }
296   }
297
298   vtkRenderer* GetCurrentRenderer(){
299     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() )
300       return wnd->getRenderer();
301     return NULL;
302   }
303
304   void RepaintCurrentView(){
305     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() )
306       {
307         wnd->getRenderer()->Render();
308         wnd->Repaint(false);
309       }
310   }
311
312   void UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
313   {
314     if(SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd)){
315       vtkRenderer *aRenderer = aViewWnd->getRenderer();
316       vtkActorCollection *aCollection = aRenderer->GetActors();
317       aCollection->InitTraversal();
318       switch(theAction){
319       case eDisplayAll: {
320         while(vtkActor *anAct = aCollection->GetNextActor()){
321           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
322             anActor->SetVisibility(true);
323           }
324         }
325         break;
326       }
327       case eDisplayOnly:
328       case eEraseAll: {
329         while(vtkActor *anAct = aCollection->GetNextActor()){
330           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
331             anActor->SetVisibility(false);
332           }
333         }
334       }
335       default: {
336         if(SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)){
337           switch(theAction) {
338             case eDisplay:
339             case eDisplayOnly:
340               anActor->SetVisibility(true);
341               break;
342             case eErase:
343               anActor->SetVisibility(false);
344               break;
345           }
346         } else {
347           switch(theAction){
348           case eDisplay:
349           case eDisplayOnly:{
350             SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theWnd->getViewManager()->study() );
351             _PTR(Study) aDocument = aStudy->studyDS();
352             if((anActor = CreateActor(aDocument,theEntry,true))) {
353               DisplayActor(theWnd,anActor);
354               FitAll();
355             }
356             break;
357           }
358           }
359         }
360       }
361       }
362     }
363   }
364
365
366   void UpdateView(EDisplaing theAction, const char* theEntry){
367     SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
368     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
369     SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
370     UpdateView(aWnd,theAction,theEntry);
371   }
372
373   void UpdateView(){
374     if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
375       SalomeApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
376       SALOME_ListIO selected; mgr->selectedObjects( selected );
377
378       if( selected.Extent() == 0){
379         vtkRenderer* aRenderer = aWnd->getRenderer();
380         vtkActorCollection *aCollection = aRenderer->GetActors();
381         aCollection->InitTraversal();
382         while(vtkActor *anAct = aCollection->GetNextActor()){
383           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
384             if(anActor->hasIO())
385               Update(anActor->getIO(),anActor->GetVisibility());
386           }
387         }
388       }else{
389         SALOME_ListIteratorOfListIO anIter( selected );
390         for(; anIter.More(); anIter.Next()){
391           Handle(SALOME_InteractiveObject) anIO = anIter.Value();
392           Update(anIO,true);
393         }
394       }
395       RepaintCurrentView();
396     }
397   }
398
399
400   void Update(const Handle(SALOME_InteractiveObject)& theIO,
401               bool theDisplay)
402   {
403     _PTR(Study) aStudy = GetActiveStudyDocument();
404     CORBA::Long anId = aStudy->StudyId();
405     TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry());
406     if( aVisualObj )
407       aVisualObj->Update();
408     if ( theDisplay )
409       UpdateView(SMESH::eDisplay,theIO->getEntry());
410   }
411
412
413   void UpdateSelectionProp( SMESHGUI* theModule ) {
414     if( !theModule )
415       return;
416
417     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
418     if( !app )
419     {
420       MESSAGE( "UpdateSelectionProp: Application is null" );
421       return;
422     }
423
424     SUIT_ViewManager* vm = app->activeViewManager();
425     if( !vm )
426     {
427       MESSAGE( "UpdateSelectionProp: View manager is null" );
428       return;
429     }
430
431     QPtrVector<SUIT_ViewWindow> views = vm->getViews();
432
433     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
434     if( !mgr )
435     {
436       MESSAGE( "UpdateSelectionProp: Resource manager is null" );
437       return;
438     }
439
440     QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
441            aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
442            aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
443
444     int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
445         PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
446
447     double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
448            SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 );
449
450     for ( int i=0, n=views.count(); i<n; i++ ) {
451         SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] );
452         if (!aVtkView) continue;
453         // update VTK viewer properties
454         SVTK_RenderWindowInteractor* anInteractor = aVtkView->getRWInteractor();
455         if (anInteractor) {
456           // mesh element selection
457           anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255.,
458                                          aSelColor.blue()/255., SW );
459
460           // tolerances
461           anInteractor->SetSelectionTolerance(SP1, SP2);
462
463           // pre-selection
464           SVTK_InteractorStyle* aStyle =
465             dynamic_cast<SVTK_InteractorStyle*>( anInteractor->GetInteractorStyle() );
466           if (aStyle) {
467             aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255.,
468                                         aPreColor.blue()/255., PW);
469           }
470         }
471         // update actors
472         vtkRenderer* aRenderer = aVtkView->getRenderer();
473         vtkActorCollection *aCollection = aRenderer->GetActors();
474         aCollection->InitTraversal();
475         while(vtkActor *anAct = aCollection->GetNextActor()){
476           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
477             anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255.,
478                                        aHiColor.blue()/255.);
479             anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255.,
480                                           aPreColor.blue()/255.);
481           }
482         }
483     }
484   }
485
486
487   //----------------------------------------------------------------------------
488   SVTK_InteractorStyle* GetInteractorStyle(SUIT_ViewWindow *theWindow){
489     if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow)){
490       if(SVTK_RenderWindowInteractor* anInteractor = aWnd->getRWInteractor()){
491         return dynamic_cast<SVTK_InteractorStyle*>( anInteractor->GetInteractorStyle() );
492       }
493     }
494     return NULL;
495   }
496
497   void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
498                  SVTK_InteractorStyle* theStyle)
499   {
500     if (theStyle)
501       theStyle->SetFilter(theFilter);
502   }
503
504   Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_InteractorStyle* theStyle)
505   {
506     return theStyle->GetFilter(theId);
507   }
508
509   bool IsFilterPresent(int theId, SVTK_InteractorStyle* theStyle)
510   {
511     return theStyle->IsFilterPresent(theId);
512   }
513
514   void RemoveFilter(int theId, SVTK_InteractorStyle* theStyle)
515   {
516     theStyle->RemoveFilter(theId);
517   }
518
519   void RemoveFilters(SVTK_InteractorStyle* theStyle)
520   {
521     for ( int id = SMESHGUI_NodeFilter; theStyle && id < SMESHGUI_LastFilter; id++ )
522       theStyle->RemoveFilter( id );
523   }
524
525   bool IsValid(SALOME_Actor* theActor, int theCellId,
526                SVTK_InteractorStyle* theStyle)
527   {
528     return theStyle->IsValid(theActor,theCellId);
529   }
530
531
532   //----------------------------------------------------------------------------
533   void SetPointRepresentation(bool theIsVisible){
534     if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
535       vtkRenderer *aRenderer = aViewWindow->getRenderer();
536       vtkActorCollection *aCollection = aRenderer->GetActors();
537       aCollection->InitTraversal();
538       while(vtkActor *anAct = aCollection->GetNextActor()){
539         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
540           if(anActor->GetVisibility()){
541             anActor->SetPointRepresentation(theIsVisible);
542           }
543         }
544       }
545       RepaintCurrentView();
546     }
547   }
548
549
550   void SetPickable(SMESH_Actor* theActor){
551     if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
552       int anIsAllPickable = (theActor == NULL);
553       vtkRenderer *aRenderer = aWnd->getRenderer();
554       vtkActorCollection *aCollection = aRenderer->GetActors();
555       aCollection->InitTraversal();
556       while(vtkActor *anAct = aCollection->GetNextActor()){
557         if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
558           if(anActor->GetVisibility()){
559             anActor->SetPickable(anIsAllPickable);
560           }
561         }
562       }
563       if(theActor)
564         theActor->SetPickable(!anIsAllPickable);
565       RepaintCurrentView();
566     }
567   }
568
569
570   //----------------------------------------------------------------------------
571   int GetNameOfSelectedNodes(SVTK_Selector* theSelector, 
572                              const Handle(SALOME_InteractiveObject)& theIO, 
573                              QString& theName)
574   {
575     theName = "";
576     TColStd_IndexedMapOfInteger aMapIndex;
577     theSelector->GetIndex(theIO,aMapIndex);
578
579     for(int i = 1; i <= aMapIndex.Extent(); i++)
580       theName += QString(" %1").arg(aMapIndex(i));
581
582     return aMapIndex.Extent();
583   }
584
585   int GetNameOfSelectedElements(SVTK_Selector* theSelector, 
586                                 const Handle(SALOME_InteractiveObject)& theIO, 
587                                 QString& theName)
588   {
589     theName = "";
590     TColStd_IndexedMapOfInteger aMapIndex;
591     theSelector->GetIndex(theIO,aMapIndex);
592
593     typedef std::set<int> TIdContainer;
594     TIdContainer anIdContainer;
595     for( int i = 1; i <= aMapIndex.Extent(); i++)
596       anIdContainer.insert(aMapIndex(i));
597
598     TIdContainer::const_iterator anIter = anIdContainer.begin();
599     for(; anIter != anIdContainer.end(); anIter++)
600       theName += QString(" %1").arg(*anIter);
601
602     return aMapIndex.Extent();
603   }
604
605
606   int GetEdgeNodes(SVTK_Selector* theSelector, 
607                    const TVisualObjPtr& theVisualObject,
608                    int& theId1, 
609                    int& theId2)
610   {
611     const SALOME_ListIO& selected = theSelector->StoredIObjects(); 
612
613     if ( selected.Extent() != 1 )
614       return -1;
615
616     Handle(SALOME_InteractiveObject) anIO = selected.First();
617     if ( anIO.IsNull() || !anIO->hasEntry() )
618       return -1;
619
620     TColStd_IndexedMapOfInteger aMapIndex;
621     theSelector->GetIndex( anIO, aMapIndex );
622     if ( aMapIndex.Extent() != 2 )
623       return -1;
624
625     int anObjId = -1, anEdgeNum = -1;
626     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
627       int aVal = aMapIndex( i );
628       if ( aVal > 0 )
629         anObjId = aVal;
630       else
631         anEdgeNum = abs( aVal ) - 1;
632     }
633
634     if ( anObjId == -1 || anEdgeNum == -1 )
635       return -1;
636
637     return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
638   }
639
640   //----------------------------------------------------------------------------
641   int GetNameOfSelectedNodes(SalomeApp_SelectionMgr *theMgr,
642                              const Handle(SALOME_InteractiveObject)& theIO,
643                              QString& theName)
644   {
645     theName = "";
646     if(theIO->hasEntry()){
647       if(FindActorByEntry(theIO->getEntry())){
648         TColStd_IndexedMapOfInteger aMapIndex;
649         theMgr->GetIndexes(theIO,aMapIndex);
650         for(int i = 1; i <= aMapIndex.Extent(); i++){
651           theName += QString(" %1").arg(aMapIndex(i));
652         }
653         return aMapIndex.Extent();
654       }
655     }
656     return -1;
657   }
658
659   int GetNameOfSelectedNodes(SalomeApp_SelectionMgr *theMgr, QString& theName){
660     theName = "";
661     SALOME_ListIO selected; theMgr->selectedObjects( selected );
662     if(selected.Extent() == 1){
663       Handle(SALOME_InteractiveObject) anIO = selected.First();
664       return GetNameOfSelectedNodes(theMgr,anIO,theName);
665     }
666     return -1;
667   }
668   
669
670   int GetNameOfSelectedElements(SalomeApp_SelectionMgr *theMgr,
671                                 const Handle(SALOME_InteractiveObject)& theIO,
672                                 QString& theName)
673   {
674     theName = "";
675     if(theIO->hasEntry()){
676       if(FindActorByEntry(theIO->getEntry())){
677         TColStd_IndexedMapOfInteger aMapIndex;
678         theMgr->GetIndexes(theIO,aMapIndex);
679         typedef set<int> TIdContainer;
680         TIdContainer anIdContainer;
681         for( int i = 1; i <= aMapIndex.Extent(); i++)
682           anIdContainer.insert(aMapIndex(i));
683         TIdContainer::const_iterator anIter = anIdContainer.begin();
684         for(; anIter != anIdContainer.end(); anIter++){
685           theName += QString(" %1").arg(*anIter);
686         }
687         return aMapIndex.Extent();
688       }
689     }
690     return -1;
691   }
692
693
694   int GetNameOfSelectedElements(SalomeApp_SelectionMgr *theMgr, QString& theName)
695   {
696     theName = "";
697     SALOME_ListIO selected; theMgr->selectedObjects( selected );
698
699     if( selected.Extent() == 1){
700       Handle(SALOME_InteractiveObject) anIO = selected.First();
701       return GetNameOfSelectedElements(theMgr,anIO,theName);
702     }
703     return -1;
704   }
705
706   int GetSelected(SalomeApp_SelectionMgr*      theMgr,
707                   TColStd_IndexedMapOfInteger& theMap,
708                   const bool                   theIsElement)
709   {
710     theMap.Clear();
711     SALOME_ListIO selected; theMgr->selectedObjects( selected );
712
713     if ( selected.Extent() == 1 )
714     {
715       Handle(SALOME_InteractiveObject) anIO = selected.First();
716       if ( anIO->hasEntry() ) {
717         theMgr->GetIndexes( anIO, theMap );
718       }
719     }
720     return theMap.Extent();
721   }
722
723
724   int GetEdgeNodes( SalomeApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
725   {
726     SALOME_ListIO selected; theMgr->selectedObjects( selected );
727
728     if ( selected.Extent() != 1 )
729       return -1;
730
731     Handle(SALOME_InteractiveObject) anIO = selected.First();
732     if ( anIO.IsNull() || !anIO->hasEntry() )
733       return -1;
734
735     SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
736     if ( anActor == 0 )
737       return -1;
738
739     TColStd_IndexedMapOfInteger aMapIndex;
740     theMgr->GetIndexes( anIO, aMapIndex );
741     if ( aMapIndex.Extent() != 2 )
742       return -1;
743
744     int anObjId = -1, anEdgeNum = -1;
745     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
746       int aVal = aMapIndex( i );
747       if ( aVal > 0 )
748         anObjId = aVal;
749       else
750         anEdgeNum = abs( aVal );
751     }
752
753     if ( anObjId == -1 || anEdgeNum == -1 )
754       return -1;
755
756     return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
757   }
758
759   void SetControlsPrecision( const long theVal )
760   {
761     if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
762     {
763       vtkRenderer *aRenderer = aWnd->getRenderer();
764       vtkActorCollection *aCollection = aRenderer->GetActors();
765       aCollection->InitTraversal();
766       
767       while ( vtkActor *anAct = aCollection->GetNextActor())
768       {
769         if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
770         {
771           anActor->SetControlsPrecision( theVal );
772           anActor->SetControlMode( anActor->GetControlMode() );
773         }
774       }
775
776     }
777   }
778 }