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 <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 == "hasHiddenChildren" )
162 v = hasHiddenChildren( idx );
163 else if ( p == "hasShownChildren" )
164 v = hasShownChildren( idx );
165 else if ( p == "compoundOfVertices" )
166 v = compoundOfVertices( idx );
167 else if ( p == "imported" )
168 v = isImported( idx );
169 else if ( p == "isPhysicalMaterial" )
170 v = isPhysicalMaterial(idx);
172 v = LightApp_Selection::parameter( idx, p );
177 // the method to skip temporary objects from selection (called from LightApp)
178 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
180 return !theOwner->entry().contains("_");
183 QString GEOMGUI_Selection::typeName( const int index ) const
185 if ( isComponent( index ) )
188 static QString aGroup( "Group" );
189 static QString aShape( "Shape" );
190 static QString anUnknown( "Unknown" );
192 GEOM::GEOM_Object_var anObj = getObject( index );
193 if ( !CORBA::is_nil( anObj ) ) {
194 const int aGeomType = anObj->GetType();
195 if ( aGeomType == GEOM_GROUP )
203 int GEOMGUI_Selection::typeId( const int index ) const
206 GEOM::GEOM_Object_var anObj = getObject( index );
207 if ( !CORBA::is_nil( anObj ) )
208 aType = (int)anObj->GetShapeType();
212 bool GEOMGUI_Selection::isVisible( const int index ) const
216 #ifdef USE_VISUAL_PROP_MAP
218 QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
219 if ( v.canConvert( QVariant::Bool ) ) {
226 GEOM::GEOM_Object_var obj = getObject( index );
227 SALOME_View* view = GEOM_Displayer::GetActiveView();
228 if ( !CORBA::is_nil( obj ) && view ) {
229 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
230 res = view->isVisible( io );
232 #ifdef USE_VISUAL_PROP_MAP
239 bool GEOMGUI_Selection::isAutoColor( const int index ) const
241 GEOM::GEOM_Object_var obj = getObject( index );
242 if ( !CORBA::is_nil( obj ) )
243 return obj->GetAutoColor();
247 bool GEOMGUI_Selection::isImported( const int index ) const
249 GEOM::GEOM_Object_var obj = getObject( index );
250 if ( !CORBA::is_nil( obj ) )
251 return obj->GetType() == GEOM_IMPORT;
255 bool GEOMGUI_Selection::hasImported() const
258 for ( int i = 0; i < count() && !res; i++ )
259 res = isImported( i );
263 bool GEOMGUI_Selection::allImported() const
266 for ( int i = 0; i < count() && res; i++ )
267 res = isImported( i );
271 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
274 LightApp_Study* aStudy = study();
276 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
277 if ( anApp && anApp->activeViewManager() ) {
278 int id = anApp->activeViewManager()->getGlobalId();
279 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
285 QString GEOMGUI_Selection::displayMode( const int index ) const
288 QString viewType = activeViewType();
289 #ifdef USE_VISUAL_PROP_MAP
290 QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
291 if ( v.canConvert( QVariant::Int ) ) {
293 if ( viewType == OCCViewer_Viewer::Type() ) {
294 OCC_DISPLAY_MODE_TO_STRING( res, dm );
295 } else if ( viewType == SVTK_Viewer::Type() ) {
296 VTK_DISPLAY_MODE_TO_STRING( res, dm );
300 if ( res.isEmpty() ) {
302 SALOME_View* view = GEOM_Displayer::GetActiveView();
303 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
304 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
306 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
307 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
308 AIS_ListOfInteractive lst;
309 occPrs->GetObjects( lst );
310 if ( lst.Extent() ) {
311 Handle(AIS_InteractiveObject) io = lst.First();
312 if ( !io.IsNull() ) {
313 int dm = io->DisplayMode();
314 OCC_DISPLAY_MODE_TO_STRING( res, dm );
315 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
316 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
317 desktop()->activeWindow()->getViewManager()->getViewModel();
318 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
319 dm = ic->DisplayMode();
320 OCC_DISPLAY_MODE_TO_STRING( res, dm );
325 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
326 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
327 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
329 lst->InitTraversal();
330 vtkActor* actor = lst->GetNextActor();
332 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
334 int dm = salActor->getDisplayMode();
335 VTK_DISPLAY_MODE_TO_STRING( res, dm );
336 } // if ( salome actor )
338 } // if ( lst == vtkPrs->GetObjects() )
343 #ifdef USE_VISUAL_PROP_MAP
350 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
354 #ifdef USE_VISUAL_PROP_MAP
356 QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
357 if ( v.canConvert( QVariant::Bool ) ) {
364 SALOME_View* view = GEOM_Displayer::GetActiveView();
365 QString viewType = activeViewType();
366 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
367 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
369 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
370 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
371 AIS_ListOfInteractive lst;
372 occPrs->GetObjects( lst );
373 if ( lst.Extent() ) {
374 Handle(AIS_InteractiveObject) io = lst.First();
375 if ( !io.IsNull() ) {
376 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
378 res = aSh->isShowVectors();
381 } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
382 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
383 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
385 lst->InitTraversal();
386 vtkActor* actor = lst->GetNextActor();
388 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
390 res = aGeomActor->GetVectorMode();
396 #ifdef USE_VISUAL_PROP_MAP
403 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
407 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
408 for ( ; it->More() && !ok; it->Next() ) {
409 _PTR(SObject) child = it->Value();
411 _PTR(SObject) refObj;
412 if ( child->ReferencedObject( refObj ) ) continue; // omit references
413 if ( child->GetName() != "" ) ok = true;
420 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
423 _PTR(GenericAttribute) anAttr;
424 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
425 _PTR(AttributeExpandable) aAttrExp = anAttr;
426 exp = aAttrExp->IsExpandable();
431 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
435 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
436 (SUIT_Session::session()->activeApplication()->activeStudy());*/
437 if ( /*appStudy && */!CORBA::is_nil( obj ) )
438 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
442 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
445 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
448 QString anEntry = entry( index );
449 _PTR(Study) study = appStudy->studyDS();
450 if ( study && !anEntry.isEmpty() ) {
451 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
452 OK = !expandable( aSO ) && hasChildren( aSO );
458 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
461 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
464 QString anEntry = entry( index );
465 _PTR(Study) study = appStudy->studyDS();
466 if ( study && !anEntry.isEmpty() ) {
467 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
468 OK = expandable( aSO ) && hasChildren( aSO );
474 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
476 GEOM::GEOM_Object_var obj = getObject( index );
477 return isCompoundOfVertices( obj );
480 bool GEOMGUI_Selection::isComponent( const int index ) const
482 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
485 QString anEntry = entry( index );
486 _PTR(Study) study = appStudy->studyDS();
487 if ( study && !anEntry.isNull() ) {
488 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
489 if ( aSO && aSO->GetFatherComponent() )
490 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
496 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
498 GEOM::GEOM_Object_var o;
499 if ( 0 <= index && index < myObjects.size() )
500 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
504 QString GEOMGUI_Selection::selectionMode() const
506 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
508 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
510 switch ( aGeomGUI->getLocalSelectionMode() )
512 case GEOM_POINT : return "VERTEX";
513 case GEOM_EDGE : return "EDGE";
514 case GEOM_WIRE : return "WIRE";
515 case GEOM_FACE : return "FACE";
516 case GEOM_SHELL : return "SHELL";
517 case GEOM_SOLID : return "SOLID";
518 case GEOM_COMPOUND : return "COMPOUND";
519 case GEOM_ALLOBJECTS : return "ALL";
527 bool GEOMGUI_Selection::topLevel( const int index ) const {
530 #ifdef USE_VISUAL_PROP_MAP
532 QVariant v = visibleProperty( entry( index ), TOP_LEVEL_PROP );
533 if ( v.canConvert<bool>() ) {
540 SALOME_View* view = GEOM_Displayer::GetActiveView();
541 QString viewType = activeViewType();
542 if ( view && viewType == OCCViewer_Viewer::Type() ) {
543 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
545 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
546 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
547 AIS_ListOfInteractive lst;
548 occPrs->GetObjects( lst );
549 if ( lst.Extent() ) {
550 Handle(AIS_InteractiveObject) io = lst.First();
551 if ( !io.IsNull() ) {
552 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
554 res = (bool)aSh->isTopLevel();
564 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const{
567 #ifdef USE_VISUAL_PROP_MAP
569 QVariant v = visibleProperty( entry( idx ), MATERIAL_PROP );
570 if ( v.canConvert<QString>() ) {
571 Material_Model* aModel = Material_Model::getMaterialModel( v.toString().split(DIGIT_SEPARATOR) );
572 res = aModel->isPhysical();
578 SALOME_View* view = GEOM_Displayer::GetActiveView();
579 QString viewType = activeViewType();
581 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
583 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
584 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
585 AIS_ListOfInteractive lst;
586 occPrs->GetObjects( lst );
587 if ( lst.Extent() ) {
588 Handle(AIS_InteractiveObject) io = lst.First();
589 if ( !io.IsNull() ) {
590 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
592 res =(bool) aSh->Attributes()->ShadingAspect()->
593 Material(Aspect_TypeOfFacingModel::Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
597 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
598 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
599 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
601 lst->InitTraversal();
602 vtkActor* actor = lst->GetNextActor();
604 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
606 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
607 res = mat->GetPhysical();
608 } // if ( salome actor )
610 } // if ( lst == vtkPrs->GetObjects() )