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);
306 if (!aHasShift) emit deselection();
308 if (myStartPnt == myEndPnt)
310 if ( !isPreselectionEnabled() ) {
311 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
312 if ( !aView3d.IsNull() ) {
313 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
317 if (aHasShift && myMultiSelectionEnabled)
318 myAISContext->ShiftSelect();
320 myAISContext->Select();
324 if (aHasShift && myMultiSelectionEnabled)
325 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
326 myEndPnt.x(), myEndPnt.y(),
327 aView->getViewPort()->getView(), Standard_False );
329 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
330 myEndPnt.x(), myEndPnt.y(),
331 aView->getViewPort()->getView(), Standard_False );
333 int Nb = myAISContext->NbSelected();
334 if( Nb>1 && !myMultiSelectionEnabled )
336 myAISContext->InitSelected();
337 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
338 if( !anOwner.IsNull() )
340 myAISContext->ClearSelected( Standard_False );
341 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
345 myAISContext->UpdateCurrentViewer();
347 emit selectionChanged();
351 SLOT: called on key press, processes selection in "key free" interaction style
353 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
355 if (!mySelectionEnabled) return;
356 if (theEvent->key() != Qt::Key_S) return;
357 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
359 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
360 if (!aView || aView->interactionStyle() != SUIT_ViewModel::KEY_FREE)
365 if ( !isPreselectionEnabled() ) {
366 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
367 if ( !aView3d.IsNull() ) {
368 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
372 myAISContext->Select();
374 emit selectionChanged();
377 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
379 Standard_Integer aViewsNb = 0;
380 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
382 if ( aViewsNb < 2 ) {
383 //clean up presentations before last view is closed
384 myAISContext->RemoveAll(Standard_False);
388 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
390 setTrihedronShown( true );
391 bool showStaticTrihedron = true;
392 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
393 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
394 viewPort->showStaticTrihedron( showStaticTrihedron );
397 int OCCViewer_Viewer::getTopLayerId()
399 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
400 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
406 \return interaction style
408 int OCCViewer_Viewer::interactionStyle() const
410 return myInteractionStyle;
414 Sets interaction style: 0 - standard, 1 - keyboard free interaction
415 \param theStyle - new interaction style
417 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
419 myInteractionStyle = theStyle;
420 //!! To be done for view windows
421 if ( !myViewManager )
424 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
425 for ( int i = 0; i < (int)wins.count(); i++ )
427 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
429 win->setInteractionStyle( theStyle );
434 \return zooming style
436 int OCCViewer_Viewer::zoomingStyle() const
438 return myZoomingStyle;
442 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
443 \param theStyle - new zooming style
445 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
447 myZoomingStyle = theStyle;
448 //!! To be done for view windows
449 if ( !myViewManager )
452 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
453 for ( int i = 0; i < (int)wins.count(); i++ )
455 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
457 win->setZoomingStyle( theStyle );
462 \return true if preselection is enabled
464 bool OCCViewer_Viewer::isPreselectionEnabled() const
466 return myPreselectionEnabled;
470 Enables/disables preselection
471 \param isEnabled - new status
473 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
475 myPreselectionEnabled = isEnabled;
477 if ( !myViewManager )
480 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
481 for ( int i = 0; i < (int)wins.count(); i++ )
483 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
485 win->enablePreselection( isEnabled );
491 \return true if selection is enabled
493 bool OCCViewer_Viewer::isSelectionEnabled() const
495 return mySelectionEnabled;
499 Enables/disables selection
500 \param isEnabled - new status
502 void OCCViewer_Viewer::enableSelection(bool isEnabled)
504 mySelectionEnabled = isEnabled;
506 //!! To be done for view windows
507 if ( !myViewManager )
510 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
511 for ( int i = 0; i < (int)wins.count(); i++ )
513 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
515 win->updateEnabledDrawMode();
516 win->enableSelection( isEnabled );
521 //clear current selection in the viewer
523 myAISContext->ClearSelected( Standard_True );
529 Sets multiselection enabled status
530 \param isEnabled - new status
532 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
534 myMultiSelectionEnabled = isEnable;
535 //!! To be done for view windows
536 if ( !myViewManager )
539 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
540 for ( int i = 0; i < (int)wins.count(); i++ )
542 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
544 win->updateEnabledDrawMode();
549 Builds popup for occ viewer
551 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
553 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
554 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
556 thePopup->addSeparator();
558 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
560 //Support of several toolbars in the popup menu
561 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
562 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
563 for ( ; it!=last; it++ ) {
564 if ( (*it)->parentWidget()->isVisible() )
565 thePopup->addAction( (*it)->toggleViewAction() );
570 SLOT: called on dump view operation is activated, stores scene to raster file
572 void OCCViewer_Viewer::onDumpView()
574 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
580 SLOT: called if background color is to be changed changed, passes new color to view port
582 void OCCViewer_Viewer::onChangeBackground()
584 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
588 // get supported gradient types
589 QStringList gradList;
590 QIntList idList, txtList;
591 QString formats = backgroundData( gradList, idList, txtList );
594 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
595 aView, // parent for dialog box
596 txtList, // allowed texture modes
597 true, // enable solid color mode
598 true, // enable gradient mode
599 false, // disable custom gradient mode
600 !txtList.isEmpty(), // enable/disable texture mode
601 gradList, // gradient names
602 idList, // gradient identifiers
603 formats ); // image formats
605 // set chosen background data to the viewer
606 if ( bgData.isValid() )
607 aView->setBackground( bgData );
611 Updates OCC 3D viewer
613 void OCCViewer_Viewer::update()
615 if (!myV3dViewer.IsNull())
616 myV3dViewer->Update();
618 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
620 aView->updateGravityCoords();
624 \return objects selected in 3D viewer
625 \param theList - list to be filled with selected objects
627 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
630 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
631 theList.Append(myAISContext->SelectedInteractive());
635 Selects objects in 3D viewer. Other selected objects are left as selected
636 \param theList - list objects to be selected
638 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
640 AIS_ListIteratorOfListOfInteractive aIt;
641 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
642 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
643 myAISContext->UpdateCurrentViewer();
647 Auxiliary method to emit signal selectionChanged()
649 void OCCViewer_Viewer::performSelectionChanged()
651 emit selectionChanged();
655 Hilights/unhilights object in viewer
656 \param obj - object to be updated
657 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
658 \param update - update current viewer
660 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
661 bool hilight, bool update )
663 bool isInLocal = myAISContext->HasOpenedContext();
667 if ( hilight && !myAISContext->IsSelected( obj ) )
668 myAISContext->AddOrRemoveCurrentObject( obj, false );
669 else if ( !hilight && myAISContext->IsSelected( obj ) )
670 myAISContext->AddOrRemoveCurrentObject( obj, false );
674 myV3dViewer->Redraw();
680 Unhilights all objects in viewer
681 \param updateviewer - update current viewer
683 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
685 if ( myAISContext->HasOpenedContext() ) {
687 myAISContext->ClearSelected( updateviewer );
689 myAISContext->UnhilightSelected( updateviewer );
693 myAISContext->ClearCurrents( updateviewer );
695 myAISContext->UnhilightCurrents( updateviewer );
703 \return true if object is in viewer or in collector
704 \param obj - object to be checked
705 \param onlyInViewer - search object only in viewer (so object must be displayed)
707 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
710 AIS_ListOfInteractive List;
711 myAISContext->DisplayedObjects(List);
713 AIS_ListIteratorOfListOfInteractive ite(List);
714 for ( ; ite.More(); ite.Next() )
715 if( ite.Value()==obj )
722 \return true if object is displayed in viewer
723 \param obj - object to be checked
725 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
727 return myAISContext->IsDisplayed( obj );
732 \param obj - object to be updated
733 \param color - new color
734 \param update - update current viewer
736 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
742 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
743 color.green() / 255.,
746 obj->SetColor( CSFColor );
750 myV3dViewer->Update();
754 Changes display mode of object
755 \param obj - object to be processed
756 \param mode - new display mode
757 \param update - update current viewer
759 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
760 int mode, bool update )
762 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
764 myV3dViewer->Update();
768 Changes transparency of object
769 \param obj - object to be processed
770 \param trans - new transparency
771 \param update - update current viewer
773 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
774 float trans, bool update )
776 myAISContext->SetTransparency( obj, trans, false );
777 myAISContext->Redisplay( obj, Standard_False, Standard_True );
779 myV3dViewer->Update();
783 Changes visibility of trihedron to opposite
785 void OCCViewer_Viewer::toggleTrihedron()
787 setTrihedronShown( !isTrihedronVisible() );
791 \return true if trihedron is visible
793 bool OCCViewer_Viewer::isTrihedronVisible() const
795 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
799 Sets visibility state of trihedron
800 \param on - new state
803 void OCCViewer_Viewer::setTrihedronShown( const bool on )
805 if ( myTrihedron.IsNull() )
809 myAISContext->Display( myTrihedron );
810 myAISContext->Deactivate(myTrihedron);
813 myAISContext->Erase( myTrihedron );
818 \return trihedron size
820 double OCCViewer_Viewer::trihedronSize() const
823 if ( !myTrihedron.IsNull() )
824 sz = myTrihedron->Size();
829 Changes trihedron size
832 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
834 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
835 myTrihedronSize = sz;
836 myIsRelative = isRelative;
842 Set number of isolines
843 \param u - u-isolines (first parametric co-ordinate)
844 \param v - v-isolines (second parametric co-ordinate)
846 void OCCViewer_Viewer::setIsos( const int u, const int v )
848 Handle(AIS_InteractiveContext) ic = getAISContext();
852 ic->SetIsoNumber( u, AIS_TOI_IsoU );
853 ic->SetIsoNumber( v, AIS_TOI_IsoV );
857 \return number of isolines
858 \param u - to return u-isolines (first parametric co-ordinate)
859 \param v - to return v-isolines (second parametric co-ordinate)
861 void OCCViewer_Viewer::isos( int& u, int& v ) const
863 Handle(AIS_InteractiveContext) ic = getAISContext();
866 u = ic->IsoNumber( AIS_TOI_IsoU );
867 v = ic->IsoNumber( AIS_TOI_IsoV );
872 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
874 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
876 return new OCCViewer_ViewWindow(0, this);
880 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
882 return background( theViewId ).color();
885 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
887 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
891 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
893 if ( theColor.isValid() ) {
894 Qtx::BackgroundData bg = background( theViewId );
895 bg.setColor( theColor );
896 setBackground( theViewId, bg );
900 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
902 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
903 myBackgrounds[theViewId] = theBackground;
908 Set the show static trihedron flag
910 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
912 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
913 if ( aView ) aView->showStaticTrihedron( on );
917 Get new and current trihedron size corresponding to the current model size
919 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
924 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
925 Handle(V3d_Viewer) viewer = getViewer3d();
926 viewer->InitActiveViews();
927 if(!viewer->MoreActiveViews()) return false;
929 Handle(V3d_View) view3d = viewer->ActiveView();
932 if ( view3d.IsNull() )
935 double aMaxSide = computeSceneSize( view3d );
938 // The boundary box of the view may be initialized but nullified
939 // (case of infinite objects)
940 if ( aMaxSide < Precision::Confusion() )
943 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
945 static float EPS = 5.0E-3;
946 theSize = getTrihedron()->Size();
947 theNewSize = aMaxSide*aSizeInPercents / 100.0;
949 return fabs( theNewSize - theSize ) > theSize * EPS ||
950 fabs( theNewSize - theSize ) > theNewSize * EPS;
956 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
959 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
961 #if OCC_VERSION_LARGE > 0x06070100
962 Bnd_Box aBox = view3d->View()->MinMaxValues();
963 Xmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
964 Ymin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
965 Zmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
966 Xmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().X();
967 Ymax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Y();
968 Zmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Z();
970 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
973 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
974 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
976 aMaxSide = Xmax - Xmin;
977 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
978 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
985 * Update the size of the trihedron
987 void OCCViewer_Viewer::updateTrihedron() {
988 if ( myTrihedron.IsNull() )
994 if(computeTrihedronSize(newSz, oldSz))
995 myTrihedron->SetSize(newSz);
997 } else if(myTrihedron->Size() != myTrihedronSize) {
998 myTrihedron->SetSize(myTrihedronSize);
1003 Set number of isolines
1004 \param u - u-isolines (first parametric co-ordinate)
1005 \param v - v-isolines (second parametric co-ordinate)
1007 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1009 myPreselectionEnabled = isPreselectionEnabled;
1010 mySelectionEnabled = isSelectionEnabled;
1011 //clear current selection in the viewer
1013 if(!mySelectionEnabled) {
1014 myAISContext->ClearSelected( Standard_True );
1019 Creates clipping plane based on the incoming plane
1021 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1023 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1024 aGraphic3dPlane->SetOn( theIsOn );
1025 aGraphic3dPlane->SetCapping( Standard_True );
1027 // load capping texture
1028 QPixmap px( ":images/hatch.png" );
1029 if( !px.isNull() ) {
1030 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
1031 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
1032 if( aTexture->IsDone() ) {
1033 aTexture->EnableModulate();
1034 aTexture->EnableRepeat();
1035 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 0.01, 0.01 ) );
1036 aGraphic3dPlane->SetCappingTexture( aTexture );
1039 return aGraphic3dPlane;
1042 Applies clipping planes to clippable objects
1044 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1046 // 1. Remove existing clipping planes
1047 myClipPlanes.clear();
1048 myInternalClipPlanes.Clear();
1050 // 2. Create new clipping planes
1051 ClipPlanesList::iterator inIt = theList.begin();
1052 for (;inIt != theList.end(); inIt++ )
1054 OCCViewer_ClipPlane aPlane = *inIt;
1056 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1057 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1059 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1060 gp_Dir aDirection( aDx, aDy, aDz );
1062 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1063 myClipPlanes.push_back( aPlane );
1066 // 3. Apply clipping planes
1067 AIS_ListOfInteractive aList;
1068 myAISContext->DisplayedObjects (aList);
1069 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1070 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1071 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1072 if (!aShape.IsNull() && aShape->IsClippable()) {
1073 aShape->SetClipPlanes(myInternalClipPlanes);
1079 Returns the clipping planes applied to the displayed objects.
1081 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1082 return myClipPlanes;
1085 Applies clipping planes to given object objects
1087 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1089 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1090 if (!aShape.IsNull() && aShape->IsClippable())
1092 aShape->SetClipPlanes (myInternalClipPlanes);
1097 Returns the pointer to the clipping dialog box.
1099 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1100 return myClippingDlg;
1105 Stores pointer to the clipping dialog box.
1107 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1108 if(myClippingDlg != theDlg) {
1109 myClippingDlg = theDlg;