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