Salome HOME
0020044: EDF 866 GEOM: Extrusion along a path : impossible to select a Wire
[modules/geom.git] / src / GEOMGUI / GEOMGUI_Selection.cxx
1 // Copyright (C) 2005  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : GEOMGUI_Selection.cxx
21 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
22 //
23
24 #include "GEOMGUI_Selection.h"
25
26 #include "GeometryGUI.h"
27 #include "GEOM_Displayer.h"
28
29 #include <SalomeApp_Application.h>
30 #include <SalomeApp_Study.h>
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ViewWindow.h>
35 #include <SUIT_ViewManager.h>
36
37 #include <SALOME_Prs.h>
38 #include <SALOME_InteractiveObject.hxx>
39
40 #include <SOCC_Prs.h>
41 #include <SVTK_Prs.h>
42 #include <SALOME_Actor.h>
43
44 #include <OCCViewer_ViewModel.h>
45 #include <SVTK_ViewModel.h>
46
47 #include <GEOMImpl_Types.hxx>
48
49 // OCCT Includes
50 #include <AIS.hxx>
51 #include <AIS_InteractiveObject.hxx>
52 #include <AIS_ListOfInteractive.hxx>
53
54 // VTK Includes
55 #include <vtkActorCollection.h>
56
57 GEOMGUI_Selection::GEOMGUI_Selection()
58 : LightApp_Selection()
59 {
60 }
61
62 GEOMGUI_Selection::~GEOMGUI_Selection()
63 {
64 }
65
66 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
67 {
68   if ( p == "isOCC" ) return QVariant( activeViewType() == OCCViewer_Viewer::Type() );
69   if ( p == "selectionmode" ){ 
70     return QVariant(selectionMode()); 
71   }
72   return LightApp_Selection::parameter( p );
73 }
74
75 QVariant GEOMGUI_Selection::parameter( const int ind, const QString& p ) const
76 {
77 //  if      ( p == "isVisible"   )    return QVariant( isVisible( ind ) );
78 // parameter isVisible is calculated in base SalomeApp_Selection
79 //  else
80   if( p == "type" )
81     return QVariant( typeName( ind ) );
82   else if ( p == "displaymode" )
83     return QVariant( displayMode( ind ) );
84   else if ( p == "isAutoColor" )
85     return QVariant( isAutoColor( ind ) );
86   else
87     return LightApp_Selection::parameter( ind, p );
88 }
89
90 QString GEOMGUI_Selection::typeName( const int index ) const
91 {
92   if ( isComponent( index ) )
93     return "Component";
94   GEOM::GEOM_Object_var anObj = getObject( index );
95   if ( !CORBA::is_nil( anObj ) ) {
96     const int aGeomType = anObj->GetType();
97     if ( aGeomType == GEOM_GROUP )
98       return "Group";
99     else
100       return "Shape";
101   }
102   return "Unknown";
103 }
104
105 bool GEOMGUI_Selection::isVisible( const int index ) const
106 {
107   GEOM::GEOM_Object_var obj = getObject( index );
108   SALOME_View* view = GEOM_Displayer::GetActiveView();
109   if ( !CORBA::is_nil( obj ) && view ) {
110     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
111     return view->isVisible( io );
112   }
113   return false;
114 }
115
116 bool GEOMGUI_Selection::isAutoColor( const int index ) const
117 {
118   GEOM::GEOM_Object_var obj = getObject( index );
119   if ( !CORBA::is_nil( obj ) )
120     return obj->GetAutoColor();
121   return false;
122 }
123
124 QString GEOMGUI_Selection::displayMode( const int index ) const
125 {
126   SALOME_View* view = GEOM_Displayer::GetActiveView();
127   QString viewType = activeViewType();
128   if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
129     SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
130     if ( prs ) {
131       if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
132         SOCC_Prs* occPrs = (SOCC_Prs*) prs;
133         AIS_ListOfInteractive lst;
134         occPrs->GetObjects( lst );
135         if ( lst.Extent() ) {
136           Handle(AIS_InteractiveObject) io = lst.First();
137           if ( !io.IsNull() ) {
138             int dm = io->DisplayMode();
139             if ( dm == AIS_WireFrame )
140               return "Wireframe";
141             else if ( dm == AIS_Shaded )
142               return "Shading";
143             else { // return default display mode of AIS_InteractiveContext
144               OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*) SUIT_Session::session()->activeApplication()->desktop(
145                                             )->activeWindow()->getViewManager()->getViewModel();
146               Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
147               dm = ic->DisplayMode();
148               if ( dm == AIS_WireFrame )
149                 return "Wireframe";
150               else if ( dm == AIS_Shaded )
151                 return "Shading";
152             }
153           }
154         }
155       }
156       else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
157         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
158         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
159         if ( lst ) {
160           lst->InitTraversal();
161           vtkActor* actor = lst->GetNextActor();
162           if ( actor ) {
163             SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
164             if ( salActor ) {
165               int dm = salActor->getDisplayMode();
166               if ( dm == 0 )
167                 return "Wireframe";
168               else if ( dm == 1 )
169                 return "Shading";
170             } // if ( salome actor )
171           } // if ( actor )
172         } // if ( lst == vtkPrs->GetObjects() )
173       } // if VTK
174     }
175   }
176   return "";
177 }
178
179 bool GEOMGUI_Selection::isComponent( const int index ) const
180 {
181   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
182     (SUIT_Session::session()->activeApplication()->activeStudy());
183
184   if ( appStudy && index >= 0 && index < count() )  {
185     _PTR(Study) study = appStudy->studyDS();
186     QString anEntry = entry( index );
187
188     if ( study && !anEntry.isNull() ) {
189       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
190       if ( aSO && aSO->GetFatherComponent() )
191         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
192     }
193   }
194   return false;
195 }
196
197 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
198 {
199   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
200     (SUIT_Session::session()->activeApplication()->activeStudy());
201
202   if (appStudy && index >= 0 && index < count()) {
203     _PTR(Study) study = appStudy->studyDS();
204     QString anEntry = entry(index);
205
206     if (study && !anEntry.isNull()) {
207       _PTR(SObject) aSO (study->FindObjectID(anEntry.toStdString()));
208       if (aSO) {
209         CORBA::Object_var anObj = GeometryGUI::ClientSObjectToObject(aSO);
210         return GEOM::GEOM_Object::_narrow(anObj);
211       }
212     }
213   }
214   return GEOM::GEOM_Object::_nil();
215 }
216
217 QString GEOMGUI_Selection::selectionMode() const
218 {
219   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
220   if (app) {
221     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
222     if (aGeomGUI) {
223       switch (aGeomGUI->getLocalSelectionMode())
224       {
225         case GEOM_POINT      : return "VERTEX";
226         case GEOM_EDGE       : return "EDGE";
227         case GEOM_WIRE       : return "WIRE";
228         case GEOM_FACE       : return "FACE";
229         case GEOM_SHELL      : return "SHELL";
230         case GEOM_SOLID      : return "SOLID";
231         case GEOM_COMPOUND   : return "COMPOUND";
232         case GEOM_ALLOBJECTS : return "ALL";
233         default: return "";
234       }
235     }
236   }
237   return "";
238 }