1 // Copyright (C) 2007-2015 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 #include "OCCViewer_ViewModel.h"
24 #include "OCCViewer_ViewWindow.h"
25 #include "OCCViewer_ViewFrame.h"
26 #include "OCCViewer_VService.h"
27 #include "OCCViewer_ViewPort3d.h"
28 #include "OCCViewer_ClippingDlg.h"
29 #include "OCCViewer_Utilities.h"
31 #include "SUIT_ViewWindow.h"
32 #include "SUIT_ViewManager.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_ResourceMgr.h"
37 #include "ViewerData_AISShape.hxx"
39 #include <Basics_OCCTVersion.hxx>
41 #include "QtxActionToolMgr.h"
42 #include "QtxBackgroundTool.h"
45 #include <QApplication>
46 #include <QColorDialog>
47 #include <QFileDialog>
51 #include <QMouseEvent>
53 #include <QDesktopWidget>
55 #include <AIS_Axis.hxx>
56 #if OCC_VERSION_LARGE > 0x06080000
57 #include <Prs3d_Drawer.hxx>
59 #include <AIS_Drawer.hxx>
61 #include <AIS_ListOfInteractive.hxx>
62 #include <AIS_ListIteratorOfListOfInteractive.hxx>
64 #include <Graphic3d_Texture2Dmanual.hxx>
65 #include <Graphic3d_MaterialAspect.hxx>
66 #include <Graphic3d_TextureParams.hxx>
68 #include <Geom_Axis2Placement.hxx>
69 #include <Prs3d_Drawer.hxx>
70 #include <Prs3d_DatumAspect.hxx>
71 #include <Prs3d_LineAspect.hxx>
72 #include <Prs3d_TextAspect.hxx>
74 #include <Visual3d_View.hxx>
77 Get data for supported background modes: gradient types, identifiers and supported image formats
79 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
81 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
82 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
83 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
84 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
85 idList << HorizontalGradient << VerticalGradient <<
86 Diagonal1Gradient << Diagonal2Gradient <<
87 Corner1Gradient << Corner2Gradient <<
88 Corner3Gradient << Corner4Gradient;
89 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
90 return tr("BG_IMAGE_FILES");
94 Get data for supported stereo pair modes: stereo types and identifiers
96 void OCCViewer_Viewer::stereoData( QStringList& typeList, QIntList& idList)
98 typeList << tr("ST_QUADBUFFER") << tr("ST_ANAGLYPH") <<
99 tr("ST_ROWINTERLACED") << tr("ST_COLUMNINTERLACED") <<
100 tr("ST_CHESSBOARD") << tr("ST_SIDEBYSIDE") <<
102 idList << QuadBufferType << AnaglyphType <<
103 RowInterlacedType << ColumnInterlacedType <<
104 ChessBoardType << SideBySideType <<
110 \param DisplayTrihedron - is trihedron displayed
112 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
114 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
117 myTrihedronSize(100),
120 // init CasCade viewers
121 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
122 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
123 myV3dViewer->SetDefaultLights();
126 myAISContext = new AIS_InteractiveContext( myV3dViewer );
127 myAISContext->SelectionColor( Quantity_NOC_WHITE );
129 // display isoline on planar faces (box for ex.)
130 myAISContext->IsoOnPlane( true );
132 /* create trihedron */
133 if ( DisplayTrihedron )
135 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
136 myTrihedron = new AIS_Trihedron(anAxis);
137 myTrihedron->SetInfiniteState( Standard_True );
139 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
140 //myTrihedron->SetColor( Col );
141 myTrihedron->SetArrowColor( Col.Name() );
142 myTrihedron->SetSize(100);
143 #if OCC_VERSION_LARGE > 0x06080000
144 Handle(Prs3d_Drawer) drawer = myTrihedron->Attributes();
145 if (drawer->HasOwnDatumAspect()) {
147 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
148 if (drawer->HasDatumAspect()) {
150 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
151 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
152 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
153 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
157 // set interaction style to standard
158 myInteractionStyle = 0;
160 // set zooming style to standard
164 myPreselectionEnabled = true;
167 mySelectionEnabled = true;
168 myMultiSelectionEnabled = true;
170 // set projection type to orthographic
171 myProjectionType = 0;
172 // set stereo parameters
174 myAnaglyphFilter = 0;
175 myToReverseStereo = 0;
177 myQuadBufferSupport = 0;
178 myStereographicFocusType = 1;
179 myInterocularDistanceType = 1;
180 myStereographicFocusValue = 1.0;
181 myInterocularDistanceValue = 0.05;
182 //set clipping color and texture to standard
183 myClippingColor = QColor( 50, 50, 50 );
184 myDefaultTextureUsed = true;
185 myClippingTexture = QString();
186 myTextureModulated = true;
187 myClippingTextureScale = 1.0;
194 OCCViewer_Viewer::~OCCViewer_Viewer()
196 myAISContext.Nullify();
197 myV3dViewer.Nullify();
202 \return background color of viewer
204 QColor OCCViewer_Viewer::backgroundColor() const
206 return backgroundColor(0);
210 \return background data of viewer
212 Qtx::BackgroundData OCCViewer_Viewer::background() const
214 return background(0);
218 Sets background color [obsolete]
219 \param c - new background color
221 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
223 setBackgroundColor( 0, c );
228 \param d - new background data
230 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
232 setBackground( 0, theBackground );
236 Start initialization of view window
237 \param view - view window to be initialized
239 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
243 view->initSketchers();
244 view->setInteractionStyle( interactionStyle() );
245 view->setProjectionType( projectionType() );
246 view->setStereoType( stereoType() );
247 view->setAnaglyphFilter( anaglyphFilter() );
248 view->setStereographicFocus( stereographicFocusType(), stereographicFocusValue() );
249 view->setInterocularDistance( interocularDistanceType(), interocularDistanceValue() );
250 view->setReverseStereo( isReverseStereo() );
251 view->setVSync( isVSync() );
252 view->setQuadBufferSupport( isQuadBufferSupport() );
253 view->setZoomingStyle( zoomingStyle() );
254 view->enablePreselection( isPreselectionEnabled() );
255 view->enableSelection( isSelectionEnabled() );
257 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
260 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
261 // connect signal from viewport
262 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
263 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
269 Creates new view window
270 \param theDesktop - main window of application
272 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
275 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
276 // get main view window (created by view frame)
277 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
278 // initialize main view window
280 // set default background for view window
281 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
287 Sets new view manager
288 \param theViewManager - new view manager
290 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
292 SUIT_ViewModel::setViewManager(theViewManager);
293 if (theViewManager) {
294 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
295 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
297 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
298 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
300 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
301 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
303 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
304 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
309 SLOT: called on mouse button press, stores current mouse position as start point for transformations
311 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
313 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
317 SLOT: called on mouse move, processes transformation or hilighting
319 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
321 if (!mySelectionEnabled) return;
322 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
324 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
326 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
328 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
329 if (aView->getViewPort()->isBusy()) {
330 QCoreApplication::processEvents();
331 return; // Check that the ViewPort initialization completed
332 // To Prevent call move event if the View port is not initialized
335 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
336 if ( !aView3d.IsNull() ) {
337 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
344 SLOT: called on mouse button release, finishes transformation or selection
346 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
348 if (!mySelectionEnabled) return;
349 if (theEvent->button() != Qt::LeftButton) return;
350 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
352 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
356 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
357 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
360 myAISContext->ClearCurrents( false );
364 if (myStartPnt == myEndPnt)
366 if ( !isPreselectionEnabled() ) {
367 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
368 if ( !aView3d.IsNull() ) {
369 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
373 if (aHasShift && myMultiSelectionEnabled)
374 myAISContext->ShiftSelect();
376 myAISContext->Select();
380 if (aHasShift && myMultiSelectionEnabled)
381 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
382 myEndPnt.x(), myEndPnt.y(),
383 aView->getViewPort()->getView(), Standard_False );
385 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
386 myEndPnt.x(), myEndPnt.y(),
387 aView->getViewPort()->getView(), Standard_False );
389 int Nb = myAISContext->NbSelected();
390 if( Nb>1 && !myMultiSelectionEnabled )
392 myAISContext->InitSelected();
393 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
394 if( !anOwner.IsNull() )
396 myAISContext->ClearSelected( Standard_False );
397 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
401 myAISContext->UpdateCurrentViewer();
403 emit selectionChanged();
407 SLOT: called on key press, processes selection in "key free" interaction style
409 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
411 if (!mySelectionEnabled) return;
413 OCCViewer_ViewWindow* aView = qobject_cast<OCCViewer_ViewWindow*>( theWindow );
414 if ( !aView ) return;
416 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
418 switch ( theEvent->key() ) {
421 myAISContext->ClearCurrents( false );
425 if ( !isPreselectionEnabled() ) {
426 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
427 if ( !aView3d.IsNull() ) {
428 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
432 if (aHasShift && myMultiSelectionEnabled)
433 myAISContext->ShiftSelect();
435 myAISContext->Select();
437 emit selectionChanged();
441 if ( isPreselectionEnabled() ) {
442 if ( getAISContext()->HasOpenedContext() )
443 getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
447 if ( isPreselectionEnabled() ) {
448 if ( getAISContext()->HasOpenedContext() )
449 getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
457 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
459 Standard_Integer aViewsNb = 0;
460 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
462 if ( aViewsNb < 2 ) {
463 //clean up presentations before last view is closed
464 myAISContext->RemoveAll(Standard_False);
468 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
470 setTrihedronShown( true );
471 bool showStaticTrihedron = true;
472 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
473 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
474 viewPort->showStaticTrihedron( showStaticTrihedron );
477 int OCCViewer_Viewer::getTopLayerId()
479 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
480 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
486 \return interaction style
488 int OCCViewer_Viewer::interactionStyle() const
490 return myInteractionStyle;
494 Sets interaction style: 0 - standard, 1 - keyboard free interaction
495 \param theStyle - new interaction style
497 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
499 myInteractionStyle = theStyle;
500 //!! To be done for view windows
501 if ( !myViewManager )
504 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
505 for ( int i = 0; i < (int)wins.count(); i++ )
507 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
509 win->setInteractionStyle( theStyle );
514 \return projection type
516 int OCCViewer_Viewer::projectionType() const
518 return myProjectionType;
522 Sets projection type: 0 - orthographic, 1 - perspective
523 \param theType - new projection type
525 void OCCViewer_Viewer::setProjectionType( const int theType )
527 if ( myProjectionType != theType ) {
528 if ( theType != OCCViewer_ViewWindow::Stereo )
529 myProjectionType = theType;
531 if ( !myViewManager )
534 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
535 for ( int i = 0; i < (int)wins.count(); i++ )
537 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
539 win->setProjectionType( (OCCViewer_ViewWindow::ProjectionType)theType );
547 int OCCViewer_Viewer::stereoType() const
554 \param theType - new stereo type
556 void OCCViewer_Viewer::setStereoType( const int theType )
558 myStereoType = theType;
560 if ( !myViewManager )
563 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
564 for ( int i = 0; i < (int)wins.count(); i++ )
566 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
568 win->setStereoType( (OCCViewer_ViewWindow::StereoType)theType );
573 \return stereographic focus type
575 int OCCViewer_Viewer::stereographicFocusType() const
577 return myStereographicFocusType;
581 \return stereographic focus value
583 double OCCViewer_Viewer::stereographicFocusValue() const
585 return myStereographicFocusValue;
589 Sets stereographic focus parameters
590 \param theType - new stereographic focus type
591 \param theValue - new stereographic focus value
593 void OCCViewer_Viewer::setStereographicFocus( const int theType, const double theValue )
595 myStereographicFocusType = theType;
596 myStereographicFocusValue = theValue;
598 if ( !myViewManager )
601 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
602 for ( int i = 0; i < (int)wins.count(); i++ )
604 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
606 win->setStereographicFocus( (OCCViewer_ViewWindow::FocusIODType)theType, theValue );
611 \return stereographic focus type
613 int OCCViewer_Viewer::interocularDistanceType() const
615 return myInterocularDistanceType;
619 \return stereographic focus value
621 double OCCViewer_Viewer::interocularDistanceValue() const
623 return myInterocularDistanceValue;
627 Sets interocular distance parameters
628 \param theType - new IOD type
629 \param theValue - new IOD value
631 void OCCViewer_Viewer::setInterocularDistance( const int theType, const double theValue )
633 myInterocularDistanceType = theType;
634 myInterocularDistanceValue = theValue;
636 if ( !myViewManager )
639 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
640 for ( int i = 0; i < (int)wins.count(); i++ )
642 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
644 win->setInterocularDistance( (OCCViewer_ViewWindow::FocusIODType)theType, theValue );
649 \return anaglyph filter
651 int OCCViewer_Viewer::anaglyphFilter() const
653 return myAnaglyphFilter;
658 \param theType - new anaglyph filter
660 void OCCViewer_Viewer::setAnaglyphFilter( const int theType )
662 myAnaglyphFilter = theType;
664 if ( !myViewManager )
667 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
668 for ( int i = 0; i < (int)wins.count(); i++ )
670 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
672 win->setAnaglyphFilter( (OCCViewer_ViewWindow::AnaglyphFilter)theType );
677 \return reverse stereo
679 bool OCCViewer_Viewer::isReverseStereo() const
681 return myToReverseStereo;
686 \param theReverse - enable/disable reverse mode
688 void OCCViewer_Viewer::setReverseStereo( const bool theReverse )
690 myToReverseStereo = theReverse;
692 if ( !myViewManager )
695 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
696 for ( int i = 0; i < (int)wins.count(); i++ )
698 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
700 win->setReverseStereo( theReverse );
707 bool OCCViewer_Viewer::isVSync() const
714 \param theEnable - enable/disable V-Sync mode
716 void OCCViewer_Viewer::setVSync( const bool theEnable )
718 myVSyncMode = theEnable;
720 if ( !myViewManager )
723 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
724 for ( int i = 0; i < (int)wins.count(); i++ )
726 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
728 win->setVSync( theEnable );
733 \return support quad-buffered stereo
735 bool OCCViewer_Viewer::isQuadBufferSupport() const
737 return myQuadBufferSupport;
741 Set support quad-buffered stereo
742 \param theEnable - enable/disable support quad-buffered stereo
744 void OCCViewer_Viewer::setQuadBufferSupport( const bool theEnable )
746 myQuadBufferSupport = theEnable;
748 if ( !myViewManager )
751 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
752 for ( int i = 0; i < (int)wins.count(); i++ )
754 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
756 win->setQuadBufferSupport( theEnable );
761 \return zooming style
763 int OCCViewer_Viewer::zoomingStyle() const
765 return myZoomingStyle;
769 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
770 \param theStyle - new zooming style
772 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
774 myZoomingStyle = theStyle;
775 //!! To be done for view windows
776 if ( !myViewManager )
779 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
780 for ( int i = 0; i < (int)wins.count(); i++ )
782 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
784 win->setZoomingStyle( theStyle );
789 \return true if preselection is enabled
791 bool OCCViewer_Viewer::isPreselectionEnabled() const
793 return myPreselectionEnabled;
797 Enables/disables preselection
798 \param isEnabled - new status
800 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
802 myPreselectionEnabled = isEnabled;
804 if ( !myViewManager )
807 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
808 for ( int i = 0; i < (int)wins.count(); i++ )
810 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
812 win->enablePreselection( isEnabled );
818 \return true if selection is enabled
820 bool OCCViewer_Viewer::isSelectionEnabled() const
822 return mySelectionEnabled;
826 Enables/disables selection
827 \param isEnabled - new status
829 void OCCViewer_Viewer::enableSelection(bool isEnabled)
831 mySelectionEnabled = isEnabled;
833 //!! To be done for view windows
834 if ( !myViewManager )
837 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
838 for ( int i = 0; i < (int)wins.count(); i++ )
840 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
842 win->updateEnabledDrawMode();
843 win->enableSelection( isEnabled );
848 //clear current selection in the viewer
850 myAISContext->ClearSelected( Standard_True );
856 Sets multiselection enabled status
857 \param isEnabled - new status
859 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
861 myMultiSelectionEnabled = isEnable;
862 //!! To be done for view windows
863 if ( !myViewManager )
866 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
867 for ( int i = 0; i < (int)wins.count(); i++ )
869 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
871 win->updateEnabledDrawMode();
876 Sets a color of the clipped region
877 \param theColor - a new color of the clipped region
879 void OCCViewer_Viewer::setClippingColor( const QColor& theColor )
881 myClippingColor = theColor;
883 if( myInternalClipPlanes.IsEmpty() )
886 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
887 aMaterialAspect.SetColor( Quantity_Color( theColor.redF(), theColor.greenF(),
888 theColor.blueF(), Quantity_TOC_RGB ) );
890 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
891 myInternalClipPlanes.Value(i)->SetCappingMaterial( aMaterialAspect );
897 \return clipping color
899 QColor OCCViewer_Viewer::clippingColor() const
901 return myClippingColor;
904 // initialize a texture for clipped region
905 Handle(Graphic3d_Texture2Dmanual) initClippingTexture( const bool isDefault, const QString& theTexture,
906 const bool isModulate, const double theScale )
908 QString aTextureFile = isDefault ? ":images/hatch.png" : theTexture;
909 QPixmap px( aTextureFile );
910 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
911 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
912 if( aTexture->IsDone() ) {
913 aTexture->EnableRepeat();
914 isModulate ? aTexture->EnableModulate() : aTexture->DisableModulate();
915 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 1/( theScale*100 ), -1 / ( theScale*100 ) ) );
921 Sets default texture parameters
922 \param isDefault - use/non-use default texture
923 \param theTexture - new texture of the clipped region
924 \param isModulate - enable/disable texture modulate mode
925 \param theScale - scale factor.
927 void OCCViewer_Viewer::setClippingTextureParams( const bool isDefault, const QString& theTexture,
928 const bool isModulate, const double theScale )
930 myDefaultTextureUsed = isDefault;
931 myClippingTexture = theTexture;
932 myTextureModulated = isModulate;
933 myClippingTextureScale = theScale;
935 if( myInternalClipPlanes.IsEmpty() )
938 Handle(Graphic3d_Texture2Dmanual) aTexture =
939 initClippingTexture( myDefaultTextureUsed, myClippingTexture,
940 myTextureModulated, myClippingTextureScale );
942 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
943 myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
949 \return true if default texture is used
951 bool OCCViewer_Viewer::isDefaultTextureUsed() const
953 return myDefaultTextureUsed;
957 \return clipping texture
959 QString OCCViewer_Viewer::clippingTexture() const
961 return myClippingTexture;
965 \return true if texture is modulated
967 bool OCCViewer_Viewer::isTextureModulated() const
969 return myTextureModulated;
973 \return scale factor of texture
975 double OCCViewer_Viewer::clippingTextureScale() const
977 return myClippingTextureScale;
981 Builds popup for occ viewer
983 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
985 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
986 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
988 thePopup->addSeparator();
990 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
992 //Support of several toolbars in the popup menu
993 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
994 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
995 for ( ; it!=last; it++ ) {
996 if ( (*it)->parentWidget()->isVisible() )
997 thePopup->addAction( (*it)->toggleViewAction() );
1002 SLOT: called on dump view operation is activated, stores scene to raster file
1004 void OCCViewer_Viewer::onDumpView()
1006 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1008 aView->onDumpView();
1012 SLOT: called if background color is to be changed changed, passes new color to view port
1014 void OCCViewer_Viewer::onChangeBackground()
1016 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
1020 // get supported gradient types
1021 QStringList gradList;
1022 QIntList idList, txtList;
1023 QString formats = backgroundData( gradList, idList, txtList );
1025 // invoke dialog box
1026 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
1027 aView, // parent for dialog box
1028 txtList, // allowed texture modes
1029 true, // enable solid color mode
1030 true, // enable gradient mode
1031 false, // disable custom gradient mode
1032 !txtList.isEmpty(), // enable/disable texture mode
1033 gradList, // gradient names
1034 idList, // gradient identifiers
1035 formats ); // image formats
1037 // set chosen background data to the viewer
1038 if ( bgData.isValid() )
1039 aView->setBackground( bgData );
1043 Updates OCC 3D viewer
1045 void OCCViewer_Viewer::update()
1047 if (!myV3dViewer.IsNull())
1048 myV3dViewer->Update();
1050 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1052 aView->updateGravityCoords();
1056 \return objects selected in 3D viewer
1057 \param theList - list to be filled with selected objects
1059 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
1062 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
1063 theList.Append(myAISContext->SelectedInteractive());
1067 Selects objects in 3D viewer. Other selected objects are left as selected
1068 \param theList - list objects to be selected
1070 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
1072 AIS_ListIteratorOfListOfInteractive aIt;
1073 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
1074 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
1075 myAISContext->UpdateCurrentViewer();
1079 Auxiliary method to emit signal selectionChanged()
1081 void OCCViewer_Viewer::performSelectionChanged()
1083 emit selectionChanged();
1087 Hilights/unhilights object in viewer
1088 \param obj - object to be updated
1089 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
1090 \param update - update current viewer
1092 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
1093 bool hilight, bool update )
1095 bool isInLocal = myAISContext->HasOpenedContext();
1099 if ( hilight && !myAISContext->IsSelected( obj ) )
1100 myAISContext->AddOrRemoveCurrentObject( obj, false );
1101 else if ( !hilight && myAISContext->IsSelected( obj ) )
1102 myAISContext->AddOrRemoveCurrentObject( obj, false );
1106 myV3dViewer->Redraw();
1112 Unhilights all objects in viewer
1113 \param updateviewer - update current viewer
1115 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
1117 if ( myAISContext->HasOpenedContext() ) {
1119 myAISContext->ClearSelected( updateviewer );
1121 myAISContext->UnhilightSelected( updateviewer );
1125 myAISContext->ClearCurrents( updateviewer );
1127 myAISContext->UnhilightCurrents( updateviewer );
1135 \return true if object is in viewer or in collector
1136 \param obj - object to be checked
1137 \param onlyInViewer - search object only in viewer (so object must be displayed)
1139 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
1142 AIS_ListOfInteractive List;
1143 myAISContext->DisplayedObjects(List);
1145 AIS_ListIteratorOfListOfInteractive ite(List);
1146 for ( ; ite.More(); ite.Next() )
1147 if( ite.Value()==obj )
1154 \return true if object is displayed in viewer
1155 \param obj - object to be checked
1157 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
1159 return myAISContext->IsDisplayed( obj );
1163 Sets color of object
1164 \param obj - object to be updated
1165 \param color - new color
1166 \param update - update current viewer
1168 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
1169 const QColor& color,
1174 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
1175 color.green() / 255.,
1176 color.blue() / 255.,
1178 obj->SetColor( CSFColor );
1182 myV3dViewer->Update();
1186 Changes display mode of object
1187 \param obj - object to be processed
1188 \param mode - new display mode
1189 \param update - update current viewer
1191 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
1192 int mode, bool update )
1194 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
1196 myV3dViewer->Update();
1200 Changes transparency of object
1201 \param obj - object to be processed
1202 \param trans - new transparency
1203 \param update - update current viewer
1205 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
1206 float trans, bool update )
1208 myAISContext->SetTransparency( obj, trans, false );
1209 myAISContext->Redisplay( obj, Standard_False, Standard_True );
1211 myV3dViewer->Update();
1215 Changes visibility of trihedron to opposite
1217 void OCCViewer_Viewer::toggleTrihedron()
1219 setTrihedronShown( !isTrihedronVisible() );
1223 \return true if trihedron is visible
1225 bool OCCViewer_Viewer::isTrihedronVisible() const
1227 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
1231 Sets visibility state of trihedron
1232 \param on - new state
1235 void OCCViewer_Viewer::setTrihedronShown( const bool on )
1237 if ( myTrihedron.IsNull() )
1241 myAISContext->Display( myTrihedron );
1242 myAISContext->Deactivate(myTrihedron);
1245 myAISContext->Erase( myTrihedron );
1250 \return trihedron size
1252 double OCCViewer_Viewer::trihedronSize() const
1255 if ( !myTrihedron.IsNull() )
1256 sz = myTrihedron->Size();
1261 Changes trihedron size
1262 \param sz - new size
1264 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
1266 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
1267 myTrihedronSize = sz;
1268 myIsRelative = isRelative;
1274 Set number of isolines
1275 \param u - u-isolines (first parametric co-ordinate)
1276 \param v - v-isolines (second parametric co-ordinate)
1278 void OCCViewer_Viewer::setIsos( const int u, const int v )
1280 Handle(AIS_InteractiveContext) ic = getAISContext();
1284 ic->SetIsoNumber( u, AIS_TOI_IsoU );
1285 ic->SetIsoNumber( v, AIS_TOI_IsoV );
1289 \return number of isolines
1290 \param u - to return u-isolines (first parametric co-ordinate)
1291 \param v - to return v-isolines (second parametric co-ordinate)
1293 void OCCViewer_Viewer::isos( int& u, int& v ) const
1295 Handle(AIS_InteractiveContext) ic = getAISContext();
1298 u = ic->IsoNumber( AIS_TOI_IsoU );
1299 v = ic->IsoNumber( AIS_TOI_IsoV );
1304 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
1306 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
1308 return new OCCViewer_ViewWindow(0, this);
1312 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
1314 return background( theViewId ).color();
1317 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
1319 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
1323 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
1325 if ( theColor.isValid() ) {
1326 Qtx::BackgroundData bg = background( theViewId );
1327 bg.setColor( theColor );
1328 setBackground( theViewId, bg );
1332 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
1334 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
1335 myBackgrounds[theViewId] = theBackground;
1340 Set the show static trihedron flag
1342 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
1344 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1345 if ( aView ) aView->showStaticTrihedron( on );
1349 Get new and current trihedron size corresponding to the current model size
1351 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
1356 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
1357 Handle(V3d_Viewer) viewer = getViewer3d();
1358 viewer->InitActiveViews();
1359 if(!viewer->MoreActiveViews()) return false;
1361 Handle(V3d_View) view3d = viewer->ActiveView();
1364 if ( view3d.IsNull() )
1367 double aMaxSide = computeSceneSize( view3d );
1370 // The boundary box of the view may be initialized but nullified
1371 // (case of infinite objects)
1372 if ( aMaxSide < Precision::Confusion() )
1375 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
1377 static float EPS = 5.0E-3;
1378 theSize = getTrihedron()->Size();
1379 theNewSize = aMaxSide*aSizeInPercents / 100.0;
1381 return fabs( theNewSize - theSize ) > theSize * EPS ||
1382 fabs( theNewSize - theSize ) > theNewSize * EPS;
1386 * Compute scene size
1388 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
1390 double aMaxSide = 0;
1391 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
1393 #if OCC_VERSION_LARGE > 0x06070100
1394 Bnd_Box aBox = view3d->View()->MinMaxValues();
1395 Xmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
1396 Ymin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
1397 Zmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
1398 Xmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().X();
1399 Ymax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Y();
1400 Zmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Z();
1402 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
1405 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
1406 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
1408 aMaxSide = Xmax - Xmin;
1409 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
1410 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1417 * Update the size of the trihedron
1419 void OCCViewer_Viewer::updateTrihedron() {
1420 if ( myTrihedron.IsNull() )
1424 double newSz, oldSz;
1426 if(computeTrihedronSize(newSz, oldSz))
1427 myTrihedron->SetSize(newSz);
1429 } else if(myTrihedron->Size() != myTrihedronSize) {
1430 myTrihedron->SetSize(myTrihedronSize);
1435 Set number of isolines
1436 \param u - u-isolines (first parametric co-ordinate)
1437 \param v - v-isolines (second parametric co-ordinate)
1439 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1441 myPreselectionEnabled = isPreselectionEnabled;
1442 mySelectionEnabled = isSelectionEnabled;
1443 //clear current selection in the viewer
1445 if(!mySelectionEnabled) {
1446 myAISContext->ClearSelected( Standard_True );
1451 Creates clipping plane based on the incoming plane
1453 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1455 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1456 aGraphic3dPlane->SetOn( theIsOn );
1457 aGraphic3dPlane->SetCapping( Standard_True );
1459 // set capping color
1460 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
1461 aMaterialAspect.SetColor( Quantity_Color( myClippingColor.redF(), myClippingColor.greenF(),
1462 myClippingColor.blueF(), Quantity_TOC_RGB ) );
1463 aGraphic3dPlane->SetCappingMaterial( aMaterialAspect );
1465 // set capping texture
1466 aGraphic3dPlane->SetCappingTexture( initClippingTexture( myDefaultTextureUsed, myClippingTexture,
1467 myTextureModulated, myClippingTextureScale ) );
1469 return aGraphic3dPlane;
1472 Applies clipping planes to clippable objects
1474 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1476 // 1. Remove existing clipping planes
1477 myClipPlanes.clear();
1478 myInternalClipPlanes.Clear();
1480 // 2. Create new clipping planes
1481 ClipPlanesList::iterator inIt = theList.begin();
1482 for (;inIt != theList.end(); inIt++ )
1484 OCCViewer_ClipPlane aPlane = *inIt;
1486 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1487 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1489 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1490 gp_Dir aDirection( aDx, aDy, aDz );
1492 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1493 myClipPlanes.push_back( aPlane );
1496 // 3. Apply clipping planes
1497 AIS_ListOfInteractive aList;
1498 myAISContext->DisplayedObjects (aList);
1499 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1500 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1501 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1502 if (!aShape.IsNull() && aShape->IsClippable()) {
1503 aShape->SetClipPlanes(myInternalClipPlanes);
1509 Returns the clipping planes applied to the displayed objects.
1511 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1512 return myClipPlanes;
1515 Applies clipping planes to given object objects
1517 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1519 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1520 if (!aShape.IsNull() && aShape->IsClippable())
1522 aShape->SetClipPlanes (myInternalClipPlanes);
1527 Returns the pointer to the clipping dialog box.
1529 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1530 return myClippingDlg;
1535 Stores pointer to the clipping dialog box.
1537 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1538 if(myClippingDlg != theDlg) {
1539 myClippingDlg = theDlg;