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
218 #ifdef USE_VISUAL_PROP_MAP
220 QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
221 if ( v.canConvert( QVariant::Bool ) ) {
228 GEOM::GEOM_Object_var obj = getObject( index );
229 SALOME_View* view = GEOM_Displayer::GetActiveView();
230 if ( !CORBA::is_nil( obj ) && view ) {
231 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
232 res = view->isVisible( io );
234 #ifdef USE_VISUAL_PROP_MAP
241 bool GEOMGUI_Selection::isAutoColor( const int index ) const
243 GEOM::GEOM_Object_var obj = getObject( index );
244 if ( !CORBA::is_nil( obj ) )
245 return obj->GetAutoColor();
249 bool GEOMGUI_Selection::isImported( const int index ) const
251 GEOM::GEOM_Object_var obj = getObject( index );
252 if ( !CORBA::is_nil( obj ) )
253 return obj->GetType() == GEOM_IMPORT;
257 bool GEOMGUI_Selection::hasImported() const
260 for ( int i = 0; i < count() && !res; i++ )
261 res = isImported( i );
265 bool GEOMGUI_Selection::allImported() const
268 for ( int i = 0; i < count() && res; i++ )
269 res = isImported( i );
273 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
276 LightApp_Study* aStudy = study();
278 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
279 if ( anApp && anApp->activeViewManager() ) {
280 int id = anApp->activeViewManager()->getGlobalId();
281 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
287 QString GEOMGUI_Selection::displayMode( const int index ) const
290 QString viewType = activeViewType();
291 #ifdef USE_VISUAL_PROP_MAP
292 QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
293 if ( v.canConvert( QVariant::Int ) ) {
295 if ( viewType == OCCViewer_Viewer::Type() ) {
296 OCC_DISPLAY_MODE_TO_STRING( res, dm );
297 } else if ( viewType == SVTK_Viewer::Type() ) {
298 VTK_DISPLAY_MODE_TO_STRING( res, dm );
302 if ( res.isEmpty() ) {
304 SALOME_View* view = GEOM_Displayer::GetActiveView();
305 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
306 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
308 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
309 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
310 AIS_ListOfInteractive lst;
311 occPrs->GetObjects( lst );
312 if ( lst.Extent() ) {
313 Handle(AIS_InteractiveObject) io = lst.First();
314 if ( !io.IsNull() ) {
316 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
318 dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
320 dm = io->DisplayMode();
322 OCC_DISPLAY_MODE_TO_STRING( res, dm );
323 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
324 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
325 desktop()->activeWindow()->getViewManager()->getViewModel();
326 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
327 dm = ic->DisplayMode();
328 OCC_DISPLAY_MODE_TO_STRING( res, dm );
333 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
334 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
335 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
337 lst->InitTraversal();
338 vtkActor* actor = lst->GetNextActor();
340 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
342 int dm = salActor->getDisplayMode();
343 VTK_DISPLAY_MODE_TO_STRING( res, dm );
344 } // if ( salome actor )
346 } // if ( lst == vtkPrs->GetObjects() )
351 #ifdef USE_VISUAL_PROP_MAP
358 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
362 #ifdef USE_VISUAL_PROP_MAP
364 QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
365 if ( v.canConvert( QVariant::Bool ) ) {
372 SALOME_View* view = GEOM_Displayer::GetActiveView();
373 QString viewType = activeViewType();
374 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
375 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
377 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
378 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
379 AIS_ListOfInteractive lst;
380 occPrs->GetObjects( lst );
381 if ( lst.Extent() ) {
382 Handle(AIS_InteractiveObject) io = lst.First();
383 if ( !io.IsNull() ) {
384 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
386 res = aSh->isShowVectors();
389 } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
390 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
391 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
393 lst->InitTraversal();
394 vtkActor* actor = lst->GetNextActor();
396 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
398 res = aGeomActor->GetVectorMode();
404 #ifdef USE_VISUAL_PROP_MAP
411 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
415 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
416 for ( ; it->More() && !ok; it->Next() ) {
417 _PTR(SObject) child = it->Value();
419 _PTR(SObject) refObj;
420 if ( child->ReferencedObject( refObj ) ) continue; // omit references
421 if ( child->GetName() != "" ) ok = true;
428 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
431 _PTR(GenericAttribute) anAttr;
432 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
433 _PTR(AttributeExpandable) aAttrExp = anAttr;
434 exp = aAttrExp->IsExpandable();
439 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
443 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
444 (SUIT_Session::session()->activeApplication()->activeStudy());*/
445 if ( /*appStudy && */!CORBA::is_nil( obj ) )
446 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
450 bool GEOMGUI_Selection::hasChildren( 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 = hasChildren( aSO );
466 bool GEOMGUI_Selection::hasConcealedChildren( 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::hasDisclosedChildren( const int index ) const
485 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
488 QString anEntry = entry( index );
489 _PTR(Study) study = appStudy->studyDS();
490 if ( study && !anEntry.isEmpty() ) {
491 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
492 OK = expandable( aSO ) && hasChildren( aSO );
498 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
500 GEOM::GEOM_Object_var obj = getObject( index );
501 return isCompoundOfVertices( obj );
504 bool GEOMGUI_Selection::isComponent( const int index ) const
506 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
509 QString anEntry = entry( index );
510 _PTR(Study) study = appStudy->studyDS();
511 if ( study && !anEntry.isNull() ) {
512 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
513 if ( aSO && aSO->GetFatherComponent() )
514 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
520 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
522 GEOM::GEOM_Object_var o;
523 if ( 0 <= index && index < myObjects.size() )
524 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
528 QString GEOMGUI_Selection::selectionMode() const
530 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
532 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
534 switch ( aGeomGUI->getLocalSelectionMode() )
536 case GEOM_POINT : return "VERTEX";
537 case GEOM_EDGE : return "EDGE";
538 case GEOM_WIRE : return "WIRE";
539 case GEOM_FACE : return "FACE";
540 case GEOM_SHELL : return "SHELL";
541 case GEOM_SOLID : return "SOLID";
542 case GEOM_COMPOUND : return "COMPOUND";
543 case GEOM_ALLOBJECTS : return "ALL";
551 bool GEOMGUI_Selection::topLevel( const int index ) const {
554 #ifdef USE_VISUAL_PROP_MAP
556 QVariant v = visibleProperty( entry( index ), TOP_LEVEL_PROP );
557 if ( v.canConvert<bool>() ) {
564 SALOME_View* view = GEOM_Displayer::GetActiveView();
565 QString viewType = activeViewType();
566 if ( view && viewType == OCCViewer_Viewer::Type() ) {
567 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
569 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
570 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
571 AIS_ListOfInteractive lst;
572 occPrs->GetObjects( lst );
573 if ( lst.Extent() ) {
574 Handle(AIS_InteractiveObject) io = lst.First();
575 if ( !io.IsNull() ) {
576 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
578 res = (bool)aSh->isTopLevel();
588 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const{
591 #ifdef USE_VISUAL_PROP_MAP
593 QVariant v = visibleProperty( entry( idx ), MATERIAL_PROP );
594 if ( v.canConvert<QString>() ) {
595 Material_Model material;
596 material.fromProperties( v.toString() );
597 res = material.isPhysical();
603 SALOME_View* view = GEOM_Displayer::GetActiveView();
604 QString viewType = activeViewType();
606 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
608 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
609 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
610 AIS_ListOfInteractive lst;
611 occPrs->GetObjects( lst );
612 if ( lst.Extent() ) {
613 Handle(AIS_InteractiveObject) io = lst.First();
614 if ( !io.IsNull() ) {
615 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
617 res = (bool) aSh->Attributes()->ShadingAspect()->
618 Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
622 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
623 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
624 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
626 lst->InitTraversal();
627 vtkActor* actor = lst->GetNextActor();
629 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
631 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
632 res = mat->GetPhysical();
633 } // if ( salome actor )
635 } // if ( lst == vtkPrs->GetObjects() )