Salome HOME
Fix previous commit
[modules/geom.git] / src / GEOMGUI / GEOMGUI_Selection.cxx
1 // Copyright (C) 2007-2012  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.
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 // File   : GEOMGUI_Selection.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
25 //
26 #include "GEOMGUI_Selection.h"
27
28 #include "GeometryGUI.h"
29 #include "GEOM_Displayer.h"
30
31 #include "Material_Model.h"
32
33 #include <GEOM_Constants.h>
34
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37
38 #include "LightApp_DataOwner.h"
39
40 #include <SUIT_Session.h>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_ViewWindow.h>
43 #include <SUIT_ViewManager.h>
44
45 #include <SALOME_Prs.h>
46 #include <SALOME_InteractiveObject.hxx>
47
48 #include <SOCC_Prs.h>
49 #include <SVTK_Prs.h>
50 #include <SALOME_Actor.h>
51 #include <GEOM_Actor.h>
52
53 #include <OCCViewer_ViewModel.h>
54 #include <SVTK_ViewModel.h>
55
56 #include <GEOMImpl_Types.hxx>
57
58 #include <GEOM_AISShape.hxx>
59 #include <GEOM_VTKPropertyMaterial.hxx>
60
61 // OCCT Includes
62 #include <AIS.hxx>
63 #include <AIS_InteractiveObject.hxx>
64 #include <AIS_ListOfInteractive.hxx>
65 #include <AIS_GraphicTool.hxx>
66 #include <AIS_Drawer.hxx>
67 #include <Aspect_TypeOfFacingModel.hxx>
68 #include <Prs3d_ShadingAspect.hxx>
69 #include<Graphic3d_MaterialAspect.hxx>
70
71 // VTK Includes
72 #include <vtkActorCollection.h>
73
74 #define OCC_DISPLAY_MODE_TO_STRING( str, dm ) { \
75     if ( dm == AIS_WireFrame ) \
76       str = QString( "Wireframe" ); \
77     else if ( dm == AIS_Shaded )    \
78       str = QString( "Shading" ); \
79     else if ( dm == GEOM_AISShape::ShadingWithEdges )    \
80       str = QString( "ShadingWithEdges" ); \
81     else if ( dm == GEOM_AISShape::TexturedShape )    \
82       str = QString( "Texture" ); \
83     else \
84       str = QString(); }
85
86 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
87     if ( dm == 0 ) \
88       str = QString( "Wireframe" ); \
89     else if ( dm == 1 )    \
90       str = QString( "Shading" ); \
91     else if ( dm == 3 )    \
92       str = QString( "ShadingWithEdges" ); \
93     else \
94       str = QString(); }
95
96 #define USE_VISUAL_PROP_MAP
97
98 GEOMGUI_Selection::GEOMGUI_Selection()
99 : LightApp_Selection()
100 {
101 }
102
103 GEOMGUI_Selection::~GEOMGUI_Selection()
104 {
105 }
106
107 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
108 {
109   LightApp_Selection::init( context, selMgr );
110
111   myObjects.resize( count() );
112
113   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
114   if ( appStudy ) {
115     _PTR(Study) study = appStudy->studyDS();
116     for ( int idx = 0; idx < count(); idx++ ) {
117       QString anEntry = entry( idx );
118       if ( study && !anEntry.isEmpty() ) {
119         _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
120         if ( aSO ) {
121           CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
122           myObjects[idx] = GEOM::GEOM_Object::_narrow( varObj );
123         }
124       }
125     }
126   }
127 }
128
129 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
130 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
131 {
132   QVariant v;
133   if ( p == "isOCC" )
134     v = activeViewType() == OCCViewer_Viewer::Type();
135   else if ( p == "selectionmode" )
136     v = selectionMode();
137   else if ( p == "hasImported" )
138     v = hasImported();
139   else if ( p == "allImported" )
140     v = allImported();
141   else
142     v = LightApp_Selection::parameter( p );
143   return v;
144 }
145
146 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
147 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
148 {
149   QVariant v;
150   if ( p == "type" )
151     v = typeName( idx );
152   else if ( p == "typeid" )
153     v = typeId( idx );
154   else if ( p == "displaymode" )
155     v = displayMode( idx );
156   else if ( p == "isAutoColor" )
157     v = isAutoColor( idx );
158   else if ( p == "isVectorsMode" )
159     v = isVectorsMode( idx );
160   else if ( p == "topLevel" )
161     v = topLevel( idx );
162   else if ( p == "hasHiddenChildren" )
163     v = hasHiddenChildren( idx );
164   else if ( p == "hasShownChildren" )
165     v = hasShownChildren( idx );
166   else if ( p == "compoundOfVertices" )
167     v = compoundOfVertices( idx );
168   else if ( p == "imported" )
169     v = isImported( idx );
170   else if ( p == "isPhysicalMaterial" )
171     v = isPhysicalMaterial(idx);
172   else
173     v = LightApp_Selection::parameter( idx, p );
174
175   return v;
176 }
177
178 // the method to skip temporary objects from selection (called from LightApp)
179 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
180 {
181   return !theOwner->entry().contains("_");
182 }
183
184 QString GEOMGUI_Selection::typeName( const int index ) const
185 {
186   if ( isComponent( index ) )
187     return "Component";
188
189   static QString aGroup( "Group" );
190   static QString aShape( "Shape" );
191   static QString anUnknown( "Unknown" );
192
193   GEOM::GEOM_Object_var anObj = getObject( index );
194   if ( !CORBA::is_nil( anObj ) ) {
195     const int aGeomType = anObj->GetType();
196     if ( aGeomType == GEOM_GROUP )
197       return aGroup;
198     else
199       return aShape;
200   }
201   return anUnknown;
202 }
203
204 int GEOMGUI_Selection::typeId( const int index ) const
205 {
206   int aType = -1;
207   GEOM::GEOM_Object_var anObj = getObject( index );
208   if ( !CORBA::is_nil( anObj ) )
209     aType = (int)anObj->GetShapeType();
210   return aType;
211 }
212
213 bool GEOMGUI_Selection::isVisible( const int index ) const
214 {
215   bool res = false;
216
217 #ifdef USE_VISUAL_PROP_MAP
218   bool found = false;
219   QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
220   if ( v.canConvert( QVariant::Bool ) ) {
221     res = v.toBool();
222     found = true;
223   }
224
225   if ( !found ) {
226 #endif
227     GEOM::GEOM_Object_var obj = getObject( index );
228     SALOME_View* view = GEOM_Displayer::GetActiveView();
229     if ( !CORBA::is_nil( obj ) && view ) {
230       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
231       res = view->isVisible( io );
232     }
233 #ifdef USE_VISUAL_PROP_MAP
234   }
235 #endif
236
237   return res;
238 }
239
240 bool GEOMGUI_Selection::isAutoColor( const int index ) const
241 {
242   GEOM::GEOM_Object_var obj = getObject( index );
243   if ( !CORBA::is_nil( obj ) )
244     return obj->GetAutoColor();
245   return false;
246 }
247
248 bool GEOMGUI_Selection::isImported( const int index ) const
249 {
250   GEOM::GEOM_Object_var obj = getObject( index );
251   if ( !CORBA::is_nil( obj ) )
252     return obj->GetType() == GEOM_IMPORT;
253   return false;
254 }
255
256 bool GEOMGUI_Selection::hasImported() const
257 {
258   bool res = false;
259   for ( int i = 0; i < count() && !res; i++ )
260     res = isImported( i );
261   return res;
262 }
263
264 bool GEOMGUI_Selection::allImported() const
265 {
266   bool res = true;
267   for ( int i = 0; i < count() && res; i++ )
268     res = isImported( i );
269   return res;
270 }
271
272 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
273 {
274   QVariant v;
275   LightApp_Study* aStudy = study();
276   if ( aStudy ) {
277     LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
278     if ( anApp && anApp->activeViewManager() ) {
279       int id = anApp->activeViewManager()->getGlobalId();
280       v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
281     }
282   }
283   return v;
284 }
285
286 QString GEOMGUI_Selection::displayMode( const int index ) const
287 {
288   QString res;
289   QString viewType = activeViewType();
290 #ifdef USE_VISUAL_PROP_MAP
291   QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
292   if ( v.canConvert( QVariant::Int ) ) {
293     int dm = v.toInt();
294     if ( viewType == OCCViewer_Viewer::Type() ) {
295       OCC_DISPLAY_MODE_TO_STRING( res, dm );
296     } else if ( viewType == SVTK_Viewer::Type() ) {
297       VTK_DISPLAY_MODE_TO_STRING( res, dm );
298     }
299   }
300
301   if ( res.isEmpty() ) {
302 #endif
303     SALOME_View* view = GEOM_Displayer::GetActiveView();
304     if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
305       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
306       if ( prs ) {
307         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
308           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
309           AIS_ListOfInteractive lst;
310           occPrs->GetObjects( lst );
311           if ( lst.Extent() ) {
312             Handle(AIS_InteractiveObject) io = lst.First();
313             if ( !io.IsNull() ) {
314               int dm = io->DisplayMode();
315               OCC_DISPLAY_MODE_TO_STRING( res, dm );
316               if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
317                 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
318                   desktop()->activeWindow()->getViewManager()->getViewModel();
319                 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
320                 dm = ic->DisplayMode();
321                 OCC_DISPLAY_MODE_TO_STRING( res, dm );
322               }
323             }
324           }
325         }
326   else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
327           SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
328           vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
329           if ( lst ) {
330             lst->InitTraversal();
331             vtkActor* actor = lst->GetNextActor();
332             if ( actor ) {
333               SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
334               if ( salActor ) {
335                 int dm = salActor->getDisplayMode();
336                 VTK_DISPLAY_MODE_TO_STRING( res, dm );
337               } // if ( salome actor )
338             } // if ( actor )
339           } // if ( lst == vtkPrs->GetObjects() )
340         } // if VTK
341       }
342     }
343
344 #ifdef USE_VISUAL_PROP_MAP
345   }
346 #endif
347
348   return res;
349 }
350
351 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
352 {
353   bool res = false;
354
355 #ifdef USE_VISUAL_PROP_MAP
356   bool found = false;
357   QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
358   if ( v.canConvert( QVariant::Bool ) ) {
359     res = v.toBool();
360     found = true;
361   }
362
363   if ( !found ) {
364 #endif
365     SALOME_View* view = GEOM_Displayer::GetActiveView();
366     QString viewType = activeViewType();
367     if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
368       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
369       if ( prs ) {
370         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
371           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
372           AIS_ListOfInteractive lst;
373           occPrs->GetObjects( lst );
374           if ( lst.Extent() ) {
375             Handle(AIS_InteractiveObject) io = lst.First();
376             if ( !io.IsNull() ) {
377               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
378               if ( !aSh.IsNull() )
379                 res = aSh->isShowVectors();
380             }
381           }
382         } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
383           SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
384           vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
385           if ( lst ) {
386             lst->InitTraversal();
387             vtkActor* actor = lst->GetNextActor();
388             if ( actor ) {
389               GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
390               if ( aGeomActor )
391                 res = aGeomActor->GetVectorMode();
392             }
393           }
394         }
395       }
396     }
397 #ifdef USE_VISUAL_PROP_MAP
398   }
399 #endif
400
401   return res;
402 }
403
404 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
405 {
406   bool ok = false;
407   if ( obj ) {
408     _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
409     for ( ; it->More() && !ok; it->Next() ) {
410       _PTR(SObject) child = it->Value();
411       if ( child ) {
412         _PTR(SObject) refObj;
413         if ( child->ReferencedObject( refObj ) ) continue; // omit references
414         if ( child->GetName() != "" ) ok = true;
415       }
416     }
417   }
418   return ok;
419 }
420
421 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
422 {
423   bool exp = true;
424   _PTR(GenericAttribute) anAttr;
425   if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
426     _PTR(AttributeExpandable) aAttrExp = anAttr;
427     exp = aAttrExp->IsExpandable();
428   }
429   return exp;
430 }
431
432 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
433 {
434   bool ret = false;
435   /*
436   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
437   (SUIT_Session::session()->activeApplication()->activeStudy());*/
438   if ( /*appStudy && */!CORBA::is_nil( obj ) )
439     ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
440   return ret;
441 }
442
443 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
444 {
445   bool OK = false;
446   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
447
448   if ( appStudy ) {
449     QString anEntry = entry( index );
450     _PTR(Study) study = appStudy->studyDS();
451     if ( study && !anEntry.isEmpty() ) {
452       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
453       OK = !expandable( aSO ) && hasChildren( aSO );
454     }
455   }
456   return OK;
457 }
458
459 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
460 {
461   bool OK = false;
462   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
463
464   if ( appStudy )  {
465     QString anEntry = entry( index );
466     _PTR(Study) study = appStudy->studyDS();
467     if ( study && !anEntry.isEmpty() ) {
468       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
469       OK = expandable( aSO ) && hasChildren( aSO );
470     }
471   }
472   return OK;
473 }
474
475 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
476 {
477   GEOM::GEOM_Object_var obj = getObject( index );
478   return isCompoundOfVertices( obj );
479 }
480
481 bool GEOMGUI_Selection::isComponent( const int index ) const
482 {
483   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
484
485   if ( appStudy ) {
486     QString anEntry = entry( index );
487     _PTR(Study) study = appStudy->studyDS();
488     if ( study && !anEntry.isNull() ) {
489       _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
490       if ( aSO && aSO->GetFatherComponent() )
491         return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
492     }
493   }
494   return false;
495 }
496
497 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
498 {
499   GEOM::GEOM_Object_var o;
500   if ( 0 <= index && index < myObjects.size() )
501     o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
502   return o._retn();
503 }
504
505 QString GEOMGUI_Selection::selectionMode() const
506 {
507   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
508   if ( app ) {
509     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
510     if ( aGeomGUI ) {
511       switch ( aGeomGUI->getLocalSelectionMode() )
512       {
513         case GEOM_POINT      : return "VERTEX";
514         case GEOM_EDGE       : return "EDGE";
515         case GEOM_WIRE       : return "WIRE";
516         case GEOM_FACE       : return "FACE";
517         case GEOM_SHELL      : return "SHELL";
518         case GEOM_SOLID      : return "SOLID";
519         case GEOM_COMPOUND   : return "COMPOUND";
520         case GEOM_ALLOBJECTS : return "ALL";
521         default: return "";
522       }
523     }
524   }
525   return "";
526 }
527
528 bool GEOMGUI_Selection::topLevel( const int index ) const {
529   bool res = false;
530
531 #ifdef USE_VISUAL_PROP_MAP
532   bool found = false;
533   QVariant v = visibleProperty( entry( index ), TOP_LEVEL_PROP );
534   if ( v.canConvert<bool>() ) {
535     res = v.toBool();
536     found = true;
537   }
538
539   if ( !found ) {
540 #endif
541     SALOME_View* view = GEOM_Displayer::GetActiveView();
542     QString viewType = activeViewType();
543     if ( view && viewType == OCCViewer_Viewer::Type() ) {
544       SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
545       if ( prs ) {
546         if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
547           SOCC_Prs* occPrs = (SOCC_Prs*) prs;
548           AIS_ListOfInteractive lst;
549           occPrs->GetObjects( lst );
550           if ( lst.Extent() ) {
551             Handle(AIS_InteractiveObject) io = lst.First();
552             if ( !io.IsNull() ) {
553               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
554               if ( !aSh.IsNull() )
555                 res = (bool)aSh->isTopLevel();
556             }
557           }
558         }
559       }
560     }
561   }
562   return res;
563 }
564
565 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const{
566    bool res = false;
567
568 #ifdef USE_VISUAL_PROP_MAP
569    bool found = false;
570    QVariant v = visibleProperty( entry( idx ), MATERIAL_PROP );
571    if ( v.canConvert<QString>() ) {
572      Material_Model* aModel = Material_Model::getMaterialModel( v.toString().split(DIGIT_SEPARATOR) );
573      res = aModel->isPhysical();
574      found = true;
575    }
576
577    if ( !found ) {
578 #endif
579      SALOME_View* view = GEOM_Displayer::GetActiveView();
580      QString viewType = activeViewType();
581      if ( view ) {
582        SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
583        if ( prs ) {
584          if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
585            SOCC_Prs* occPrs = (SOCC_Prs*) prs;
586            AIS_ListOfInteractive lst;
587            occPrs->GetObjects( lst );
588            if ( lst.Extent() ) {
589              Handle(AIS_InteractiveObject) io = lst.First();
590              if ( !io.IsNull() ) {
591                Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
592                if ( !aSh.IsNull() )
593                  res = (bool) aSh->Attributes()->ShadingAspect()->
594                          Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
595              }
596            }
597          }
598          else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
599                 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
600                 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
601                 if ( lst ) {
602                   lst->InitTraversal();
603                   vtkActor* actor = lst->GetNextActor();
604                   if ( actor ) {
605               GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
606                     if ( aGeomGActor ) {
607                 GEOM_VTKPropertyMaterial* mat  = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
608                 res = mat->GetPhysical();
609                     } // if ( salome actor )
610                   } // if ( actor )
611           } // if ( lst == vtkPrs->GetObjects() )
612          }
613        }
614      }
615    }
616    return res;
617 }