1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : GEOMGUI_Selection.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
26 #include "GEOMGUI_Selection.h"
28 #include "GeometryGUI.h"
29 #include "GEOM_Displayer.h"
31 #include "Material_Model.h"
33 #include <GEOM_Constants.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
38 #include "LightApp_DataOwner.h"
40 #include <SUIT_Session.h>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_ViewWindow.h>
43 #include <SUIT_ViewManager.h>
45 #include <SALOME_Prs.h>
46 #include <SALOME_InteractiveObject.hxx>
50 #include <SALOME_Actor.h>
51 #include <GEOM_Actor.h>
53 #include <OCCViewer_ViewModel.h>
54 #include <SVTK_ViewModel.h>
56 #include <GEOMImpl_Types.hxx>
58 #include <GEOM_AISShape.hxx>
59 #include <GEOM_VTKPropertyMaterial.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>
72 #include <vtkActorCollection.h>
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" ); \
86 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
88 str = QString( "Wireframe" ); \
90 str = QString( "Shading" ); \
92 str = QString( "ShadingWithEdges" ); \
96 #define USE_VISUAL_PROP_MAP
98 GEOMGUI_Selection::GEOMGUI_Selection()
99 : LightApp_Selection()
103 GEOMGUI_Selection::~GEOMGUI_Selection()
107 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
109 LightApp_Selection::init( context, selMgr );
111 myObjects.resize( count() );
113 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
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() ) );
121 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
122 myObjects[idx] = GEOM::GEOM_Object::_narrow( varObj );
129 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
130 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
134 v = activeViewType() == OCCViewer_Viewer::Type();
135 else if ( p == "selectionmode" )
137 else if ( p == "hasImported" )
139 else if ( p == "allImported" )
142 v = LightApp_Selection::parameter( p );
146 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
147 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
152 else if ( p == "typeid" )
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" )
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);
173 v = LightApp_Selection::parameter( idx, p );
178 // the method to skip temporary objects from selection (called from LightApp)
179 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
181 return !theOwner->entry().contains("_");
184 QString GEOMGUI_Selection::typeName( const int index ) const
186 if ( isComponent( index ) )
189 static QString aGroup( "Group" );
190 static QString aShape( "Shape" );
191 static QString anUnknown( "Unknown" );
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 )
204 int GEOMGUI_Selection::typeId( const int index ) const
207 GEOM::GEOM_Object_var anObj = getObject( index );
208 if ( !CORBA::is_nil( anObj ) )
209 aType = (int)anObj->GetShapeType();
213 bool GEOMGUI_Selection::isVisible( const int index ) const
217 #ifdef USE_VISUAL_PROP_MAP
219 QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
220 if ( v.canConvert( QVariant::Bool ) ) {
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 );
233 #ifdef USE_VISUAL_PROP_MAP
240 bool GEOMGUI_Selection::isAutoColor( const int index ) const
242 GEOM::GEOM_Object_var obj = getObject( index );
243 if ( !CORBA::is_nil( obj ) )
244 return obj->GetAutoColor();
248 bool GEOMGUI_Selection::isImported( const int index ) const
250 GEOM::GEOM_Object_var obj = getObject( index );
251 if ( !CORBA::is_nil( obj ) )
252 return obj->GetType() == GEOM_IMPORT;
256 bool GEOMGUI_Selection::hasImported() const
259 for ( int i = 0; i < count() && !res; i++ )
260 res = isImported( i );
264 bool GEOMGUI_Selection::allImported() const
267 for ( int i = 0; i < count() && res; i++ )
268 res = isImported( i );
272 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
275 LightApp_Study* aStudy = study();
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() );
286 QString GEOMGUI_Selection::displayMode( const int index ) const
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 ) ) {
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 );
301 if ( res.isEmpty() ) {
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() );
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() ) {
315 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
317 dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
319 dm = io->DisplayMode();
321 OCC_DISPLAY_MODE_TO_STRING( res, dm );
322 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
323 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
324 desktop()->activeWindow()->getViewManager()->getViewModel();
325 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
326 dm = ic->DisplayMode();
327 OCC_DISPLAY_MODE_TO_STRING( res, dm );
332 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
333 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
334 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
336 lst->InitTraversal();
337 vtkActor* actor = lst->GetNextActor();
339 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
341 int dm = salActor->getDisplayMode();
342 VTK_DISPLAY_MODE_TO_STRING( res, dm );
343 } // if ( salome actor )
345 } // if ( lst == vtkPrs->GetObjects() )
350 #ifdef USE_VISUAL_PROP_MAP
357 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
361 #ifdef USE_VISUAL_PROP_MAP
363 QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
364 if ( v.canConvert( QVariant::Bool ) ) {
371 SALOME_View* view = GEOM_Displayer::GetActiveView();
372 QString viewType = activeViewType();
373 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
374 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
376 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
377 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
378 AIS_ListOfInteractive lst;
379 occPrs->GetObjects( lst );
380 if ( lst.Extent() ) {
381 Handle(AIS_InteractiveObject) io = lst.First();
382 if ( !io.IsNull() ) {
383 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
385 res = aSh->isShowVectors();
388 } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
389 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
390 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
392 lst->InitTraversal();
393 vtkActor* actor = lst->GetNextActor();
395 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
397 res = aGeomActor->GetVectorMode();
403 #ifdef USE_VISUAL_PROP_MAP
410 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
414 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
415 for ( ; it->More() && !ok; it->Next() ) {
416 _PTR(SObject) child = it->Value();
418 _PTR(SObject) refObj;
419 if ( child->ReferencedObject( refObj ) ) continue; // omit references
420 if ( child->GetName() != "" ) ok = true;
427 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
430 _PTR(GenericAttribute) anAttr;
431 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
432 _PTR(AttributeExpandable) aAttrExp = anAttr;
433 exp = aAttrExp->IsExpandable();
438 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
442 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
443 (SUIT_Session::session()->activeApplication()->activeStudy());*/
444 if ( /*appStudy && */!CORBA::is_nil( obj ) )
445 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
449 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
452 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
455 QString anEntry = entry( index );
456 _PTR(Study) study = appStudy->studyDS();
457 if ( study && !anEntry.isEmpty() ) {
458 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
459 OK = !expandable( aSO ) && hasChildren( aSO );
465 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
468 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
471 QString anEntry = entry( index );
472 _PTR(Study) study = appStudy->studyDS();
473 if ( study && !anEntry.isEmpty() ) {
474 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
475 OK = expandable( aSO ) && hasChildren( aSO );
481 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
483 GEOM::GEOM_Object_var obj = getObject( index );
484 return isCompoundOfVertices( obj );
487 bool GEOMGUI_Selection::isComponent( const int index ) const
489 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
492 QString anEntry = entry( index );
493 _PTR(Study) study = appStudy->studyDS();
494 if ( study && !anEntry.isNull() ) {
495 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
496 if ( aSO && aSO->GetFatherComponent() )
497 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
503 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
505 GEOM::GEOM_Object_var o;
506 if ( 0 <= index && index < myObjects.size() )
507 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
511 QString GEOMGUI_Selection::selectionMode() const
513 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
515 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
517 switch ( aGeomGUI->getLocalSelectionMode() )
519 case GEOM_POINT : return "VERTEX";
520 case GEOM_EDGE : return "EDGE";
521 case GEOM_WIRE : return "WIRE";
522 case GEOM_FACE : return "FACE";
523 case GEOM_SHELL : return "SHELL";
524 case GEOM_SOLID : return "SOLID";
525 case GEOM_COMPOUND : return "COMPOUND";
526 case GEOM_ALLOBJECTS : return "ALL";
534 bool GEOMGUI_Selection::topLevel( const int index ) const {
537 #ifdef USE_VISUAL_PROP_MAP
539 QVariant v = visibleProperty( entry( index ), TOP_LEVEL_PROP );
540 if ( v.canConvert<bool>() ) {
547 SALOME_View* view = GEOM_Displayer::GetActiveView();
548 QString viewType = activeViewType();
549 if ( view && viewType == OCCViewer_Viewer::Type() ) {
550 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
552 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
553 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
554 AIS_ListOfInteractive lst;
555 occPrs->GetObjects( lst );
556 if ( lst.Extent() ) {
557 Handle(AIS_InteractiveObject) io = lst.First();
558 if ( !io.IsNull() ) {
559 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
561 res = (bool)aSh->isTopLevel();
571 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const{
574 #ifdef USE_VISUAL_PROP_MAP
576 QVariant v = visibleProperty( entry( idx ), MATERIAL_PROP );
577 if ( v.canConvert<QString>() ) {
578 Material_Model material;
579 material.fromProperties( v.toString() );
580 res = material.isPhysical();
586 SALOME_View* view = GEOM_Displayer::GetActiveView();
587 QString viewType = activeViewType();
589 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
591 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
592 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
593 AIS_ListOfInteractive lst;
594 occPrs->GetObjects( lst );
595 if ( lst.Extent() ) {
596 Handle(AIS_InteractiveObject) io = lst.First();
597 if ( !io.IsNull() ) {
598 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
600 res = (bool) aSh->Attributes()->ShadingAspect()->
601 Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
605 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
606 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
607 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
609 lst->InitTraversal();
610 vtkActor* actor = lst->GetNextActor();
612 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
614 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
615 res = mat->GetPhysical();
616 } // if ( salome actor )
618 } // if ( lst == vtkPrs->GetObjects() )