1 // Copyright (C) 2007-2008 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.
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
22 #include "SOCC_ViewModel.h"
25 #include "SOCC_ViewWindow.h"
27 #include "OCCViewer_Trihedron.h"
29 #include "SUIT_Session.h"
30 #include "SUIT_ResourceMgr.h"
31 //#include "SUIT_Application.h"
33 //#include "ToolsGUI.h"
35 // Temporarily commented to avoid awful dependecy on SALOMEDS
36 // TODO: better mechanism of storing display/erse status in a study
37 // should be provided...
38 //#include <SALOMEconfig.h>
39 //#include CORBA_CLIENT_HEADER(SALOMEDS)
41 #include <AIS_ListIteratorOfListOfInteractive.hxx>
42 #include <Visual3d_View.hxx>
44 #include <SALOME_AISShape.hxx>
45 #include <SALOME_AISObject.hxx>
46 #include <SALOME_InteractiveObject.hxx>
48 // Temporarily commented to avoid awful dependecy on SALOMEDS
49 // TODO: better mechanism of storing display/erse status in a study
50 // should be provided...
51 //#include <Utils_ORB_INIT.hxx>
52 //#include <Utils_SINGLETON.hxx>
53 //#include <SALOME_ModuleCatalog_impl.hxx>
54 //#include <SALOME_NamingService.hxx>
56 //#include "SALOMEDSClient.hxx"
57 //#include "SALOMEDS_StudyManager.hxx"
59 #include <AIS_TypeOfIso.hxx>
61 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
62 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
63 // SALOMEDS::StudyManager - no linkage with SalomeApp.
65 // Temporarily commented to avoid awful dependecy on SALOMEDS
66 // TODO: better mechanism of storing display/erse status in a study
67 // should be provided...
68 //static _PTR(Study) getStudyDS()
70 // SALOMEDSClient_Study* aStudy = NULL;
71 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
73 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
74 // id of its underlying SALOMEDS::Study
75 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
76 // if ( !app ) return _PTR(Study)(aStudy);
77 // SUIT_Study* stud = app->activeStudy();
78 // if ( !stud ) return _PTR(Study)(aStudy);
79 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
80 // get SALOMEDS_Study with this id from StudyMgr
81 // return aMgr->GetStudyByID( id );
86 \param DisplayTrihedron - is trihedron displayed
88 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
89 : OCCViewer_Viewer( DisplayTrihedron )
96 SOCC_Viewer::~SOCC_Viewer()
101 Hilights/unhilights object in viewer
102 \param obj - object to be updated
103 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
104 \param update - update current viewer
106 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
107 bool hilight, bool upd )
109 bool isInLocal = getAISContext()->HasOpenedContext();
110 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
111 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
113 AIS_ListOfInteractive List;
114 getAISContext()->DisplayedObjects(List);
116 AIS_ListIteratorOfListOfInteractive ite(List);
117 for ( ; ite.More(); ite.Next() )
119 Handle(SALOME_InteractiveObject) anObj =
120 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
122 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
125 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
126 // highlight subshapes only when local selection is active
129 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
131 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
132 TColStd_IndexedMapOfInteger MapIndex;
133 Sel->GetIndex( IObject, MapIndex );
134 aSh->highlightSubShapes( MapIndex, highlight );
148 \return true if object is in viewer or in collector
149 \param obj - object to be checked
150 \param onlyInViewer - search object only in viewer (so object must be displayed)
152 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
155 AIS_ListOfInteractive List;
156 getAISContext()->DisplayedObjects(List);
158 if( !onlyInViewer ) {
159 AIS_ListOfInteractive List1;
160 getAISContext()->ObjectsInCollector(List1);
164 AIS_ListIteratorOfListOfInteractive ite(List);
165 for ( ; ite.More(); ite.Next() )
167 Handle(SALOME_InteractiveObject) anObj =
168 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
170 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
177 \return true if object is displayed in viewer
178 \param obj - object to be checked
180 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
182 AIS_ListOfInteractive List;
183 getAISContext()->DisplayedObjects( List );
185 AIS_ListIteratorOfListOfInteractive ite( List );
186 for ( ; ite.More(); ite.Next() )
188 Handle(SALOME_InteractiveObject) anObj =
189 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
191 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
192 return getAISContext()->IsDisplayed( ite.Value() );
200 \param obj - object to be updated
201 \param color - new color
202 \param update - update current viewer
204 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
205 const QColor& color, bool update )
207 AIS_ListOfInteractive List;
208 getAISContext()->DisplayedObjects(List);
210 AIS_ListIteratorOfListOfInteractive ite(List);
211 for ( ; ite.More(); ite.Next() )
213 Handle(SALOME_InteractiveObject) anObj =
214 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
216 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
218 OCCViewer_Viewer::setColor( ite.Value(), color, update );
225 Changes display mode of object
226 \param obj - object to be processed
227 \param mode - new display mode
228 \param update - update current viewer
230 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
231 int mode, bool update )
233 AIS_ListOfInteractive List;
234 getAISContext()->DisplayedObjects(List);
236 AIS_ListIteratorOfListOfInteractive ite(List);
237 for ( ; ite.More(); ite.Next() )
239 Handle(SALOME_InteractiveObject) anObj =
240 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
242 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
244 OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
251 Changes transparency of object
252 \param obj - object to be processed
253 \param trans - new transparency
254 \param update - update current viewer
256 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
257 float trans, bool update )
259 AIS_ListOfInteractive List;
260 getAISContext()->DisplayedObjects( List );
262 AIS_ListIteratorOfListOfInteractive ite( List );
263 for ( ; ite.More(); ite.Next() )
265 Handle(SALOME_InteractiveObject) anObj =
266 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
268 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
270 OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
277 Changes name of object
278 \param obj - object to be renamed
279 \param name - new name
281 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
282 const QString& name )
284 AIS_ListOfInteractive List;
285 getAISContext()->DisplayedObjects(List);
287 AIS_ListIteratorOfListOfInteractive ite(List);
290 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
292 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
296 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
297 if ( IO->isSame( obj ) )
299 aSh->setName( name.toLatin1().data() );
304 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
306 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
308 // Add code here, if someone create a MODULE_AISObject.
317 \param prs - presentation
319 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
321 // try do downcast object
322 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
323 if ( !anOCCPrs || anOCCPrs->IsNull() )
326 // get SALOMEDS Study
327 // Temporarily commented to avoid awful dependecy on SALOMEDS
328 // TODO: better mechanism of storing display/erse status in a study
329 // should be provided...
330 // _PTR(Study) study(getStudyDS());
333 Handle (AIS_InteractiveContext) ic = getAISContext();
335 // get all displayed objects
336 AIS_ListOfInteractive List;
337 ic->DisplayedObjects( List );
338 // get objects in the collector
339 AIS_ListOfInteractive ListCollector;
340 ic->ObjectsInCollector( ListCollector );
342 // get objects to be displayed
343 AIS_ListOfInteractive anAISObjects;
344 anOCCPrs->GetObjects( anAISObjects );
346 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
347 for ( ; aIter.More(); aIter.Next() )
349 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
350 if ( !anAIS.IsNull() )
352 // try to find presentation in the viewer
353 bool bDisplayed = false;
354 AIS_ListIteratorOfListOfInteractive ite( List );
355 for ( ; ite.More(); ite.Next() )
357 // compare presentations by handles
358 // if the object is already displayed - nothing to do more
359 if ( ite.Value() == anAIS )
361 // Deactivate object if necessary
362 if ( !anOCCPrs->ToActivate() )
363 ic->Deactivate( anAIS );
372 // then try to find presentation in the collector
374 ite.Initialize( ListCollector );
375 for ( ; ite.More(); ite.Next() )
377 // compare presentations by handles
378 // if the object is in collector - display it
379 if ( ite.Value() == anAIS )
381 ic->DisplayFromCollector( anAIS, false );
383 // Deactivate object if necessary
384 if ( !anOCCPrs->ToActivate() )
385 ic->Deactivate( anAIS );
388 // Set visibility flag
389 // Temporarily commented to avoid awful dependecy on SALOMEDS
390 // TODO: better mechanism of storing display/erse status in a study
391 // should be provided...
392 //Handle(SALOME_InteractiveObject) anObj =
393 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
394 //if ( !anObj.IsNull() && anObj->hasEntry() )
397 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
405 // if object is not displayed and not found in the collector - display it
406 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
408 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
409 double aNewSize = 100, aSize = 100;
410 getTrihedronSize( aNewSize, aSize );
411 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
414 ic->Display( anAIS, false );
416 // Set visibility flag
417 // Temporarily commented to avoid awful dependecy on SALOMEDS
418 // TODO: better mechanism of storing display/erse status in a study
419 // should be provided...
420 //Handle(SALOME_InteractiveObject) anObj =
421 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
422 //if ( !anObj.IsNull() && anObj->hasEntry() )
425 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
428 // Deactivate object if necessary
429 if ( !anOCCPrs->ToActivate() )
430 ic->Deactivate( anAIS );
438 \param prs - presentation
439 \param forced - removes object from context
441 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
443 // try do downcast object
444 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
445 if ( !anOCCPrs || anOCCPrs->IsNull() )
448 // get SALOMEDS Study
449 // Temporarily commented to avoid awful dependecy on SALOMEDS
450 // TODO: better mechanism of storing display/erse status in a study
451 // should be provided...
452 // _PTR(Study) study(getStudyDS());
455 Handle(AIS_InteractiveContext) ic = getAISContext();
457 // get objects to be erased
458 AIS_ListOfInteractive anAISObjects;
459 anOCCPrs->GetObjects( anAISObjects );
461 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
462 for ( ; aIter.More(); aIter.Next() ) {
463 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
464 if ( !anAIS.IsNull() ) {
465 // erase the object from context : move it to collector
466 ic->Erase( anAIS, false, forced ? false : true );
468 // Set visibility flag if necessary
469 // Temporarily commented to avoid awful dependecy on SALOMEDS
470 // TODO: better mechanism of storing display/erse status in a study
471 // should be provided...
474 // Handle(SALOME_InteractiveObject) anObj =
475 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
476 // if ( !anObj.IsNull() && anObj->hasEntry() )
479 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
488 Erase all presentations
489 \param forced - removes all objects from context
491 void SOCC_Viewer::EraseAll( const bool forced )
493 // get SALOMEDS Study
494 // Temporarily commented to avoid awful dependecy on SALOMEDS
495 // TODO: better mechanism of storing display/erse status in a study
496 // should be provided...
497 // _PTR(Study) study(getStudyDS());
500 Handle(AIS_InteractiveContext) ic = getAISContext();
502 // check if trihedron is displayed
503 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
505 // get objects to be erased (all currently displayed objects)
506 AIS_ListOfInteractive aList;
507 ic->DisplayedObjects( aList );
508 AIS_ListIteratorOfListOfInteractive anIter( aList );
509 for ( ; anIter.More(); anIter.Next() ) {
510 if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) ||
511 anIter.Value()->DynamicType() == STANDARD_TYPE( OCCViewer_Trihedron ))
515 Handle(AIS_InteractiveObject) anIO = anIter.Value();
516 ic->Erase( anIO, false, forced ? false : true );
518 // Set visibility flag if necessary
519 // Temporarily commented to avoid awful dependecy on SALOMEDS
520 // TODO: better mechanism of storing display/erse status in a study
521 // should be provided...
523 // Handle(SALOME_InteractiveObject) anObj =
524 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
526 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
528 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
537 Create presentation corresponding to the entry
540 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
542 SOCC_Prs* prs = new SOCC_Prs();
546 Handle(AIS_InteractiveContext) ic = getAISContext();
548 // get displayed objects
549 AIS_ListOfInteractive List;
550 ic->DisplayedObjects( List );
551 // get objects in the collector
552 AIS_ListOfInteractive ListCollector;
553 ic->ObjectsInCollector( ListCollector );
554 List.Append( ListCollector );
556 AIS_ListIteratorOfListOfInteractive ite( List );
557 for ( ; ite.More(); ite.Next() )
559 Handle(SALOME_InteractiveObject) anObj =
560 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
562 if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
563 prs->AddObject( ite.Value() );
570 Activates selection of sub shapes
572 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
574 Handle(AIS_InteractiveContext) ic = getAISContext();
576 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
580 // Open local context if there is no one
581 bool allObjects = thePrs == 0 || thePrs->IsNull();
582 if ( !ic->HasOpenedContext() ) {
583 ic->ClearCurrents( false );
584 ic->OpenLocalContext( allObjects, true, true );
587 AIS_ListOfInteractive anObjs;
588 // Get objects to be activated
590 ic->DisplayedObjects( anObjs );
592 anOCCPrs->GetObjects( anObjs );
594 // Activate selection of objects from prs
595 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
596 for ( ; aIter.More(); aIter.Next() ) {
597 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
598 if ( !anAIS.IsNull() )
600 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
602 ic->Load( anAIS, -1, false );
603 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
605 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
607 ic->Load( anAIS, -1, false );
608 ic->Activate( anAIS, theMode );
615 Deactivates selection of sub shapes
617 void SOCC_Viewer::GlobalSelection( const bool update ) const
619 Handle(AIS_InteractiveContext) ic = getAISContext();
622 ic->CloseAllContexts( false );
624 ic->CurrentViewer()->Redraw();
629 Auxiliary method called before displaying of objects
631 void SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
633 d->BeforeDisplay( this, SALOME_OCCViewType() );
637 Auxiliary method called after displaying of objects
639 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
641 d->AfterDisplay( this, SALOME_OCCViewType() );
645 Get new and current trihedron size corresponding to the current model size
647 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
652 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
653 Handle(V3d_Viewer) viewer = getViewer3d();
654 viewer->InitActiveViews();
655 if(!viewer->MoreActiveViews()) return false;
657 Handle(V3d_View) view3d = viewer->ActiveView();
660 if ( view3d.IsNull() )
663 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
666 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
668 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
669 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
672 aMaxSide = Xmax - Xmin;
673 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
674 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
676 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer","TrihedronSize", 105.);
678 static float EPS = 5.0E-3;
679 theSize = getTrihedron()->Size();
680 theNewSize = aMaxSide*aSizeInPercents / 100.0;
682 return fabs( theNewSize - theSize ) > theSize * EPS ||
683 fabs( theNewSize - theSize) > theNewSize * EPS;
688 Updates current viewer
690 void SOCC_Viewer::Repaint()
692 // onAdjustTrihedron();
693 getViewer3d()->Update();
698 create SOCC_ViewWindow
700 SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
702 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);