1 #include "SOCC_ViewModel.h"
4 #include "SOCC_ViewWindow.h"
6 #include "SUIT_Session.h"
7 #include "SUIT_Application.h"
11 #include <SALOMEconfig.h>
12 #include CORBA_CLIENT_HEADER(SALOMEDS)
14 #include <AIS_ListIteratorOfListOfInteractive.hxx>
15 #include <Visual3d_View.hxx>
17 #include <SALOME_AISShape.hxx>
18 #include <SALOME_AISObject.hxx>
20 #include <Utils_ORB_INIT.hxx>
21 #include <Utils_SINGLETON.hxx>
22 #include <SALOME_ModuleCatalog_impl.hxx>
23 #include <SALOME_NamingService.hxx>
25 #include "SALOMEDSClient.hxx"
26 #include "SALOMEDS_StudyManager.hxx"
28 #include <AIS_TypeOfIso.hxx>
30 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
31 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
32 // SALOMEDS::StudyManager - no linkage with SalomeApp.
34 static _PTR(Study) getStudyDS()
36 SALOMEDSClient_Study* aStudy = NULL;
37 _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
39 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
40 // id of its underlying SALOMEDS::Study
41 SUIT_Application* app = SUIT_Session::session()->activeApplication();
42 if ( !app ) return _PTR(Study)(aStudy);
43 SUIT_Study* stud = app->activeStudy();
44 if ( !stud ) return _PTR(Study)(aStudy);
45 const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
46 // get SALOMEDS_Study with this id from StudyMgr
47 return aMgr->GetStudyByID( id );
50 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
51 : OCCViewer_Viewer( DisplayTrihedron )
55 SOCC_Viewer::~SOCC_Viewer()
59 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
60 bool hilight, bool upd )
62 bool isInLocal = getAISContext()->HasOpenedContext();
63 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
64 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
66 AIS_ListOfInteractive List;
67 getAISContext()->DisplayedObjects(List);
69 AIS_ListIteratorOfListOfInteractive ite(List);
70 for ( ; ite.More(); ite.Next() )
72 Handle(SALOME_InteractiveObject) anObj =
73 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
75 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
78 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
79 // highlight subshapes only when local selection is active
82 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
84 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
85 TColStd_IndexedMapOfInteger MapIndex;
86 Sel->GetIndex( IObject, MapIndex );
87 aSh->highlightSubShapes( MapIndex, highlight );
100 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
103 AIS_ListOfInteractive List;
104 getAISContext()->DisplayedObjects(List);
106 if( !onlyInViewer ) {
107 AIS_ListOfInteractive List1;
108 getAISContext()->ObjectsInCollector(List1);
112 AIS_ListIteratorOfListOfInteractive ite(List);
113 for ( ; ite.More(); ite.Next() )
115 Handle(SALOME_InteractiveObject) anObj =
116 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
118 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
124 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
126 AIS_ListOfInteractive List;
127 getAISContext()->DisplayedObjects( List );
129 AIS_ListIteratorOfListOfInteractive ite( List );
130 for ( ; ite.More(); ite.Next() )
132 Handle(SALOME_InteractiveObject) anObj =
133 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
135 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
136 return getAISContext()->IsDisplayed( ite.Value() );
142 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
143 const QColor& color, bool update )
145 AIS_ListOfInteractive List;
146 getAISContext()->DisplayedObjects(List);
148 AIS_ListIteratorOfListOfInteractive ite(List);
149 for ( ; ite.More(); ite.Next() )
151 Handle(SALOME_InteractiveObject) anObj =
152 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
154 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
156 OCCViewer_Viewer::setColor( ite.Value(), color, update );
162 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
163 int mode, bool update )
165 AIS_ListOfInteractive List;
166 getAISContext()->DisplayedObjects(List);
168 AIS_ListIteratorOfListOfInteractive ite(List);
169 for ( ; ite.More(); ite.Next() )
171 Handle(SALOME_InteractiveObject) anObj =
172 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
174 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
176 OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
182 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
183 float trans, bool update )
185 AIS_ListOfInteractive List;
186 getAISContext()->DisplayedObjects( List );
188 AIS_ListIteratorOfListOfInteractive ite( List );
189 for ( ; ite.More(); ite.Next() )
191 Handle(SALOME_InteractiveObject) anObj =
192 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
194 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
196 OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
202 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
203 const QString& name )
205 AIS_ListOfInteractive List;
206 getAISContext()->DisplayedObjects(List);
208 AIS_ListIteratorOfListOfInteractive ite(List);
211 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
213 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
217 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
218 if ( IO->isSame( obj ) )
220 aSh->setName( (char*)name.latin1() );
225 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
227 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
229 // Add code here, if someone create a MODULE_AISObject.
235 //=======================================================================
237 // Purpose : Display presentation
238 //=======================================================================
239 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
241 // try do downcast object
242 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
243 if ( !anOCCPrs || anOCCPrs->IsNull() )
246 // get SALOMEDS Study
247 _PTR(Study) study(getStudyDS());
250 Handle (AIS_InteractiveContext) ic = getAISContext();
252 // get all displayed objects
253 AIS_ListOfInteractive List;
254 ic->DisplayedObjects( List );
255 // get objects in the collector
256 AIS_ListOfInteractive ListCollector;
257 ic->ObjectsInCollector( ListCollector );
259 // get objects to be displayed
260 AIS_ListOfInteractive anAISObjects;
261 anOCCPrs->GetObjects( anAISObjects );
263 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
264 for ( ; aIter.More(); aIter.Next() )
266 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
267 if ( !anAIS.IsNull() )
269 // try to find presentation in the viewer
270 bool bDisplayed = false;
271 AIS_ListIteratorOfListOfInteractive ite( List );
272 for ( ; ite.More(); ite.Next() )
274 // compare presentations by handles
275 // if the object is already displayed - nothing to do more
276 if ( ite.Value() == anAIS )
278 // Deactivate object if necessary
279 if ( !anOCCPrs->ToActivate() )
280 ic->Deactivate( anAIS );
289 // then try to find presentation in the collector
291 ite.Initialize( ListCollector );
292 for ( ; ite.More(); ite.Next() )
294 // compare presentations by handles
295 // if the object is in collector - display it
296 if ( ite.Value() == anAIS )
298 ic->DisplayFromCollector( anAIS, false );
300 // Deactivate object if necessary
301 if ( !anOCCPrs->ToActivate() )
302 ic->Deactivate( anAIS );
305 // Set visibility flag
306 Handle(SALOME_InteractiveObject) anObj =
307 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
308 if ( !anObj.IsNull() && anObj->hasEntry() )
311 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
320 // if object is not displayed and not found in the collector - display it
321 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
323 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
324 double aNewSize = 100, aSize = 100;
325 getTrihedronSize( aNewSize, aSize );
326 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
329 ic->Display( anAIS, false );
331 // Set visibility flag
332 Handle(SALOME_InteractiveObject) anObj =
333 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
334 if ( !anObj.IsNull() && anObj->hasEntry() )
337 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
340 // Deactivate object if necessary
341 if ( !anOCCPrs->ToActivate() )
342 ic->Deactivate( anAIS );
347 //=======================================================================
349 // Purpose : Erase presentation
350 //=======================================================================
351 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
353 // try do downcast object
354 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
355 if ( !anOCCPrs || anOCCPrs->IsNull() )
358 // get SALOMEDS Study
359 _PTR(Study) study(getStudyDS());
362 Handle(AIS_InteractiveContext) ic = getAISContext();
364 // get objects to be erased
365 AIS_ListOfInteractive anAISObjects;
366 anOCCPrs->GetObjects( anAISObjects );
368 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
369 for ( ; aIter.More(); aIter.Next() ) {
370 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
371 if ( !anAIS.IsNull() ) {
372 // erase the object from context : move it to collector
373 ic->Erase( anAIS, false, forced ? false : true );
375 // Set visibility flag if necessary
378 Handle(SALOME_InteractiveObject) anObj =
379 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
380 if ( !anObj.IsNull() && anObj->hasEntry() )
383 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
390 //=======================================================================
392 // Purpose : Erase all presentations
393 //=======================================================================
394 void SOCC_Viewer::EraseAll( const bool forced )
396 // get SALOMEDS Study
397 _PTR(Study) study(getStudyDS());
400 Handle(AIS_InteractiveContext) ic = getAISContext();
402 // check if trihedron is displayed
403 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
405 // get objects to be erased (all currently displayed objects)
406 AIS_ListOfInteractive aList;
407 ic->DisplayedObjects( aList );
408 AIS_ListIteratorOfListOfInteractive anIter( aList );
409 for ( ; anIter.More(); anIter.Next() ) {
410 if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
414 Handle(AIS_InteractiveObject) anIO = anIter.Value();
415 ic->Erase( anIO, false, forced ? false : true );
417 // Set visibility flag if necessary
419 Handle(SALOME_InteractiveObject) anObj =
420 Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
422 if ( !anObj.IsNull() && anObj->hasEntry() ) {
424 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
429 // display trihedron if necessary
430 if ( isTrihedronDisplayed )
431 getAISContext()->Display( getTrihedron() );
436 //=======================================================================
438 // Purpose : Create presentation corresponding to the entry
439 //=======================================================================
440 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
442 SOCC_Prs* prs = new SOCC_Prs();
446 Handle(AIS_InteractiveContext) ic = getAISContext();
448 // get displayed objects
449 AIS_ListOfInteractive List;
450 ic->DisplayedObjects( List );
451 // get objects in the collector
452 AIS_ListOfInteractive ListCollector;
453 ic->ObjectsInCollector( ListCollector );
454 List.Append( ListCollector );
456 AIS_ListIteratorOfListOfInteractive ite( List );
457 for ( ; ite.More(); ite.Next() )
459 Handle(SALOME_InteractiveObject) anObj =
460 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
462 if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
463 prs->AddObject( ite.Value() );
469 //=======================================================================
470 // name : LocalSelection
471 // Purpose : Activates selection of sub shapes
472 //=======================================================================
473 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
475 Handle(AIS_InteractiveContext) ic = getAISContext();
477 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
481 // Open local context if there is no one
482 bool allObjects = thePrs == 0 || thePrs->IsNull();
483 if ( !ic->HasOpenedContext() ) {
484 ic->ClearCurrents( false );
485 ic->OpenLocalContext( allObjects, true, true );
488 AIS_ListOfInteractive anObjs;
489 // Get objects to be activated
491 ic->DisplayedObjects( anObjs );
493 anOCCPrs->GetObjects( anObjs );
495 // Activate selection of objects from prs
496 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
497 for ( ; aIter.More(); aIter.Next() ) {
498 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
499 if ( !anAIS.IsNull() )
501 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
503 ic->Load( anAIS, -1, false );
504 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
506 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
508 ic->Load( anAIS, -1, false );
509 ic->Activate( anAIS, theMode );
515 //=======================================================================
516 // name : GlobalSelection
517 // Purpose : Deactivates selection of sub shapes
518 //=======================================================================
519 void SOCC_Viewer::GlobalSelection( const bool update ) const
521 Handle(AIS_InteractiveContext) ic = getAISContext();
524 ic->CloseAllContexts( false );
526 ic->CurrentViewer()->Redraw();
530 //=======================================================================
531 // name : BeforeDisplay
532 // Purpose : Axiluary method called before displaying of objects
533 //=======================================================================
534 void SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
536 d->BeforeDisplay( this, SALOME_OCCViewType() );
539 //=======================================================================
540 // name : AfterDisplay
541 // Purpose : Axiluary method called after displaying of objects
542 //=======================================================================
543 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
545 d->AfterDisplay( this, SALOME_OCCViewType() );
548 //=======================================================================
549 // name : getTrihedronSize
550 // Purpose : Get new and current trihedron size corresponding to the
551 // current model size
552 //=======================================================================
553 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
558 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
559 Handle(V3d_Viewer) viewer = getViewer3d();
560 viewer->InitActiveViews();
561 if(!viewer->MoreActiveViews()) return false;
563 Handle(V3d_View) view3d = viewer->ActiveView();
566 if ( view3d.IsNull() )
569 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
572 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
574 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
575 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
578 aMaxSide = Xmax - Xmin;
579 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
580 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
582 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer","TrihedronSize", 105.);
584 static float EPS = 5.0E-3;
585 theSize = getTrihedron()->Size();
586 theNewSize = aMaxSide*aSizeInPercents / 100.0;
588 return fabs( theNewSize - theSize ) > theSize * EPS ||
589 fabs( theNewSize - theSize) > theNewSize * EPS;
592 //=======================================================================
595 //=======================================================================
596 void SOCC_Viewer::Repaint()
598 // onAdjustTrihedron();
599 getViewer3d()->Update();
602 //=======================================================================
604 // Purpose : create SOCC_ViewWindow
605 //=======================================================================
606 SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
608 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);