1 // Copyright (C) 2007-2013 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>
44 #include <SUIT_ResourceMgr.h>
46 #include <SALOME_Prs.h>
47 #include <SALOME_InteractiveObject.hxx>
51 #include <SALOME_Actor.h>
52 #include <GEOM_Actor.h>
54 #include <OCCViewer_ViewModel.h>
55 #include <SVTK_ViewModel.h>
57 #include <GEOMImpl_Types.hxx>
59 #include <GEOM_AISShape.hxx>
60 #include <GEOM_VTKPropertyMaterial.hxx>
64 #include <AIS_InteractiveObject.hxx>
65 #include <AIS_ListOfInteractive.hxx>
66 #include <AIS_GraphicTool.hxx>
67 #include <AIS_Drawer.hxx>
68 #include <Aspect_TypeOfFacingModel.hxx>
69 #include <Prs3d_ShadingAspect.hxx>
70 #include<Graphic3d_MaterialAspect.hxx>
73 #include <vtkActorCollection.h>
75 #define OCC_DISPLAY_MODE_TO_STRING( str, dm ) { \
76 if ( dm == AIS_WireFrame ) \
77 str = QString( "Wireframe" ); \
78 else if ( dm == AIS_Shaded ) \
79 str = QString( "Shading" ); \
80 else if ( dm == GEOM_AISShape::ShadingWithEdges ) \
81 str = QString( "ShadingWithEdges" ); \
82 else if ( dm == GEOM_AISShape::TexturedShape ) \
83 str = QString( "Texture" ); \
87 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
89 str = QString( "Wireframe" ); \
91 str = QString( "Shading" ); \
93 str = QString( "ShadingWithEdges" ); \
97 #define USE_VISUAL_PROP_MAP
99 GEOMGUI_Selection::GEOMGUI_Selection()
100 : LightApp_Selection()
104 GEOMGUI_Selection::~GEOMGUI_Selection()
108 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
110 LightApp_Selection::init( context, selMgr );
112 myObjects.resize( count() );
114 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
116 _PTR(Study) study = appStudy->studyDS();
117 for ( int idx = 0; idx < count(); idx++ ) {
118 QString anEntry = entry( idx );
119 if ( study && !anEntry.isEmpty() ) {
120 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
122 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
123 myObjects[idx] = GEOM::GEOM_BaseObject::_narrow( varObj );
130 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
131 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
135 v = activeViewType() == OCCViewer_Viewer::Type();
136 else if ( p == "selectionmode" )
138 else if ( p == "hasImported" )
140 else if ( p == "allImported" )
143 v = LightApp_Selection::parameter( p );
147 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
148 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
153 else if ( p == "typeid" )
155 else if ( p == "displaymode" )
156 v = displayMode( idx );
157 else if ( p == "isAutoColor" )
158 v = isAutoColor( idx );
159 else if ( p == "isVectorsMode" )
160 v = isVectorsMode( idx );
161 else if ( p == "topLevel" )
163 else if ( p == "autoBringToFront" )
164 v = autoBringToFront( idx );
165 else if ( p == "hasChildren" )
166 v = hasChildren( idx );
167 else if ( p == "nbChildren" )
169 else if ( p == "hasConcealedChildren" )
170 v = hasConcealedChildren( idx );
171 else if ( p == "hasDisclosedChildren" )
172 v = hasDisclosedChildren( idx );
173 else if ( p == "compoundOfVertices" )
174 v = compoundOfVertices( idx );
175 else if ( p == "imported" )
176 v = isImported( idx );
177 else if ( p == "isPhysicalMaterial" )
178 v = isPhysicalMaterial(idx);
179 else if ( p == "isFolder" )
182 v = LightApp_Selection::parameter( idx, p );
187 // the method to skip temporary objects from selection (called from LightApp)
188 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
190 return !theOwner->entry().contains("_");
193 QString GEOMGUI_Selection::typeName( const int index ) const
195 if ( isComponent( index ) )
197 if ( isFolder( index ) )
200 static QString aGroup( "Group" );
201 static QString aShape( "Shape" );
202 static QString aField( "Field" );
203 static QString aFieldStep( "FieldStep" );
204 static QString anUnknown( "Unknown" );
206 GEOM::GEOM_BaseObject_var anObj = getBaseObject( index );
207 if ( !CORBA::is_nil( anObj ) ) {
208 const int aGeomType = anObj->GetType();
209 switch ( aGeomType ) {
210 case GEOM_GROUP : return aGroup;
211 case GEOM_FIELD : return aField;
212 case GEOM_FIELD_STEP: return aFieldStep;
213 default : return aShape;
219 int GEOMGUI_Selection::typeId( const int index ) const
222 GEOM::GEOM_Object_var anObj = getObject( index );
223 if ( !CORBA::is_nil( anObj ) )
224 aType = (int)anObj->GetShapeType();
228 bool GEOMGUI_Selection::isVisible( const int index ) const
230 #ifdef USE_VISUAL_PROP_MAP
231 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) );
232 if ( v.canConvert( QVariant::Bool ) )
238 GEOM::GEOM_Object_var obj = getObject( index );
239 SALOME_View* view = GEOM_Displayer::GetActiveView();
240 if ( !CORBA::is_nil( obj ) && view ) {
241 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
242 res = view->isVisible( io );
248 bool GEOMGUI_Selection::isAutoColor( const int index ) const
250 GEOM::GEOM_Object_var obj = getObject( index );
251 if ( !CORBA::is_nil( obj ) )
252 return obj->GetAutoColor();
256 bool GEOMGUI_Selection::isImported( const int index ) const
258 GEOM::GEOM_Object_var obj = getObject( index );
259 if ( !CORBA::is_nil( obj ) )
260 return obj->GetType() == GEOM_IMPORT;
264 bool GEOMGUI_Selection::hasImported() const
267 for ( int i = 0; i < count() && !res; i++ )
268 res = isImported( i );
272 bool GEOMGUI_Selection::allImported() const
275 for ( int i = 0; i < count() && res; i++ )
276 res = isImported( i );
280 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
283 LightApp_Study* aStudy = study();
285 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
286 if ( anApp && anApp->activeViewManager() ) {
287 int id = anApp->activeViewManager()->getGlobalId();
288 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
294 QString GEOMGUI_Selection::displayMode( const int index ) const
297 QString viewType = activeViewType();
299 #ifdef USE_VISUAL_PROP_MAP
300 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) );
301 if ( v.canConvert( QVariant::Int ) ) {
303 if ( viewType == OCCViewer_Viewer::Type() ) {
304 OCC_DISPLAY_MODE_TO_STRING( res, dm );
305 } else if ( viewType == SVTK_Viewer::Type() ) {
306 VTK_DISPLAY_MODE_TO_STRING( res, dm );
312 SALOME_View* view = GEOM_Displayer::GetActiveView();
313 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
314 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
316 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
317 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
318 AIS_ListOfInteractive lst;
319 occPrs->GetObjects( lst );
320 if ( lst.Extent() ) {
321 Handle(AIS_InteractiveObject) io = lst.First();
322 if ( !io.IsNull() ) {
324 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
326 dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
328 dm = io->DisplayMode();
330 OCC_DISPLAY_MODE_TO_STRING( res, dm );
331 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
332 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
333 desktop()->activeWindow()->getViewManager()->getViewModel();
334 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
335 dm = ic->DisplayMode();
336 OCC_DISPLAY_MODE_TO_STRING( res, dm );
341 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
342 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
343 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
345 lst->InitTraversal();
346 vtkActor* actor = lst->GetNextActor();
348 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
350 int dm = salActor->getDisplayMode();
351 VTK_DISPLAY_MODE_TO_STRING( res, dm );
352 } // if ( salome actor )
354 } // if ( lst == vtkPrs->GetObjects() )
362 bool GEOMGUI_Selection::autoBringToFront( const int /*index*/ ) const
364 return SUIT_Session::session()->resourceMgr()->booleanValue( "Geometry", "auto_bring_to_front" );
367 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
369 #ifdef USE_VISUAL_PROP_MAP
370 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) );
371 if ( v.canConvert( QVariant::Bool ) )
377 SALOME_View* view = GEOM_Displayer::GetActiveView();
378 QString viewType = activeViewType();
379 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
380 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
382 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
383 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
384 AIS_ListOfInteractive lst;
385 occPrs->GetObjects( lst );
386 if ( lst.Extent() ) {
387 Handle(AIS_InteractiveObject) io = lst.First();
388 if ( !io.IsNull() ) {
389 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
391 res = aSh->isShowVectors();
395 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
396 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
397 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
399 lst->InitTraversal();
400 vtkActor* actor = lst->GetNextActor();
402 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
404 res = aGeomActor->GetVectorMode();
414 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
417 // as soon as Use Case browser data tree was added
418 return obj->GetStudy()->GetUseCaseBuilder()->HasChildren( obj );
422 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
425 _PTR(GenericAttribute) anAttr;
426 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
427 _PTR(AttributeExpandable) aAttrExp = anAttr;
428 exp = aAttrExp->IsExpandable();
433 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
437 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
438 (SUIT_Session::session()->activeApplication()->activeStudy());*/
439 if ( /*appStudy && */!CORBA::is_nil( obj ) )
440 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
444 bool GEOMGUI_Selection::isFolder( const _PTR(SObject)& obj )
447 _PTR(GenericAttribute) anAttr;
448 if ( obj && obj->FindAttribute(anAttr, "AttributeLocalID") ) {
449 _PTR(AttributeLocalID) aLocalID( anAttr );
450 ret = aLocalID->Value() == 999;
455 bool GEOMGUI_Selection::hasChildren( const int index ) const
458 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
461 QString anEntry = entry( index );
462 _PTR(Study) study = appStudy->studyDS();
463 if ( study && !anEntry.isEmpty() ) {
464 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
465 ok = hasChildren( aSO );
471 int GEOMGUI_Selection::nbChildren( const int index ) const
474 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
477 QString anEntry = entry( index );
478 _PTR(Study) study = appStudy->studyDS();
479 if ( study && !anEntry.isEmpty() ) {
480 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
481 if ( aSO && aSO->GetStudy()->GetUseCaseBuilder()->IsUseCaseNode(aSO) ) {
482 _PTR(UseCaseIterator) it = aSO->GetStudy()->GetUseCaseBuilder()->GetUseCaseIterator( aSO );
483 for (it->Init(false); it->More(); it->Next()) nb++;
490 bool GEOMGUI_Selection::hasConcealedChildren( const int index ) const
493 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
496 QString anEntry = entry( index );
497 _PTR(Study) study = appStudy->studyDS();
498 if ( study && !anEntry.isEmpty() ) {
499 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
500 OK = !expandable( aSO ) && hasChildren( aSO );
506 bool GEOMGUI_Selection::hasDisclosedChildren( const int index ) const
509 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
512 QString anEntry = entry( index );
513 _PTR(Study) study = appStudy->studyDS();
514 if ( study && !anEntry.isEmpty() ) {
515 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
516 OK = expandable( aSO ) && hasChildren( aSO );
522 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
524 GEOM::GEOM_Object_var obj = getObject( index );
525 return isCompoundOfVertices( obj );
528 bool GEOMGUI_Selection::isComponent( const int index ) const
530 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
533 QString anEntry = entry( index );
534 _PTR(Study) study = appStudy->studyDS();
535 if ( study && !anEntry.isNull() ) {
536 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
537 if ( aSO && aSO->GetFatherComponent() )
538 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
544 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
546 GEOM::GEOM_Object_var o;
547 if ( 0 <= index && index < myObjects.size() )
548 o = GEOM::GEOM_Object::_narrow( myObjects[index] );
552 GEOM::GEOM_BaseObject_ptr GEOMGUI_Selection::getBaseObject( const int index ) const
554 GEOM::GEOM_BaseObject_var o;
555 if ( 0 <= index && index < myObjects.size() )
556 o = GEOM::GEOM_BaseObject::_duplicate( myObjects[index] );
560 QString GEOMGUI_Selection::selectionMode() const
562 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
564 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
566 switch ( aGeomGUI->getLocalSelectionMode() )
568 case GEOM_POINT : return "VERTEX";
569 case GEOM_EDGE : return "EDGE";
570 case GEOM_WIRE : return "WIRE";
571 case GEOM_FACE : return "FACE";
572 case GEOM_SHELL : return "SHELL";
573 case GEOM_SOLID : return "SOLID";
574 case GEOM_COMPOUND : return "COMPOUND";
575 case GEOM_ALLOBJECTS : return "ALL";
583 bool GEOMGUI_Selection::topLevel( const int index ) const
585 #ifdef USE_VISUAL_PROP_MAP
586 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) );
587 if ( v.canConvert<bool>() )
593 SALOME_View* view = GEOM_Displayer::GetActiveView();
594 QString viewType = activeViewType();
595 if ( view && viewType == OCCViewer_Viewer::Type() ) {
596 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
598 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
599 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
600 AIS_ListOfInteractive lst;
601 occPrs->GetObjects( lst );
602 if ( lst.Extent() ) {
603 Handle(AIS_InteractiveObject) io = lst.First();
604 if ( !io.IsNull() ) {
605 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
607 res = (bool)aSh->isTopLevel();
616 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const
618 #ifdef USE_VISUAL_PROP_MAP
619 QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) );
620 if ( v.canConvert<QString>() ) {
621 Material_Model material;
622 material.fromProperties( v.toString() );
623 return material.isPhysical();
629 SALOME_View* view = GEOM_Displayer::GetActiveView();
630 QString viewType = activeViewType();
632 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
634 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
635 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
636 AIS_ListOfInteractive lst;
637 occPrs->GetObjects( lst );
638 if ( lst.Extent() ) {
639 Handle(AIS_InteractiveObject) io = lst.First();
640 if ( !io.IsNull() ) {
641 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
643 res = (bool) aSh->Attributes()->ShadingAspect()->
644 Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
648 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
649 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
650 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
652 lst->InitTraversal();
653 vtkActor* actor = lst->GetNextActor();
655 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
657 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
658 res = mat->GetPhysical();
659 } // if ( salome actor )
661 } // if ( lst == vtkPrs->GetObjects() )
669 bool GEOMGUI_Selection::isFolder( const int index ) const
672 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
675 QString anEntry = entry( index );
676 _PTR(Study) study = appStudy->studyDS();
677 if ( study && !anEntry.isNull() ) {
678 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
679 if ( aSO ) res = isFolder( aSO );