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 #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 #include <AIS_Drawer.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
60 #include <Graphic3d_Texture2Dmanual.hxx>
61 #include <Graphic3d_TextureParams.hxx>
63 #include <Geom_Axis2Placement.hxx>
64 #include <Prs3d_Drawer.hxx>
65 #include <Prs3d_DatumAspect.hxx>
66 #include <Prs3d_LineAspect.hxx>
67 #include <Prs3d_TextAspect.hxx>
69 #include <Visual3d_View.hxx>
72 Get data for supported background modes: gradient types, identifiers and supported image formats
74 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
76 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
77 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
78 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
79 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
80 idList << HorizontalGradient << VerticalGradient <<
81 Diagonal1Gradient << Diagonal2Gradient <<
82 Corner1Gradient << Corner2Gradient <<
83 Corner3Gradient << Corner4Gradient;
84 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
85 return tr("BG_IMAGE_FILES");
90 \param DisplayTrihedron - is trihedron displayed
92 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
94 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
100 // init CasCade viewers
101 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
102 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
103 myV3dViewer->SetDefaultLights();
106 myAISContext = new AIS_InteractiveContext( myV3dViewer );
107 myAISContext->SelectionColor( Quantity_NOC_WHITE );
109 // display isoline on planar faces (box for ex.)
110 myAISContext->IsoOnPlane( true );
112 /* create trihedron */
113 if ( DisplayTrihedron )
115 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
116 myTrihedron = new AIS_Trihedron(anAxis);
117 myTrihedron->SetInfiniteState( Standard_True );
119 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
120 //myTrihedron->SetColor( Col );
121 myTrihedron->SetArrowColor( Col.Name() );
122 myTrihedron->SetSize(100);
123 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
124 if (drawer->HasDatumAspect()) {
125 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
126 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
127 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
128 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
132 // set interaction style to standard
133 myInteractionStyle = 0;
135 // set zooming style to standard
139 myPreselectionEnabled = true;
142 mySelectionEnabled = true;
143 myMultiSelectionEnabled = true;
149 OCCViewer_Viewer::~OCCViewer_Viewer()
151 myAISContext.Nullify();
152 myV3dViewer.Nullify();
157 \return background color of viewer
159 QColor OCCViewer_Viewer::backgroundColor() const
161 return backgroundColor(0);
165 \return background data of viewer
167 Qtx::BackgroundData OCCViewer_Viewer::background() const
169 return background(0);
173 Sets background color [obsolete]
174 \param c - new background color
176 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
178 setBackgroundColor( 0, c );
183 \param d - new background data
185 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
187 setBackground( 0, theBackground );
191 Start initialization of view window
192 \param view - view window to be initialized
194 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
198 view->initSketchers();
199 view->setInteractionStyle( interactionStyle() );
200 view->setZoomingStyle( zoomingStyle() );
201 view->enablePreselection( isPreselectionEnabled() );
202 view->enableSelection( isSelectionEnabled() );
204 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
207 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
208 // connect signal from viewport
209 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
210 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
216 Creates new view window
217 \param theDesktop - main window of application
219 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
222 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
223 // get main view window (created by view frame)
224 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
225 // initialize main view window
227 // set default background for view window
228 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
234 Sets new view manager
235 \param theViewManager - new view manager
237 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
239 SUIT_ViewModel::setViewManager(theViewManager);
240 if (theViewManager) {
241 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
242 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
244 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
245 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
247 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
248 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
250 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
251 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
256 SLOT: called on mouse button press, stores current mouse position as start point for transformations
258 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
260 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
264 SLOT: called on mouse move, processes transformation or hilighting
266 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
268 if (!mySelectionEnabled) return;
269 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
271 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
273 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
275 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
276 if (aView->getViewPort()->isBusy()) {
277 QCoreApplication::processEvents();
278 return; // Check that the ViewPort initialization completed
279 // To Prevent call move event if the View port is not initialized
282 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
283 if ( !aView3d.IsNull() ) {
284 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
291 SLOT: called on mouse button release, finishes transformation or selection
293 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
295 if (!mySelectionEnabled) return;
296 if (theEvent->button() != Qt::LeftButton) return;
297 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
299 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
303 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
304 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
307 myAISContext->ClearCurrents( false );
311 if (myStartPnt == myEndPnt)
313 if ( !isPreselectionEnabled() ) {
314 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
315 if ( !aView3d.IsNull() ) {
316 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
320 if (aHasShift && myMultiSelectionEnabled)
321 myAISContext->ShiftSelect();
323 myAISContext->Select();
327 if (aHasShift && myMultiSelectionEnabled)
328 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
329 myEndPnt.x(), myEndPnt.y(),
330 aView->getViewPort()->getView(), Standard_False );
332 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
333 myEndPnt.x(), myEndPnt.y(),
334 aView->getViewPort()->getView(), Standard_False );
336 int Nb = myAISContext->NbSelected();
337 if( Nb>1 && !myMultiSelectionEnabled )
339 myAISContext->InitSelected();
340 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
341 if( !anOwner.IsNull() )
343 myAISContext->ClearSelected( Standard_False );
344 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
348 myAISContext->UpdateCurrentViewer();
350 emit selectionChanged();
354 SLOT: called on key press, processes selection in "key free" interaction style
356 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
358 if (!mySelectionEnabled) return;
360 OCCViewer_ViewWindow* aView = qobject_cast<OCCViewer_ViewWindow*>( theWindow );
361 if ( !aView ) return;
363 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
365 switch ( theEvent->key() ) {
368 myAISContext->ClearCurrents( false );
372 if ( !isPreselectionEnabled() ) {
373 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
374 if ( !aView3d.IsNull() ) {
375 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
379 if (aHasShift && myMultiSelectionEnabled)
380 myAISContext->ShiftSelect();
382 myAISContext->Select();
384 emit selectionChanged();
388 if ( isPreselectionEnabled() ) {
389 if ( getAISContext()->HasOpenedContext() )
390 getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
394 if ( isPreselectionEnabled() ) {
395 if ( getAISContext()->HasOpenedContext() )
396 getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
404 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
406 Standard_Integer aViewsNb = 0;
407 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
409 if ( aViewsNb < 2 ) {
410 //clean up presentations before last view is closed
411 myAISContext->RemoveAll(Standard_False);
415 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
417 setTrihedronShown( true );
418 bool showStaticTrihedron = true;
419 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
420 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
421 viewPort->showStaticTrihedron( showStaticTrihedron );
424 int OCCViewer_Viewer::getTopLayerId()
426 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
427 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
433 \return interaction style
435 int OCCViewer_Viewer::interactionStyle() const
437 return myInteractionStyle;
441 Sets interaction style: 0 - standard, 1 - keyboard free interaction
442 \param theStyle - new interaction style
444 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
446 myInteractionStyle = theStyle;
447 //!! To be done for view windows
448 if ( !myViewManager )
451 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
452 for ( int i = 0; i < (int)wins.count(); i++ )
454 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
456 win->setInteractionStyle( theStyle );
461 \return zooming style
463 int OCCViewer_Viewer::zoomingStyle() const
465 return myZoomingStyle;
469 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
470 \param theStyle - new zooming style
472 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
474 myZoomingStyle = theStyle;
475 //!! To be done for view windows
476 if ( !myViewManager )
479 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
480 for ( int i = 0; i < (int)wins.count(); i++ )
482 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
484 win->setZoomingStyle( theStyle );
489 \return true if preselection is enabled
491 bool OCCViewer_Viewer::isPreselectionEnabled() const
493 return myPreselectionEnabled;
497 Enables/disables preselection
498 \param isEnabled - new status
500 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
502 myPreselectionEnabled = isEnabled;
504 if ( !myViewManager )
507 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
508 for ( int i = 0; i < (int)wins.count(); i++ )
510 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
512 win->enablePreselection( isEnabled );
518 \return true if selection is enabled
520 bool OCCViewer_Viewer::isSelectionEnabled() const
522 return mySelectionEnabled;
526 Enables/disables selection
527 \param isEnabled - new status
529 void OCCViewer_Viewer::enableSelection(bool isEnabled)
531 mySelectionEnabled = isEnabled;
533 //!! To be done for view windows
534 if ( !myViewManager )
537 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
538 for ( int i = 0; i < (int)wins.count(); i++ )
540 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
542 win->updateEnabledDrawMode();
543 win->enableSelection( isEnabled );
548 //clear current selection in the viewer
550 myAISContext->ClearSelected( Standard_True );
556 Sets multiselection enabled status
557 \param isEnabled - new status
559 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
561 myMultiSelectionEnabled = isEnable;
562 //!! To be done for view windows
563 if ( !myViewManager )
566 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
567 for ( int i = 0; i < (int)wins.count(); i++ )
569 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
571 win->updateEnabledDrawMode();
576 Builds popup for occ viewer
578 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
580 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
581 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
583 thePopup->addSeparator();
585 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
587 //Support of several toolbars in the popup menu
588 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
589 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
590 for ( ; it!=last; it++ ) {
591 if ( (*it)->parentWidget()->isVisible() )
592 thePopup->addAction( (*it)->toggleViewAction() );
597 SLOT: called on dump view operation is activated, stores scene to raster file
599 void OCCViewer_Viewer::onDumpView()
601 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
607 SLOT: called if background color is to be changed changed, passes new color to view port
609 void OCCViewer_Viewer::onChangeBackground()
611 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
615 // get supported gradient types
616 QStringList gradList;
617 QIntList idList, txtList;
618 QString formats = backgroundData( gradList, idList, txtList );
621 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
622 aView, // parent for dialog box
623 txtList, // allowed texture modes
624 true, // enable solid color mode
625 true, // enable gradient mode
626 false, // disable custom gradient mode
627 !txtList.isEmpty(), // enable/disable texture mode
628 gradList, // gradient names
629 idList, // gradient identifiers
630 formats ); // image formats
632 // set chosen background data to the viewer
633 if ( bgData.isValid() )
634 aView->setBackground( bgData );
638 Updates OCC 3D viewer
640 void OCCViewer_Viewer::update()
642 if (!myV3dViewer.IsNull())
643 myV3dViewer->Update();
645 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
647 aView->updateGravityCoords();
651 \return objects selected in 3D viewer
652 \param theList - list to be filled with selected objects
654 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
657 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
658 theList.Append(myAISContext->SelectedInteractive());
662 Selects objects in 3D viewer. Other selected objects are left as selected
663 \param theList - list objects to be selected
665 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
667 AIS_ListIteratorOfListOfInteractive aIt;
668 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
669 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
670 myAISContext->UpdateCurrentViewer();
674 Auxiliary method to emit signal selectionChanged()
676 void OCCViewer_Viewer::performSelectionChanged()
678 emit selectionChanged();
682 Hilights/unhilights object in viewer
683 \param obj - object to be updated
684 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
685 \param update - update current viewer
687 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
688 bool hilight, bool update )
690 bool isInLocal = myAISContext->HasOpenedContext();
694 if ( hilight && !myAISContext->IsSelected( obj ) )
695 myAISContext->AddOrRemoveCurrentObject( obj, false );
696 else if ( !hilight && myAISContext->IsSelected( obj ) )
697 myAISContext->AddOrRemoveCurrentObject( obj, false );
701 myV3dViewer->Redraw();
707 Unhilights all objects in viewer
708 \param updateviewer - update current viewer
710 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
712 if ( myAISContext->HasOpenedContext() ) {
714 myAISContext->ClearSelected( updateviewer );
716 myAISContext->UnhilightSelected( updateviewer );
720 myAISContext->ClearCurrents( updateviewer );
722 myAISContext->UnhilightCurrents( updateviewer );
730 \return true if object is in viewer or in collector
731 \param obj - object to be checked
732 \param onlyInViewer - search object only in viewer (so object must be displayed)
734 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
737 AIS_ListOfInteractive List;
738 myAISContext->DisplayedObjects(List);
740 AIS_ListIteratorOfListOfInteractive ite(List);
741 for ( ; ite.More(); ite.Next() )
742 if( ite.Value()==obj )
749 \return true if object is displayed in viewer
750 \param obj - object to be checked
752 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
754 return myAISContext->IsDisplayed( obj );
759 \param obj - object to be updated
760 \param color - new color
761 \param update - update current viewer
763 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
769 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
770 color.green() / 255.,
773 obj->SetColor( CSFColor );
777 myV3dViewer->Update();
781 Changes display mode of object
782 \param obj - object to be processed
783 \param mode - new display mode
784 \param update - update current viewer
786 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
787 int mode, bool update )
789 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
791 myV3dViewer->Update();
795 Changes transparency of object
796 \param obj - object to be processed
797 \param trans - new transparency
798 \param update - update current viewer
800 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
801 float trans, bool update )
803 myAISContext->SetTransparency( obj, trans, false );
804 myAISContext->Redisplay( obj, Standard_False, Standard_True );
806 myV3dViewer->Update();
810 Changes visibility of trihedron to opposite
812 void OCCViewer_Viewer::toggleTrihedron()
814 setTrihedronShown( !isTrihedronVisible() );
818 \return true if trihedron is visible
820 bool OCCViewer_Viewer::isTrihedronVisible() const
822 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
826 Sets visibility state of trihedron
827 \param on - new state
830 void OCCViewer_Viewer::setTrihedronShown( const bool on )
832 if ( myTrihedron.IsNull() )
836 myAISContext->Display( myTrihedron );
837 myAISContext->Deactivate(myTrihedron);
840 myAISContext->Erase( myTrihedron );
845 \return trihedron size
847 double OCCViewer_Viewer::trihedronSize() const
850 if ( !myTrihedron.IsNull() )
851 sz = myTrihedron->Size();
856 Changes trihedron size
859 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
861 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
862 myTrihedronSize = sz;
863 myIsRelative = isRelative;
869 Set number of isolines
870 \param u - u-isolines (first parametric co-ordinate)
871 \param v - v-isolines (second parametric co-ordinate)
873 void OCCViewer_Viewer::setIsos( const int u, const int v )
875 Handle(AIS_InteractiveContext) ic = getAISContext();
879 ic->SetIsoNumber( u, AIS_TOI_IsoU );
880 ic->SetIsoNumber( v, AIS_TOI_IsoV );
884 \return number of isolines
885 \param u - to return u-isolines (first parametric co-ordinate)
886 \param v - to return v-isolines (second parametric co-ordinate)
888 void OCCViewer_Viewer::isos( int& u, int& v ) const
890 Handle(AIS_InteractiveContext) ic = getAISContext();
893 u = ic->IsoNumber( AIS_TOI_IsoU );
894 v = ic->IsoNumber( AIS_TOI_IsoV );
899 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
901 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
903 return new OCCViewer_ViewWindow(0, this);
907 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
909 return background( theViewId ).color();
912 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
914 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
918 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
920 if ( theColor.isValid() ) {
921 Qtx::BackgroundData bg = background( theViewId );
922 bg.setColor( theColor );
923 setBackground( theViewId, bg );
927 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
929 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
930 myBackgrounds[theViewId] = theBackground;
935 Set the show static trihedron flag
937 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
939 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
940 if ( aView ) aView->showStaticTrihedron( on );
944 Get new and current trihedron size corresponding to the current model size
946 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
951 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
952 Handle(V3d_Viewer) viewer = getViewer3d();
953 viewer->InitActiveViews();
954 if(!viewer->MoreActiveViews()) return false;
956 Handle(V3d_View) view3d = viewer->ActiveView();
959 if ( view3d.IsNull() )
962 double aMaxSide = computeSceneSize( view3d );
965 // The boundary box of the view may be initialized but nullified
966 // (case of infinite objects)
967 if ( aMaxSide < Precision::Confusion() )
970 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
972 static float EPS = 5.0E-3;
973 theSize = getTrihedron()->Size();
974 theNewSize = aMaxSide*aSizeInPercents / 100.0;
976 return fabs( theNewSize - theSize ) > theSize * EPS ||
977 fabs( theNewSize - theSize ) > theNewSize * EPS;
983 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
986 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
988 #if OCC_VERSION_LARGE > 0x06070100
989 Bnd_Box aBox = view3d->View()->MinMaxValues();
990 Xmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
991 Ymin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
992 Zmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
993 Xmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().X();
994 Ymax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Y();
995 Zmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Z();
997 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
1000 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
1001 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
1003 aMaxSide = Xmax - Xmin;
1004 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
1005 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1012 * Update the size of the trihedron
1014 void OCCViewer_Viewer::updateTrihedron() {
1015 if ( myTrihedron.IsNull() )
1019 double newSz, oldSz;
1021 if(computeTrihedronSize(newSz, oldSz))
1022 myTrihedron->SetSize(newSz);
1024 } else if(myTrihedron->Size() != myTrihedronSize) {
1025 myTrihedron->SetSize(myTrihedronSize);
1030 Set number of isolines
1031 \param u - u-isolines (first parametric co-ordinate)
1032 \param v - v-isolines (second parametric co-ordinate)
1034 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1036 myPreselectionEnabled = isPreselectionEnabled;
1037 mySelectionEnabled = isSelectionEnabled;
1038 //clear current selection in the viewer
1040 if(!mySelectionEnabled) {
1041 myAISContext->ClearSelected( Standard_True );
1046 Creates clipping plane based on the incoming plane
1048 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1050 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1051 aGraphic3dPlane->SetOn( theIsOn );
1052 aGraphic3dPlane->SetCapping( Standard_True );
1054 // load capping texture
1055 QPixmap px( ":images/hatch.png" );
1056 if( !px.isNull() ) {
1057 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
1058 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
1059 if( aTexture->IsDone() ) {
1060 aTexture->EnableModulate();
1061 aTexture->EnableRepeat();
1062 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 0.01, 0.01 ) );
1063 aGraphic3dPlane->SetCappingTexture( aTexture );
1066 return aGraphic3dPlane;
1069 Applies clipping planes to clippable objects
1071 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1073 // 1. Remove existing clipping planes
1074 myClipPlanes.clear();
1075 myInternalClipPlanes.Clear();
1077 // 2. Create new clipping planes
1078 ClipPlanesList::iterator inIt = theList.begin();
1079 for (;inIt != theList.end(); inIt++ )
1081 OCCViewer_ClipPlane aPlane = *inIt;
1083 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1084 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1086 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1087 gp_Dir aDirection( aDx, aDy, aDz );
1089 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1090 myClipPlanes.push_back( aPlane );
1093 // 3. Apply clipping planes
1094 AIS_ListOfInteractive aList;
1095 myAISContext->DisplayedObjects (aList);
1096 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1097 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1098 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1099 if (!aShape.IsNull() && aShape->IsClippable()) {
1100 aShape->SetClipPlanes(myInternalClipPlanes);
1106 Returns the clipping planes applied to the displayed objects.
1108 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1109 return myClipPlanes;
1112 Applies clipping planes to given object objects
1114 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1116 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1117 if (!aShape.IsNull() && aShape->IsClippable())
1119 aShape->SetClipPlanes (myInternalClipPlanes);
1124 Returns the pointer to the clipping dialog box.
1126 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1127 return myClippingDlg;
1132 Stores pointer to the clipping dialog box.
1134 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1135 if(myClippingDlg != theDlg) {
1136 myClippingDlg = theDlg;