1 // Copyright (C) 2007-2010 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 // GEOM GEOMGUI : GUI for Geometry component
24 // File : DisplayGUI.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
27 #include "DisplayGUI.h"
28 #include <GeometryGUI.h>
29 #include "GeometryGUI_Operations.h"
30 #include <GEOM_Displayer.h>
31 #include <GEOM_AISShape.hxx>
32 #include <GEOM_Actor.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_OverrideCursor.h>
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
41 #include <SALOME_ListIO.hxx>
42 #include <SALOME_ListIteratorOfListIO.hxx>
44 #include <SVTK_ViewWindow.h>
45 #include <SVTK_View.h>
46 #include <SVTK_ViewModel.h>
47 #include <SOCC_ViewModel.h>
51 #include <QtxActionMenuMgr.h>
53 #include <SalomeApp_Application.h>
54 #include <LightApp_SelectionMgr.h>
55 #include <SalomeApp_Study.h>
57 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <vtkActorCollection.h>
60 #include <vtkRenderer.h>
64 //=======================================================================
65 // function : DisplayGUI::DisplayGUI()
66 // purpose : Constructor
67 //=======================================================================
68 DisplayGUI::DisplayGUI( GeometryGUI* parent ) : GEOMGUI( parent )
72 //=======================================================================
73 // function : DisplayGUI::~DisplayGUI()
74 // purpose : Destructor
75 //=======================================================================
76 DisplayGUI::~DisplayGUI()
81 //=======================================================================
82 // function : DisplayGUI::OnGUIEvent()
83 // purpose : Dispatch menu command
84 //=======================================================================
85 bool DisplayGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
87 SalomeApp_Application* app = getGeometryGUI()->getApp();
88 if (!app) return false;
90 LightApp_SelectionMgr *Sel = app->selectionMgr();
91 SALOME_ListIO selected;
92 Sel->selectedObjects( selected );
94 switch ( theCommandID ) {
95 case GEOMOp::OpDisplayMode: // MENU VIEW - DISPLAY MODE - WIREFRAME/SHADING
97 getGeometryGUI()->action( GEOMOp::OpDisplayMode )->setText
98 ( GetDisplayMode() == 1 ? tr( "GEOM_MEN_WIREFRAME" ) : tr("GEOM_MEN_SHADING") );
99 getGeometryGUI()->menuMgr()->update();
101 case GEOMOp::OpShowAll: // MENU VIEW - SHOW ALL
102 getGeometryGUI()->EmitSignalDeactivateDialog();
105 case GEOMOp::OpShowOnly: // POPUP MENU - SHOW ONLY
106 getGeometryGUI()->EmitSignalDeactivateDialog();
109 case GEOMOp::OpHideAll: // MENU VIEW - HIDE ALL
112 case GEOMOp::OpHide: // POPUP MENU - HIDE
115 case GEOMOp::OpShow: // POPUP MENU - SHOW
116 getGeometryGUI()->EmitSignalDeactivateDialog();
119 case GEOMOp::OpSwitchVectors: // MENU VIEW - DISPLAY MODE - SHOW EDGE DIRECTION
120 SetVectorMode(!GetVectorMode());
121 getGeometryGUI()->action( GEOMOp::OpSwitchVectors )->setText
122 ( GetVectorMode() ? tr("MEN_VECTOR_MODE_ON") : tr( "MEN_VECTOR_MODE_OFF" ) );
123 getGeometryGUI()->menuMgr()->update();
125 case GEOMOp::OpWireframe: // POPUP MENU - DISPLAY MODE - WIREFRAME
126 ChangeDisplayMode( 0 );
128 case GEOMOp::OpShading: // POPUP MENU - DISPLAY MODE - SHADING
129 ChangeDisplayMode( 1 );
131 case GEOMOp::OpVectors: // POPUP MENU - DISPLAY MODE - SHOW EDGE DIRECTION
132 ChangeDisplayMode( 2 );
135 app->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
138 Sel->setSelectedObjects( selected );
142 //=====================================================================================
143 // function : DisplayGUI::DisplayAll()
144 // purpose : Display all GEOM objects
145 //=====================================================================================
146 void DisplayGUI::DisplayAll()
148 SalomeApp_Application* app = getGeometryGUI()->getApp();
151 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
152 if ( !appStudy ) return;
153 _PTR(Study) aStudy = appStudy->studyDS();
154 if ( !aStudy ) return;
155 _PTR(SComponent) SC ( aStudy->FindComponent( "GEOM" ) );
159 SALOME_ListIO listIO;
160 _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( SC ) );
161 anIter->InitEx( true );
163 SUIT_OverrideCursor();
165 while( anIter->More() ) {
166 _PTR(SObject) valSO ( anIter->Value() );
168 if ( !valSO->ReferencedObject( refSO ) ) {
169 listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
170 SC->ComponentDataType().c_str(),
171 valSO->GetName().c_str()) );
175 GEOM_Displayer( appStudy ).Display( listIO, true );
178 //=====================================================================================
179 // function : DisplayGUI::EraseAll()
180 // purpose : Erase all GEOM objects
181 //=====================================================================================
182 void DisplayGUI::EraseAll()
184 SUIT_OverrideCursor();
186 SUIT_Application* app = getGeometryGUI()->getApp();
188 SUIT_ViewWindow* vw = app->desktop()->activeWindow();
190 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
191 SUIT_ViewManager* vman = vw->getViewManager();
192 if ( vman->getType() == OCCViewer_Viewer::Type() ||
193 vman->getType() == SVTK_Viewer::Type() ) {
194 GEOM_Displayer( appStudy ).EraseAll();
200 //=====================================================================================
201 // function : DisplayGUI::DisplayOnly()
202 // purpose : Display selected GEOM objects and erase other
203 //=====================================================================================
204 void DisplayGUI::DisplayOnly()
210 //=====================================================================================
211 // function : DisplayGUI::Display()
212 // purpose : Display selected GEOM objects
213 //=====================================================================================
214 void DisplayGUI::Display()
216 SALOME_ListIO listIO;
218 SalomeApp_Application* app = getGeometryGUI()->getApp();
221 SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
222 if ( !anActiveStudy ) return;
224 //get SalomeApp selection manager
225 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
226 if ( !aSelMgr ) return;
229 aSelMgr->selectedObjects( aList );
230 SALOME_ListIteratorOfListIO It( aList );
232 SUIT_OverrideCursor();
234 for( ;It.More();It.Next() ) {
235 Handle(SALOME_InteractiveObject) anIObject = It.Value();
236 if ( anIObject->hasEntry() ) {
237 _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
238 if ( SO && QString(SO->GetID().c_str()) == QString(SO->GetFatherComponent()->GetID().c_str()) ) {
239 _PTR(SComponent) SC ( SO->GetFatherComponent() );
240 // if component is selected
242 _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
243 anIter->InitEx( true );
244 while( anIter->More() ) {
245 _PTR(SObject) valSO ( anIter->Value() );
247 if ( !valSO->ReferencedObject( refSO ) ) {
248 listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
249 SC->ComponentDataType().c_str(),
250 valSO->GetName().c_str()) );
257 listIO.Append( anIObject );
261 listIO.Append( anIObject );
264 GEOM_Displayer( anActiveStudy ).Display( listIO, true );
268 //=====================================================================================
269 // function : DisplayGUI::Erase()
270 // purpose : Erase selected GEOM objects
271 //=====================================================================================
272 void DisplayGUI::Erase()
274 SALOME_ListIO listIO;
276 SalomeApp_Application* app = getGeometryGUI()->getApp();
279 SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
280 if ( !anActiveStudy ) return;
282 //get SalomeApp selection manager
283 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
284 if ( !aSelMgr ) return;
287 aSelMgr->selectedObjects( aList );
288 SALOME_ListIteratorOfListIO It( aList );
290 SUIT_OverrideCursor();
292 for( ; It.More(); It.Next() ) {
293 Handle(SALOME_InteractiveObject) anIObject = It.Value();
294 if ( anIObject->hasEntry() ) {
295 _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
296 if ( SO && QString(SO->GetID().c_str()) == QString(SO->GetFatherComponent()->GetID().c_str()) ) {
297 _PTR(SComponent) SC ( SO->GetFatherComponent() );
298 // if component is selected
300 _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
301 anIter->InitEx( true );
302 while( anIter->More() ) {
303 _PTR(SObject) valSO ( anIter->Value() );
305 if ( !valSO->ReferencedObject( refSO ) ) {
306 listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
307 SC->ComponentDataType().c_str(),
308 valSO->GetName().c_str()) );
315 listIO.Append( anIObject );
319 listIO.Append( anIObject );
323 SUIT_ViewWindow* viewWindow = app->desktop()->activeWindow();
324 bool aIsForced = true;
325 if(viewWindow->getViewManager()->getType() == SVTK_Viewer::Type())
328 GEOM_Displayer(anActiveStudy).Erase( listIO, aIsForced);
329 getGeometryGUI()->getApp()->selectionMgr()->clearSelected();
332 //=====================================================================================
333 // function : DisplayGUI::SetDisplayMode()
334 // purpose : Set display mode for the viewer (current viewer if <viewWindow> - 0 )
335 //=====================================================================================
336 void DisplayGUI::SetDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
338 SUIT_OverrideCursor();
341 viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
342 if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
343 SVTK_View* aView = ((SVTK_ViewWindow*)viewWindow)->getView();
344 aView->SetDisplayMode( mode );
346 else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
347 OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
348 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
349 AIS_DisplayMode newmode = (mode == 1 ? AIS_Shaded : AIS_WireFrame);
350 AIS_ListOfInteractive List;
351 ic->DisplayedObjects( List );
352 AIS_ListOfInteractive List1;
353 ic->ObjectsInCollector( List1 );
354 List.Append( List1 );
356 AIS_ListIteratorOfListOfInteractive ite( List );
357 while( ite.More() ) {
358 if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
359 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
360 ic->SetDisplayMode( aSh, Standard_Integer( newmode ),true );
365 ic->SetDisplayMode( newmode, Standard_False );
369 //=====================================================================================
370 // function : DisplayGUI::GetDisplayMode()
371 // purpose : Get display mode of the viewer (current viewer if <viewWindow> - 0 )
372 //=====================================================================================
373 int DisplayGUI::GetDisplayMode( SUIT_ViewWindow* viewWindow )
377 viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
378 if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
379 SVTK_View* aView = ((SVTK_ViewWindow*)viewWindow)->getView();
380 dispMode = aView->GetDisplayMode();
382 else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
383 OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
384 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
385 AIS_DisplayMode mode = (AIS_DisplayMode)ic->DisplayMode();
386 dispMode = (mode == AIS_WireFrame ? 0 : 1 );
391 //=====================================================================================
392 // function : DisplayGUI::SetVectorsMode()
393 // purpose : Set vector mode for the viewer
394 //=====================================================================================
395 void DisplayGUI::SetVectorMode( const bool mode, SUIT_ViewWindow* viewWindow )
397 SUIT_OverrideCursor();
400 viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
401 if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
402 viewWindow->setCustomData( "VectorsMode", QVariant( mode ) );
403 SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
404 vtkActorCollection* allActors = vw->getRenderer()->GetActors();
405 allActors->InitTraversal();
406 while (vtkActor* actor = allActors->GetNextActor()) {
407 if (actor->GetVisibility()) { // only for visible actors
408 GEOM_Actor* aGeomActor = 0;
409 if ( actor->IsA( "GEOM_Actor" ) ) {
410 aGeomActor = GEOM_Actor::SafeDownCast( actor );
412 aGeomActor->SetVectorMode( mode );
417 else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
418 viewWindow->setCustomData( "VectorsMode", QVariant( mode ) );
419 OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
420 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
421 AIS_ListOfInteractive List;
422 ic->DisplayedObjects( List );
423 AIS_ListOfInteractive List1;
424 ic->ObjectsInCollector( List1 );
425 List.Append( List1 );
427 AIS_ListIteratorOfListOfInteractive ite( List );
428 while( ite.More() ) {
429 if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
430 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
431 aSh->SetDisplayVectors(mode);
432 ic->RecomputePrsOnly(ite.Value());
439 //=====================================================================================
440 // function : DisplayGUI::GetVectorMode()
441 // purpose : Get the "show edge direction" mode of the viewer
442 //=====================================================================================
443 int DisplayGUI::GetVectorMode( SUIT_ViewWindow* viewWindow )
446 viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
447 return viewWindow->getCustomData( "VectorsMode" ).toBool();
450 //=====================================================================================
451 // function : DisplayGUI::InvertDisplayMode()
452 // purpose : Invert display mode ( shadin <-> wireframe ) for the viewer
453 // (current viewer if <viewWindow> = 0 )
454 //=====================================================================================
455 void DisplayGUI::InvertDisplayMode( SUIT_ViewWindow* viewWindow )
457 SetDisplayMode( 1 - GetDisplayMode( viewWindow ) );
460 //=====================================================================================
461 // function : DisplayGUI::ChangeDisplayMode()
462 // purpose : Set display mode for selected objects in the viewer given
463 // (current viewer if <viewWindow> = 0 )
464 //=====================================================================================
465 void DisplayGUI::ChangeDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
467 SalomeApp_Application* app = getGeometryGUI()->getApp();
471 viewWindow = app->desktop()->activeWindow();
473 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
474 if ( !aSelMgr ) return;
476 SUIT_OverrideCursor();
480 if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
481 SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
482 SVTK_View* aView = vw->getView();
484 aSelMgr->selectedObjects( aList );
485 SALOME_ListIteratorOfListIO It( aList );
487 for( ;It.More(); It.Next() ) {
488 SVTK_Viewer* stvkViewer = dynamic_cast<SVTK_Viewer*>(vw->getViewManager()->getViewModel());
490 stvkViewer ? dynamic_cast<SVTK_Prs*>( stvkViewer->CreatePrs( It.Value()->getEntry() ) ) : 0;
491 if ( vtkPrs && !vtkPrs->IsNull() ) {
493 aView->ChangeRepresentationToWireframe( vtkPrs->GetObjects() );
494 else if ( mode == 1 )
495 aView->ChangeRepresentationToSurface( vtkPrs->GetObjects() );
496 else if ( mode == 2 ) {
497 vtkActorCollection* anActors = vtkPrs->GetObjects();
498 anActors->InitTraversal();
499 while (vtkActor* anAct = anActors->GetNextActor()) {
500 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(anAct);
501 aGeomActor->SetVectorMode(!aGeomActor->GetVectorMode());
508 else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
509 OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
510 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
512 aSelMgr->selectedObjects( aList );
513 SALOME_ListIteratorOfListIO It( aList );
515 for( ;It.More(); It.Next() ) {
516 SOCC_Viewer* soccViewer = (SOCC_Viewer*)(viewWindow->getViewManager()->getViewModel());
517 SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( It.Value()->getEntry() ) );
518 if ( occPrs && !occPrs->IsNull() ) {
519 AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
520 AIS_ListIteratorOfListOfInteractive interIter( shapes );
521 for ( ; interIter.More(); interIter.Next() ) {
523 ic->SetDisplayMode( interIter.Value(), AIS_WireFrame, false );
524 else if ( mode == 1 )
525 ic->SetDisplayMode( interIter.Value(), AIS_Shaded, false );
527 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( interIter.Value() );
528 if ( !aSh.IsNull() ) {
529 aSh->SetDisplayVectors(!aSh->isShowVectors());
530 ic->RecomputePrsOnly(interIter.Value());
536 ic->UpdateCurrentViewer();
540 //=====================================================================================
542 //=====================================================================================
546 __declspec( dllexport )
548 GEOMGUI* GetLibGUI( GeometryGUI* parent )
550 return new DisplayGUI( parent );