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
22 // File : GEOMGUI_Selection.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
25 #include "GEOMGUI_Selection.h"
27 #include "GeometryGUI.h"
28 #include "GEOM_Displayer.h"
30 #include "Material_Model.h"
32 #include <GEOM_Constants.h>
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Study.h>
37 #include "LightApp_DataOwner.h"
39 #include <SUIT_Session.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_ViewWindow.h>
42 #include <SUIT_ViewManager.h>
44 #include <SALOME_Prs.h>
45 #include <SALOME_InteractiveObject.hxx>
49 #include <SALOME_Actor.h>
50 #include <GEOM_Actor.h>
52 #include <OCCViewer_ViewModel.h>
53 #include <SVTK_ViewModel.h>
55 #include <GEOMImpl_Types.hxx>
57 #include <GEOM_AISShape.hxx>
58 #include <GEOM_VTKPropertyMaterial.hxx>
62 #include <AIS_InteractiveObject.hxx>
63 #include <AIS_ListOfInteractive.hxx>
64 #include <AIS_GraphicTool.hxx>
65 #include <AIS_Drawer.hxx>
66 #include <Aspect_TypeOfFacingModel.hxx>
67 #include <Prs3d_ShadingAspect.hxx>
68 #include<Graphic3d_MaterialAspect.hxx>
71 #include <vtkActorCollection.h>
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" ); \
85 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
87 str = QString( "Wireframe" ); \
89 str = QString( "Shading" ); \
91 str = QString( "ShadingWithEdges" ); \
95 #define USE_VISUAL_PROP_MAP
97 GEOMGUI_Selection::GEOMGUI_Selection()
98 : LightApp_Selection()
102 GEOMGUI_Selection::~GEOMGUI_Selection()
106 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
108 LightApp_Selection::init( context, selMgr );
110 myObjects.resize( count() );
112 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
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() ) );
120 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
121 myObjects[idx] = GEOM::GEOM_Object::_narrow( varObj );
128 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
129 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
133 v = activeViewType() == OCCViewer_Viewer::Type();
134 else if ( p == "selectionmode" )
136 else if ( p == "hasImported" )
138 else if ( p == "allImported" )
141 v = LightApp_Selection::parameter( p );
145 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
146 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
151 else if ( p == "typeid" )
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" )
161 else if ( p == "hasChildren" )
162 v = hasChildren( idx );
163 else if ( p == "hasConcealedChildren" )
164 v = hasConcealedChildren( idx );
165 else if ( p == "hasDisclosedChildren" )
166 v = hasDisclosedChildren( idx );
167 else if ( p == "compoundOfVertices" )
168 v = compoundOfVertices( idx );
169 else if ( p == "imported" )
170 v = isImported( idx );
171 else if ( p == "isPhysicalMaterial" )
172 v = isPhysicalMaterial(idx);
174 v = LightApp_Selection::parameter( idx, p );
179 // the method to skip temporary objects from selection (called from LightApp)
180 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
182 return !theOwner->entry().contains("_");
185 QString GEOMGUI_Selection::typeName( const int index ) const
187 if ( isComponent( index ) )
190 static QString aGroup( "Group" );
191 static QString aShape( "Shape" );
192 static QString anUnknown( "Unknown" );
194 GEOM::GEOM_Object_var anObj = getObject( index );
195 if ( !CORBA::is_nil( anObj ) ) {
196 const int aGeomType = anObj->GetType();
197 if ( aGeomType == GEOM_GROUP )
205 int GEOMGUI_Selection::typeId( const int index ) const
208 GEOM::GEOM_Object_var anObj = getObject( index );
209 if ( !CORBA::is_nil( anObj ) )
210 aType = (int)anObj->GetShapeType();
214 bool GEOMGUI_Selection::isVisible( const int index ) const
216 #ifdef USE_VISUAL_PROP_MAP
217 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) );
218 if ( v.canConvert( QVariant::Bool ) )
224 GEOM::GEOM_Object_var obj = getObject( index );
225 SALOME_View* view = GEOM_Displayer::GetActiveView();
226 if ( !CORBA::is_nil( obj ) && view ) {
227 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
228 res = view->isVisible( io );
234 bool GEOMGUI_Selection::isAutoColor( const int index ) const
236 GEOM::GEOM_Object_var obj = getObject( index );
237 if ( !CORBA::is_nil( obj ) )
238 return obj->GetAutoColor();
242 bool GEOMGUI_Selection::isImported( const int index ) const
244 GEOM::GEOM_Object_var obj = getObject( index );
245 if ( !CORBA::is_nil( obj ) )
246 return obj->GetType() == GEOM_IMPORT;
250 bool GEOMGUI_Selection::hasImported() const
253 for ( int i = 0; i < count() && !res; i++ )
254 res = isImported( i );
258 bool GEOMGUI_Selection::allImported() const
261 for ( int i = 0; i < count() && res; i++ )
262 res = isImported( i );
266 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
269 LightApp_Study* aStudy = study();
271 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
272 if ( anApp && anApp->activeViewManager() ) {
273 int id = anApp->activeViewManager()->getGlobalId();
274 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
280 QString GEOMGUI_Selection::displayMode( const int index ) const
283 QString viewType = activeViewType();
285 #ifdef USE_VISUAL_PROP_MAP
286 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) );
287 if ( v.canConvert( QVariant::Int ) ) {
289 if ( viewType == OCCViewer_Viewer::Type() ) {
290 OCC_DISPLAY_MODE_TO_STRING( res, dm );
291 } else if ( viewType == SVTK_Viewer::Type() ) {
292 VTK_DISPLAY_MODE_TO_STRING( res, dm );
298 SALOME_View* view = GEOM_Displayer::GetActiveView();
299 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
300 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
302 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
303 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
304 AIS_ListOfInteractive lst;
305 occPrs->GetObjects( lst );
306 if ( lst.Extent() ) {
307 Handle(AIS_InteractiveObject) io = lst.First();
308 if ( !io.IsNull() ) {
310 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
312 dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
314 dm = io->DisplayMode();
316 OCC_DISPLAY_MODE_TO_STRING( res, dm );
317 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
318 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
319 desktop()->activeWindow()->getViewManager()->getViewModel();
320 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
321 dm = ic->DisplayMode();
322 OCC_DISPLAY_MODE_TO_STRING( res, dm );
327 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
328 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
329 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
331 lst->InitTraversal();
332 vtkActor* actor = lst->GetNextActor();
334 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
336 int dm = salActor->getDisplayMode();
337 VTK_DISPLAY_MODE_TO_STRING( res, dm );
338 } // if ( salome actor )
340 } // if ( lst == vtkPrs->GetObjects() )
348 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
350 #ifdef USE_VISUAL_PROP_MAP
351 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) );
352 if ( v.canConvert( QVariant::Bool ) )
358 SALOME_View* view = GEOM_Displayer::GetActiveView();
359 QString viewType = activeViewType();
360 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
361 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
363 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
364 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
365 AIS_ListOfInteractive lst;
366 occPrs->GetObjects( lst );
367 if ( lst.Extent() ) {
368 Handle(AIS_InteractiveObject) io = lst.First();
369 if ( !io.IsNull() ) {
370 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
372 res = aSh->isShowVectors();
376 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
377 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
378 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
380 lst->InitTraversal();
381 vtkActor* actor = lst->GetNextActor();
383 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
385 res = aGeomActor->GetVectorMode();
395 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
399 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
400 for ( ; it->More() && !ok; it->Next() ) {
401 _PTR(SObject) child = it->Value();
403 _PTR(SObject) refObj;
404 if ( child->ReferencedObject( refObj ) ) continue; // omit references
405 if ( child->GetName() != "" ) ok = true;
412 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
415 _PTR(GenericAttribute) anAttr;
416 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
417 _PTR(AttributeExpandable) aAttrExp = anAttr;
418 exp = aAttrExp->IsExpandable();
423 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
427 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
428 (SUIT_Session::session()->activeApplication()->activeStudy());*/
429 if ( /*appStudy && */!CORBA::is_nil( obj ) )
430 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
434 bool GEOMGUI_Selection::hasChildren( const int index ) const
437 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
440 QString anEntry = entry( index );
441 _PTR(Study) study = appStudy->studyDS();
442 if ( study && !anEntry.isEmpty() ) {
443 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
444 ok = hasChildren( aSO );
450 bool GEOMGUI_Selection::hasConcealedChildren( const int index ) const
453 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
456 QString anEntry = entry( index );
457 _PTR(Study) study = appStudy->studyDS();
458 if ( study && !anEntry.isEmpty() ) {
459 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
460 OK = !expandable( aSO ) && hasChildren( aSO );
466 bool GEOMGUI_Selection::hasDisclosedChildren( const int index ) const
469 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
472 QString anEntry = entry( index );
473 _PTR(Study) study = appStudy->studyDS();
474 if ( study && !anEntry.isEmpty() ) {
475 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
476 OK = expandable( aSO ) && hasChildren( aSO );
482 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
484 GEOM::GEOM_Object_var obj = getObject( index );
485 return isCompoundOfVertices( obj );
488 bool GEOMGUI_Selection::isComponent( const int index ) const
490 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
493 QString anEntry = entry( index );
494 _PTR(Study) study = appStudy->studyDS();
495 if ( study && !anEntry.isNull() ) {
496 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
497 if ( aSO && aSO->GetFatherComponent() )
498 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
504 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
506 GEOM::GEOM_Object_var o;
507 if ( 0 <= index && index < myObjects.size() )
508 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
512 QString GEOMGUI_Selection::selectionMode() const
514 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
516 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
518 switch ( aGeomGUI->getLocalSelectionMode() )
520 case GEOM_POINT : return "VERTEX";
521 case GEOM_EDGE : return "EDGE";
522 case GEOM_WIRE : return "WIRE";
523 case GEOM_FACE : return "FACE";
524 case GEOM_SHELL : return "SHELL";
525 case GEOM_SOLID : return "SOLID";
526 case GEOM_COMPOUND : return "COMPOUND";
527 case GEOM_ALLOBJECTS : return "ALL";
535 bool GEOMGUI_Selection::topLevel( const int index ) const
537 #ifdef USE_VISUAL_PROP_MAP
538 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) );
539 if ( v.canConvert<bool>() )
545 SALOME_View* view = GEOM_Displayer::GetActiveView();
546 QString viewType = activeViewType();
547 if ( view && viewType == OCCViewer_Viewer::Type() ) {
548 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
550 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
551 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
552 AIS_ListOfInteractive lst;
553 occPrs->GetObjects( lst );
554 if ( lst.Extent() ) {
555 Handle(AIS_InteractiveObject) io = lst.First();
556 if ( !io.IsNull() ) {
557 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
559 res = (bool)aSh->isTopLevel();
568 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const
570 #ifdef USE_VISUAL_PROP_MAP
571 QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) );
572 if ( v.canConvert<QString>() ) {
573 Material_Model material;
574 material.fromProperties( v.toString() );
575 return material.isPhysical();
581 SALOME_View* view = GEOM_Displayer::GetActiveView();
582 QString viewType = activeViewType();
584 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
586 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
587 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
588 AIS_ListOfInteractive lst;
589 occPrs->GetObjects( lst );
590 if ( lst.Extent() ) {
591 Handle(AIS_InteractiveObject) io = lst.First();
592 if ( !io.IsNull() ) {
593 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
595 res = (bool) aSh->Attributes()->ShadingAspect()->
596 Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
600 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
601 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
602 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
604 lst->InitTraversal();
605 vtkActor* actor = lst->GetNextActor();
607 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
609 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
610 res = mat->GetPhysical();
611 } // if ( salome actor )
613 } // if ( lst == vtkPrs->GetObjects() )