1 // Copyright (C) 2007-2015 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 #if OCC_VERSION_LARGE > 0x06080000
57 #include <Prs3d_Drawer.hxx>
59 #include <AIS_Drawer.hxx>
61 #include <AIS_ListOfInteractive.hxx>
62 #include <AIS_ListIteratorOfListOfInteractive.hxx>
64 #include <Graphic3d_Texture2Dmanual.hxx>
65 #include <Graphic3d_MaterialAspect.hxx>
66 #include <Graphic3d_TextureParams.hxx>
68 #include <Geom_Axis2Placement.hxx>
69 #include <Prs3d_Drawer.hxx>
70 #include <Prs3d_DatumAspect.hxx>
71 #include <Prs3d_LineAspect.hxx>
72 #include <Prs3d_TextAspect.hxx>
74 #include <Visual3d_View.hxx>
77 Get data for supported background modes: gradient types, identifiers and supported image formats
79 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
81 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
82 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
83 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
84 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
85 idList << HorizontalGradient << VerticalGradient <<
86 Diagonal1Gradient << Diagonal2Gradient <<
87 Corner1Gradient << Corner2Gradient <<
88 Corner3Gradient << Corner4Gradient;
89 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
90 return tr("BG_IMAGE_FILES");
95 \param DisplayTrihedron - is trihedron displayed
97 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
99 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
102 myTrihedronSize(100),
105 // init CasCade viewers
106 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
107 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
108 myV3dViewer->SetDefaultLights();
111 myAISContext = new AIS_InteractiveContext( myV3dViewer );
112 myAISContext->SelectionColor( Quantity_NOC_WHITE );
114 // display isoline on planar faces (box for ex.)
115 myAISContext->IsoOnPlane( true );
117 /* create trihedron */
118 if ( DisplayTrihedron )
120 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
121 myTrihedron = new AIS_Trihedron(anAxis);
122 myTrihedron->SetInfiniteState( Standard_True );
124 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
125 //myTrihedron->SetColor( Col );
126 myTrihedron->SetArrowColor( Col.Name() );
127 myTrihedron->SetSize(100);
128 #if OCC_VERSION_LARGE > 0x06080000
129 Handle(Prs3d_Drawer) drawer = myTrihedron->Attributes();
130 if (drawer->HasOwnDatumAspect()) {
132 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
133 if (drawer->HasDatumAspect()) {
135 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
136 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
137 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
138 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
142 // set interaction style to standard
143 myInteractionStyle = 0;
145 // set zooming style to standard
149 myPreselectionEnabled = true;
152 mySelectionEnabled = true;
153 myMultiSelectionEnabled = true;
155 //set clipping color and texture to standard
156 myClippingColor = QColor( 50, 50, 50 );
157 myDefaultTextureUsed = true;
158 myClippingTexture = QString();
159 myTextureModulated = true;
160 myClippingTextureScale = 1.0;
167 OCCViewer_Viewer::~OCCViewer_Viewer()
169 myAISContext.Nullify();
170 myV3dViewer.Nullify();
175 \return background color of viewer
177 QColor OCCViewer_Viewer::backgroundColor() const
179 return backgroundColor(0);
183 \return background data of viewer
185 Qtx::BackgroundData OCCViewer_Viewer::background() const
187 return background(0);
191 Sets background color [obsolete]
192 \param c - new background color
194 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
196 setBackgroundColor( 0, c );
201 \param d - new background data
203 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
205 setBackground( 0, theBackground );
209 Start initialization of view window
210 \param view - view window to be initialized
212 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
216 view->initSketchers();
217 view->setInteractionStyle( interactionStyle() );
218 view->setZoomingStyle( zoomingStyle() );
219 view->enablePreselection( isPreselectionEnabled() );
220 view->enableSelection( isSelectionEnabled() );
222 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
225 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
226 // connect signal from viewport
227 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
228 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
234 Creates new view window
235 \param theDesktop - main window of application
237 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
240 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
241 // get main view window (created by view frame)
242 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
243 // initialize main view window
245 // set default background for view window
246 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
252 Sets new view manager
253 \param theViewManager - new view manager
255 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
257 SUIT_ViewModel::setViewManager(theViewManager);
258 if (theViewManager) {
259 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
260 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
262 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
263 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
265 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
266 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
268 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
269 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
274 SLOT: called on mouse button press, stores current mouse position as start point for transformations
276 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
278 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
282 SLOT: called on mouse move, processes transformation or hilighting
284 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
286 if (!mySelectionEnabled) return;
287 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
289 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
291 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
293 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
294 if (aView->getViewPort()->isBusy()) {
295 QCoreApplication::processEvents();
296 return; // Check that the ViewPort initialization completed
297 // To Prevent call move event if the View port is not initialized
300 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
301 if ( !aView3d.IsNull() ) {
302 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
309 SLOT: called on mouse button release, finishes transformation or selection
311 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
313 if (!mySelectionEnabled) return;
314 if (theEvent->button() != Qt::LeftButton) return;
315 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
317 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
321 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
322 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
325 myAISContext->ClearCurrents( false );
329 if (myStartPnt == myEndPnt)
331 if ( !isPreselectionEnabled() ) {
332 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
333 if ( !aView3d.IsNull() ) {
334 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
338 if (aHasShift && myMultiSelectionEnabled)
339 myAISContext->ShiftSelect();
341 myAISContext->Select();
345 if (aHasShift && myMultiSelectionEnabled)
346 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
347 myEndPnt.x(), myEndPnt.y(),
348 aView->getViewPort()->getView(), Standard_False );
350 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
351 myEndPnt.x(), myEndPnt.y(),
352 aView->getViewPort()->getView(), Standard_False );
354 int Nb = myAISContext->NbSelected();
355 if( Nb>1 && !myMultiSelectionEnabled )
357 myAISContext->InitSelected();
358 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
359 if( !anOwner.IsNull() )
361 myAISContext->ClearSelected( Standard_False );
362 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
366 myAISContext->UpdateCurrentViewer();
368 emit selectionChanged();
372 SLOT: called on key press, processes selection in "key free" interaction style
374 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
376 if (!mySelectionEnabled) return;
378 OCCViewer_ViewWindow* aView = qobject_cast<OCCViewer_ViewWindow*>( theWindow );
379 if ( !aView ) return;
381 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
383 switch ( theEvent->key() ) {
386 myAISContext->ClearCurrents( false );
390 if ( !isPreselectionEnabled() ) {
391 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
392 if ( !aView3d.IsNull() ) {
393 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
397 if (aHasShift && myMultiSelectionEnabled)
398 myAISContext->ShiftSelect();
400 myAISContext->Select();
402 emit selectionChanged();
406 if ( isPreselectionEnabled() ) {
407 if ( getAISContext()->HasOpenedContext() )
408 getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
412 if ( isPreselectionEnabled() ) {
413 if ( getAISContext()->HasOpenedContext() )
414 getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
422 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
424 Standard_Integer aViewsNb = 0;
425 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
427 if ( aViewsNb < 2 ) {
428 //clean up presentations before last view is closed
429 myAISContext->RemoveAll(Standard_False);
433 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
435 setTrihedronShown( true );
436 bool showStaticTrihedron = true;
437 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
438 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
439 viewPort->showStaticTrihedron( showStaticTrihedron );
442 int OCCViewer_Viewer::getTopLayerId()
444 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
445 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
451 \return interaction style
453 int OCCViewer_Viewer::interactionStyle() const
455 return myInteractionStyle;
459 Sets interaction style: 0 - standard, 1 - keyboard free interaction
460 \param theStyle - new interaction style
462 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
464 myInteractionStyle = 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->setInteractionStyle( theStyle );
479 \return zooming style
481 int OCCViewer_Viewer::zoomingStyle() const
483 return myZoomingStyle;
487 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
488 \param theStyle - new zooming style
490 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
492 myZoomingStyle = theStyle;
493 //!! To be done for view windows
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->setZoomingStyle( theStyle );
507 \return true if preselection is enabled
509 bool OCCViewer_Viewer::isPreselectionEnabled() const
511 return myPreselectionEnabled;
515 Enables/disables preselection
516 \param isEnabled - new status
518 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
520 myPreselectionEnabled = isEnabled;
522 if ( !myViewManager )
525 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
526 for ( int i = 0; i < (int)wins.count(); i++ )
528 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
530 win->enablePreselection( isEnabled );
536 \return true if selection is enabled
538 bool OCCViewer_Viewer::isSelectionEnabled() const
540 return mySelectionEnabled;
544 Enables/disables selection
545 \param isEnabled - new status
547 void OCCViewer_Viewer::enableSelection(bool isEnabled)
549 mySelectionEnabled = isEnabled;
551 //!! To be done for view windows
552 if ( !myViewManager )
555 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
556 for ( int i = 0; i < (int)wins.count(); i++ )
558 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
560 win->updateEnabledDrawMode();
561 win->enableSelection( isEnabled );
566 //clear current selection in the viewer
568 myAISContext->ClearSelected( Standard_True );
574 Sets multiselection enabled status
575 \param isEnabled - new status
577 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
579 myMultiSelectionEnabled = isEnable;
580 //!! To be done for view windows
581 if ( !myViewManager )
584 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
585 for ( int i = 0; i < (int)wins.count(); i++ )
587 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
589 win->updateEnabledDrawMode();
594 Sets a color of the clipped region
595 \param theColor - a new color of the clipped region
597 void OCCViewer_Viewer::setClippingColor( const QColor& theColor )
599 myClippingColor = theColor;
601 if( myInternalClipPlanes.IsEmpty() )
604 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
605 aMaterialAspect.SetColor( Quantity_Color( theColor.redF(), theColor.greenF(),
606 theColor.blueF(), Quantity_TOC_RGB ) );
608 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
609 myInternalClipPlanes.Value(i)->SetCappingMaterial( aMaterialAspect );
615 \return clipping color
617 QColor OCCViewer_Viewer::clippingColor() const
619 return myClippingColor;
622 // initialize a texture for clipped region
623 Handle(Graphic3d_Texture2Dmanual) initClippingTexture( const bool isDefault, const QString& theTexture,
624 const bool isModulate, const double theScale )
626 QString aTextureFile = isDefault ? ":images/hatch.png" : theTexture;
627 QPixmap px( aTextureFile );
628 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
629 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
630 if( aTexture->IsDone() ) {
631 aTexture->EnableRepeat();
632 isModulate ? aTexture->EnableModulate() : aTexture->DisableModulate();
633 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 1/( theScale*100 ), -1 / ( theScale*100 ) ) );
639 Sets default texture parameters
640 \param isDefault - use/non-use default texture
641 \param theTexture - new texture of the clipped region
642 \param isModulate - enable/disable texture modulate mode
643 \param theScale - scale factor.
645 void OCCViewer_Viewer::setClippingTextureParams( const bool isDefault, const QString& theTexture,
646 const bool isModulate, const double theScale )
648 myDefaultTextureUsed = isDefault;
649 myClippingTexture = theTexture;
650 myTextureModulated = isModulate;
651 myClippingTextureScale = theScale;
653 if( myInternalClipPlanes.IsEmpty() )
656 Handle(Graphic3d_Texture2Dmanual) aTexture =
657 initClippingTexture( myDefaultTextureUsed, myClippingTexture,
658 myTextureModulated, myClippingTextureScale );
660 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
661 myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
667 \return true if default texture is used
669 bool OCCViewer_Viewer::isDefaultTextureUsed() const
671 return myDefaultTextureUsed;
675 \return clipping texture
677 QString OCCViewer_Viewer::clippingTexture() const
679 return myClippingTexture;
683 \return true if texture is modulated
685 bool OCCViewer_Viewer::isTextureModulated() const
687 return myTextureModulated;
691 \return scale factor of texture
693 double OCCViewer_Viewer::clippingTextureScale() const
695 return myClippingTextureScale;
699 Builds popup for occ viewer
701 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
703 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
704 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
706 thePopup->addSeparator();
708 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
710 //Support of several toolbars in the popup menu
711 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
712 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
713 for ( ; it!=last; it++ ) {
714 if ( (*it)->parentWidget()->isVisible() )
715 thePopup->addAction( (*it)->toggleViewAction() );
720 SLOT: called on dump view operation is activated, stores scene to raster file
722 void OCCViewer_Viewer::onDumpView()
724 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
730 SLOT: called if background color is to be changed changed, passes new color to view port
732 void OCCViewer_Viewer::onChangeBackground()
734 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
738 // get supported gradient types
739 QStringList gradList;
740 QIntList idList, txtList;
741 QString formats = backgroundData( gradList, idList, txtList );
744 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
745 aView, // parent for dialog box
746 txtList, // allowed texture modes
747 true, // enable solid color mode
748 true, // enable gradient mode
749 false, // disable custom gradient mode
750 !txtList.isEmpty(), // enable/disable texture mode
751 gradList, // gradient names
752 idList, // gradient identifiers
753 formats ); // image formats
755 // set chosen background data to the viewer
756 if ( bgData.isValid() )
757 aView->setBackground( bgData );
761 Updates OCC 3D viewer
763 void OCCViewer_Viewer::update()
765 if (!myV3dViewer.IsNull())
766 myV3dViewer->Update();
768 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
770 aView->updateGravityCoords();
774 \return objects selected in 3D viewer
775 \param theList - list to be filled with selected objects
777 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
780 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
781 theList.Append(myAISContext->SelectedInteractive());
785 Selects objects in 3D viewer. Other selected objects are left as selected
786 \param theList - list objects to be selected
788 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
790 AIS_ListIteratorOfListOfInteractive aIt;
791 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
792 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
793 myAISContext->UpdateCurrentViewer();
797 Auxiliary method to emit signal selectionChanged()
799 void OCCViewer_Viewer::performSelectionChanged()
801 emit selectionChanged();
805 Hilights/unhilights object in viewer
806 \param obj - object to be updated
807 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
808 \param update - update current viewer
810 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
811 bool hilight, bool update )
813 bool isInLocal = myAISContext->HasOpenedContext();
817 if ( hilight && !myAISContext->IsSelected( obj ) )
818 myAISContext->AddOrRemoveCurrentObject( obj, false );
819 else if ( !hilight && myAISContext->IsSelected( obj ) )
820 myAISContext->AddOrRemoveCurrentObject( obj, false );
824 myV3dViewer->Redraw();
830 Unhilights all objects in viewer
831 \param updateviewer - update current viewer
833 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
835 if ( myAISContext->HasOpenedContext() ) {
837 myAISContext->ClearSelected( updateviewer );
839 myAISContext->UnhilightSelected( updateviewer );
843 myAISContext->ClearCurrents( updateviewer );
845 myAISContext->UnhilightCurrents( updateviewer );
853 \return true if object is in viewer or in collector
854 \param obj - object to be checked
855 \param onlyInViewer - search object only in viewer (so object must be displayed)
857 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
860 AIS_ListOfInteractive List;
861 myAISContext->DisplayedObjects(List);
863 AIS_ListIteratorOfListOfInteractive ite(List);
864 for ( ; ite.More(); ite.Next() )
865 if( ite.Value()==obj )
872 \return true if object is displayed in viewer
873 \param obj - object to be checked
875 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
877 return myAISContext->IsDisplayed( obj );
882 \param obj - object to be updated
883 \param color - new color
884 \param update - update current viewer
886 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
892 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
893 color.green() / 255.,
896 obj->SetColor( CSFColor );
900 myV3dViewer->Update();
904 Changes display mode of object
905 \param obj - object to be processed
906 \param mode - new display mode
907 \param update - update current viewer
909 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
910 int mode, bool update )
912 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
914 myV3dViewer->Update();
918 Changes transparency of object
919 \param obj - object to be processed
920 \param trans - new transparency
921 \param update - update current viewer
923 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
924 float trans, bool update )
926 myAISContext->SetTransparency( obj, trans, false );
927 myAISContext->Redisplay( obj, Standard_False, Standard_True );
929 myV3dViewer->Update();
933 Changes visibility of trihedron to opposite
935 void OCCViewer_Viewer::toggleTrihedron()
937 setTrihedronShown( !isTrihedronVisible() );
941 \return true if trihedron is visible
943 bool OCCViewer_Viewer::isTrihedronVisible() const
945 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
949 Sets visibility state of trihedron
950 \param on - new state
953 void OCCViewer_Viewer::setTrihedronShown( const bool on )
955 if ( myTrihedron.IsNull() )
959 myAISContext->Display( myTrihedron );
960 myAISContext->Deactivate(myTrihedron);
963 myAISContext->Erase( myTrihedron );
968 \return trihedron size
970 double OCCViewer_Viewer::trihedronSize() const
973 if ( !myTrihedron.IsNull() )
974 sz = myTrihedron->Size();
979 Changes trihedron size
982 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
984 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
985 myTrihedronSize = sz;
986 myIsRelative = isRelative;
992 Set number of isolines
993 \param u - u-isolines (first parametric co-ordinate)
994 \param v - v-isolines (second parametric co-ordinate)
996 void OCCViewer_Viewer::setIsos( const int u, const int v )
998 Handle(AIS_InteractiveContext) ic = getAISContext();
1002 ic->SetIsoNumber( u, AIS_TOI_IsoU );
1003 ic->SetIsoNumber( v, AIS_TOI_IsoV );
1007 \return number of isolines
1008 \param u - to return u-isolines (first parametric co-ordinate)
1009 \param v - to return v-isolines (second parametric co-ordinate)
1011 void OCCViewer_Viewer::isos( int& u, int& v ) const
1013 Handle(AIS_InteractiveContext) ic = getAISContext();
1016 u = ic->IsoNumber( AIS_TOI_IsoU );
1017 v = ic->IsoNumber( AIS_TOI_IsoV );
1022 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
1024 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
1026 return new OCCViewer_ViewWindow(0, this);
1030 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
1032 return background( theViewId ).color();
1035 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
1037 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
1041 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
1043 if ( theColor.isValid() ) {
1044 Qtx::BackgroundData bg = background( theViewId );
1045 bg.setColor( theColor );
1046 setBackground( theViewId, bg );
1050 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
1052 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
1053 myBackgrounds[theViewId] = theBackground;
1058 Set the show static trihedron flag
1060 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
1062 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1063 if ( aView ) aView->showStaticTrihedron( on );
1067 Get new and current trihedron size corresponding to the current model size
1069 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
1074 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
1075 Handle(V3d_Viewer) viewer = getViewer3d();
1076 viewer->InitActiveViews();
1077 if(!viewer->MoreActiveViews()) return false;
1079 Handle(V3d_View) view3d = viewer->ActiveView();
1082 if ( view3d.IsNull() )
1085 double aMaxSide = computeSceneSize( view3d );
1088 // The boundary box of the view may be initialized but nullified
1089 // (case of infinite objects)
1090 if ( aMaxSide < Precision::Confusion() )
1093 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
1095 static float EPS = 5.0E-3;
1096 theSize = getTrihedron()->Size();
1097 theNewSize = aMaxSide*aSizeInPercents / 100.0;
1099 return fabs( theNewSize - theSize ) > theSize * EPS ||
1100 fabs( theNewSize - theSize ) > theNewSize * EPS;
1104 * Compute scene size
1106 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
1108 double aMaxSide = 0;
1109 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
1111 #if OCC_VERSION_LARGE > 0x06070100
1112 Bnd_Box aBox = view3d->View()->MinMaxValues();
1113 Xmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
1114 Ymin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
1115 Zmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
1116 Xmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().X();
1117 Ymax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Y();
1118 Zmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Z();
1120 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
1123 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
1124 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
1126 aMaxSide = Xmax - Xmin;
1127 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
1128 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1135 * Update the size of the trihedron
1137 void OCCViewer_Viewer::updateTrihedron() {
1138 if ( myTrihedron.IsNull() )
1142 double newSz, oldSz;
1144 if(computeTrihedronSize(newSz, oldSz))
1145 myTrihedron->SetSize(newSz);
1147 } else if(myTrihedron->Size() != myTrihedronSize) {
1148 myTrihedron->SetSize(myTrihedronSize);
1153 Set number of isolines
1154 \param u - u-isolines (first parametric co-ordinate)
1155 \param v - v-isolines (second parametric co-ordinate)
1157 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1159 myPreselectionEnabled = isPreselectionEnabled;
1160 mySelectionEnabled = isSelectionEnabled;
1161 //clear current selection in the viewer
1163 if(!mySelectionEnabled) {
1164 myAISContext->ClearSelected( Standard_True );
1169 Creates clipping plane based on the incoming plane
1171 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1173 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1174 aGraphic3dPlane->SetOn( theIsOn );
1175 aGraphic3dPlane->SetCapping( Standard_True );
1177 // set capping color
1178 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
1179 aMaterialAspect.SetColor( Quantity_Color( myClippingColor.redF(), myClippingColor.greenF(),
1180 myClippingColor.blueF(), Quantity_TOC_RGB ) );
1181 aGraphic3dPlane->SetCappingMaterial( aMaterialAspect );
1183 // set capping texture
1184 aGraphic3dPlane->SetCappingTexture( initClippingTexture( myDefaultTextureUsed, myClippingTexture,
1185 myTextureModulated, myClippingTextureScale ) );
1187 return aGraphic3dPlane;
1190 Applies clipping planes to clippable objects
1192 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1194 // 1. Remove existing clipping planes
1195 myClipPlanes.clear();
1196 myInternalClipPlanes.Clear();
1198 // 2. Create new clipping planes
1199 ClipPlanesList::iterator inIt = theList.begin();
1200 for (;inIt != theList.end(); inIt++ )
1202 OCCViewer_ClipPlane aPlane = *inIt;
1204 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1205 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1207 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1208 gp_Dir aDirection( aDx, aDy, aDz );
1210 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1211 myClipPlanes.push_back( aPlane );
1214 // 3. Apply clipping planes
1215 AIS_ListOfInteractive aList;
1216 myAISContext->DisplayedObjects (aList);
1217 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1218 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1219 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1220 if (!aShape.IsNull() && aShape->IsClippable()) {
1221 aShape->SetClipPlanes(myInternalClipPlanes);
1227 Returns the clipping planes applied to the displayed objects.
1229 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1230 return myClipPlanes;
1233 Applies clipping planes to given object objects
1235 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1237 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1238 if (!aShape.IsNull() && aShape->IsClippable())
1240 aShape->SetClipPlanes (myInternalClipPlanes);
1245 Returns the pointer to the clipping dialog box.
1247 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1248 return myClippingDlg;
1253 Stores pointer to the clipping dialog box.
1255 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1256 if(myClippingDlg != theDlg) {
1257 myClippingDlg = theDlg;