1 #include "SOCC_ViewModel.h"
5 #include "SUIT_Session.h"
6 #include "SUIT_Application.h"
10 #include <SALOMEconfig.h>
11 #include CORBA_CLIENT_HEADER(SALOMEDS)
13 #include <AIS_ListIteratorOfListOfInteractive.hxx>
14 #include <Visual3d_View.hxx>
16 #include <SALOME_AISShape.hxx>
17 #include <SALOME_AISObject.hxx>
19 #include <Utils_ORB_INIT.hxx>
20 #include <Utils_SINGLETON.hxx>
21 #include <SALOME_ModuleCatalog_impl.hxx>
22 #include <SALOME_NamingService.hxx>
24 #include "SALOMEDSClient.hxx"
25 #include "SALOMEDS_StudyManager.hxx"
27 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
28 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
29 // SALOMEDS::StudyManager - no linkage with SalomeApp.
31 static _PTR(Study) getStudyDS()
33 SALOMEDSClient_Study* aStudy = NULL;
34 _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
36 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
37 // id of its underlying SALOMEDS::Study
38 SUIT_Application* app = SUIT_Session::session()->activeApplication();
39 if ( !app ) return _PTR(Study)(aStudy);
40 SUIT_Study* stud = app->activeStudy();
41 if ( !stud ) return _PTR(Study)(aStudy);
42 const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
43 // get SALOMEDS_Study with this id from StudyMgr
44 return aMgr->GetStudyByID( id );
47 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
48 : OCCViewer_Viewer( DisplayTrihedron )
52 SOCC_Viewer::~SOCC_Viewer()
56 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
57 bool hilight, bool upd )
59 bool isInLocal = getAISContext()->HasOpenedContext();
60 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
61 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
63 AIS_ListOfInteractive List;
64 getAISContext()->DisplayedObjects(List);
66 AIS_ListIteratorOfListOfInteractive ite(List);
67 for ( ; ite.More(); ite.Next() )
69 Handle(SALOME_InteractiveObject) anObj =
70 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
72 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
75 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
76 // highlight subshapes only when local selection is active
79 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
81 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
82 TColStd_IndexedMapOfInteger MapIndex;
83 Sel->GetIndex( IObject, MapIndex );
84 aSh->highlightSubShapes( MapIndex, highlight );
97 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
100 AIS_ListOfInteractive List;
101 getAISContext()->DisplayedObjects(List);
103 if( !onlyInViewer ) {
104 AIS_ListOfInteractive List1;
105 getAISContext()->ObjectsInCollector(List1);
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 ) )
121 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
123 AIS_ListOfInteractive List;
124 getAISContext()->DisplayedObjects( List );
126 AIS_ListIteratorOfListOfInteractive ite( List );
127 for ( ; ite.More(); ite.Next() )
129 Handle(SALOME_InteractiveObject) anObj =
130 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
132 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
133 return getAISContext()->IsDisplayed( ite.Value() );
139 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
140 const QColor& color, bool update )
142 AIS_ListOfInteractive List;
143 getAISContext()->DisplayedObjects(List);
145 AIS_ListIteratorOfListOfInteractive ite(List);
146 for ( ; ite.More(); ite.Next() )
148 Handle(SALOME_InteractiveObject) anObj =
149 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
151 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
153 OCCViewer_Viewer::setColor( ite.Value(), color, update );
159 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
160 int mode, bool update )
162 AIS_ListOfInteractive List;
163 getAISContext()->DisplayedObjects(List);
165 AIS_ListIteratorOfListOfInteractive ite(List);
166 for ( ; ite.More(); ite.Next() )
168 Handle(SALOME_InteractiveObject) anObj =
169 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
171 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
173 OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
179 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
180 float trans, bool update )
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 ) )
193 OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
199 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
200 const QString& name )
202 AIS_ListOfInteractive List;
203 getAISContext()->DisplayedObjects(List);
205 AIS_ListIteratorOfListOfInteractive ite(List);
208 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
210 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
214 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
215 if ( IO->isSame( obj ) )
217 aSh->setName( (char*)name.latin1() );
222 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
224 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
226 // Add code here, if someone create a MODULE_AISObject.
232 //=======================================================================
234 // Purpose : Display presentation
235 //=======================================================================
236 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
238 // try do downcast object
239 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
240 if ( !anOCCPrs || anOCCPrs->IsNull() )
243 // get SALOMEDS Study
244 _PTR(Study) study(getStudyDS());
247 Handle (AIS_InteractiveContext) ic = getAISContext();
249 // get all displayed objects
250 AIS_ListOfInteractive List;
251 ic->DisplayedObjects( List );
252 // get objects in the collector
253 AIS_ListOfInteractive ListCollector;
254 ic->ObjectsInCollector( ListCollector );
256 // get objects to be displayed
257 AIS_ListOfInteractive anAISObjects;
258 anOCCPrs->GetObjects( anAISObjects );
260 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
261 for ( ; aIter.More(); aIter.Next() )
263 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
264 if ( !anAIS.IsNull() )
266 // try to find presentation in the viewer
267 bool bDisplayed = false;
268 AIS_ListIteratorOfListOfInteractive ite( List );
269 for ( ; ite.More(); ite.Next() )
271 // compare presentations by handles
272 // if the object is already displayed - nothing to do more
273 if ( ite.Value() == anAIS )
275 // Deactivate object if necessary
276 if ( !anOCCPrs->ToActivate() )
277 ic->Deactivate( anAIS );
286 // then try to find presentation in the collector
288 ite.Initialize( ListCollector );
289 for ( ; ite.More(); ite.Next() )
291 // compare presentations by handles
292 // if the object is in collector - display it
293 if ( ite.Value() == anAIS )
295 ic->DisplayFromCollector( anAIS, false );
297 // Deactivate object if necessary
298 if ( !anOCCPrs->ToActivate() )
299 ic->Deactivate( anAIS );
302 // Set visibility flag
303 Handle(SALOME_InteractiveObject) anObj =
304 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
305 if ( !anObj.IsNull() && anObj->hasEntry() )
308 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
317 // if object is not displayed and not found in the collector - display it
318 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
320 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
321 double aNewSize = 100, aSize = 100;
322 getTrihedronSize( aNewSize, aSize );
323 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
326 ic->Display( anAIS, false );
328 // Set visibility flag
329 Handle(SALOME_InteractiveObject) anObj =
330 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
331 if ( !anObj.IsNull() && anObj->hasEntry() )
334 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
337 // Deactivate object if necessary
338 if ( !anOCCPrs->ToActivate() )
339 ic->Deactivate( anAIS );
344 //=======================================================================
346 // Purpose : Erase presentation
347 //=======================================================================
348 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
350 // try do downcast object
351 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
352 if ( !anOCCPrs || anOCCPrs->IsNull() )
355 // get SALOMEDS Study
356 _PTR(Study) study(getStudyDS());
359 Handle(AIS_InteractiveContext) ic = getAISContext();
361 // get objects to be erased
362 AIS_ListOfInteractive anAISObjects;
363 anOCCPrs->GetObjects( anAISObjects );
365 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
366 for ( ; aIter.More(); aIter.Next() ) {
367 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
368 if ( !anAIS.IsNull() ) {
369 // erase the object from context : move it to collector
370 ic->Erase( anAIS, false, forced ? false : true );
372 // Set visibility flag if necessary
375 Handle(SALOME_InteractiveObject) anObj =
376 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
377 if ( !anObj.IsNull() && anObj->hasEntry() )
380 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
387 //=======================================================================
389 // Purpose : Erase all presentations
390 //=======================================================================
391 void SOCC_Viewer::EraseAll( const bool forced )
393 // get SALOMEDS Study
394 _PTR(Study) study(getStudyDS());
397 Handle(AIS_InteractiveContext) ic = getAISContext();
399 // check if trihedron is displayed
400 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
402 // get objects to be erased (all currently displayed objects)
403 AIS_ListOfInteractive aList;
404 ic->DisplayedObjects( aList );
405 AIS_ListIteratorOfListOfInteractive anIter( aList );
406 for ( ; anIter.More(); anIter.Next() ) {
407 if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
411 Handle(AIS_InteractiveObject) anIO = anIter.Value();
412 ic->Erase( anIO, false, forced ? false : true );
414 // Set visibility flag if necessary
416 Handle(SALOME_InteractiveObject) anObj =
417 Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
419 if ( !anObj.IsNull() && anObj->hasEntry() ) {
421 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
426 // display trihedron if necessary
427 if ( isTrihedronDisplayed )
428 getAISContext()->Display( getTrihedron() );
433 //=======================================================================
435 // Purpose : Create presentation corresponding to the entry
436 //=======================================================================
437 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
439 SOCC_Prs* prs = new SOCC_Prs();
443 Handle(AIS_InteractiveContext) ic = getAISContext();
445 // get displayed objects
446 AIS_ListOfInteractive List;
447 ic->DisplayedObjects( List );
448 // get objects in the collector
449 AIS_ListOfInteractive ListCollector;
450 ic->ObjectsInCollector( ListCollector );
451 List.Append( ListCollector );
453 AIS_ListIteratorOfListOfInteractive ite( List );
454 for ( ; ite.More(); ite.Next() )
456 Handle(SALOME_InteractiveObject) anObj =
457 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
459 if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
460 prs->AddObject( ite.Value() );
466 //=======================================================================
467 // name : LocalSelection
468 // Purpose : Activates selection of sub shapes
469 //=======================================================================
470 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
472 Handle(AIS_InteractiveContext) anIC = getAISContext();
474 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
478 // Open local context if there is no one
479 bool allObjects = thePrs == 0 || thePrs->IsNull();
480 if ( !anIC->HasOpenedContext() ) {
481 anIC->ClearCurrents( false );
482 anIC->OpenLocalContext( allObjects, true, true );
485 AIS_ListOfInteractive anObjs;
486 // Get objects to be activated
488 anIC->DisplayedObjects( anObjs );
490 anOCCPrs->GetObjects( anObjs );
492 // Activate selection of objects from prs
493 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
494 for ( ; aIter.More(); aIter.Next() ) {
495 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
496 if ( !anAIS.IsNull() )
498 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
500 anIC->Load( anAIS, -1, false );
501 anIC->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
503 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
505 anIC->Load( anAIS, -1, false );
506 anIC->Activate( anAIS, theMode );
512 //=======================================================================
513 // name : GlobalSelection
514 // Purpose : Deactivates selection of sub shapes
515 //=======================================================================
516 void SOCC_Viewer::GlobalSelection( const bool update ) const
518 Handle(AIS_InteractiveContext) anIC = getAISContext();
519 if ( !anIC.IsNull() )
520 anIC->CloseAllContexts( false );
522 anIC->CurrentViewer()->Redraw();
525 //=======================================================================
526 // name : BeforeDisplay
527 // Purpose : Axiluary method called before displaying of objects
528 //=======================================================================
529 void SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
531 d->BeforeDisplay( this, SALOME_OCCViewType() );
534 //=======================================================================
535 // name : AfterDisplay
536 // Purpose : Axiluary method called after displaying of objects
537 //=======================================================================
538 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
540 d->AfterDisplay( this, SALOME_OCCViewType() );
543 //=======================================================================
544 // name : getTrihedronSize
545 // Purpose : Get new and current trihedron size corresponding to the
546 // current model size
547 //=======================================================================
548 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
553 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
554 Handle(V3d_Viewer) viewer = getViewer3d();
555 viewer->InitActiveViews();
556 if(!viewer->MoreActiveViews()) return false;
558 Handle(V3d_View) view3d = viewer->ActiveView();
561 if ( view3d.IsNull() )
564 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
567 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
569 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
570 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
573 aMaxSide = Xmax - Xmin;
574 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
575 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
577 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer:TrihedronSize", 105.);
579 static float EPS = 5.0E-3;
580 theSize = getTrihedron()->Size();
581 theNewSize = aMaxSide*aSizeInPercents / 100.0;
583 return fabs( theNewSize - theSize ) > theSize * EPS ||
584 fabs( theNewSize - theSize) > theNewSize * EPS;
587 //=======================================================================
590 //=======================================================================
591 void SOCC_Viewer::Repaint()
593 // onAdjustTrihedron();
594 getViewer3d()->Update();