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"
30 #include "SUIT_ViewWindow.h"
31 #include "SUIT_ViewManager.h"
32 #include "SUIT_Desktop.h"
33 #include "SUIT_Session.h"
34 #include "SUIT_ResourceMgr.h"
36 #include "ViewerData_AISShape.hxx"
38 #include "QtxActionToolMgr.h"
39 #include "QtxBackgroundTool.h"
42 #include <QApplication>
43 #include <QColorDialog>
44 #include <QFileDialog>
48 #include <QMouseEvent>
50 #include <QDesktopWidget>
52 #include <AIS_Axis.hxx>
53 #include <AIS_Drawer.hxx>
54 #include <AIS_ListOfInteractive.hxx>
55 #include <AIS_ListIteratorOfListOfInteractive.hxx>
57 #include <Geom_Axis2Placement.hxx>
58 #include <Prs3d_Drawer.hxx>
59 #include <Prs3d_DatumAspect.hxx>
60 #include <Prs3d_LineAspect.hxx>
61 #include <Prs3d_TextAspect.hxx>
63 #include <Visual3d_View.hxx>
66 // VSR: Uncomment below line to allow texture background support in OCC viewer
67 #define OCC_ENABLE_TEXTURED_BACKGROUND
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 #if OCC_VERSION_LARGE > 0x06050200 // enabled since OCCT 6.5.3, since in previous version this functionality is buggy
83 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
84 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
87 return tr("BG_IMAGE_FILES");
92 \param DisplayTrihedron - is trihedron displayed
94 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
96 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
102 // init CasCade viewers
103 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
104 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
105 myV3dViewer->SetDefaultLights();
107 #if OCC_VERSION_LARGE <= 0x06060000 // Porting to OCCT higher 6.6.0 version
108 myV3dCollector = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Collector3d").ToExtString() );
109 //myV3dCollector->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
110 myV3dCollector->SetDefaultLights();
114 #if OCC_VERSION_LARGE <= 0x06060000
115 myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector );
117 myAISContext = new AIS_InteractiveContext( myV3dViewer );
119 myAISContext->SelectionColor( Quantity_NOC_WHITE );
121 // display isoline on planar faces (box for ex.)
122 myAISContext->IsoOnPlane( true );
124 /* create trihedron */
125 if ( DisplayTrihedron )
127 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
128 myTrihedron = new AIS_Trihedron(anAxis);
129 myTrihedron->SetInfiniteState( Standard_True );
131 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
132 //myTrihedron->SetColor( Col );
133 myTrihedron->SetArrowColor( Col.Name() );
134 myTrihedron->SetSize(100);
135 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
136 if (drawer->HasDatumAspect()) {
137 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
138 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
139 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
140 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
144 // set interaction style to standard
145 myInteractionStyle = 0;
147 // set zooming style to standard
151 myPreselectionEnabled = true;
154 mySelectionEnabled = true;
155 myMultiSelectionEnabled = true;
161 OCCViewer_Viewer::~OCCViewer_Viewer()
163 myAISContext.Nullify();
164 myV3dViewer.Nullify();
165 #if OCC_VERSION_LARGE <= 0x06060000
166 myV3dCollector.Nullify();
172 \return background color of viewer
174 QColor OCCViewer_Viewer::backgroundColor() const
176 return backgroundColor(0);
180 \return background data of viewer
182 Qtx::BackgroundData OCCViewer_Viewer::background() const
184 return background(0);
188 Sets background color [obsolete]
189 \param c - new background color
191 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
193 setBackgroundColor( 0, c );
198 \param d - new background data
200 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
202 setBackground( 0, theBackground );
206 Start initialization of view window
207 \param view - view window to be initialized
209 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
213 view->initSketchers();
214 view->setInteractionStyle( interactionStyle() );
215 view->setZoomingStyle( zoomingStyle() );
216 view->enablePreselection( isPreselectionEnabled() );
217 view->enableSelection( isSelectionEnabled() );
219 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
222 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
223 // connect signal from viewport
224 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
225 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
231 Creates new view window
232 \param theDesktop - main window of application
234 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
237 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
238 // get main view window (created by view frame)
239 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
240 // initialize main view window
242 // set default background for view window
243 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
249 Sets new view manager
250 \param theViewManager - new view manager
252 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
254 SUIT_ViewModel::setViewManager(theViewManager);
255 if (theViewManager) {
256 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
257 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
259 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
260 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
262 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
263 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
265 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
266 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
271 SLOT: called on mouse button press, stores current mouse position as start point for transformations
273 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
275 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
279 SLOT: called on mouse move, processes transformation or hilighting
281 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
283 if (!mySelectionEnabled) return;
284 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
286 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
288 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
290 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
291 if (aView->getViewPort()->isBusy()) {
292 QCoreApplication::processEvents();
293 return; // Check that the ViewPort initialization completed
294 // To Prevent call move event if the View port is not initialized
297 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
298 if ( !aView3d.IsNull() ) {
299 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
306 SLOT: called on mouse button release, finishes transformation or selection
308 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
310 if (!mySelectionEnabled) return;
311 if (theEvent->button() != Qt::LeftButton) return;
312 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
314 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
318 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
319 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
321 if (!aHasShift) emit deselection();
323 if (myStartPnt == myEndPnt)
325 if ( !isPreselectionEnabled() ) {
326 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
327 if ( !aView3d.IsNull() ) {
328 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
332 if (aHasShift && myMultiSelectionEnabled)
333 myAISContext->ShiftSelect();
335 myAISContext->Select();
339 if (aHasShift && myMultiSelectionEnabled)
340 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
341 myEndPnt.x(), myEndPnt.y(),
342 aView->getViewPort()->getView(), Standard_False );
344 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
345 myEndPnt.x(), myEndPnt.y(),
346 aView->getViewPort()->getView(), Standard_False );
348 int Nb = myAISContext->NbSelected();
349 if( Nb>1 && !myMultiSelectionEnabled )
351 myAISContext->InitSelected();
352 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
353 if( !anOwner.IsNull() )
355 myAISContext->ClearSelected( Standard_False );
356 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
360 myAISContext->UpdateCurrentViewer();
362 emit selectionChanged();
366 SLOT: called on key press, processes selection in "key free" interaction style
368 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
370 if (!mySelectionEnabled) return;
371 if (theEvent->key() != Qt::Key_S) return;
372 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
374 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
375 if (!aView || aView->interactionStyle() != SUIT_ViewModel::KEY_FREE)
380 if ( !isPreselectionEnabled() ) {
381 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
382 if ( !aView3d.IsNull() ) {
383 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
387 myAISContext->Select();
389 emit selectionChanged();
392 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
394 Standard_Integer aViewsNb = 0;
395 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
397 if ( aViewsNb < 2 ) {
398 //clean up presentations before last view is closed
399 myAISContext->RemoveAll(Standard_False);
403 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
405 setTrihedronShown( true );
406 bool showStaticTrihedron = true;
407 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
408 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
409 viewPort->showStaticTrihedron( showStaticTrihedron );
412 int OCCViewer_Viewer::getTopLayerId()
414 #if OCC_VERSION_LARGE > 0x06050200
415 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
416 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
423 \return interaction style
425 int OCCViewer_Viewer::interactionStyle() const
427 return myInteractionStyle;
431 Sets interaction style: 0 - standard, 1 - keyboard free interaction
432 \param theStyle - new interaction style
434 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
436 myInteractionStyle = theStyle;
437 //!! To be done for view windows
438 if ( !myViewManager )
441 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
442 for ( int i = 0; i < (int)wins.count(); i++ )
444 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
446 win->setInteractionStyle( theStyle );
451 \return zooming style
453 int OCCViewer_Viewer::zoomingStyle() const
455 return myZoomingStyle;
459 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
460 \param theStyle - new zooming style
462 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
464 myZoomingStyle = theStyle;
465 //!! To be done for view windows
466 if ( !myViewManager )
469 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
470 for ( int i = 0; i < (int)wins.count(); i++ )
472 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
474 win->setZoomingStyle( theStyle );
479 \return true if preselection is enabled
481 bool OCCViewer_Viewer::isPreselectionEnabled() const
483 return myPreselectionEnabled;
487 Enables/disables preselection
488 \param isEnabled - new status
490 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
492 myPreselectionEnabled = isEnabled;
494 if ( !myViewManager )
497 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
498 for ( int i = 0; i < (int)wins.count(); i++ )
500 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
502 win->enablePreselection( isEnabled );
508 \return true if selection is enabled
510 bool OCCViewer_Viewer::isSelectionEnabled() const
512 return mySelectionEnabled;
516 Enables/disables selection
517 \param isEnabled - new status
519 void OCCViewer_Viewer::enableSelection(bool isEnabled)
521 mySelectionEnabled = isEnabled;
523 //!! To be done for view windows
524 if ( !myViewManager )
527 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
528 for ( int i = 0; i < (int)wins.count(); i++ )
530 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
532 win->updateEnabledDrawMode();
533 win->enableSelection( isEnabled );
538 //clear current selection in the viewer
540 myAISContext->ClearSelected( Standard_True );
546 Sets multiselection enabled status
547 \param isEnabled - new status
549 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
551 myMultiSelectionEnabled = isEnable;
552 //!! To be done for view windows
553 if ( !myViewManager )
556 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
557 for ( int i = 0; i < (int)wins.count(); i++ )
559 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
561 win->updateEnabledDrawMode();
566 Builds popup for occ viewer
568 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
570 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
571 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
573 thePopup->addSeparator();
575 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
577 //Support of several toolbars in the popup menu
578 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
579 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
580 for ( ; it!=last; it++ ) {
581 if ( (*it)->parentWidget()->isVisible() )
582 thePopup->addAction( (*it)->toggleViewAction() );
587 SLOT: called on dump view operation is activated, stores scene to raster file
589 void OCCViewer_Viewer::onDumpView()
591 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
597 SLOT: called if background color is to be changed changed, passes new color to view port
599 void OCCViewer_Viewer::onChangeBackground()
601 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
605 // get supported gradient types
606 QStringList gradList;
607 QIntList idList, txtList;
608 QString formats = backgroundData( gradList, idList, txtList );
611 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
612 aView, // parent for dialog box
613 txtList, // allowed texture modes
614 true, // enable solid color mode
615 true, // enable gradient mode
616 false, // disable custom gradient mode
617 !txtList.isEmpty(), // enable/disable texture mode
618 gradList, // gradient names
619 idList, // gradient identifiers
620 formats ); // image formats
622 // set chosen background data to the viewer
623 if ( bgData.isValid() )
624 aView->setBackground( bgData );
628 Updates OCC 3D viewer
630 void OCCViewer_Viewer::update()
632 if (!myV3dViewer.IsNull())
633 myV3dViewer->Update();
635 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
637 aView->updateGravityCoords();
641 \return objects selected in 3D viewer
642 \param theList - list to be filled with selected objects
644 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
647 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
648 theList.Append(myAISContext->SelectedInteractive());
652 Selects objects in 3D viewer. Other selected objects are left as selected
653 \param theList - list objects to be selected
655 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
657 AIS_ListIteratorOfListOfInteractive aIt;
658 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
659 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
660 myAISContext->UpdateCurrentViewer();
664 Auxiliary method to emit signal selectionChanged()
666 void OCCViewer_Viewer::performSelectionChanged()
668 emit selectionChanged();
672 Hilights/unhilights object in viewer
673 \param obj - object to be updated
674 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
675 \param update - update current viewer
677 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
678 bool hilight, bool update )
680 bool isInLocal = myAISContext->HasOpenedContext();
684 if ( hilight && !myAISContext->IsSelected( obj ) )
685 myAISContext->AddOrRemoveCurrentObject( obj, false );
686 else if ( !hilight && myAISContext->IsSelected( obj ) )
687 myAISContext->AddOrRemoveCurrentObject( obj, false );
691 myV3dViewer->Redraw();
697 Unhilights all objects in viewer
698 \param updateviewer - update current viewer
700 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
702 if ( myAISContext->HasOpenedContext() ) {
704 myAISContext->ClearSelected( updateviewer );
706 myAISContext->UnhilightSelected( updateviewer );
710 myAISContext->ClearCurrents( updateviewer );
712 myAISContext->UnhilightCurrents( updateviewer );
720 \return true if object is in viewer or in collector
721 \param obj - object to be checked
722 \param onlyInViewer - search object only in viewer (so object must be displayed)
724 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
727 AIS_ListOfInteractive List;
728 myAISContext->DisplayedObjects(List);
730 #if OCC_VERSION_LARGE <= 0x06060000
733 AIS_ListOfInteractive List1;
734 myAISContext->ObjectsInCollector(List1);
739 AIS_ListIteratorOfListOfInteractive ite(List);
740 for ( ; ite.More(); ite.Next() )
741 if( ite.Value()==obj )
748 \return true if object is displayed in viewer
749 \param obj - object to be checked
751 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
753 return myAISContext->IsDisplayed( obj );
758 \param obj - object to be updated
759 \param color - new color
760 \param update - update current viewer
762 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
768 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
769 color.green() / 255.,
772 obj->SetColor( CSFColor );
776 myV3dViewer->Update();
780 Changes display mode of object
781 \param obj - object to be processed
782 \param mode - new display mode
783 \param update - update current viewer
785 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
786 int mode, bool update )
788 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
790 myV3dViewer->Update();
794 Changes transparency of object
795 \param obj - object to be processed
796 \param trans - new transparency
797 \param update - update current viewer
799 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
800 float trans, bool update )
802 myAISContext->SetTransparency( obj, trans, false );
803 myAISContext->Redisplay( obj, Standard_False, Standard_True );
805 myV3dViewer->Update();
809 Changes visibility of trihedron to opposite
811 void OCCViewer_Viewer::toggleTrihedron()
813 setTrihedronShown( !isTrihedronVisible() );
817 \return true if trihedron is visible
819 bool OCCViewer_Viewer::isTrihedronVisible() const
821 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
825 Sets visibility state of trihedron
826 \param on - new state
829 void OCCViewer_Viewer::setTrihedronShown( const bool on )
831 if ( myTrihedron.IsNull() )
835 myAISContext->Display( myTrihedron );
836 myAISContext->Deactivate(myTrihedron);
839 myAISContext->Erase( myTrihedron );
844 \return trihedron size
846 double OCCViewer_Viewer::trihedronSize() const
849 if ( !myTrihedron.IsNull() )
850 sz = myTrihedron->Size();
855 Changes trihedron size
858 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
860 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
861 myTrihedronSize = sz;
862 myIsRelative = isRelative;
868 Set number of isolines
869 \param u - u-isolines (first parametric co-ordinate)
870 \param v - v-isolines (second parametric co-ordinate)
872 void OCCViewer_Viewer::setIsos( const int u, const int v )
874 Handle(AIS_InteractiveContext) ic = getAISContext();
878 ic->SetIsoNumber( u, AIS_TOI_IsoU );
879 ic->SetIsoNumber( v, AIS_TOI_IsoV );
883 \return number of isolines
884 \param u - to return u-isolines (first parametric co-ordinate)
885 \param v - to return v-isolines (second parametric co-ordinate)
887 void OCCViewer_Viewer::isos( int& u, int& v ) const
889 Handle(AIS_InteractiveContext) ic = getAISContext();
892 u = ic->IsoNumber( AIS_TOI_IsoU );
893 v = ic->IsoNumber( AIS_TOI_IsoV );
898 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
900 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
902 return new OCCViewer_ViewWindow(0, this);
906 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
908 return background( theViewId ).color();
911 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
913 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
917 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
919 if ( theColor.isValid() ) {
920 Qtx::BackgroundData bg = background( theViewId );
921 bg.setColor( theColor );
922 setBackground( theViewId, bg );
926 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
928 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
929 myBackgrounds[theViewId] = theBackground;
934 Set the show static trihedron flag
936 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
938 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
939 if ( aView ) aView->showStaticTrihedron( on );
943 Get new and current trihedron size corresponding to the current model size
945 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
950 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
951 Handle(V3d_Viewer) viewer = getViewer3d();
952 viewer->InitActiveViews();
953 if(!viewer->MoreActiveViews()) return false;
955 Handle(V3d_View) view3d = viewer->ActiveView();
958 if ( view3d.IsNull() )
961 double aMaxSide = computeSceneSize( view3d );
964 // The boundary box of the view may be initialized but nullified
965 // (case of infinite objects)
966 if ( aMaxSide < Precision::Confusion() )
969 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
971 static float EPS = 5.0E-3;
972 theSize = getTrihedron()->Size();
973 theNewSize = aMaxSide*aSizeInPercents / 100.0;
975 return fabs( theNewSize - theSize ) > theSize * EPS ||
976 fabs( theNewSize - theSize ) > theNewSize * EPS;
982 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
985 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
987 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
989 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
990 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
992 aMaxSide = Xmax - Xmin;
993 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
994 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1001 * Update the size of the trihedron
1003 void OCCViewer_Viewer::updateTrihedron() {
1004 if ( myTrihedron.IsNull() )
1008 double newSz, oldSz;
1010 if(computeTrihedronSize(newSz, oldSz))
1011 myTrihedron->SetSize(newSz);
1013 } else if(myTrihedron->Size() != myTrihedronSize) {
1014 myTrihedron->SetSize(myTrihedronSize);
1019 Set number of isolines
1020 \param u - u-isolines (first parametric co-ordinate)
1021 \param v - v-isolines (second parametric co-ordinate)
1023 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1025 myPreselectionEnabled = isPreselectionEnabled;
1026 mySelectionEnabled = isSelectionEnabled;
1027 //clear current selection in the viewer
1029 if(!mySelectionEnabled) {
1030 myAISContext->ClearSelected( Standard_True );
1035 Creates clipping plane based on the incoming plane
1037 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1039 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1040 aGraphic3dPlane->SetOn( theIsOn );
1041 aGraphic3dPlane->SetCapping( Standard_True );
1042 return aGraphic3dPlane;
1045 Applies clipping planes to clippable objects
1047 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1049 // 1. Remove existing clipping planes
1050 myClipPlanes.clear();
1051 myInternalClipPlanes.Clear();
1053 // 2. Create new clipping planes
1054 ClipPlanesList::iterator inIt = theList.begin();
1055 for (;inIt != theList.end(); inIt++ )
1057 OCCViewer_ClipPlane aPlane = *inIt;
1059 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1060 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1062 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1063 gp_Dir aDirection( aDx, aDy, aDz );
1065 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1066 myClipPlanes.push_back( aPlane );
1069 // 3. Apply clipping planes
1070 AIS_ListOfInteractive aList;
1071 myAISContext->DisplayedObjects (aList);
1072 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1073 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1074 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1075 if (!aShape.IsNull() && aShape->IsClippable()) {
1076 aShape->SetClipPlanes(myInternalClipPlanes);
1082 Returns the clipping planes applied to the displayed objects.
1084 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1085 return myClipPlanes;
1088 Applies clipping planes to given object objects
1090 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1092 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1093 if (!aShape.IsNull() && aShape->IsClippable())
1095 aShape->SetClipPlanes (myInternalClipPlanes);
1100 Returns the pointer to the clipping dialog box.
1102 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1103 return myClippingDlg;
1108 Stores pointer to the clipping dialog box.
1110 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1111 if(myClippingDlg != theDlg) {
1112 myClippingDlg = theDlg;