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 VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
90 str = QString( "Wireframe" ); \
92 str = QString( "Shading" ); \
94 str = QString( "ShadingWithEdges" ); \
98 #define USE_VISUAL_PROP_MAP
100 GEOMGUI_Selection::GEOMGUI_Selection()
101 : LightApp_Selection()
105 GEOMGUI_Selection::~GEOMGUI_Selection()
109 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
111 LightApp_Selection::init( context, selMgr );
113 myObjects.resize( count() );
115 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
117 _PTR(Study) study = appStudy->studyDS();
118 for ( int idx = 0; idx < count(); idx++ ) {
119 QString anEntry = entry( idx );
120 if ( study && !anEntry.isEmpty() ) {
121 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
123 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
124 myObjects[idx] = GEOM::GEOM_BaseObject::_narrow( varObj );
131 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
132 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
136 v = activeViewType() == OCCViewer_Viewer::Type();
137 else if ( p == "selectionmode" )
139 else if ( p == "hasImported" )
141 else if ( p == "allImported" )
144 v = LightApp_Selection::parameter( p );
148 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
149 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
154 else if ( p == "typeid" )
156 else if ( p == "displaymode" )
157 v = displayMode( idx );
158 else if ( p == "isAutoColor" )
159 v = isAutoColor( idx );
160 else if ( p == "isVectorsMode" )
161 v = isVectorsMode( idx );
162 else if ( p == "topLevel" )
164 else if ( p == "autoBringToFront" )
165 v = autoBringToFront( idx );
166 else if ( p == "hasChildren" )
167 v = hasChildren( idx );
168 else if ( p == "nbChildren" )
170 else if ( p == "hasConcealedChildren" )
171 v = hasConcealedChildren( idx );
172 else if ( p == "hasDisclosedChildren" )
173 v = hasDisclosedChildren( idx );
174 else if ( p == "compoundOfVertices" )
175 v = compoundOfVertices( idx );
176 else if ( p == "imported" )
177 v = isImported( idx );
178 else if ( p == "isPhysicalMaterial" )
179 v = isPhysicalMaterial(idx);
180 else if ( p == "isFolder" )
182 else if ( p == "hasHiddenDimensions" )
183 v = hasHiddenDimensions(idx);
184 else if ( p == "hasVisibleDimensions" )
185 v = hasVisibleDimensions(idx);
187 v = LightApp_Selection::parameter( idx, p );
192 // the method to skip temporary objects from selection (called from LightApp)
193 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
195 return !theOwner->entry().contains("_");
198 QString GEOMGUI_Selection::typeName( const int index ) const
200 if ( isComponent( index ) )
202 if ( isFolder( index ) )
205 static QString aGroup( "Group" );
206 static QString aShape( "Shape" );
207 static QString aField( "Field" );
208 static QString aFieldStep( "FieldStep" );
209 static QString anUnknown( "Unknown" );
211 GEOM::GEOM_BaseObject_var anObj = getBaseObject( index );
212 if ( !CORBA::is_nil( anObj ) ) {
213 const int aGeomType = anObj->GetType();
214 switch ( aGeomType ) {
215 case GEOM_GROUP : return aGroup;
216 case GEOM_FIELD : return aField;
217 case GEOM_FIELD_STEP: return aFieldStep;
218 default : return aShape;
224 int GEOMGUI_Selection::typeId( const int index ) const
227 GEOM::GEOM_Object_var anObj = getObject( index );
228 if ( !CORBA::is_nil( anObj ) )
229 aType = (int)anObj->GetShapeType();
233 bool GEOMGUI_Selection::isVisible( const int index ) const
235 #ifdef USE_VISUAL_PROP_MAP
236 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) );
237 if ( v.canConvert( QVariant::Bool ) )
243 GEOM::GEOM_Object_var obj = getObject( index );
244 SALOME_View* view = GEOM_Displayer::GetActiveView();
245 if ( !CORBA::is_nil( obj ) && view ) {
246 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
247 res = view->isVisible( io );
253 bool GEOMGUI_Selection::isAutoColor( const int index ) const
255 GEOM::GEOM_Object_var obj = getObject( index );
256 if ( !CORBA::is_nil( obj ) )
257 return obj->GetAutoColor();
261 bool GEOMGUI_Selection::isImported( const int index ) const
263 GEOM::GEOM_Object_var obj = getObject( index );
264 if ( !CORBA::is_nil( obj ) )
265 return obj->GetType() == GEOM_IMPORT;
269 bool GEOMGUI_Selection::hasImported() const
272 for ( int i = 0; i < count() && !res; i++ )
273 res = isImported( i );
277 bool GEOMGUI_Selection::allImported() const
280 for ( int i = 0; i < count() && res; i++ )
281 res = isImported( i );
285 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
288 LightApp_Study* aStudy = study();
290 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
291 if ( anApp && anApp->activeViewManager() ) {
292 int id = anApp->activeViewManager()->getGlobalId();
293 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
299 QString GEOMGUI_Selection::displayMode( const int index ) const
302 QString viewType = activeViewType();
304 #ifdef USE_VISUAL_PROP_MAP
305 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) );
306 if ( v.canConvert( QVariant::Int ) ) {
308 if ( viewType == OCCViewer_Viewer::Type() ) {
309 OCC_DISPLAY_MODE_TO_STRING( res, dm );
310 } else if ( viewType == SVTK_Viewer::Type() ) {
311 VTK_DISPLAY_MODE_TO_STRING( res, dm );
317 SALOME_View* view = GEOM_Displayer::GetActiveView();
318 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
319 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
321 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
322 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
323 AIS_ListOfInteractive lst;
324 occPrs->GetObjects( lst );
325 if ( lst.Extent() ) {
326 Handle(AIS_InteractiveObject) io = lst.First();
327 if ( !io.IsNull() ) {
329 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
331 dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
333 dm = io->DisplayMode();
335 OCC_DISPLAY_MODE_TO_STRING( res, dm );
336 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
337 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
338 desktop()->activeWindow()->getViewManager()->getViewModel();
339 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
340 dm = ic->DisplayMode();
341 OCC_DISPLAY_MODE_TO_STRING( res, dm );
346 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
347 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
348 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
350 lst->InitTraversal();
351 vtkActor* actor = lst->GetNextActor();
353 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
355 int dm = salActor->getDisplayMode();
356 VTK_DISPLAY_MODE_TO_STRING( res, dm );
357 } // if ( salome actor )
359 } // if ( lst == vtkPrs->GetObjects() )
367 bool GEOMGUI_Selection::autoBringToFront( const int /*index*/ ) const
369 return SUIT_Session::session()->resourceMgr()->booleanValue( "Geometry", "auto_bring_to_front" );
372 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
374 #ifdef USE_VISUAL_PROP_MAP
375 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) );
376 if ( v.canConvert( QVariant::Bool ) )
382 SALOME_View* view = GEOM_Displayer::GetActiveView();
383 QString viewType = activeViewType();
384 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
385 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
387 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
388 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
389 AIS_ListOfInteractive lst;
390 occPrs->GetObjects( lst );
391 if ( lst.Extent() ) {
392 Handle(AIS_InteractiveObject) io = lst.First();
393 if ( !io.IsNull() ) {
394 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
396 res = aSh->isShowVectors();
400 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
401 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
402 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
404 lst->InitTraversal();
405 vtkActor* actor = lst->GetNextActor();
407 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
409 res = aGeomActor->GetVectorMode();
419 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
422 // as soon as Use Case browser data tree was added
423 return obj->GetStudy()->GetUseCaseBuilder()->HasChildren( obj );
427 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
430 _PTR(GenericAttribute) anAttr;
431 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
432 _PTR(AttributeExpandable) aAttrExp = anAttr;
433 exp = aAttrExp->IsExpandable();
438 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
442 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
443 (SUIT_Session::session()->activeApplication()->activeStudy());*/
444 if ( /*appStudy && */!CORBA::is_nil( obj ) )
445 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
449 bool GEOMGUI_Selection::isFolder( const _PTR(SObject)& obj )
452 _PTR(GenericAttribute) anAttr;
453 if ( obj && obj->FindAttribute(anAttr, "AttributeLocalID") ) {
454 _PTR(AttributeLocalID) aLocalID( anAttr );
455 ret = aLocalID->Value() == 999;
460 bool GEOMGUI_Selection::hasChildren( const int index ) const
463 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
466 QString anEntry = entry( index );
467 _PTR(Study) study = appStudy->studyDS();
468 if ( study && !anEntry.isEmpty() ) {
469 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
470 ok = hasChildren( aSO );
476 int GEOMGUI_Selection::nbChildren( const int index ) const
479 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
482 QString anEntry = entry( index );
483 _PTR(Study) study = appStudy->studyDS();
484 if ( study && !anEntry.isEmpty() ) {
485 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
486 if ( aSO && aSO->GetStudy()->GetUseCaseBuilder()->IsUseCaseNode(aSO) ) {
487 _PTR(UseCaseIterator) it = aSO->GetStudy()->GetUseCaseBuilder()->GetUseCaseIterator( aSO );
488 for (it->Init(false); it->More(); it->Next()) nb++;
495 bool GEOMGUI_Selection::hasConcealedChildren( const int index ) const
498 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
501 QString anEntry = entry( index );
502 _PTR(Study) study = appStudy->studyDS();
503 if ( study && !anEntry.isEmpty() ) {
504 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
505 OK = !expandable( aSO ) && hasChildren( aSO );
511 bool GEOMGUI_Selection::hasDisclosedChildren( const int index ) const
514 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
517 QString anEntry = entry( index );
518 _PTR(Study) study = appStudy->studyDS();
519 if ( study && !anEntry.isEmpty() ) {
520 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
521 OK = expandable( aSO ) && hasChildren( aSO );
527 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
529 GEOM::GEOM_Object_var obj = getObject( index );
530 return isCompoundOfVertices( obj );
533 bool GEOMGUI_Selection::isComponent( const int index ) const
535 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
538 QString anEntry = entry( index );
539 _PTR(Study) study = appStudy->studyDS();
540 if ( study && !anEntry.isNull() ) {
541 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
542 if ( aSO && aSO->GetFatherComponent() )
543 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
549 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
551 GEOM::GEOM_Object_var o;
552 if ( 0 <= index && index < myObjects.size() )
553 o = GEOM::GEOM_Object::_narrow( myObjects[index] );
557 GEOM::GEOM_BaseObject_ptr GEOMGUI_Selection::getBaseObject( const int index ) const
559 GEOM::GEOM_BaseObject_var o;
560 if ( 0 <= index && index < myObjects.size() )
561 o = GEOM::GEOM_BaseObject::_duplicate( myObjects[index] );
565 QString GEOMGUI_Selection::selectionMode() const
567 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
569 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
571 switch ( aGeomGUI->getLocalSelectionMode() )
573 case GEOM_POINT : return "VERTEX";
574 case GEOM_EDGE : return "EDGE";
575 case GEOM_WIRE : return "WIRE";
576 case GEOM_FACE : return "FACE";
577 case GEOM_SHELL : return "SHELL";
578 case GEOM_SOLID : return "SOLID";
579 case GEOM_COMPOUND : return "COMPOUND";
580 case GEOM_ALLOBJECTS : return "ALL";
588 bool GEOMGUI_Selection::topLevel( const int index ) const
590 #ifdef USE_VISUAL_PROP_MAP
591 QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) );
592 if ( v.canConvert<bool>() )
598 SALOME_View* view = GEOM_Displayer::GetActiveView();
599 QString viewType = activeViewType();
600 if ( view && viewType == OCCViewer_Viewer::Type() ) {
601 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
603 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
604 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
605 AIS_ListOfInteractive lst;
606 occPrs->GetObjects( lst );
607 if ( lst.Extent() ) {
608 Handle(AIS_InteractiveObject) io = lst.First();
609 if ( !io.IsNull() ) {
610 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
612 res = (bool)aSh->isTopLevel();
621 bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const
623 #ifdef USE_VISUAL_PROP_MAP
624 QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) );
625 if ( v.canConvert<QString>() ) {
626 Material_Model material;
627 material.fromProperties( v.toString() );
628 return material.isPhysical();
634 SALOME_View* view = GEOM_Displayer::GetActiveView();
635 QString viewType = activeViewType();
637 SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
639 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
640 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
641 AIS_ListOfInteractive lst;
642 occPrs->GetObjects( lst );
643 if ( lst.Extent() ) {
644 Handle(AIS_InteractiveObject) io = lst.First();
645 if ( !io.IsNull() ) {
646 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
648 res = (bool) aSh->Attributes()->ShadingAspect()->
649 Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
653 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
654 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
655 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
657 lst->InitTraversal();
658 vtkActor* actor = lst->GetNextActor();
660 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
662 GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
663 res = mat->GetPhysical();
664 } // if ( salome actor )
666 } // if ( lst == vtkPrs->GetObjects() )
674 bool GEOMGUI_Selection::isFolder( const int index ) const
677 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
680 QString anEntry = entry( index );
681 _PTR(Study) study = appStudy->studyDS();
682 if ( study && !anEntry.isNull() ) {
683 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
684 if ( aSO ) res = isFolder( aSO );
690 bool GEOMGUI_Selection::hasDimensions( const int theIndex, bool& theHidden, bool& theVisible ) const
692 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
699 QString anEntry = entry( theIndex );
700 _PTR(Study) aStudy = appStudy->studyDS();
701 if ( !aStudy || anEntry.isNull() )
706 GEOMGUI_DimensionProperty aDimensions( appStudy, anEntry.toStdString() );
711 for ( int it = 0; it < aDimensions.GetNumber(); ++it )
713 if ( aDimensions.IsVisible( it ) )
719 return aDimensions.GetNumber() > 0;
722 bool GEOMGUI_Selection::hasHiddenDimensions( const int theIndex ) const
724 bool isAnyVisible = false;
725 bool isAnyHidden = false;
726 if ( !hasDimensions( theIndex, isAnyHidden, isAnyVisible ) )
734 bool GEOMGUI_Selection::hasVisibleDimensions( const int theIndex ) const
736 bool isAnyVisible = false;
737 bool isAnyHidden = false;
738 if ( !hasDimensions( theIndex, isAnyHidden, isAnyVisible ) )