1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "SOCC_ViewModel.h"
22 #include "SOCC_ViewWindow.h"
24 #include "SUIT_Session.h"
25 #include "SUIT_Application.h"
27 //#include "ToolsGUI.h"
29 // Temporarily commented to avoid awful dependecy on SALOMEDS
30 // TODO: better mechanism of storing display/erse status in a study
31 // should be provided...
32 //#include <SALOMEconfig.h>
33 //#include CORBA_CLIENT_HEADER(SALOMEDS)
35 #include <AIS_ListIteratorOfListOfInteractive.hxx>
36 #include <Visual3d_View.hxx>
38 #include <SALOME_AISShape.hxx>
39 #include <SALOME_AISObject.hxx>
41 // Temporarily commented to avoid awful dependecy on SALOMEDS
42 // TODO: better mechanism of storing display/erse status in a study
43 // should be provided...
44 //#include <Utils_ORB_INIT.hxx>
45 //#include <Utils_SINGLETON.hxx>
46 //#include <SALOME_ModuleCatalog_impl.hxx>
47 //#include <SALOME_NamingService.hxx>
49 //#include "SALOMEDSClient.hxx"
50 //#include "SALOMEDS_StudyManager.hxx"
52 #include <AIS_TypeOfIso.hxx>
54 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
55 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
56 // SALOMEDS::StudyManager - no linkage with SalomeApp.
58 // Temporarily commented to avoid awful dependecy on SALOMEDS
59 // TODO: better mechanism of storing display/erse status in a study
60 // should be provided...
61 //static _PTR(Study) getStudyDS()
63 // SALOMEDSClient_Study* aStudy = NULL;
64 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
66 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
67 // id of its underlying SALOMEDS::Study
68 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
69 // if ( !app ) return _PTR(Study)(aStudy);
70 // SUIT_Study* stud = app->activeStudy();
71 // if ( !stud ) return _PTR(Study)(aStudy);
72 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
73 // get SALOMEDS_Study with this id from StudyMgr
74 // return aMgr->GetStudyByID( id );
79 \param DisplayTrihedron - is trihedron displayed
81 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
82 : OCCViewer_Viewer( DisplayTrihedron )
89 SOCC_Viewer::~SOCC_Viewer()
94 Hilights/unhilights object in viewer
95 \param obj - object to be updated
96 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
97 \param update - update current viewer
99 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
100 bool hilight, bool upd )
102 bool isInLocal = getAISContext()->HasOpenedContext();
103 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
104 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
106 AIS_ListOfInteractive List;
107 getAISContext()->DisplayedObjects(List);
109 AIS_ListIteratorOfListOfInteractive ite(List);
110 for ( ; ite.More(); ite.Next() )
112 Handle(SALOME_InteractiveObject) anObj =
113 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
115 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
118 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
119 // highlight subshapes only when local selection is active
122 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
124 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
125 TColStd_IndexedMapOfInteger MapIndex;
126 Sel->GetIndex( IObject, MapIndex );
127 aSh->highlightSubShapes( MapIndex, highlight );
141 \return true if object is in viewer or in collector
142 \param obj - object to be checked
143 \param onlyInViewer - search object only in viewer (so object must be displayed)
145 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
148 AIS_ListOfInteractive List;
149 getAISContext()->DisplayedObjects(List);
151 if( !onlyInViewer ) {
152 AIS_ListOfInteractive List1;
153 getAISContext()->ObjectsInCollector(List1);
157 AIS_ListIteratorOfListOfInteractive ite(List);
158 for ( ; ite.More(); ite.Next() )
160 Handle(SALOME_InteractiveObject) anObj =
161 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
163 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
170 \return true if object is displayed in viewer
171 \param obj - object to be checked
173 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
175 AIS_ListOfInteractive List;
176 getAISContext()->DisplayedObjects( List );
178 AIS_ListIteratorOfListOfInteractive ite( List );
179 for ( ; ite.More(); ite.Next() )
181 Handle(SALOME_InteractiveObject) anObj =
182 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
184 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
185 return getAISContext()->IsDisplayed( ite.Value() );
193 \param obj - object to be updated
194 \param color - new color
195 \param update - update current viewer
197 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
198 const QColor& color, bool update )
200 AIS_ListOfInteractive List;
201 getAISContext()->DisplayedObjects(List);
203 AIS_ListIteratorOfListOfInteractive ite(List);
204 for ( ; ite.More(); ite.Next() )
206 Handle(SALOME_InteractiveObject) anObj =
207 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
209 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
211 OCCViewer_Viewer::setColor( ite.Value(), color, update );
218 Changes display mode of object
219 \param obj - object to be processed
220 \param mode - new display mode
221 \param update - update current viewer
223 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
224 int mode, bool update )
226 AIS_ListOfInteractive List;
227 getAISContext()->DisplayedObjects(List);
229 AIS_ListIteratorOfListOfInteractive ite(List);
230 for ( ; ite.More(); ite.Next() )
232 Handle(SALOME_InteractiveObject) anObj =
233 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
235 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
237 OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
244 Changes transparency of object
245 \param obj - object to be processed
246 \param trans - new transparency
247 \param update - update current viewer
249 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
250 float trans, bool update )
252 AIS_ListOfInteractive List;
253 getAISContext()->DisplayedObjects( List );
255 AIS_ListIteratorOfListOfInteractive ite( List );
256 for ( ; ite.More(); ite.Next() )
258 Handle(SALOME_InteractiveObject) anObj =
259 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
261 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
263 OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
270 Changes name of object
271 \param obj - object to be renamed
272 \param name - new name
274 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
275 const QString& name )
277 AIS_ListOfInteractive List;
278 getAISContext()->DisplayedObjects(List);
280 AIS_ListIteratorOfListOfInteractive ite(List);
283 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
285 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
289 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
290 if ( IO->isSame( obj ) )
292 aSh->setName( (char*)name.latin1() );
297 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
299 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
301 // Add code here, if someone create a MODULE_AISObject.
310 \param prs - presentation
312 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
314 // try do downcast object
315 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
316 if ( !anOCCPrs || anOCCPrs->IsNull() )
319 // get SALOMEDS Study
320 // Temporarily commented to avoid awful dependecy on SALOMEDS
321 // TODO: better mechanism of storing display/erse status in a study
322 // should be provided...
323 // _PTR(Study) study(getStudyDS());
326 Handle (AIS_InteractiveContext) ic = getAISContext();
328 // get all displayed objects
329 AIS_ListOfInteractive List;
330 ic->DisplayedObjects( List );
331 // get objects in the collector
332 AIS_ListOfInteractive ListCollector;
333 ic->ObjectsInCollector( ListCollector );
335 // get objects to be displayed
336 AIS_ListOfInteractive anAISObjects;
337 anOCCPrs->GetObjects( anAISObjects );
339 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
340 for ( ; aIter.More(); aIter.Next() )
342 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
343 if ( !anAIS.IsNull() )
345 // try to find presentation in the viewer
346 bool bDisplayed = false;
347 AIS_ListIteratorOfListOfInteractive ite( List );
348 for ( ; ite.More(); ite.Next() )
350 // compare presentations by handles
351 // if the object is already displayed - nothing to do more
352 if ( ite.Value() == anAIS )
354 // Deactivate object if necessary
355 if ( !anOCCPrs->ToActivate() )
356 ic->Deactivate( anAIS );
365 // then try to find presentation in the collector
367 ite.Initialize( ListCollector );
368 for ( ; ite.More(); ite.Next() )
370 // compare presentations by handles
371 // if the object is in collector - display it
372 if ( ite.Value() == anAIS )
374 ic->DisplayFromCollector( anAIS, false );
376 // Deactivate object if necessary
377 if ( !anOCCPrs->ToActivate() )
378 ic->Deactivate( anAIS );
381 // Set visibility flag
382 // Temporarily commented to avoid awful dependecy on SALOMEDS
383 // TODO: better mechanism of storing display/erse status in a study
384 // should be provided...
385 //Handle(SALOME_InteractiveObject) anObj =
386 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
387 //if ( !anObj.IsNull() && anObj->hasEntry() )
390 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
398 // if object is not displayed and not found in the collector - display it
399 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
401 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
402 double aNewSize = 100, aSize = 100;
403 getTrihedronSize( aNewSize, aSize );
404 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
407 ic->Display( anAIS, false );
409 // Set visibility flag
410 // Temporarily commented to avoid awful dependecy on SALOMEDS
411 // TODO: better mechanism of storing display/erse status in a study
412 // should be provided...
413 //Handle(SALOME_InteractiveObject) anObj =
414 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
415 //if ( !anObj.IsNull() && anObj->hasEntry() )
418 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
421 // Deactivate object if necessary
422 if ( !anOCCPrs->ToActivate() )
423 ic->Deactivate( anAIS );
431 \param prs - presentation
432 \param forced - removes object from context
434 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
436 // try do downcast object
437 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
438 if ( !anOCCPrs || anOCCPrs->IsNull() )
441 // get SALOMEDS Study
442 // Temporarily commented to avoid awful dependecy on SALOMEDS
443 // TODO: better mechanism of storing display/erse status in a study
444 // should be provided...
445 // _PTR(Study) study(getStudyDS());
448 Handle(AIS_InteractiveContext) ic = getAISContext();
450 // get objects to be erased
451 AIS_ListOfInteractive anAISObjects;
452 anOCCPrs->GetObjects( anAISObjects );
454 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
455 for ( ; aIter.More(); aIter.Next() ) {
456 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
457 if ( !anAIS.IsNull() ) {
458 // erase the object from context : move it to collector
459 ic->Erase( anAIS, false, forced ? false : true );
461 // Set visibility flag if necessary
462 // Temporarily commented to avoid awful dependecy on SALOMEDS
463 // TODO: better mechanism of storing display/erse status in a study
464 // should be provided...
467 // Handle(SALOME_InteractiveObject) anObj =
468 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
469 // if ( !anObj.IsNull() && anObj->hasEntry() )
472 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
481 Erase all presentations
482 \param forced - removes all objects from context
484 void SOCC_Viewer::EraseAll( const bool forced )
486 // get SALOMEDS Study
487 // Temporarily commented to avoid awful dependecy on SALOMEDS
488 // TODO: better mechanism of storing display/erse status in a study
489 // should be provided...
490 // _PTR(Study) study(getStudyDS());
493 Handle(AIS_InteractiveContext) ic = getAISContext();
495 // check if trihedron is displayed
496 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
498 // get objects to be erased (all currently displayed objects)
499 AIS_ListOfInteractive aList;
500 ic->DisplayedObjects( aList );
501 AIS_ListIteratorOfListOfInteractive anIter( aList );
502 for ( ; anIter.More(); anIter.Next() ) {
503 if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
507 Handle(AIS_InteractiveObject) anIO = anIter.Value();
508 ic->Erase( anIO, false, forced ? false : true );
510 // Set visibility flag if necessary
511 // Temporarily commented to avoid awful dependecy on SALOMEDS
512 // TODO: better mechanism of storing display/erse status in a study
513 // should be provided...
515 // Handle(SALOME_InteractiveObject) anObj =
516 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
518 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
520 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
529 Create presentation corresponding to the entry
532 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
534 SOCC_Prs* prs = new SOCC_Prs();
538 Handle(AIS_InteractiveContext) ic = getAISContext();
540 // get displayed objects
541 AIS_ListOfInteractive List;
542 ic->DisplayedObjects( List );
543 // get objects in the collector
544 AIS_ListOfInteractive ListCollector;
545 ic->ObjectsInCollector( ListCollector );
546 List.Append( ListCollector );
548 AIS_ListIteratorOfListOfInteractive ite( List );
549 for ( ; ite.More(); ite.Next() )
551 Handle(SALOME_InteractiveObject) anObj =
552 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
554 if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
555 prs->AddObject( ite.Value() );
562 Activates selection of sub shapes
564 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
566 Handle(AIS_InteractiveContext) ic = getAISContext();
568 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
572 // Open local context if there is no one
573 bool allObjects = thePrs == 0 || thePrs->IsNull();
574 if ( !ic->HasOpenedContext() ) {
575 ic->ClearCurrents( false );
576 ic->OpenLocalContext( allObjects, true, true );
579 AIS_ListOfInteractive anObjs;
580 // Get objects to be activated
582 ic->DisplayedObjects( anObjs );
584 anOCCPrs->GetObjects( anObjs );
586 // Activate selection of objects from prs
587 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
588 for ( ; aIter.More(); aIter.Next() ) {
589 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
590 if ( !anAIS.IsNull() )
592 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
594 ic->Load( anAIS, -1, false );
595 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
597 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
599 ic->Load( anAIS, -1, false );
600 ic->Activate( anAIS, theMode );
607 Deactivates selection of sub shapes
609 void SOCC_Viewer::GlobalSelection( const bool update ) const
611 Handle(AIS_InteractiveContext) ic = getAISContext();
614 ic->CloseAllContexts( false );
616 ic->CurrentViewer()->Redraw();
621 Auxiliary method called before displaying of objects
623 void SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
625 d->BeforeDisplay( this, SALOME_OCCViewType() );
629 Auxiliary method called after displaying of objects
631 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
633 d->AfterDisplay( this, SALOME_OCCViewType() );
637 Get new and current trihedron size corresponding to the current model size
639 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
644 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
645 Handle(V3d_Viewer) viewer = getViewer3d();
646 viewer->InitActiveViews();
647 if(!viewer->MoreActiveViews()) return false;
649 Handle(V3d_View) view3d = viewer->ActiveView();
652 if ( view3d.IsNull() )
655 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
658 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
660 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
661 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
664 aMaxSide = Xmax - Xmin;
665 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
666 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
668 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer","TrihedronSize", 105.);
670 static float EPS = 5.0E-3;
671 theSize = getTrihedron()->Size();
672 theNewSize = aMaxSide*aSizeInPercents / 100.0;
674 return fabs( theNewSize - theSize ) > theSize * EPS ||
675 fabs( theNewSize - theSize) > theNewSize * EPS;
680 Updates current viewer
682 void SOCC_Viewer::Repaint()
684 // onAdjustTrihedron();
685 getViewer3d()->Update();
690 create SOCC_ViewWindow
692 SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
694 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);