Salome HOME
PAL6938
[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
24 #include <vtkRenderer.h>
25 #include <vtkActorCollection.h>
26
27 #include <TColStd_IndexedMapOfInteger.hxx>
28
29 #include "QAD_Config.h"
30 #include "QAD_Desktop.h"
31 #include "QAD_Study.h"
32 #include "QAD_Settings.h"
33 #include "QAD_RightFrame.h"
34
35 #include "SALOME_Selection.h"
36 #include "SALOME_ListIteratorOfListIO.hxx"
37
38 #include "VTKViewer_ViewFrame.h"
39 #include "VTKViewer_RenderWindow.h"
40 #include "VTKViewer_InteractorStyleSALOME.h"
41 #include "VTKViewer_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 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
51
52 #include "SMESH_Actor.h"
53 #include "SMESH_ObjectDef.h"
54
55 using namespace std;
56
57 namespace SMESH{
58
59   typedef map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
60   static TVisualObjCont VISUAL_OBJ_CONT;
61
62   TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry){
63     TVisualObjPtr aVisualObj;
64     try{
65       TVisualObjCont::key_type aKey(theStudyId,theEntry);
66       TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
67       if(anIter != VISUAL_OBJ_CONT.end()){
68         aVisualObj = anIter->second;
69       }else{
70         SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
71         SALOMEDS::SObject_var aSObj = aStudy->FindObjectID(theEntry);
72         if(!CORBA::is_nil(aSObj)){
73           SALOMEDS::GenericAttribute_var anAttr;
74           if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
75             SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
76             CORBA::String_var aVal = anIOR->Value();
77             CORBA::Object_var anObj = aStudy->ConvertIORToObject(aVal.in());
78             if(!CORBA::is_nil(anObj)){
79               //Try narrow to SMESH_Mesh interafce
80               SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
81               if(!aMesh->_is_nil()){
82                 aVisualObj.reset(new SMESH_MeshObj(aMesh));
83                 aVisualObj->Update();
84                 TVisualObjCont::value_type aValue(aKey,aVisualObj); 
85                 VISUAL_OBJ_CONT.insert(aValue);
86                 return aVisualObj;
87               }
88               //Try narrow to SMESH_Group interafce
89               SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
90               if(!aGroup->_is_nil()){
91                 SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
92                 if(aFatherSObj->_is_nil()) return aVisualObj;
93                 aFatherSObj = aFatherSObj->GetFather();
94                 if(aFatherSObj->_is_nil()) return aVisualObj;
95                 CORBA::String_var anEntry = aFatherSObj->GetID();
96                 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
97                 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
98                   aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
99                   aVisualObj->Update();
100                   TVisualObjCont::value_type aValue(aKey,aVisualObj); 
101                   VISUAL_OBJ_CONT.insert(aValue);
102                   return aVisualObj;
103                 }
104               }
105               //Try narrow to SMESH_subMesh interafce
106               SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
107               if(!aSubMesh->_is_nil()){
108                 SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
109                 if(aFatherSObj->_is_nil()) return aVisualObj;
110                 aFatherSObj = aFatherSObj->GetFather();
111                 if(aFatherSObj->_is_nil()) return aVisualObj;
112                 CORBA::String_var anEntry = aFatherSObj->GetID();
113                 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
114                 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
115                   aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
116                   aVisualObj->Update();
117                   TVisualObjCont::value_type aValue(aKey,aVisualObj); 
118                   VISUAL_OBJ_CONT.insert(aValue);
119                   return aVisualObj;
120                 }
121               }
122             }
123           }
124         }
125       }
126     }catch(...){
127       INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
128     }
129     return aVisualObj;
130   }
131
132
133   VTKViewer_ViewFrame* FindVtkViewFrame(QAD_Study* theStudy,
134                                         QAD_StudyFrame* theStudyFrame)
135   {
136     QList<QAD_Study>& aStudies = 
137       QAD_Application::getDesktop()->getActiveApp()->getStudies();
138     if(aStudies.containsRef(theStudy)){
139       const QList<QAD_StudyFrame>& aStudyFrames = theStudy->getStudyFrames();
140       if(aStudyFrames.containsRef(theStudyFrame)){
141         return GetVtkViewFrame(theStudyFrame);
142       }
143     }
144     return NULL;
145   }
146
147
148   VTKViewer_ViewFrame* GetVtkViewFrame(QAD_StudyFrame* theStudyFrame){
149     QAD_ViewFrame* aViewFrame = theStudyFrame->getRightFrame()->getViewFrame();
150     return dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
151   }
152
153
154   VTKViewer_ViewFrame* GetCurrentVtkView(){
155     return GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame());
156   }
157
158
159   void RepaintViewFrame(VTKViewer_ViewFrame* theFrame)
160   {
161     theFrame->Repaint();
162   }
163
164
165   void RenderViewFrame(VTKViewer_ViewFrame* theFrame)
166   {
167     theFrame->getRW()->getRenderWindow()->Render();
168     theFrame->Repaint();
169   }
170
171
172   SMESH_Actor* FindActorByEntry(QAD_StudyFrame *theStudyFrame, 
173                                 const char* theEntry)
174   {
175     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
176       vtkRenderer *aRenderer = aViewFrame->getRenderer();
177       vtkActorCollection *aCollection = aRenderer->GetActors();
178       aCollection->InitTraversal();
179       while(vtkActor *anAct = aCollection->GetNextActor()){
180         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
181           if(anActor->hasIO()){
182             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
183             if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
184               return anActor;
185             }
186           }
187         }
188       }
189     }
190     return NULL;
191   }
192   
193   
194   SMESH_Actor* FindActorByEntry(const char* theEntry){
195     return FindActorByEntry(GetActiveStudy()->getActiveStudyFrame(),theEntry);
196   }
197   
198   
199   SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
200     if(!CORBA::is_nil(theObject)){
201       SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
202       CORBA::String_var anIOR = aStudy->ConvertObjectToIOR(theObject);
203       SALOMEDS::SObject_var aSObject = aStudy->FindObjectIOR(anIOR.in());
204       if(!aSObject->_is_nil()){
205         CORBA::String_var anEntry = aSObject->GetID();
206         return FindActorByEntry(anEntry.in());
207       }
208     }
209     return NULL;
210   }
211
212
213   SMESH_Actor* CreateActor(SALOMEDS::Study_ptr theStudy,
214                            const char* theEntry,
215                            int theIsClear)
216   {
217     SMESH_Actor *anActor = NULL;
218     CORBA::Long anId = theStudy->StudyId();
219     if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
220       SALOMEDS::SObject_var aSObj = theStudy->FindObjectID(theEntry);
221       if(!aSObj->_is_nil()){
222         SALOMEDS::GenericAttribute_var anAttr;
223         if(aSObj->FindAttribute(anAttr,"AttributeName")){
224           SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
225           CORBA::String_var aVal = aName->Value();
226           string aNameVal = aVal.in();
227           anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
228         }
229       }
230     }
231     return anActor;
232   }
233
234
235   void DisplayActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
236     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
237       aViewFrame->AddActor(theActor);
238       aViewFrame->Repaint();
239     }
240   }
241
242
243   void RemoveActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
244     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
245       aViewFrame->RemoveActor(theActor);
246       aViewFrame->Repaint();
247     }
248   }
249   
250
251   void FitAll(){
252     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
253       aViewFrame->onViewFitAll();
254       aViewFrame->Repaint();
255     }
256   }
257   
258   vtkRenderer* GetCurrentRenderer(){
259     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame()))
260       return aViewFrame->getRenderer();
261     return NULL;
262   }
263
264   void RepaintCurrentView(){
265     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
266       aViewFrame->getRW()->getRenderWindow()->Render();
267       //aViewFrame->Repaint();
268     }
269   }
270   
271   
272   void UpdateView(QAD_StudyFrame *theStudyFrame, EDisplaing theAction, const char* theEntry)
273   {
274     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
275       vtkRenderer *aRenderer = aViewFrame->getRenderer();
276       vtkActorCollection *aCollection = aRenderer->GetActors();
277       aCollection->InitTraversal();
278       switch(theAction){
279       case eDisplayAll: {
280         while(vtkActor *anAct = aCollection->GetNextActor()){
281           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
282             anActor->SetVisibility(true);
283           }
284         }
285         break;
286       }
287       case eDisplayOnly:
288       case eEraseAll: {
289         while(vtkActor *anAct = aCollection->GetNextActor()){
290           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
291             anActor->SetVisibility(false);
292           }
293         }
294       }
295       default: {
296         if(SMESH_Actor *anActor = FindActorByEntry(theStudyFrame,theEntry)){
297           switch(theAction) {
298             case eDisplay:
299             case eDisplayOnly:
300               anActor->SetVisibility(true);
301               break;
302             case eErase:
303               anActor->SetVisibility(false);
304               break;
305           }
306         } else {
307           switch(theAction){
308           case eDisplay:
309           case eDisplayOnly:{
310             QAD_Study* aStudy = theStudyFrame->getStudy();
311             SALOMEDS::Study_var aDocument = aStudy->getStudyDocument();
312             if((anActor = CreateActor(aDocument,theEntry,true))) {
313               DisplayActor(theStudyFrame,anActor);
314               FitAll();
315             }
316             break;
317           }
318           }
319         }
320       }
321       }
322     }
323   }
324
325
326   void UpdateView(EDisplaing theAction, const char* theEntry){
327     QAD_Study* aStudy = GetActiveStudy();
328     QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
329     UpdateView(aStudyFrame,theAction,theEntry);
330   }
331   
332   void UpdateView(){
333     if(VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView()){
334       SALOME_Selection *aSel = SALOME_Selection::Selection(GetActiveStudy()->getSelection());
335       if(aSel->IObjectCount() == 0){
336         vtkRenderer* aRenderer = aViewFrame->getRenderer();
337         vtkActorCollection *aCollection = aRenderer->GetActors();
338         aCollection->InitTraversal();
339         while(vtkActor *anAct = aCollection->GetNextActor()){
340           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
341             if(anActor->hasIO())
342               Update(anActor->getIO(),anActor->GetVisibility());
343           }
344         }
345       }else{
346         SALOME_ListIteratorOfListIO anIter(aSel->StoredIObjects());
347         for(; anIter.More(); anIter.Next()){
348           Handle(SALOME_InteractiveObject) anIO = anIter.Value();
349           Update(anIO,true);
350         }
351       }
352       RepaintCurrentView();
353     }
354   }
355
356
357   void Update(const Handle(SALOME_InteractiveObject)& theIO,
358               bool theDisplay)
359   {
360     SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
361     CORBA::Long anId = aStudy->StudyId();
362     TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry());
363     aVisualObj->Update();
364     if ( theDisplay )
365       UpdateView(SMESH::eDisplay,theIO->getEntry());
366   }
367
368
369   void UpdateSelectionProp() {
370     QAD_Study* aStudy = GetActiveStudy();
371     QList<QAD_StudyFrame> aFrameList = aStudy->getStudyFrames();
372     
373     QString SCr, SCg, SCb;
374     SCr = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorRed");
375     SCg = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorGreen");
376     SCb = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorBlue");
377     QColor aHiColor = Qt::white;
378     if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
379       aHiColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
380     
381     SCr = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorRed");
382     SCg = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorGreen");
383     SCb = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorBlue");
384     QColor aSelColor = Qt::yellow;
385     if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
386       aSelColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
387     QString SW = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectWidth");
388     if (SW.isEmpty()) SW = "5";
389     
390     SCr = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorRed");
391     SCg = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorGreen");
392     SCb = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorBlue");
393     QColor aPreColor = Qt::cyan;
394     if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
395       aPreColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
396     QString PW = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectWidth");
397     if (PW.isEmpty()) PW = "5";
398     
399     QString SP1 = QAD_CONFIG->getSetting("SMESH:SettingsNodeSelectTol");
400     if (SP1.isEmpty()) SP1 = "0.025";
401     QString SP2 = QAD_CONFIG->getSetting("SMESH:SettingsElementsSelectTol");
402     if (SP2.isEmpty()) SP2 = "0.001";
403     
404     for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
405       if (aStudyFrame->getTypeView() == VIEW_VTK) {
406         VTKViewer_ViewFrame* aVtkViewFrame = GetVtkViewFrame(aStudyFrame);
407         if (!aVtkViewFrame) continue;
408         // update VTK viewer properties
409         VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
410         if (anInteractor) {
411           // mesh element selection
412           anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255., 
413                                          aSelColor.blue()/255., SW.toInt());
414           
415           // tolerances
416           anInteractor->SetSelectionTolerance(SP1.toDouble(), SP2.toDouble());
417           
418           // pre-selection
419           VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
420           if (aStyle) {
421             aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255., 
422                                         aPreColor.blue()/255., PW.toInt());
423           }
424         }
425         // update actors
426         vtkRenderer* aRenderer = aVtkViewFrame->getRenderer();
427         vtkActorCollection *aCollection = aRenderer->GetActors();
428         aCollection->InitTraversal();
429         while(vtkActor *anAct = aCollection->GetNextActor()){
430           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
431             anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255., 
432                                        aHiColor.blue()/255.);
433             anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255., 
434                                           aPreColor.blue()/255.);
435           }
436         }
437       }
438     }
439   }
440
441   
442   //----------------------------------------------------------------------------
443   VTKViewer_InteractorStyleSALOME* GetInteractorStyle(QAD_StudyFrame *theStudyFrame){
444     if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
445       if(VTKViewer_RenderWindowInteractor* anInteractor = aViewFrame->getRWInteractor()){
446         return anInteractor->GetInteractorStyleSALOME();
447       }
448     }
449     return NULL;
450   }
451
452   void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
453                  VTKViewer_InteractorStyleSALOME* theStyle)
454   {
455     theStyle->SetFilter(theFilter);
456   }
457
458   Handle(VTKViewer_Filter) GetFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
459   {
460     return theStyle->GetFilter(theId);
461   }
462
463   bool IsFilterPresent(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
464   {
465     return theStyle->IsFilterPresent(theId);
466   }
467
468   void RemoveFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle){
469     theStyle->RemoveFilter(theId);
470   }
471
472   void RemoveFilters(VTKViewer_InteractorStyleSALOME* theStyle){
473     for ( int id = SMESHGUI_NodeFilter; theStyle && id < SMESHGUI_LastFilter; id++ )
474       theStyle->RemoveFilter( id );
475   }
476
477   bool IsValid(SALOME_Actor* theActor, int theCellId,
478                VTKViewer_InteractorStyleSALOME* theStyle)
479   {
480     return theStyle->IsValid(theActor,theCellId);
481   }
482
483
484   //----------------------------------------------------------------------------
485   void SetPointRepresentation(bool theIsVisible){
486     if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
487       vtkRenderer *aRenderer = aViewFrame->getRenderer();
488       vtkActorCollection *aCollection = aRenderer->GetActors();
489       aCollection->InitTraversal();
490       while(vtkActor *anAct = aCollection->GetNextActor()){
491         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
492           if(anActor->GetVisibility()){
493             anActor->SetPointRepresentation(theIsVisible);
494           }
495         }
496       }
497       RepaintCurrentView();
498     }
499   }
500
501
502   void SetPickable(SMESH_Actor* theActor){
503     if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
504       int anIsAllPickable = (theActor == NULL);
505       vtkRenderer *aRenderer = aViewFrame->getRenderer();
506       vtkActorCollection *aCollection = aRenderer->GetActors();
507       aCollection->InitTraversal();
508       while(vtkActor *anAct = aCollection->GetNextActor()){
509         if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
510           if(anActor->GetVisibility()){
511             anActor->SetPickable(anIsAllPickable);
512           }
513         }
514       }
515       if(theActor)
516         theActor->SetPickable(!anIsAllPickable);
517       RepaintCurrentView();
518     }
519   }
520
521
522   int GetNameOfSelectedNodes(SALOME_Selection *theSel, 
523                              const Handle(SALOME_InteractiveObject)& theIO, 
524                              QString& theName)
525   {
526     theName = "";
527     if(theIO->hasEntry()){
528       if(FindActorByEntry(theIO->getEntry())){
529         TColStd_IndexedMapOfInteger aMapIndex;
530         theSel->GetIndex(theIO,aMapIndex);
531         for(int i = 1; i <= aMapIndex.Extent(); i++){
532           theName += QString(" %1").arg(aMapIndex(i));
533         }
534         return aMapIndex.Extent();
535       }
536     }
537     return -1;
538   }
539   
540
541   int GetNameOfSelectedNodes(SALOME_Selection *theSel, QString& theName){
542     theName = "";
543     if(theSel->IObjectCount() == 1){
544       Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
545       return GetNameOfSelectedNodes(theSel,anIO,theName);
546     }
547     return -1;
548   }
549   
550
551   int GetNameOfSelectedElements(SALOME_Selection *theSel, 
552                                 const Handle(SALOME_InteractiveObject)& theIO, 
553                                 QString& theName)
554   {
555     theName = "";
556     if(theIO->hasEntry()){
557       if(FindActorByEntry(theIO->getEntry())){
558         TColStd_IndexedMapOfInteger aMapIndex;
559         theSel->GetIndex(theIO,aMapIndex);
560         typedef std::set<int> TIdContainer;
561         TIdContainer anIdContainer;
562         for( int i = 1; i <= aMapIndex.Extent(); i++)
563           anIdContainer.insert(aMapIndex(i));
564         TIdContainer::const_iterator anIter = anIdContainer.begin();
565         for(; anIter != anIdContainer.end(); anIter++){
566           theName += QString(" %1").arg(*anIter);
567         }
568         return aMapIndex.Extent();
569       }
570     }
571     return -1;
572   }
573
574
575   int GetNameOfSelectedElements(SALOME_Selection *theSel, QString& theName)
576   {
577     theName = "";
578     if(theSel->IObjectCount() == 1){
579       Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
580       return GetNameOfSelectedElements(theSel,anIO,theName);
581     }
582     return -1;
583   }
584
585   
586   int GetSelected(SALOME_Selection*            theSel, 
587                   TColStd_IndexedMapOfInteger& theMap, 
588                   const bool                   theIsElement)
589   {
590     theMap.Clear();
591
592     if ( theSel->IObjectCount() == 1 )
593     {
594       Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
595       if ( anIO->hasEntry() ) {
596         theSel->GetIndex( anIO, theMap );
597       }
598     }
599     return theMap.Extent();
600   }
601
602
603   int GetEdgeNodes( SALOME_Selection* theSel, int& theId1, int& theId2 )
604   {
605     if ( theSel->IObjectCount() != 1 )
606       return -1;
607
608     Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
609     if ( anIO.IsNull() || !anIO->hasEntry() )
610       return -1;
611
612     SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
613     if ( anActor == 0 )
614       return -1;
615
616     TColStd_IndexedMapOfInteger aMapIndex;
617     theSel->GetIndex( anIO, aMapIndex );
618     if ( aMapIndex.Extent() != 2 )
619       return -1;
620     
621     int anObjId = -1, anEdgeNum = -1;
622     for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
623       int aVal = aMapIndex( i );
624       if ( aVal > 0 )
625         anObjId = aVal;
626       else
627         anEdgeNum = abs( aVal );
628     }
629     
630     if ( anObjId == -1 || anEdgeNum == -1 )
631       return -1;
632     
633     return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
634   }
635   
636   void SetControlsPrecision( const long theVal )
637   {
638     if( VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView() )
639     {
640       vtkRenderer *aRenderer = aViewFrame->getRenderer();
641       vtkActorCollection *aCollection = aRenderer->GetActors();
642       aCollection->InitTraversal();
643       
644       while ( vtkActor *anAct = aCollection->GetNextActor())
645       {
646         if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
647         {
648           anActor->SetControlsPrecision( theVal );
649           anActor->SetControlMode( anActor->GetControlMode() );
650         }
651       }
652
653     }
654   }
655 }
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672