Salome HOME
Movement of examples to CVS EXAMPLES SAMPLES_SRC.
[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     return NULL;
151   }
152
153   SVTK_ViewWindow* FindVtkViewWindow( SUIT_ViewManager* theMgr,
154                                            SUIT_ViewWindow* theWindow )
155   {
156     if( !theMgr )
157       return NULL;
158
159     QPtrVector<SUIT_ViewWindow> views = theMgr->getViews();
160     if( views.containsRef( theWindow ) )
161       return GetVtkViewWindow( theWindow );
162     else
163       return NULL;
164   }
165
166
167   SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
168     return dynamic_cast<SVTK_ViewWindow*>(theWindow);
169   }
170
171
172 /*  SUIT_ViewWindow* GetActiveWindow()
173   {
174     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
175     if( !app )
176       return NULL;
177     SUIT_ViewManager* mgr = app->activeViewManager();
178     if( mgr )
179       return mgr->getActiveView();
180     else
181       return NULL;
182   }*/
183
184   SVTK_ViewWindow* GetCurrentVtkView(){
185     return GetVtkViewWindow( GetActiveWindow() );
186   }
187
188   void RepaintViewWindow(SVTK_ViewWindow* theWindow)
189   {
190     theWindow->Repaint();
191   }
192
193   void RenderViewWindow(SVTK_ViewWindow* theWindow)
194   {
195     theWindow->getRenderer()->Render();
196     theWindow->Repaint();
197   }
198
199   SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
200                                 const char* theEntry)
201   {
202     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
203       vtkRenderer *aRenderer = aViewWindow->getRenderer();
204       vtkActorCollection *aCollection = aRenderer->GetActors();
205       aCollection->InitTraversal();
206       while(vtkActor *anAct = aCollection->GetNextActor()){
207         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
208           if(anActor->hasIO()){
209             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
210             if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
211               return anActor;
212             }
213           }
214         }
215       }
216     }
217     return NULL;
218   }
219
220
221   SMESH_Actor* FindActorByEntry(const char* theEntry){
222     return FindActorByEntry(GetActiveWindow(),theEntry);
223   }
224
225
226   SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
227     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
228     if( !app )
229       return NULL;
230
231     if(!CORBA::is_nil(theObject)){
232       _PTR(Study) aStudy = GetActiveStudyDocument();
233       CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
234       _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
235       if(aSObject){
236         CORBA::String_var anEntry = aSObject->GetID().c_str();
237         return FindActorByEntry(anEntry.in());
238       }
239     }
240     return NULL;
241   }
242
243
244   SMESH_Actor* CreateActor(_PTR(Study) theStudy,
245                            const char* theEntry,
246                            int theIsClear)
247   {
248     SMESH_Actor *anActor = NULL;
249     CORBA::Long anId = theStudy->StudyId();
250     if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
251       _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
252       if(aSObj){
253         _PTR(GenericAttribute) anAttr;
254         if(aSObj->FindAttribute(anAttr,"AttributeName")){
255           _PTR(AttributeName) aName = anAttr;
256           std::string aNameVal = aName->Value();
257           anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
258         }
259       }
260     }
261     return anActor;
262   }
263
264
265   void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
266     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
267       vtkWnd->AddActor(theActor);
268       vtkWnd->Repaint();
269     }
270   }
271
272
273   void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
274     if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
275       vtkWnd->RemoveActor(theActor);
276       if(theActor->hasIO()){
277         Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
278         if(anIO->hasEntry()){
279           std::string anEntry = anIO->getEntry();
280           SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
281           int aStudyId = aStudy->id();
282           TVisualObjCont::key_type aKey(aStudyId,anEntry);
283           VISUAL_OBJ_CONT.erase(aKey);
284         }
285       }
286       theActor->Delete();
287       vtkWnd->Repaint();
288     }
289   }
290
291
292   void FitAll(){
293     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
294       wnd->onFitAll();
295       wnd->Repaint();
296     }
297   }
298
299   vtkRenderer* GetCurrentRenderer(){
300     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() )
301       return wnd->getRenderer();
302     return NULL;
303   }
304
305   void RepaintCurrentView(){
306     if(SVTK_ViewWindow* wnd = GetCurrentVtkView() )
307       {
308         wnd->getRenderer()->Render();
309         wnd->Repaint(false);
310       }
311   }
312
313   void UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
314   {
315     if(SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd)){
316       vtkRenderer *aRenderer = aViewWnd->getRenderer();
317       vtkActorCollection *aCollection = aRenderer->GetActors();
318       aCollection->InitTraversal();
319       switch(theAction){
320       case eDisplayAll: {
321         while(vtkActor *anAct = aCollection->GetNextActor()){
322           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
323             anActor->SetVisibility(true);
324           }
325         }
326         break;
327       }
328       case eDisplayOnly:
329       case eEraseAll: {
330         while(vtkActor *anAct = aCollection->GetNextActor()){
331           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
332             anActor->SetVisibility(false);
333           }
334         }
335       }
336       default: {
337         if(SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)){
338           switch(theAction) {
339             case eDisplay:
340             case eDisplayOnly:
341               anActor->SetVisibility(true);
342               break;
343             case eErase:
344               anActor->SetVisibility(false);
345               break;
346           }
347         } else {
348           switch(theAction){
349           case eDisplay:
350           case eDisplayOnly:{
351             SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theWnd->getViewManager()->study() );
352             _PTR(Study) aDocument = aStudy->studyDS();
353             if((anActor = CreateActor(aDocument,theEntry,true))) {
354               DisplayActor(theWnd,anActor);
355               FitAll();
356             }
357             break;
358           }
359           }
360         }
361       }
362       }
363     }
364   }
365
366
367   void UpdateView(EDisplaing theAction, const char* theEntry){
368     SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
369     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
370     SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
371     UpdateView(aWnd,theAction,theEntry);
372   }
373
374   void UpdateView(){
375     if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
376       SalomeApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
377       SALOME_ListIO selected; mgr->selectedObjects( selected );
378
379       if( selected.Extent() == 0){
380         vtkRenderer* aRenderer = aWnd->getRenderer();
381         vtkActorCollection *aCollection = aRenderer->GetActors();
382         aCollection->InitTraversal();
383         while(vtkActor *anAct = aCollection->GetNextActor()){
384           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
385             if(anActor->hasIO())
386               Update(anActor->getIO(),anActor->GetVisibility());
387           }
388         }
389       }else{
390         SALOME_ListIteratorOfListIO anIter( selected );
391         for(; anIter.More(); anIter.Next()){
392           Handle(SALOME_InteractiveObject) anIO = anIter.Value();
393           Update(anIO,true);
394         }
395       }
396       RepaintCurrentView();
397     }
398   }
399
400
401   void Update(const Handle(SALOME_InteractiveObject)& theIO,
402               bool theDisplay)
403   {
404     _PTR(Study) aStudy = GetActiveStudyDocument();
405     CORBA::Long anId = aStudy->StudyId();
406     TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry());
407     if( aVisualObj )
408       aVisualObj->Update();
409     if ( theDisplay )
410       UpdateView(SMESH::eDisplay,theIO->getEntry());
411   }
412
413
414   void UpdateSelectionProp( SMESHGUI* theModule ) {
415     if( !theModule )
416       return;
417
418     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
419     if( !app )
420     {
421       MESSAGE( "UpdateSelectionProp: Application is null" );
422       return;
423     }
424
425     SUIT_ViewManager* vm = app->activeViewManager();
426     if( !vm )
427     {
428       MESSAGE( "UpdateSelectionProp: View manager is null" );
429       return;
430     }
431
432     QPtrVector<SUIT_ViewWindow> views = vm->getViews();
433
434     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
435     if( !mgr )
436     {
437       MESSAGE( "UpdateSelectionProp: Resource manager is null" );
438       return;
439     }
440
441     QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
442            aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
443            aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
444
445     int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
446         PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
447
448     double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
449            SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 );
450
451     for ( int i=0, n=views.count(); i<n; i++ ) {
452         SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] );
453         if (!aVtkView) continue;
454         // update VTK viewer properties
455         SVTK_RenderWindowInteractor* anInteractor = aVtkView->getRWInteractor();
456         if (anInteractor) {
457           // mesh element selection
458           anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255.,
459                                          aSelColor.blue()/255., SW );
460
461           // tolerances
462           anInteractor->SetSelectionTolerance(SP1, SP2);
463
464           // pre-selection
465           SVTK_InteractorStyle* aStyle =
466             dynamic_cast<SVTK_InteractorStyle*>( anInteractor->GetInteractorStyle() );
467           if (aStyle) {
468             aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255.,
469                                         aPreColor.blue()/255., PW);
470           }
471         }
472         // update actors
473         vtkRenderer* aRenderer = aVtkView->getRenderer();
474         vtkActorCollection *aCollection = aRenderer->GetActors();
475         aCollection->InitTraversal();
476         while(vtkActor *anAct = aCollection->GetNextActor()){
477           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
478             anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255.,
479                                        aHiColor.blue()/255.);
480             anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255.,
481                                           aPreColor.blue()/255.);
482           }
483         }
484     }
485   }
486
487
488   //----------------------------------------------------------------------------
489   SVTK_InteractorStyle* GetInteractorStyle(SUIT_ViewWindow *theWindow){
490     if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow)){
491       if(SVTK_RenderWindowInteractor* anInteractor = aWnd->getRWInteractor()){
492         return dynamic_cast<SVTK_InteractorStyle*>( anInteractor->GetInteractorStyle() );
493       }
494     }
495     return NULL;
496   }
497
498   void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
499                  SVTK_InteractorStyle* theStyle)
500   {
501     if (theStyle)
502       theStyle->SetFilter(theFilter);
503   }
504
505   Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_InteractorStyle* theStyle)
506   {
507     return theStyle->GetFilter(theId);
508   }
509
510   bool IsFilterPresent(int theId, SVTK_InteractorStyle* theStyle)
511   {
512     return theStyle->IsFilterPresent(theId);
513   }
514
515   void RemoveFilter(int theId, SVTK_InteractorStyle* theStyle)
516   {
517     theStyle->RemoveFilter(theId);
518   }
519
520   void RemoveFilters(SVTK_InteractorStyle* theStyle)
521   {
522     for ( int id = SMESHGUI_NodeFilter; theStyle && id < SMESHGUI_LastFilter; id++ )
523       theStyle->RemoveFilter( id );
524   }
525
526   bool IsValid(SALOME_Actor* theActor, int theCellId,
527                SVTK_InteractorStyle* theStyle)
528   {
529     return theStyle->IsValid(theActor,theCellId);
530   }
531
532
533   //----------------------------------------------------------------------------
534   void SetPointRepresentation(bool theIsVisible){
535     if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
536       vtkRenderer *aRenderer = aViewWindow->getRenderer();
537       vtkActorCollection *aCollection = aRenderer->GetActors();
538       aCollection->InitTraversal();
539       while(vtkActor *anAct = aCollection->GetNextActor()){
540         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
541           if(anActor->GetVisibility()){
542             anActor->SetPointRepresentation(theIsVisible);
543           }
544         }
545       }
546       RepaintCurrentView();
547     }
548   }
549
550
551   void SetPickable(SMESH_Actor* theActor){
552     if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
553       int anIsAllPickable = (theActor == NULL);
554       vtkRenderer *aRenderer = aWnd->getRenderer();
555       vtkActorCollection *aCollection = aRenderer->GetActors();
556       aCollection->InitTraversal();
557       while(vtkActor *anAct = aCollection->GetNextActor()){
558         if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
559           if(anActor->GetVisibility()){
560             anActor->SetPickable(anIsAllPickable);
561           }
562         }
563       }
564       if(theActor)
565         theActor->SetPickable(!anIsAllPickable);
566       RepaintCurrentView();
567     }
568   }
569
570
571   //----------------------------------------------------------------------------
572   int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
573                              const Handle(SALOME_InteractiveObject)& theIO,
574                              QString& theName)
575   {
576     theName = "";
577     TColStd_IndexedMapOfInteger aMapIndex;
578     theSelector->GetIndex(theIO,aMapIndex);
579
580     for(int i = 1; i <= aMapIndex.Extent(); i++)
581       theName += QString(" %1").arg(aMapIndex(i));
582
583     return aMapIndex.Extent();
584   }
585
586   int GetNameOfSelectedElements(SVTK_Selector* theSelector,
587                                 const Handle(SALOME_InteractiveObject)& theIO,
588                                 QString& theName)
589   {
590     theName = "";
591     TColStd_IndexedMapOfInteger aMapIndex;
592     theSelector->GetIndex(theIO,aMapIndex);
593
594     typedef std::set<int> TIdContainer;
595     TIdContainer anIdContainer;
596     for( int i = 1; i <= aMapIndex.Extent(); i++)
597       anIdContainer.insert(aMapIndex(i));
598
599     TIdContainer::const_iterator anIter = anIdContainer.begin();
600     for(; anIter != anIdContainer.end(); anIter++)
601       theName += QString(" %1").arg(*anIter);
602
603     return aMapIndex.Extent();
604   }
605
606
607   int GetEdgeNodes(SVTK_Selector* theSelector,
608                    const TVisualObjPtr& theVisualObject,
609                    int& theId1,
610                    int& theId2)
611   {
612     const SALOME_ListIO& selected = theSelector->StoredIObjects();
613
614     if ( selected.Extent() != 1 )
615       return -1;
616
617     Handle(SALOME_InteractiveObject) anIO = selected.First();
618     if ( anIO.IsNull() || !anIO->hasEntry() )
619       return -1;
620
621     TColStd_IndexedMapOfInteger aMapIndex;
622     theSelector->GetIndex( anIO, aMapIndex );
623     if ( aMapIndex.Extent() != 2 )
624       return -1;
625
626     int anObjId = -1, anEdgeNum = -1;
627     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
628       int aVal = aMapIndex( i );
629       if ( aVal > 0 )
630         anObjId = aVal;
631       else
632         anEdgeNum = abs( aVal ) - 1;
633     }
634
635     if ( anObjId == -1 || anEdgeNum == -1 )
636       return -1;
637
638     return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
639   }
640
641   //----------------------------------------------------------------------------
642   int GetNameOfSelectedNodes(SalomeApp_SelectionMgr *theMgr,
643                              const Handle(SALOME_InteractiveObject)& theIO,
644                              QString& theName)
645   {
646     theName = "";
647     if(theIO->hasEntry()){
648       if(FindActorByEntry(theIO->getEntry())){
649         TColStd_IndexedMapOfInteger aMapIndex;
650         theMgr->GetIndexes(theIO,aMapIndex);
651         for(int i = 1; i <= aMapIndex.Extent(); i++){
652           theName += QString(" %1").arg(aMapIndex(i));
653         }
654         return aMapIndex.Extent();
655       }
656     }
657     return -1;
658   }
659
660   int GetNameOfSelectedNodes(SalomeApp_SelectionMgr *theMgr, QString& theName){
661     theName = "";
662     SALOME_ListIO selected; theMgr->selectedObjects( selected );
663     if(selected.Extent() == 1){
664       Handle(SALOME_InteractiveObject) anIO = selected.First();
665       return GetNameOfSelectedNodes(theMgr,anIO,theName);
666     }
667     return -1;
668   }
669
670
671   int GetNameOfSelectedElements(SalomeApp_SelectionMgr *theMgr,
672                                 const Handle(SALOME_InteractiveObject)& theIO,
673                                 QString& theName)
674   {
675     theName = "";
676     if(theIO->hasEntry()){
677       if(FindActorByEntry(theIO->getEntry())){
678         TColStd_IndexedMapOfInteger aMapIndex;
679         theMgr->GetIndexes(theIO,aMapIndex);
680         typedef set<int> TIdContainer;
681         TIdContainer anIdContainer;
682         for( int i = 1; i <= aMapIndex.Extent(); i++)
683           anIdContainer.insert(aMapIndex(i));
684         TIdContainer::const_iterator anIter = anIdContainer.begin();
685         for(; anIter != anIdContainer.end(); anIter++){
686           theName += QString(" %1").arg(*anIter);
687         }
688         return aMapIndex.Extent();
689       }
690     }
691     return -1;
692   }
693
694
695   int GetNameOfSelectedElements(SalomeApp_SelectionMgr *theMgr, QString& theName)
696   {
697     theName = "";
698     SALOME_ListIO selected; theMgr->selectedObjects( selected );
699
700     if( selected.Extent() == 1){
701       Handle(SALOME_InteractiveObject) anIO = selected.First();
702       return GetNameOfSelectedElements(theMgr,anIO,theName);
703     }
704     return -1;
705   }
706
707   int GetSelected(SalomeApp_SelectionMgr*      theMgr,
708                   TColStd_IndexedMapOfInteger& theMap,
709                   const bool                   theIsElement)
710   {
711     theMap.Clear();
712     SALOME_ListIO selected; theMgr->selectedObjects( selected );
713
714     if ( selected.Extent() == 1 )
715     {
716       Handle(SALOME_InteractiveObject) anIO = selected.First();
717       if ( anIO->hasEntry() ) {
718         theMgr->GetIndexes( anIO, theMap );
719       }
720     }
721     return theMap.Extent();
722   }
723
724
725   int GetEdgeNodes( SalomeApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
726   {
727     SALOME_ListIO selected; theMgr->selectedObjects( selected );
728
729     if ( selected.Extent() != 1 )
730       return -1;
731
732     Handle(SALOME_InteractiveObject) anIO = selected.First();
733     if ( anIO.IsNull() || !anIO->hasEntry() )
734       return -1;
735
736     SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
737     if ( anActor == 0 )
738       return -1;
739
740     TColStd_IndexedMapOfInteger aMapIndex;
741     theMgr->GetIndexes( anIO, aMapIndex );
742     if ( aMapIndex.Extent() != 2 )
743       return -1;
744
745     int anObjId = -1, anEdgeNum = -1;
746     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
747       int aVal = aMapIndex( i );
748       if ( aVal > 0 )
749         anObjId = aVal;
750       else
751         anEdgeNum = abs( aVal );
752     }
753
754     if ( anObjId == -1 || anEdgeNum == -1 )
755       return -1;
756
757     return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
758   }
759
760   void SetControlsPrecision( const long theVal )
761   {
762     if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
763     {
764       vtkRenderer *aRenderer = aWnd->getRenderer();
765       vtkActorCollection *aCollection = aRenderer->GetActors();
766       aCollection->InitTraversal();
767
768       while ( vtkActor *anAct = aCollection->GetNextActor())
769       {
770         if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
771         {
772           anActor->SetControlsPrecision( theVal );
773           anActor->SetControlMode( anActor->GetControlMode() );
774         }
775       }
776
777     }
778   }
779 }