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 "QtxActionToolMgr.h"
40 #include "QtxBackgroundTool.h"
43 #include <QApplication>
44 #include <QColorDialog>
45 #include <QFileDialog>
49 #include <QMouseEvent>
51 #include <QDesktopWidget>
53 #include <AIS_Axis.hxx>
54 #include <AIS_Drawer.hxx>
55 #include <AIS_ListOfInteractive.hxx>
56 #include <AIS_ListIteratorOfListOfInteractive.hxx>
58 #include <Graphic3d_Texture2Dmanual.hxx>
59 #include <Graphic3d_TextureParams.hxx>
61 #include <Geom_Axis2Placement.hxx>
62 #include <Prs3d_Drawer.hxx>
63 #include <Prs3d_DatumAspect.hxx>
64 #include <Prs3d_LineAspect.hxx>
65 #include <Prs3d_TextAspect.hxx>
67 #include <Visual3d_View.hxx>
70 Get data for supported background modes: gradient types, identifiers and supported image formats
72 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
74 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
75 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
76 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
77 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
78 idList << HorizontalGradient << VerticalGradient <<
79 Diagonal1Gradient << Diagonal2Gradient <<
80 Corner1Gradient << Corner2Gradient <<
81 Corner3Gradient << Corner4Gradient;
82 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
83 return tr("BG_IMAGE_FILES");
88 \param DisplayTrihedron - is trihedron displayed
90 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
92 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
98 // init CasCade viewers
99 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
100 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
101 myV3dViewer->SetDefaultLights();
104 myAISContext = new AIS_InteractiveContext( myV3dViewer );
105 myAISContext->SelectionColor( Quantity_NOC_WHITE );
107 // display isoline on planar faces (box for ex.)
108 myAISContext->IsoOnPlane( true );
110 /* create trihedron */
111 if ( DisplayTrihedron )
113 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
114 myTrihedron = new AIS_Trihedron(anAxis);
115 myTrihedron->SetInfiniteState( Standard_True );
117 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
118 //myTrihedron->SetColor( Col );
119 myTrihedron->SetArrowColor( Col.Name() );
120 myTrihedron->SetSize(100);
121 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
122 if (drawer->HasDatumAspect()) {
123 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
124 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
125 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
126 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
130 // set interaction style to standard
131 myInteractionStyle = 0;
133 // set zooming style to standard
137 myPreselectionEnabled = true;
140 mySelectionEnabled = true;
141 myMultiSelectionEnabled = true;
147 OCCViewer_Viewer::~OCCViewer_Viewer()
149 myAISContext.Nullify();
150 myV3dViewer.Nullify();
155 \return background color of viewer
157 QColor OCCViewer_Viewer::backgroundColor() const
159 return backgroundColor(0);
163 \return background data of viewer
165 Qtx::BackgroundData OCCViewer_Viewer::background() const
167 return background(0);
171 Sets background color [obsolete]
172 \param c - new background color
174 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
176 setBackgroundColor( 0, c );
181 \param d - new background data
183 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
185 setBackground( 0, theBackground );
189 Start initialization of view window
190 \param view - view window to be initialized
192 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
196 view->initSketchers();
197 view->setInteractionStyle( interactionStyle() );
198 view->setZoomingStyle( zoomingStyle() );
199 view->enablePreselection( isPreselectionEnabled() );
200 view->enableSelection( isSelectionEnabled() );
202 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
205 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
206 // connect signal from viewport
207 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
208 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
214 Creates new view window
215 \param theDesktop - main window of application
217 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
220 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
221 // get main view window (created by view frame)
222 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
223 // initialize main view window
225 // set default background for view window
226 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
232 Sets new view manager
233 \param theViewManager - new view manager
235 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
237 SUIT_ViewModel::setViewManager(theViewManager);
238 if (theViewManager) {
239 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
240 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
242 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
243 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
245 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
246 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
248 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
249 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
254 SLOT: called on mouse button press, stores current mouse position as start point for transformations
256 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
258 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
262 SLOT: called on mouse move, processes transformation or hilighting
264 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
266 if (!mySelectionEnabled) return;
267 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
269 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
271 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
273 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
274 if (aView->getViewPort()->isBusy()) {
275 QCoreApplication::processEvents();
276 return; // Check that the ViewPort initialization completed
277 // To Prevent call move event if the View port is not initialized
280 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
281 if ( !aView3d.IsNull() ) {
282 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
289 SLOT: called on mouse button release, finishes transformation or selection
291 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
293 if (!mySelectionEnabled) return;
294 if (theEvent->button() != Qt::LeftButton) return;
295 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
297 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
301 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
302 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
304 if (!aHasShift) emit deselection();
306 if (myStartPnt == myEndPnt)
308 if ( !isPreselectionEnabled() ) {
309 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
310 if ( !aView3d.IsNull() ) {
311 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
315 if (aHasShift && myMultiSelectionEnabled)
316 myAISContext->ShiftSelect();
318 myAISContext->Select();
322 if (aHasShift && myMultiSelectionEnabled)
323 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
324 myEndPnt.x(), myEndPnt.y(),
325 aView->getViewPort()->getView(), Standard_False );
327 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
328 myEndPnt.x(), myEndPnt.y(),
329 aView->getViewPort()->getView(), Standard_False );
331 int Nb = myAISContext->NbSelected();
332 if( Nb>1 && !myMultiSelectionEnabled )
334 myAISContext->InitSelected();
335 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
336 if( !anOwner.IsNull() )
338 myAISContext->ClearSelected( Standard_False );
339 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
343 myAISContext->UpdateCurrentViewer();
345 emit selectionChanged();
349 SLOT: called on key press, processes selection in "key free" interaction style
351 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
353 if (!mySelectionEnabled) return;
354 if (theEvent->key() != Qt::Key_S) return;
355 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
357 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
358 if (!aView || aView->interactionStyle() != SUIT_ViewModel::KEY_FREE)
363 if ( !isPreselectionEnabled() ) {
364 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
365 if ( !aView3d.IsNull() ) {
366 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
370 myAISContext->Select();
372 emit selectionChanged();
375 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
377 Standard_Integer aViewsNb = 0;
378 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
380 if ( aViewsNb < 2 ) {
381 //clean up presentations before last view is closed
382 myAISContext->RemoveAll(Standard_False);
386 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
388 setTrihedronShown( true );
389 bool showStaticTrihedron = true;
390 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
391 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
392 viewPort->showStaticTrihedron( showStaticTrihedron );
395 int OCCViewer_Viewer::getTopLayerId()
397 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
398 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
404 \return interaction style
406 int OCCViewer_Viewer::interactionStyle() const
408 return myInteractionStyle;
412 Sets interaction style: 0 - standard, 1 - keyboard free interaction
413 \param theStyle - new interaction style
415 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
417 myInteractionStyle = theStyle;
418 //!! To be done for view windows
419 if ( !myViewManager )
422 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
423 for ( int i = 0; i < (int)wins.count(); i++ )
425 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
427 win->setInteractionStyle( theStyle );
432 \return zooming style
434 int OCCViewer_Viewer::zoomingStyle() const
436 return myZoomingStyle;
440 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
441 \param theStyle - new zooming style
443 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
445 myZoomingStyle = theStyle;
446 //!! To be done for view windows
447 if ( !myViewManager )
450 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
451 for ( int i = 0; i < (int)wins.count(); i++ )
453 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
455 win->setZoomingStyle( theStyle );
460 \return true if preselection is enabled
462 bool OCCViewer_Viewer::isPreselectionEnabled() const
464 return myPreselectionEnabled;
468 Enables/disables preselection
469 \param isEnabled - new status
471 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
473 myPreselectionEnabled = isEnabled;
475 if ( !myViewManager )
478 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
479 for ( int i = 0; i < (int)wins.count(); i++ )
481 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
483 win->enablePreselection( isEnabled );
489 \return true if selection is enabled
491 bool OCCViewer_Viewer::isSelectionEnabled() const
493 return mySelectionEnabled;
497 Enables/disables selection
498 \param isEnabled - new status
500 void OCCViewer_Viewer::enableSelection(bool isEnabled)
502 mySelectionEnabled = isEnabled;
504 //!! To be done for view windows
505 if ( !myViewManager )
508 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
509 for ( int i = 0; i < (int)wins.count(); i++ )
511 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
513 win->updateEnabledDrawMode();
514 win->enableSelection( isEnabled );
519 //clear current selection in the viewer
521 myAISContext->ClearSelected( Standard_True );
527 Sets multiselection enabled status
528 \param isEnabled - new status
530 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
532 myMultiSelectionEnabled = isEnable;
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();
547 Builds popup for occ viewer
549 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
551 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
552 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
554 thePopup->addSeparator();
556 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
558 //Support of several toolbars in the popup menu
559 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
560 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
561 for ( ; it!=last; it++ ) {
562 if ( (*it)->parentWidget()->isVisible() )
563 thePopup->addAction( (*it)->toggleViewAction() );
568 SLOT: called on dump view operation is activated, stores scene to raster file
570 void OCCViewer_Viewer::onDumpView()
572 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
578 SLOT: called if background color is to be changed changed, passes new color to view port
580 void OCCViewer_Viewer::onChangeBackground()
582 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
586 // get supported gradient types
587 QStringList gradList;
588 QIntList idList, txtList;
589 QString formats = backgroundData( gradList, idList, txtList );
592 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
593 aView, // parent for dialog box
594 txtList, // allowed texture modes
595 true, // enable solid color mode
596 true, // enable gradient mode
597 false, // disable custom gradient mode
598 !txtList.isEmpty(), // enable/disable texture mode
599 gradList, // gradient names
600 idList, // gradient identifiers
601 formats ); // image formats
603 // set chosen background data to the viewer
604 if ( bgData.isValid() )
605 aView->setBackground( bgData );
609 Updates OCC 3D viewer
611 void OCCViewer_Viewer::update()
613 if (!myV3dViewer.IsNull())
614 myV3dViewer->Update();
616 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
618 aView->updateGravityCoords();
622 \return objects selected in 3D viewer
623 \param theList - list to be filled with selected objects
625 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
628 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
629 theList.Append(myAISContext->SelectedInteractive());
633 Selects objects in 3D viewer. Other selected objects are left as selected
634 \param theList - list objects to be selected
636 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
638 AIS_ListIteratorOfListOfInteractive aIt;
639 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
640 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
641 myAISContext->UpdateCurrentViewer();
645 Auxiliary method to emit signal selectionChanged()
647 void OCCViewer_Viewer::performSelectionChanged()
649 emit selectionChanged();
653 Hilights/unhilights object in viewer
654 \param obj - object to be updated
655 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
656 \param update - update current viewer
658 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
659 bool hilight, bool update )
661 bool isInLocal = myAISContext->HasOpenedContext();
665 if ( hilight && !myAISContext->IsSelected( obj ) )
666 myAISContext->AddOrRemoveCurrentObject( obj, false );
667 else if ( !hilight && myAISContext->IsSelected( obj ) )
668 myAISContext->AddOrRemoveCurrentObject( obj, false );
672 myV3dViewer->Redraw();
678 Unhilights all objects in viewer
679 \param updateviewer - update current viewer
681 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
683 if ( myAISContext->HasOpenedContext() ) {
685 myAISContext->ClearSelected( updateviewer );
687 myAISContext->UnhilightSelected( updateviewer );
691 myAISContext->ClearCurrents( updateviewer );
693 myAISContext->UnhilightCurrents( updateviewer );
701 \return true if object is in viewer or in collector
702 \param obj - object to be checked
703 \param onlyInViewer - search object only in viewer (so object must be displayed)
705 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
708 AIS_ListOfInteractive List;
709 myAISContext->DisplayedObjects(List);
711 AIS_ListIteratorOfListOfInteractive ite(List);
712 for ( ; ite.More(); ite.Next() )
713 if( ite.Value()==obj )
720 \return true if object is displayed in viewer
721 \param obj - object to be checked
723 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
725 return myAISContext->IsDisplayed( obj );
730 \param obj - object to be updated
731 \param color - new color
732 \param update - update current viewer
734 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
740 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
741 color.green() / 255.,
744 obj->SetColor( CSFColor );
748 myV3dViewer->Update();
752 Changes display mode of object
753 \param obj - object to be processed
754 \param mode - new display mode
755 \param update - update current viewer
757 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
758 int mode, bool update )
760 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
762 myV3dViewer->Update();
766 Changes transparency of object
767 \param obj - object to be processed
768 \param trans - new transparency
769 \param update - update current viewer
771 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
772 float trans, bool update )
774 myAISContext->SetTransparency( obj, trans, false );
775 myAISContext->Redisplay( obj, Standard_False, Standard_True );
777 myV3dViewer->Update();
781 Changes visibility of trihedron to opposite
783 void OCCViewer_Viewer::toggleTrihedron()
785 setTrihedronShown( !isTrihedronVisible() );
789 \return true if trihedron is visible
791 bool OCCViewer_Viewer::isTrihedronVisible() const
793 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
797 Sets visibility state of trihedron
798 \param on - new state
801 void OCCViewer_Viewer::setTrihedronShown( const bool on )
803 if ( myTrihedron.IsNull() )
807 myAISContext->Display( myTrihedron );
808 myAISContext->Deactivate(myTrihedron);
811 myAISContext->Erase( myTrihedron );
816 \return trihedron size
818 double OCCViewer_Viewer::trihedronSize() const
821 if ( !myTrihedron.IsNull() )
822 sz = myTrihedron->Size();
827 Changes trihedron size
830 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
832 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
833 myTrihedronSize = sz;
834 myIsRelative = isRelative;
840 Set number of isolines
841 \param u - u-isolines (first parametric co-ordinate)
842 \param v - v-isolines (second parametric co-ordinate)
844 void OCCViewer_Viewer::setIsos( const int u, const int v )
846 Handle(AIS_InteractiveContext) ic = getAISContext();
850 ic->SetIsoNumber( u, AIS_TOI_IsoU );
851 ic->SetIsoNumber( v, AIS_TOI_IsoV );
855 \return number of isolines
856 \param u - to return u-isolines (first parametric co-ordinate)
857 \param v - to return v-isolines (second parametric co-ordinate)
859 void OCCViewer_Viewer::isos( int& u, int& v ) const
861 Handle(AIS_InteractiveContext) ic = getAISContext();
864 u = ic->IsoNumber( AIS_TOI_IsoU );
865 v = ic->IsoNumber( AIS_TOI_IsoV );
870 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
872 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
874 return new OCCViewer_ViewWindow(0, this);
878 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
880 return background( theViewId ).color();
883 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
885 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
889 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
891 if ( theColor.isValid() ) {
892 Qtx::BackgroundData bg = background( theViewId );
893 bg.setColor( theColor );
894 setBackground( theViewId, bg );
898 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
900 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
901 myBackgrounds[theViewId] = theBackground;
906 Set the show static trihedron flag
908 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
910 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
911 if ( aView ) aView->showStaticTrihedron( on );
915 Get new and current trihedron size corresponding to the current model size
917 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
922 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
923 Handle(V3d_Viewer) viewer = getViewer3d();
924 viewer->InitActiveViews();
925 if(!viewer->MoreActiveViews()) return false;
927 Handle(V3d_View) view3d = viewer->ActiveView();
930 if ( view3d.IsNull() )
933 double aMaxSide = computeSceneSize( view3d );
936 // The boundary box of the view may be initialized but nullified
937 // (case of infinite objects)
938 if ( aMaxSide < Precision::Confusion() )
941 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
943 static float EPS = 5.0E-3;
944 theSize = getTrihedron()->Size();
945 theNewSize = aMaxSide*aSizeInPercents / 100.0;
947 return fabs( theNewSize - theSize ) > theSize * EPS ||
948 fabs( theNewSize - theSize ) > theNewSize * EPS;
954 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
957 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
959 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
961 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
962 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
964 aMaxSide = Xmax - Xmin;
965 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
966 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
973 * Update the size of the trihedron
975 void OCCViewer_Viewer::updateTrihedron() {
976 if ( myTrihedron.IsNull() )
982 if(computeTrihedronSize(newSz, oldSz))
983 myTrihedron->SetSize(newSz);
985 } else if(myTrihedron->Size() != myTrihedronSize) {
986 myTrihedron->SetSize(myTrihedronSize);
991 Set number of isolines
992 \param u - u-isolines (first parametric co-ordinate)
993 \param v - v-isolines (second parametric co-ordinate)
995 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
997 myPreselectionEnabled = isPreselectionEnabled;
998 mySelectionEnabled = isSelectionEnabled;
999 //clear current selection in the viewer
1001 if(!mySelectionEnabled) {
1002 myAISContext->ClearSelected( Standard_True );
1007 Creates clipping plane based on the incoming plane
1009 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1011 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1012 aGraphic3dPlane->SetOn( theIsOn );
1013 aGraphic3dPlane->SetCapping( Standard_True );
1015 // load capping texture
1016 QPixmap px( ":images/hatch.png" );
1017 if( !px.isNull() ) {
1018 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
1019 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
1020 if( aTexture->IsDone() ) {
1021 aTexture->EnableModulate();
1022 aTexture->EnableRepeat();
1023 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 0.01, 0.01 ) );
1024 aGraphic3dPlane->SetCappingTexture( aTexture );
1027 return aGraphic3dPlane;
1030 Applies clipping planes to clippable objects
1032 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1034 // 1. Remove existing clipping planes
1035 myClipPlanes.clear();
1036 myInternalClipPlanes.Clear();
1038 // 2. Create new clipping planes
1039 ClipPlanesList::iterator inIt = theList.begin();
1040 for (;inIt != theList.end(); inIt++ )
1042 OCCViewer_ClipPlane aPlane = *inIt;
1044 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1045 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1047 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1048 gp_Dir aDirection( aDx, aDy, aDz );
1050 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1051 myClipPlanes.push_back( aPlane );
1054 // 3. Apply clipping planes
1055 AIS_ListOfInteractive aList;
1056 myAISContext->DisplayedObjects (aList);
1057 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1058 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1059 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1060 if (!aShape.IsNull() && aShape->IsClippable()) {
1061 aShape->SetClipPlanes(myInternalClipPlanes);
1067 Returns the clipping planes applied to the displayed objects.
1069 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1070 return myClipPlanes;
1073 Applies clipping planes to given object objects
1075 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1077 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1078 if (!aShape.IsNull() && aShape->IsClippable())
1080 aShape->SetClipPlanes (myInternalClipPlanes);
1085 Returns the pointer to the clipping dialog box.
1087 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1088 return myClippingDlg;
1093 Stores pointer to the clipping dialog box.
1095 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1096 if(myClippingDlg != theDlg) {
1097 myClippingDlg = theDlg;