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_MaterialAspect.hxx>
62 #include <Graphic3d_TextureParams.hxx>
64 #include <Geom_Axis2Placement.hxx>
65 #include <Prs3d_Drawer.hxx>
66 #include <Prs3d_DatumAspect.hxx>
67 #include <Prs3d_LineAspect.hxx>
68 #include <Prs3d_TextAspect.hxx>
70 #include <Visual3d_View.hxx>
73 Get data for supported background modes: gradient types, identifiers and supported image formats
75 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
77 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
78 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
79 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
80 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
81 idList << HorizontalGradient << VerticalGradient <<
82 Diagonal1Gradient << Diagonal2Gradient <<
83 Corner1Gradient << Corner2Gradient <<
84 Corner3Gradient << Corner4Gradient;
85 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
86 return tr("BG_IMAGE_FILES");
91 \param DisplayTrihedron - is trihedron displayed
93 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
95 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
101 // init CasCade viewers
102 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
103 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
104 myV3dViewer->SetDefaultLights();
107 myAISContext = new AIS_InteractiveContext( myV3dViewer );
108 myAISContext->SelectionColor( Quantity_NOC_WHITE );
110 // display isoline on planar faces (box for ex.)
111 myAISContext->IsoOnPlane( true );
113 /* create trihedron */
114 if ( DisplayTrihedron )
116 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
117 myTrihedron = new AIS_Trihedron(anAxis);
118 myTrihedron->SetInfiniteState( Standard_True );
120 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
121 //myTrihedron->SetColor( Col );
122 myTrihedron->SetArrowColor( Col.Name() );
123 myTrihedron->SetSize(100);
124 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
125 if (drawer->HasDatumAspect()) {
126 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
127 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
128 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
129 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
133 // set interaction style to standard
134 myInteractionStyle = 0;
136 // set zooming style to standard
140 myPreselectionEnabled = true;
143 mySelectionEnabled = true;
144 myMultiSelectionEnabled = true;
146 //set clipping color and texture to standard
147 myClippingColor = QColor( 50, 50, 50 );
148 myDefaultTextureUsed = true;
149 myClippingTexture = QString();
156 OCCViewer_Viewer::~OCCViewer_Viewer()
158 myAISContext.Nullify();
159 myV3dViewer.Nullify();
164 \return background color of viewer
166 QColor OCCViewer_Viewer::backgroundColor() const
168 return backgroundColor(0);
172 \return background data of viewer
174 Qtx::BackgroundData OCCViewer_Viewer::background() const
176 return background(0);
180 Sets background color [obsolete]
181 \param c - new background color
183 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
185 setBackgroundColor( 0, c );
190 \param d - new background data
192 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
194 setBackground( 0, theBackground );
198 Start initialization of view window
199 \param view - view window to be initialized
201 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
205 view->initSketchers();
206 view->setInteractionStyle( interactionStyle() );
207 view->setZoomingStyle( zoomingStyle() );
208 view->enablePreselection( isPreselectionEnabled() );
209 view->enableSelection( isSelectionEnabled() );
211 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
214 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
215 // connect signal from viewport
216 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
217 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
223 Creates new view window
224 \param theDesktop - main window of application
226 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
229 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
230 // get main view window (created by view frame)
231 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
232 // initialize main view window
234 // set default background for view window
235 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
241 Sets new view manager
242 \param theViewManager - new view manager
244 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
246 SUIT_ViewModel::setViewManager(theViewManager);
247 if (theViewManager) {
248 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
249 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
251 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
252 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
254 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
255 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
257 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
258 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
263 SLOT: called on mouse button press, stores current mouse position as start point for transformations
265 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
267 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
271 SLOT: called on mouse move, processes transformation or hilighting
273 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
275 if (!mySelectionEnabled) return;
276 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
278 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
280 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
282 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
283 if (aView->getViewPort()->isBusy()) {
284 QCoreApplication::processEvents();
285 return; // Check that the ViewPort initialization completed
286 // To Prevent call move event if the View port is not initialized
289 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
290 if ( !aView3d.IsNull() ) {
291 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
298 SLOT: called on mouse button release, finishes transformation or selection
300 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
302 if (!mySelectionEnabled) return;
303 if (theEvent->button() != Qt::LeftButton) return;
304 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
306 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
310 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
311 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
314 myAISContext->ClearCurrents( false );
318 if (myStartPnt == myEndPnt)
320 if ( !isPreselectionEnabled() ) {
321 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
322 if ( !aView3d.IsNull() ) {
323 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
327 if (aHasShift && myMultiSelectionEnabled)
328 myAISContext->ShiftSelect();
330 myAISContext->Select();
334 if (aHasShift && myMultiSelectionEnabled)
335 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
336 myEndPnt.x(), myEndPnt.y(),
337 aView->getViewPort()->getView(), Standard_False );
339 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
340 myEndPnt.x(), myEndPnt.y(),
341 aView->getViewPort()->getView(), Standard_False );
343 int Nb = myAISContext->NbSelected();
344 if( Nb>1 && !myMultiSelectionEnabled )
346 myAISContext->InitSelected();
347 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
348 if( !anOwner.IsNull() )
350 myAISContext->ClearSelected( Standard_False );
351 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
355 myAISContext->UpdateCurrentViewer();
357 emit selectionChanged();
361 SLOT: called on key press, processes selection in "key free" interaction style
363 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
365 if (!mySelectionEnabled) return;
367 OCCViewer_ViewWindow* aView = qobject_cast<OCCViewer_ViewWindow*>( theWindow );
368 if ( !aView ) return;
370 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
372 switch ( theEvent->key() ) {
375 myAISContext->ClearCurrents( false );
379 if ( !isPreselectionEnabled() ) {
380 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
381 if ( !aView3d.IsNull() ) {
382 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
386 if (aHasShift && myMultiSelectionEnabled)
387 myAISContext->ShiftSelect();
389 myAISContext->Select();
391 emit selectionChanged();
395 if ( isPreselectionEnabled() ) {
396 if ( getAISContext()->HasOpenedContext() )
397 getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
401 if ( isPreselectionEnabled() ) {
402 if ( getAISContext()->HasOpenedContext() )
403 getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
411 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
413 Standard_Integer aViewsNb = 0;
414 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
416 if ( aViewsNb < 2 ) {
417 //clean up presentations before last view is closed
418 myAISContext->RemoveAll(Standard_False);
422 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
424 setTrihedronShown( true );
425 bool showStaticTrihedron = true;
426 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
427 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
428 viewPort->showStaticTrihedron( showStaticTrihedron );
431 int OCCViewer_Viewer::getTopLayerId()
433 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
434 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
440 \return interaction style
442 int OCCViewer_Viewer::interactionStyle() const
444 return myInteractionStyle;
448 Sets interaction style: 0 - standard, 1 - keyboard free interaction
449 \param theStyle - new interaction style
451 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
453 myInteractionStyle = theStyle;
454 //!! To be done for view windows
455 if ( !myViewManager )
458 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
459 for ( int i = 0; i < (int)wins.count(); i++ )
461 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
463 win->setInteractionStyle( theStyle );
468 \return zooming style
470 int OCCViewer_Viewer::zoomingStyle() const
472 return myZoomingStyle;
476 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
477 \param theStyle - new zooming style
479 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
481 myZoomingStyle = theStyle;
482 //!! To be done for view windows
483 if ( !myViewManager )
486 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
487 for ( int i = 0; i < (int)wins.count(); i++ )
489 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
491 win->setZoomingStyle( theStyle );
496 \return true if preselection is enabled
498 bool OCCViewer_Viewer::isPreselectionEnabled() const
500 return myPreselectionEnabled;
504 Enables/disables preselection
505 \param isEnabled - new status
507 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
509 myPreselectionEnabled = isEnabled;
511 if ( !myViewManager )
514 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
515 for ( int i = 0; i < (int)wins.count(); i++ )
517 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
519 win->enablePreselection( isEnabled );
525 \return true if selection is enabled
527 bool OCCViewer_Viewer::isSelectionEnabled() const
529 return mySelectionEnabled;
533 Enables/disables selection
534 \param isEnabled - new status
536 void OCCViewer_Viewer::enableSelection(bool isEnabled)
538 mySelectionEnabled = isEnabled;
540 //!! To be done for view windows
541 if ( !myViewManager )
544 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
545 for ( int i = 0; i < (int)wins.count(); i++ )
547 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
549 win->updateEnabledDrawMode();
550 win->enableSelection( isEnabled );
555 //clear current selection in the viewer
557 myAISContext->ClearSelected( Standard_True );
563 Sets multiselection enabled status
564 \param isEnabled - new status
566 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
568 myMultiSelectionEnabled = isEnable;
569 //!! To be done for view windows
570 if ( !myViewManager )
573 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
574 for ( int i = 0; i < (int)wins.count(); i++ )
576 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
578 win->updateEnabledDrawMode();
583 Sets a color of the clipped region
584 \param theColor - a new color of the clipped region
586 void OCCViewer_Viewer::setClippingColor( const QColor& theColor )
588 myClippingColor = theColor;
590 if( myInternalClipPlanes.IsEmpty() )
593 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
594 aMaterialAspect.SetColor( Quantity_Color( theColor.redF(), theColor.greenF(),
595 theColor.blueF(), Quantity_TOC_RGB ) );
597 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
598 myInternalClipPlanes.Value(i)->SetCappingMaterial( aMaterialAspect );
604 \return clipping color
606 QColor OCCViewer_Viewer::clippingColor() const
608 return myClippingColor;
611 // initialize a texture for clipped region
612 Handle(Graphic3d_Texture2Dmanual) initClippingTexture( const bool isDefault, const QString& theTexture)
614 QString aTextureFile = isDefault ? ":images/hatch.png" : theTexture;
615 QPixmap px( aTextureFile );
616 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
617 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
618 if( aTexture->IsDone() ) {
619 aTexture->EnableRepeat();
621 aTexture->EnableModulate();
622 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 0.01, 0.01 ) );
625 aTexture->DisableModulate();
626 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 0.005, 0.005 ) );
633 Sets default texture enabled status
634 \param isUsed - new status
636 void OCCViewer_Viewer::useDefaultTexture( const bool isUsed )
638 myDefaultTextureUsed = isUsed;
640 if( myInternalClipPlanes.IsEmpty() )
643 Handle(Graphic3d_Texture2Dmanual) aTexture =
644 initClippingTexture( myDefaultTextureUsed, myClippingTexture );
646 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
647 myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
653 \return true if default texture is used
655 bool OCCViewer_Viewer::isDefaultTextureUsed() const
657 return myDefaultTextureUsed;
661 Sets a texture of the clipped region
662 \param theTexture - a new texture of the clipped region
664 void OCCViewer_Viewer::setClippingTexture( const QString& theTexture )
666 myClippingTexture = theTexture;
668 if( myInternalClipPlanes.IsEmpty() || myDefaultTextureUsed )
671 Handle(Graphic3d_Texture2Dmanual) aTexture =
672 initClippingTexture( myDefaultTextureUsed, myClippingTexture );
674 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
675 myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
681 \return clipping texture
683 QString OCCViewer_Viewer::clippingTexture() const
685 return myClippingTexture;
689 Builds popup for occ viewer
691 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
693 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
694 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
696 thePopup->addSeparator();
698 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
700 //Support of several toolbars in the popup menu
701 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
702 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
703 for ( ; it!=last; it++ ) {
704 if ( (*it)->parentWidget()->isVisible() )
705 thePopup->addAction( (*it)->toggleViewAction() );
710 SLOT: called on dump view operation is activated, stores scene to raster file
712 void OCCViewer_Viewer::onDumpView()
714 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
720 SLOT: called if background color is to be changed changed, passes new color to view port
722 void OCCViewer_Viewer::onChangeBackground()
724 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
728 // get supported gradient types
729 QStringList gradList;
730 QIntList idList, txtList;
731 QString formats = backgroundData( gradList, idList, txtList );
734 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
735 aView, // parent for dialog box
736 txtList, // allowed texture modes
737 true, // enable solid color mode
738 true, // enable gradient mode
739 false, // disable custom gradient mode
740 !txtList.isEmpty(), // enable/disable texture mode
741 gradList, // gradient names
742 idList, // gradient identifiers
743 formats ); // image formats
745 // set chosen background data to the viewer
746 if ( bgData.isValid() )
747 aView->setBackground( bgData );
751 Updates OCC 3D viewer
753 void OCCViewer_Viewer::update()
755 if (!myV3dViewer.IsNull())
756 myV3dViewer->Update();
758 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
760 aView->updateGravityCoords();
764 \return objects selected in 3D viewer
765 \param theList - list to be filled with selected objects
767 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
770 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
771 theList.Append(myAISContext->SelectedInteractive());
775 Selects objects in 3D viewer. Other selected objects are left as selected
776 \param theList - list objects to be selected
778 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
780 AIS_ListIteratorOfListOfInteractive aIt;
781 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
782 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
783 myAISContext->UpdateCurrentViewer();
787 Auxiliary method to emit signal selectionChanged()
789 void OCCViewer_Viewer::performSelectionChanged()
791 emit selectionChanged();
795 Hilights/unhilights object in viewer
796 \param obj - object to be updated
797 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
798 \param update - update current viewer
800 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
801 bool hilight, bool update )
803 bool isInLocal = myAISContext->HasOpenedContext();
807 if ( hilight && !myAISContext->IsSelected( obj ) )
808 myAISContext->AddOrRemoveCurrentObject( obj, false );
809 else if ( !hilight && myAISContext->IsSelected( obj ) )
810 myAISContext->AddOrRemoveCurrentObject( obj, false );
814 myV3dViewer->Redraw();
820 Unhilights all objects in viewer
821 \param updateviewer - update current viewer
823 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
825 if ( myAISContext->HasOpenedContext() ) {
827 myAISContext->ClearSelected( updateviewer );
829 myAISContext->UnhilightSelected( updateviewer );
833 myAISContext->ClearCurrents( updateviewer );
835 myAISContext->UnhilightCurrents( updateviewer );
843 \return true if object is in viewer or in collector
844 \param obj - object to be checked
845 \param onlyInViewer - search object only in viewer (so object must be displayed)
847 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
850 AIS_ListOfInteractive List;
851 myAISContext->DisplayedObjects(List);
853 AIS_ListIteratorOfListOfInteractive ite(List);
854 for ( ; ite.More(); ite.Next() )
855 if( ite.Value()==obj )
862 \return true if object is displayed in viewer
863 \param obj - object to be checked
865 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
867 return myAISContext->IsDisplayed( obj );
872 \param obj - object to be updated
873 \param color - new color
874 \param update - update current viewer
876 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
882 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
883 color.green() / 255.,
886 obj->SetColor( CSFColor );
890 myV3dViewer->Update();
894 Changes display mode of object
895 \param obj - object to be processed
896 \param mode - new display mode
897 \param update - update current viewer
899 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
900 int mode, bool update )
902 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
904 myV3dViewer->Update();
908 Changes transparency of object
909 \param obj - object to be processed
910 \param trans - new transparency
911 \param update - update current viewer
913 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
914 float trans, bool update )
916 myAISContext->SetTransparency( obj, trans, false );
917 myAISContext->Redisplay( obj, Standard_False, Standard_True );
919 myV3dViewer->Update();
923 Changes visibility of trihedron to opposite
925 void OCCViewer_Viewer::toggleTrihedron()
927 setTrihedronShown( !isTrihedronVisible() );
931 \return true if trihedron is visible
933 bool OCCViewer_Viewer::isTrihedronVisible() const
935 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
939 Sets visibility state of trihedron
940 \param on - new state
943 void OCCViewer_Viewer::setTrihedronShown( const bool on )
945 if ( myTrihedron.IsNull() )
949 myAISContext->Display( myTrihedron );
950 myAISContext->Deactivate(myTrihedron);
953 myAISContext->Erase( myTrihedron );
958 \return trihedron size
960 double OCCViewer_Viewer::trihedronSize() const
963 if ( !myTrihedron.IsNull() )
964 sz = myTrihedron->Size();
969 Changes trihedron size
972 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
974 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
975 myTrihedronSize = sz;
976 myIsRelative = isRelative;
982 Set number of isolines
983 \param u - u-isolines (first parametric co-ordinate)
984 \param v - v-isolines (second parametric co-ordinate)
986 void OCCViewer_Viewer::setIsos( const int u, const int v )
988 Handle(AIS_InteractiveContext) ic = getAISContext();
992 ic->SetIsoNumber( u, AIS_TOI_IsoU );
993 ic->SetIsoNumber( v, AIS_TOI_IsoV );
997 \return number of isolines
998 \param u - to return u-isolines (first parametric co-ordinate)
999 \param v - to return v-isolines (second parametric co-ordinate)
1001 void OCCViewer_Viewer::isos( int& u, int& v ) const
1003 Handle(AIS_InteractiveContext) ic = getAISContext();
1006 u = ic->IsoNumber( AIS_TOI_IsoU );
1007 v = ic->IsoNumber( AIS_TOI_IsoV );
1012 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
1014 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
1016 return new OCCViewer_ViewWindow(0, this);
1020 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
1022 return background( theViewId ).color();
1025 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
1027 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
1031 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
1033 if ( theColor.isValid() ) {
1034 Qtx::BackgroundData bg = background( theViewId );
1035 bg.setColor( theColor );
1036 setBackground( theViewId, bg );
1040 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
1042 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
1043 myBackgrounds[theViewId] = theBackground;
1048 Set the show static trihedron flag
1050 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
1052 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1053 if ( aView ) aView->showStaticTrihedron( on );
1057 Get new and current trihedron size corresponding to the current model size
1059 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
1064 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
1065 Handle(V3d_Viewer) viewer = getViewer3d();
1066 viewer->InitActiveViews();
1067 if(!viewer->MoreActiveViews()) return false;
1069 Handle(V3d_View) view3d = viewer->ActiveView();
1072 if ( view3d.IsNull() )
1075 double aMaxSide = computeSceneSize( view3d );
1078 // The boundary box of the view may be initialized but nullified
1079 // (case of infinite objects)
1080 if ( aMaxSide < Precision::Confusion() )
1083 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
1085 static float EPS = 5.0E-3;
1086 theSize = getTrihedron()->Size();
1087 theNewSize = aMaxSide*aSizeInPercents / 100.0;
1089 return fabs( theNewSize - theSize ) > theSize * EPS ||
1090 fabs( theNewSize - theSize ) > theNewSize * EPS;
1094 * Compute scene size
1096 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
1098 double aMaxSide = 0;
1099 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
1101 #if OCC_VERSION_LARGE > 0x06070100
1102 Bnd_Box aBox = view3d->View()->MinMaxValues();
1103 Xmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
1104 Ymin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
1105 Zmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
1106 Xmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().X();
1107 Ymax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Y();
1108 Zmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Z();
1110 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
1113 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
1114 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
1116 aMaxSide = Xmax - Xmin;
1117 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
1118 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1125 * Update the size of the trihedron
1127 void OCCViewer_Viewer::updateTrihedron() {
1128 if ( myTrihedron.IsNull() )
1132 double newSz, oldSz;
1134 if(computeTrihedronSize(newSz, oldSz))
1135 myTrihedron->SetSize(newSz);
1137 } else if(myTrihedron->Size() != myTrihedronSize) {
1138 myTrihedron->SetSize(myTrihedronSize);
1143 Set number of isolines
1144 \param u - u-isolines (first parametric co-ordinate)
1145 \param v - v-isolines (second parametric co-ordinate)
1147 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1149 myPreselectionEnabled = isPreselectionEnabled;
1150 mySelectionEnabled = isSelectionEnabled;
1151 //clear current selection in the viewer
1153 if(!mySelectionEnabled) {
1154 myAISContext->ClearSelected( Standard_True );
1159 Creates clipping plane based on the incoming plane
1161 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1163 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1164 aGraphic3dPlane->SetOn( theIsOn );
1165 aGraphic3dPlane->SetCapping( Standard_True );
1167 // set capping color
1168 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
1169 aMaterialAspect.SetColor( Quantity_Color( myClippingColor.redF(), myClippingColor.greenF(),
1170 myClippingColor.blueF(), Quantity_TOC_RGB ) );
1171 aGraphic3dPlane->SetCappingMaterial( aMaterialAspect );
1173 // set capping texture
1174 aGraphic3dPlane->SetCappingTexture( initClippingTexture( myDefaultTextureUsed, myClippingTexture) );
1176 return aGraphic3dPlane;
1179 Applies clipping planes to clippable objects
1181 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1183 // 1. Remove existing clipping planes
1184 myClipPlanes.clear();
1185 myInternalClipPlanes.Clear();
1187 // 2. Create new clipping planes
1188 ClipPlanesList::iterator inIt = theList.begin();
1189 for (;inIt != theList.end(); inIt++ )
1191 OCCViewer_ClipPlane aPlane = *inIt;
1193 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1194 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1196 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1197 gp_Dir aDirection( aDx, aDy, aDz );
1199 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1200 myClipPlanes.push_back( aPlane );
1203 // 3. Apply clipping planes
1204 AIS_ListOfInteractive aList;
1205 myAISContext->DisplayedObjects (aList);
1206 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1207 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1208 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1209 if (!aShape.IsNull() && aShape->IsClippable()) {
1210 aShape->SetClipPlanes(myInternalClipPlanes);
1216 Returns the clipping planes applied to the displayed objects.
1218 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1219 return myClipPlanes;
1222 Applies clipping planes to given object objects
1224 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1226 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1227 if (!aShape.IsNull() && aShape->IsClippable())
1229 aShape->SetClipPlanes (myInternalClipPlanes);
1234 Returns the pointer to the clipping dialog box.
1236 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1237 return myClippingDlg;
1242 Stores pointer to the clipping dialog box.
1244 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1245 if(myClippingDlg != theDlg) {
1246 myClippingDlg = theDlg;