1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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"
27 #include <GEOMGUI_DimensionProperty.h>
29 #include "GeometryGUI.h"
30 #include "GEOM_Displayer.h"
32 #include "Material_Model.h"
34 #include <GEOM_Constants.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
39 #include "LightApp_DataOwner.h"
41 #include <SUIT_Session.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_ViewWindow.h>
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_ResourceMgr.h>
47 #include <SALOME_Prs.h>
48 #include <SALOME_InteractiveObject.hxx>
52 #include <SALOME_Actor.h>
53 #include <GEOM_Actor.h>
55 #include <OCCViewer_ViewModel.h>
56 #include <SVTK_ViewModel.h>
58 #include <GEOMImpl_Types.hxx>
60 #include <GEOM_AISShape.hxx>
61 #include <GEOM_VTKPropertyMaterial.hxx>
65 #include <AIS_InteractiveObject.hxx>
66 #include <AIS_ListOfInteractive.hxx>
67 #include <AIS_GraphicTool.hxx>
68 #include <AIS_Drawer.hxx>
69 #include <Aspect_TypeOfFacingModel.hxx>
70 #include <Prs3d_ShadingAspect.hxx>
71 #include<Graphic3d_MaterialAspect.hxx>
74 #include <vtkActorCollection.h>
76 #define OCC_DISPLAY_MODE_TO_STRING( str, dm ) { \
77 if ( dm == AIS_WireFrame ) \
78 str = QString( "Wireframe" ); \
79 else if ( dm == AIS_Shaded ) \
80 str = QString( "Shading" ); \
81 else if ( dm == GEOM_AISShape::ShadingWithEdges ) \
82 str = QString( "ShadingWithEdges" ); \
83 else if ( dm == GEOM_AISShape::TexturedShape ) \
84 str = QString( "Texture" ); \
88 #define USE_VISUAL_PROP_MAP
90 #ifdef USE_VISUAL_PROP_MAP
91 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
93 str = QString( "Wireframe" ); \
95 str = QString( "Shading" ); \
97 str = QString( "ShadingWithEdges" ); \
101 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
103 str = QString( "Wireframe" ); \
104 else if ( dm == 1 ) \
105 str = QString( "Shading" ); \
106 else if ( dm == 3 ) \
107 str = QString( "ShadingWithEdges" ); \
112 GEOMGUI_Selection::GEOMGUI_Selection()
113 : LightApp_Selection()
117 GEOMGUI_Selection::~GEOMGUI_Selection()
121 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
123 LightApp_Selection::init( context, selMgr );
125 myObjects.resize( count() );
127 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
129 _PTR(Study) study = appStudy->studyDS();
130 for ( int idx = 0; idx < count(); idx++ ) {
131 QString anEntry = entry( idx );
132 if ( study && !anEntry.isEmpty() ) {
133 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
135 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
136 myObjects[idx] = GEOM::GEOM_BaseObject::_narrow( varObj );
143 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
144 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
148 v = activeViewType() == OCCViewer_Viewer::Type();
149 else if ( p == "selectionmode" )
151 else if ( p == "hasImported" )
153 else if ( p == "allImported" )
156 v = LightApp_Selection::parameter( p );
160 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
161 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
166 else if ( p == "typeid" )
168 else if ( p == "displaymode" )
169 v = displayMode( idx );
170 else if ( p == "isAutoColor" )
171 v = isAutoColor( idx );
172 else if ( p == "isVectorsMode" )
173 v = isVectorsMode( idx );
174 else if ( p == "topLevel" )
176 else if ( p == "autoBringToFront" )
177 v = autoBringToFront( idx );
178 else if ( p == "hasChildren" )
179 v = hasChildren( idx );
180 else if ( p == "nbChildren" )
182 else if ( p == "hasConcealedChildren" )
183 v = hasConcealedChildren( idx );
184 else if ( p == "hasDisclosedChildren" )
185 v = hasDisclosedChildren( idx );
186 else if ( p == "compoundOfVertices" )
187 v = compoundOfVertices( idx );
188 else if ( p == "imported" )
189 v = isImported( idx );
190 else if ( p == "isPhysicalMaterial" )
191 v = isPhysicalMaterial(idx);
192 else if ( p == "isFolder" )
194 else if ( p == "hasHiddenDimensions" )
195 v = hasHiddenDimensions(idx);
196 else if ( p == "hasVisibleDimensions" )
197 v = hasVisibleDimensions(idx);
199 v = LightApp_Selection::parameter( idx, p );
204 // the method to skip temporary objects from selection (called from LightApp)
205 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
207 return !theOwner->entry().contains("_");
210 QString GEOMGUI_Selection::typeName( const int index ) const
212 if ( isComponent( index ) )
214 if ( isFolder( index ) )
217 static QString aGroup( "Group" );
218 static QString aShape( "Shape" );
219 static QString aField( "Field" );
220 static QString aFieldStep( "FieldStep" );
221 static QString anUnknown( "Unknown" );
223 GEOM::GEOM_BaseObject_var anObj = getBaseObject( index );
224 if ( !CORBA::is_nil( anObj ) ) {
225 const int aGeomType = anObj->GetType();
226 switch ( aGeomType ) {
227 case GEOM_GROUP : return aGroup;
228 case GEOM_FIELD : return aField;
229 case GEOM_FIELD_STEP: return aFieldStep;
230 default : return aShape;
236 int GEOMGUI_Selection::typeId( const int index ) const
239 GEOM::GEOM_Object_var anObj = getObject( index );
240 if ( !CORBA::is_nil( anObj ) )
241 aType = (int)anObj->GetShapeType();
245 bool GEOMGUI_Selection::isVisible( const int index ) const
247 #ifdef USE_VISUAL_PROP_MAP
248 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) );
249 if ( v.canConvert( QVariant::Bool ) )
255 GEOM::GEOM_Object_var obj = getObject( index );
256 SALOME_View* view = GEOM_Displayer::GetActiveView();
257 if ( !CORBA::is_nil( obj ) && view ) {
258 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
259 res = view->isVisible( io );
265 bool GEOMGUI_Selection::isAutoColor( const int index ) const
267 GEOM::GEOM_Object_var obj = getObject( index );
268 if ( !CORBA::is_nil( obj ) )
269 return obj->GetAutoColor();
273 bool GEOMGUI_Selection::isImported( const int index ) const
275 GEOM::GEOM_Object_var obj = getObject( index );
276 if ( !CORBA::is_nil( obj ) )
277 return obj->GetType() == GEOM_IMPORT;
281 bool GEOMGUI_Selection::hasImported() const
284 for ( int i = 0; i < count() && !res; i++ )
285 res = isImported( i );
289 bool GEOMGUI_Selection::allImported() const
292 for ( int i = 0; i < count() && res; i++ )
293 res = isImported( i );
297 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
300 LightApp_Study* aStudy = study();
302 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
303 if ( anApp && anApp->activeViewManager() ) {
304 int id = anApp->activeViewManager()->getGlobalId();
305 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
311 QString GEOMGUI_Selection::displayMode( const int index ) const
314 QString viewType = activeViewType();
316 #ifdef USE_VISUAL_PROP_MAP
317 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) );
318 if ( v.canConvert( QVariant::Int ) ) {
320 if ( viewType == OCCViewer_Viewer::Type() ) {
321 OCC_DISPLAY_MODE_TO_STRING( res, dm );
322 } else if ( viewType == SVTK_Viewer::Type() ) {
323 VTK_DISPLAY_MODE_TO_STRING( res, dm );
329 SALOME_View* view = GEOM_Displayer::GetActiveView();
330 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
331 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
333 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
334 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
335 AIS_ListOfInteractive lst;
336 occPrs->GetObjects( lst );
337 if ( lst.Extent() ) {
338 Handle(AIS_InteractiveObject) io = lst.First();
339 if ( !io.IsNull() ) {
341 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
343 dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
345 dm = io->DisplayMode();
347 OCC_DISPLAY_MODE_TO_STRING( res, dm );
348 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
349 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
350 desktop()->activeWindow()->getViewManager()->getViewModel();
351 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
352 dm = ic->DisplayMode();
353 OCC_DISPLAY_MODE_TO_STRING( res, dm );
358 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
359 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
360 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
362 lst->InitTraversal();
363 vtkActor* actor = lst->GetNextActor();
365 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
367 int dm = salActor->getDisplayMode();
368 VTK_DISPLAY_MODE_TO_STRING( res, dm );
369 } // if ( salome actor )
371 } // if ( lst == vtkPrs->GetObjects() )
379 bool GEOMGUI_Selection::autoBringToFront( const int /*index*/ ) const
381 return SUIT_Session::session()->resourceMgr()->booleanValue( "Geometry", "auto_bring_to_front" );
384 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
386 #ifdef USE_VISUAL_PROP_MAP
387 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) );
388 if ( v.canConvert( QVariant::Bool ) )
394 SALOME_View* view = GEOM_Displayer::GetActiveView();
395 QString viewType = activeViewType();
396 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
397 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
399 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
400 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
401 AIS_ListOfInteractive lst;
402 occPrs->GetObjects( lst );
403 if ( lst.Extent() ) {
404 Handle(AIS_InteractiveObject) io = lst.First();
405 if ( !io.IsNull() ) {
406 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
408 res = aSh->isShowVectors();
412 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
413 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
414 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
416 lst->InitTraversal();
417 vtkActor* actor = lst->GetNextActor();
419 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
421 res = aGeomActor->GetVectorMode();
431 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
434 // as soon as Use Case browser data tree was added
435 return obj->GetStudy()->GetUseCaseBuilder()->HasChildren( obj );
439 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
442 _PTR(GenericAttribute) anAttr;
443 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
444 _PTR(AttributeExpandable) aAttrExp = anAttr;
445 exp = aAttrExp->IsExpandable();
450 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
454 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
455 (SUIT_Session::session()->activeApplication()->activeStudy());*/
456 if ( /*appStudy && */!CORBA::is_nil( obj ) )
457 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
461 bool GEOMGUI_Selection::isFolder( const _PTR(SObject)& obj )
464 _PTR(GenericAttribute) anAttr;
465 if ( obj && obj->FindAttribute(anAttr, "AttributeLocalID") ) {
466 _PTR(AttributeLocalID) aLocalID( anAttr );
467 ret = aLocalID->Value() == 999;
472 bool GEOMGUI_Selection::hasChildren( const int index ) const
475 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
478 QString anEntry = entry( index );
479 _PTR(Study) study = appStudy->studyDS();
480 if ( study && !anEntry.isEmpty() ) {
481 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
482 ok = hasChildren( aSO );
488 int GEOMGUI_Selection::nbChildren( const int index ) const
491 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
494 QString anEntry = entry( index );
495 _PTR(Study) study = appStudy->studyDS();
496 if ( study && !anEntry.isEmpty() ) {
497 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
498 if ( aSO && aSO->GetStudy()->GetUseCaseBuilder()->IsUseCaseNode(aSO) ) {
499 _PTR(UseCaseIterator) it = aSO->GetStudy()->GetUseCaseBuilder()->GetUseCaseIterator( aSO );
500 for (it->Init(false); it->More(); it->Next()) nb++;
507 bool GEOMGUI_Selection::hasConcealedChildren( const int index ) const
510 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
513 QString anEntry = entry( index );
514 _PTR(Study) study = appStudy->studyDS();
515 if ( study && !anEntry.isEmpty() ) {
516 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
517 OK = !expandable( aSO ) && hasChildren( aSO );
523 bool GEOMGUI_Selection::hasDisclosedChildren( const int index ) const
526 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
529 QString anEntry = entry( index );
530 _PTR(Study) study = appStudy->studyDS();
531 if ( study && !anEntry.isEmpty() ) {
532 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
533 OK = expandable( aSO ) && hasChildren( aSO );
539 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
541 GEOM::GEOM_Object_var obj = getObject( index );
542 return isCompoundOfVertices( obj );
545 bool GEOMGUI_Selection::isComponent( const int index ) const
547 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
550 QString anEntry = entry( index );
551 _PTR(Study) study = appStudy->studyDS();
552 if ( study && !anEntry.isNull() ) {
553 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
554 if ( aSO && aSO->GetFatherComponent() )
555 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
561 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
563 GEOM::GEOM_Object_var o;
564 if ( 0 <= index && index < myObjects.size() )
565 o = GEOM::GEOM_Object::_narrow( myObjects[index] );
569 GEOM::GEOM_BaseObject_ptr GEOMGUI_Selection::getBaseObject( const int index ) const
571 GEOM::GEOM_BaseObject_var o;
572 if ( 0 <= index && index < myObjects.size() )
573 o = GEOM::GEOM_BaseObject::_duplicate( myObjects[index] );
577 QString GEOMGUI_Selection::selectionMode() const
579 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
581 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
583 switch ( aGeomGUI->getLocalSelectionMode() )
585 case GEOM_POINT : return "VERTEX";
586 case GEOM_EDGE : return "EDGE";
587 case GEOM_WIRE : return "WIRE";
588 case GEOM_FACE : return "FACE";
589 case GEOM_SHELL : return "SHELL";
590 case GEOM_SOLID : return "SOLID";
591 case GEOM_COMPOUND : return "COMPOUND";
592 case GEOM_ALLOBJECTS : return "ALL";
600 bool GEOMGUI_Selection::topLevel( const int index ) const
602 #ifdef USE_VISUAL_PROP_MAP
603 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) );
604 if ( v.canConvert<bool>() )
610 SALOME_View* view = GEOM_Displayer::GetActiveView();
611 QString viewType = activeViewType();
612 if ( view && viewType == OCCViewer_Viewer::Type() ) {
613 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
615 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
616 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
617 AIS_ListOfInteractive lst;
618 occPrs->GetObjects( lst );
619 if ( lst.Extent() ) {
620 Handle(AIS_InteractiveObject) io = lst.First();
621 if ( !io.IsNull() ) {
622 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
624 res = (bool)aSh->isTopLevel();
633 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const
635 #ifdef USE_VISUAL_PROP_MAP
636 QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) );
637 if ( v.canConvert<QString>() ) {
638 Material_Model material;
639 material.fromProperties( v.toString() );
640 return material.isPhysical();
646 SALOME_View* view = GEOM_Displayer::GetActiveView();
647 QString viewType = activeViewType();
649 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
651 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
652 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
653 AIS_ListOfInteractive lst;
654 occPrs->GetObjects( lst );
655 if ( lst.Extent() ) {
656 Handle(AIS_InteractiveObject) io = lst.First();
657 if ( !io.IsNull() ) {
658 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
660 res = (bool) aSh->Attributes()->ShadingAspect()->
661 Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
665 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
666 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
667 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
669 lst->InitTraversal();
670 vtkActor* actor = lst->GetNextActor();
672 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
674 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
675 res = mat->GetPhysical();
676 } // if ( salome actor )
678 } // if ( lst == vtkPrs->GetObjects() )
686 bool GEOMGUI_Selection::isFolder( const int index ) const
689 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
692 QString anEntry = entry( index );
693 _PTR(Study) study = appStudy->studyDS();
694 if ( study && !anEntry.isNull() ) {
695 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
696 if ( aSO ) res = isFolder( aSO );
702 bool GEOMGUI_Selection::hasDimensions( const int theIndex, bool& theHidden, bool& theVisible ) const
704 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
711 QString anEntry = entry( theIndex );
712 _PTR(Study) aStudy = appStudy->studyDS();
713 if ( !aStudy || anEntry.isNull() )
718 GEOMGUI_DimensionProperty aDimensions( appStudy, anEntry.toStdString() );
723 for ( int it = 0; it < aDimensions.GetNumber(); ++it )
725 if ( aDimensions.IsVisible( it ) )
731 return aDimensions.GetNumber() > 0;
734 bool GEOMGUI_Selection::hasHiddenDimensions( const int theIndex ) const
736 bool isAnyVisible = false;
737 bool isAnyHidden = false;
738 if ( !hasDimensions( theIndex, isAnyHidden, isAnyVisible ) )
746 bool GEOMGUI_Selection::hasVisibleDimensions( const int theIndex ) const
748 bool isAnyVisible = false;
749 bool isAnyHidden = false;
750 if ( !hasDimensions( theIndex, isAnyHidden, isAnyVisible ) )