1 #include "SOCC_ViewModel.h"
4 #include "SOCC_ViewWindow.h"
6 #include "SUIT_Session.h"
7 #include "SUIT_Application.h"
11 // Temporarily commented to avoid awful dependecy on SALOMEDS
12 // TODO: better mechanism of storing display/erse status in a study
13 // should be provided...
14 //#include <SALOMEconfig.h>
15 //#include CORBA_CLIENT_HEADER(SALOMEDS)
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
18 #include <Visual3d_View.hxx>
20 #include <SALOME_AISShape.hxx>
21 #include <SALOME_AISObject.hxx>
23 // Temporarily commented to avoid awful dependecy on SALOMEDS
24 // TODO: better mechanism of storing display/erse status in a study
25 // should be provided...
26 //#include <Utils_ORB_INIT.hxx>
27 //#include <Utils_SINGLETON.hxx>
28 //#include <SALOME_ModuleCatalog_impl.hxx>
29 //#include <SALOME_NamingService.hxx>
31 //#include "SALOMEDSClient.hxx"
32 //#include "SALOMEDS_StudyManager.hxx"
34 #include <AIS_TypeOfIso.hxx>
36 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
37 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
38 // SALOMEDS::StudyManager - no linkage with SalomeApp.
40 // Temporarily commented to avoid awful dependecy on SALOMEDS
41 // TODO: better mechanism of storing display/erse status in a study
42 // should be provided...
43 //static _PTR(Study) getStudyDS()
45 // SALOMEDSClient_Study* aStudy = NULL;
46 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
48 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
49 // id of its underlying SALOMEDS::Study
50 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
51 // if ( !app ) return _PTR(Study)(aStudy);
52 // SUIT_Study* stud = app->activeStudy();
53 // if ( !stud ) return _PTR(Study)(aStudy);
54 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
55 // get SALOMEDS_Study with this id from StudyMgr
56 // return aMgr->GetStudyByID( id );
59 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
60 : OCCViewer_Viewer( DisplayTrihedron )
64 SOCC_Viewer::~SOCC_Viewer()
68 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
69 bool hilight, bool upd )
71 bool isInLocal = getAISContext()->HasOpenedContext();
72 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
73 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
75 AIS_ListOfInteractive List;
76 getAISContext()->DisplayedObjects(List);
78 AIS_ListIteratorOfListOfInteractive ite(List);
79 for ( ; ite.More(); ite.Next() )
81 Handle(SALOME_InteractiveObject) anObj =
82 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
84 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
87 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
88 // highlight subshapes only when local selection is active
91 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
93 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
94 TColStd_IndexedMapOfInteger MapIndex;
95 Sel->GetIndex( IObject, MapIndex );
96 aSh->highlightSubShapes( MapIndex, highlight );
109 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
112 AIS_ListOfInteractive List;
113 getAISContext()->DisplayedObjects(List);
115 if( !onlyInViewer ) {
116 AIS_ListOfInteractive List1;
117 getAISContext()->ObjectsInCollector(List1);
121 AIS_ListIteratorOfListOfInteractive ite(List);
122 for ( ; ite.More(); ite.Next() )
124 Handle(SALOME_InteractiveObject) anObj =
125 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
127 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
133 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
135 AIS_ListOfInteractive List;
136 getAISContext()->DisplayedObjects( List );
138 AIS_ListIteratorOfListOfInteractive ite( List );
139 for ( ; ite.More(); ite.Next() )
141 Handle(SALOME_InteractiveObject) anObj =
142 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
144 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
145 return getAISContext()->IsDisplayed( ite.Value() );
151 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
152 const QColor& color, bool update )
154 AIS_ListOfInteractive List;
155 getAISContext()->DisplayedObjects(List);
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 ) )
165 OCCViewer_Viewer::setColor( ite.Value(), color, update );
171 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
172 int mode, bool update )
174 AIS_ListOfInteractive List;
175 getAISContext()->DisplayedObjects(List);
177 AIS_ListIteratorOfListOfInteractive ite(List);
178 for ( ; ite.More(); ite.Next() )
180 Handle(SALOME_InteractiveObject) anObj =
181 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
183 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
185 OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
191 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
192 float trans, bool update )
194 AIS_ListOfInteractive List;
195 getAISContext()->DisplayedObjects( List );
197 AIS_ListIteratorOfListOfInteractive ite( List );
198 for ( ; ite.More(); ite.Next() )
200 Handle(SALOME_InteractiveObject) anObj =
201 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
203 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
205 OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
211 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
212 const QString& name )
214 AIS_ListOfInteractive List;
215 getAISContext()->DisplayedObjects(List);
217 AIS_ListIteratorOfListOfInteractive ite(List);
220 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
222 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
226 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
227 if ( IO->isSame( obj ) )
229 aSh->setName( (char*)name.latin1() );
234 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
236 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
238 // Add code here, if someone create a MODULE_AISObject.
244 //=======================================================================
246 // Purpose : Display presentation
247 //=======================================================================
248 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
250 // try do downcast object
251 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
252 if ( !anOCCPrs || anOCCPrs->IsNull() )
255 // get SALOMEDS Study
256 // Temporarily commented to avoid awful dependecy on SALOMEDS
257 // TODO: better mechanism of storing display/erse status in a study
258 // should be provided...
259 // _PTR(Study) study(getStudyDS());
262 Handle (AIS_InteractiveContext) ic = getAISContext();
264 // get all displayed objects
265 AIS_ListOfInteractive List;
266 ic->DisplayedObjects( List );
267 // get objects in the collector
268 AIS_ListOfInteractive ListCollector;
269 ic->ObjectsInCollector( ListCollector );
271 // get objects to be displayed
272 AIS_ListOfInteractive anAISObjects;
273 anOCCPrs->GetObjects( anAISObjects );
275 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
276 for ( ; aIter.More(); aIter.Next() )
278 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
279 if ( !anAIS.IsNull() )
281 // try to find presentation in the viewer
282 bool bDisplayed = false;
283 AIS_ListIteratorOfListOfInteractive ite( List );
284 for ( ; ite.More(); ite.Next() )
286 // compare presentations by handles
287 // if the object is already displayed - nothing to do more
288 if ( ite.Value() == anAIS )
290 // Deactivate object if necessary
291 if ( !anOCCPrs->ToActivate() )
292 ic->Deactivate( anAIS );
301 // then try to find presentation in the collector
303 ite.Initialize( ListCollector );
304 for ( ; ite.More(); ite.Next() )
306 // compare presentations by handles
307 // if the object is in collector - display it
308 if ( ite.Value() == anAIS )
310 ic->DisplayFromCollector( anAIS, false );
312 // Deactivate object if necessary
313 if ( !anOCCPrs->ToActivate() )
314 ic->Deactivate( anAIS );
317 // Set visibility flag
318 // Temporarily commented to avoid awful dependecy on SALOMEDS
319 // TODO: better mechanism of storing display/erse status in a study
320 // should be provided...
321 //Handle(SALOME_InteractiveObject) anObj =
322 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
323 //if ( !anObj.IsNull() && anObj->hasEntry() )
326 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
334 // if object is not displayed and not found in the collector - display it
335 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
337 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
338 double aNewSize = 100, aSize = 100;
339 getTrihedronSize( aNewSize, aSize );
340 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
343 ic->Display( anAIS, false );
345 // Set visibility flag
346 // Temporarily commented to avoid awful dependecy on SALOMEDS
347 // TODO: better mechanism of storing display/erse status in a study
348 // should be provided...
349 //Handle(SALOME_InteractiveObject) anObj =
350 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
351 //if ( !anObj.IsNull() && anObj->hasEntry() )
354 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
357 // Deactivate object if necessary
358 if ( !anOCCPrs->ToActivate() )
359 ic->Deactivate( anAIS );
364 //=======================================================================
366 // Purpose : Erase presentation
367 //=======================================================================
368 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
370 // try do downcast object
371 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
372 if ( !anOCCPrs || anOCCPrs->IsNull() )
375 // get SALOMEDS Study
376 // Temporarily commented to avoid awful dependecy on SALOMEDS
377 // TODO: better mechanism of storing display/erse status in a study
378 // should be provided...
379 // _PTR(Study) study(getStudyDS());
382 Handle(AIS_InteractiveContext) ic = getAISContext();
384 // get objects to be erased
385 AIS_ListOfInteractive anAISObjects;
386 anOCCPrs->GetObjects( anAISObjects );
388 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
389 for ( ; aIter.More(); aIter.Next() ) {
390 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
391 if ( !anAIS.IsNull() ) {
392 // erase the object from context : move it to collector
393 ic->Erase( anAIS, false, forced ? false : true );
395 // Set visibility flag if necessary
396 // Temporarily commented to avoid awful dependecy on SALOMEDS
397 // TODO: better mechanism of storing display/erse status in a study
398 // should be provided...
401 // Handle(SALOME_InteractiveObject) anObj =
402 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
403 // if ( !anObj.IsNull() && anObj->hasEntry() )
406 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
413 //=======================================================================
415 // Purpose : Erase all presentations
416 //=======================================================================
417 void SOCC_Viewer::EraseAll( const bool forced )
419 // get SALOMEDS Study
420 // Temporarily commented to avoid awful dependecy on SALOMEDS
421 // TODO: better mechanism of storing display/erse status in a study
422 // should be provided...
423 // _PTR(Study) study(getStudyDS());
426 Handle(AIS_InteractiveContext) ic = getAISContext();
428 // check if trihedron is displayed
429 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
431 // get objects to be erased (all currently displayed objects)
432 AIS_ListOfInteractive aList;
433 ic->DisplayedObjects( aList );
434 AIS_ListIteratorOfListOfInteractive anIter( aList );
435 for ( ; anIter.More(); anIter.Next() ) {
436 if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
440 Handle(AIS_InteractiveObject) anIO = anIter.Value();
441 ic->Erase( anIO, false, forced ? false : true );
443 // Set visibility flag if necessary
444 // Temporarily commented to avoid awful dependecy on SALOMEDS
445 // TODO: better mechanism of storing display/erse status in a study
446 // should be provided...
448 // Handle(SALOME_InteractiveObject) anObj =
449 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
451 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
453 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
458 // display trihedron if necessary
459 if ( isTrihedronDisplayed )
460 getAISContext()->Display( getTrihedron() );
465 //=======================================================================
467 // Purpose : Create presentation corresponding to the entry
468 //=======================================================================
469 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
471 SOCC_Prs* prs = new SOCC_Prs();
475 Handle(AIS_InteractiveContext) ic = getAISContext();
477 // get displayed objects
478 AIS_ListOfInteractive List;
479 ic->DisplayedObjects( List );
480 // get objects in the collector
481 AIS_ListOfInteractive ListCollector;
482 ic->ObjectsInCollector( ListCollector );
483 List.Append( ListCollector );
485 AIS_ListIteratorOfListOfInteractive ite( List );
486 for ( ; ite.More(); ite.Next() )
488 Handle(SALOME_InteractiveObject) anObj =
489 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
491 if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
492 prs->AddObject( ite.Value() );
498 //=======================================================================
499 // name : LocalSelection
500 // Purpose : Activates selection of sub shapes
501 //=======================================================================
502 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
504 Handle(AIS_InteractiveContext) ic = getAISContext();
506 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
510 // Open local context if there is no one
511 bool allObjects = thePrs == 0 || thePrs->IsNull();
512 if ( !ic->HasOpenedContext() ) {
513 ic->ClearCurrents( false );
514 ic->OpenLocalContext( allObjects, true, true );
517 AIS_ListOfInteractive anObjs;
518 // Get objects to be activated
520 ic->DisplayedObjects( anObjs );
522 anOCCPrs->GetObjects( anObjs );
524 // Activate selection of objects from prs
525 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
526 for ( ; aIter.More(); aIter.Next() ) {
527 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
528 if ( !anAIS.IsNull() )
530 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
532 ic->Load( anAIS, -1, false );
533 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
535 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
537 ic->Load( anAIS, -1, false );
538 ic->Activate( anAIS, theMode );
544 //=======================================================================
545 // name : GlobalSelection
546 // Purpose : Deactivates selection of sub shapes
547 //=======================================================================
548 void SOCC_Viewer::GlobalSelection( const bool update ) const
550 Handle(AIS_InteractiveContext) ic = getAISContext();
553 ic->CloseAllContexts( false );
555 ic->CurrentViewer()->Redraw();
559 //=======================================================================
560 // name : BeforeDisplay
561 // Purpose : Axiluary method called before displaying of objects
562 //=======================================================================
563 void SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
565 d->BeforeDisplay( this, SALOME_OCCViewType() );
568 //=======================================================================
569 // name : AfterDisplay
570 // Purpose : Axiluary method called after displaying of objects
571 //=======================================================================
572 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
574 d->AfterDisplay( this, SALOME_OCCViewType() );
577 //=======================================================================
578 // name : getTrihedronSize
579 // Purpose : Get new and current trihedron size corresponding to the
580 // current model size
581 //=======================================================================
582 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
587 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
588 Handle(V3d_Viewer) viewer = getViewer3d();
589 viewer->InitActiveViews();
590 if(!viewer->MoreActiveViews()) return false;
592 Handle(V3d_View) view3d = viewer->ActiveView();
595 if ( view3d.IsNull() )
598 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
601 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
603 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
604 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
607 aMaxSide = Xmax - Xmin;
608 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
609 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
611 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer","TrihedronSize", 105.);
613 static float EPS = 5.0E-3;
614 theSize = getTrihedron()->Size();
615 theNewSize = aMaxSide*aSizeInPercents / 100.0;
617 return fabs( theNewSize - theSize ) > theSize * EPS ||
618 fabs( theNewSize - theSize) > theNewSize * EPS;
621 //=======================================================================
624 //=======================================================================
625 void SOCC_Viewer::Repaint()
627 // onAdjustTrihedron();
628 getViewer3d()->Update();
631 //=======================================================================
633 // Purpose : create SOCC_ViewWindow
634 //=======================================================================
635 SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
637 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);