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 #include <AIS_TypeOfIso.hxx>
29 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
30 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
31 // SALOMEDS::StudyManager - no linkage with SalomeApp.
33 static _PTR(Study) getStudyDS()
35 SALOMEDSClient_Study* aStudy = NULL;
36 _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
38 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
39 // id of its underlying SALOMEDS::Study
40 SUIT_Application* app = SUIT_Session::session()->activeApplication();
41 if ( !app ) return _PTR(Study)(aStudy);
42 SUIT_Study* stud = app->activeStudy();
43 if ( !stud ) return _PTR(Study)(aStudy);
44 const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
45 // get SALOMEDS_Study with this id from StudyMgr
46 return aMgr->GetStudyByID( id );
49 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
50 : OCCViewer_Viewer( DisplayTrihedron )
54 SOCC_Viewer::~SOCC_Viewer()
58 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
59 bool hilight, bool upd )
61 bool isInLocal = getAISContext()->HasOpenedContext();
62 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
63 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
65 AIS_ListOfInteractive List;
66 getAISContext()->DisplayedObjects(List);
68 AIS_ListIteratorOfListOfInteractive ite(List);
69 for ( ; ite.More(); ite.Next() )
71 Handle(SALOME_InteractiveObject) anObj =
72 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
74 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
77 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
78 // highlight subshapes only when local selection is active
81 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
83 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
84 TColStd_IndexedMapOfInteger MapIndex;
85 Sel->GetIndex( IObject, MapIndex );
86 aSh->highlightSubShapes( MapIndex, highlight );
99 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
102 AIS_ListOfInteractive List;
103 getAISContext()->DisplayedObjects(List);
105 if( !onlyInViewer ) {
106 AIS_ListOfInteractive List1;
107 getAISContext()->ObjectsInCollector(List1);
111 AIS_ListIteratorOfListOfInteractive ite(List);
112 for ( ; ite.More(); ite.Next() )
114 Handle(SALOME_InteractiveObject) anObj =
115 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
117 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
123 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
125 AIS_ListOfInteractive List;
126 getAISContext()->DisplayedObjects( List );
128 AIS_ListIteratorOfListOfInteractive ite( List );
129 for ( ; ite.More(); ite.Next() )
131 Handle(SALOME_InteractiveObject) anObj =
132 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
134 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
135 return getAISContext()->IsDisplayed( ite.Value() );
141 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
142 const QColor& color, bool update )
144 AIS_ListOfInteractive List;
145 getAISContext()->DisplayedObjects(List);
147 AIS_ListIteratorOfListOfInteractive ite(List);
148 for ( ; ite.More(); ite.Next() )
150 Handle(SALOME_InteractiveObject) anObj =
151 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
153 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
155 OCCViewer_Viewer::setColor( ite.Value(), color, update );
161 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
162 int mode, bool update )
164 AIS_ListOfInteractive List;
165 getAISContext()->DisplayedObjects(List);
167 AIS_ListIteratorOfListOfInteractive ite(List);
168 for ( ; ite.More(); ite.Next() )
170 Handle(SALOME_InteractiveObject) anObj =
171 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
173 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
175 OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
181 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
182 float trans, bool update )
184 AIS_ListOfInteractive List;
185 getAISContext()->DisplayedObjects( List );
187 AIS_ListIteratorOfListOfInteractive ite( List );
188 for ( ; ite.More(); ite.Next() )
190 Handle(SALOME_InteractiveObject) anObj =
191 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
193 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
195 OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
201 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
202 const QString& name )
204 AIS_ListOfInteractive List;
205 getAISContext()->DisplayedObjects(List);
207 AIS_ListIteratorOfListOfInteractive ite(List);
210 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
212 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
216 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
217 if ( IO->isSame( obj ) )
219 aSh->setName( (char*)name.latin1() );
224 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
226 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
228 // Add code here, if someone create a MODULE_AISObject.
234 //=======================================================================
236 // Purpose : Display presentation
237 //=======================================================================
238 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
240 // try do downcast object
241 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
242 if ( !anOCCPrs || anOCCPrs->IsNull() )
245 // get SALOMEDS Study
246 _PTR(Study) study(getStudyDS());
249 Handle (AIS_InteractiveContext) ic = getAISContext();
251 // get all displayed objects
252 AIS_ListOfInteractive List;
253 ic->DisplayedObjects( List );
254 // get objects in the collector
255 AIS_ListOfInteractive ListCollector;
256 ic->ObjectsInCollector( ListCollector );
258 // get objects to be displayed
259 AIS_ListOfInteractive anAISObjects;
260 anOCCPrs->GetObjects( anAISObjects );
262 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
263 for ( ; aIter.More(); aIter.Next() )
265 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
266 if ( !anAIS.IsNull() )
268 // try to find presentation in the viewer
269 bool bDisplayed = false;
270 AIS_ListIteratorOfListOfInteractive ite( List );
271 for ( ; ite.More(); ite.Next() )
273 // compare presentations by handles
274 // if the object is already displayed - nothing to do more
275 if ( ite.Value() == anAIS )
277 // Deactivate object if necessary
278 if ( !anOCCPrs->ToActivate() )
279 ic->Deactivate( anAIS );
288 // then try to find presentation in the collector
290 ite.Initialize( ListCollector );
291 for ( ; ite.More(); ite.Next() )
293 // compare presentations by handles
294 // if the object is in collector - display it
295 if ( ite.Value() == anAIS )
297 ic->DisplayFromCollector( anAIS, false );
299 // Deactivate object if necessary
300 if ( !anOCCPrs->ToActivate() )
301 ic->Deactivate( anAIS );
304 // Set visibility flag
305 Handle(SALOME_InteractiveObject) anObj =
306 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
307 if ( !anObj.IsNull() && anObj->hasEntry() )
310 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
319 // if object is not displayed and not found in the collector - display it
320 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
322 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
323 double aNewSize = 100, aSize = 100;
324 getTrihedronSize( aNewSize, aSize );
325 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
328 ic->Display( anAIS, false );
330 // Set visibility flag
331 Handle(SALOME_InteractiveObject) anObj =
332 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
333 if ( !anObj.IsNull() && anObj->hasEntry() )
336 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
339 // Deactivate object if necessary
340 if ( !anOCCPrs->ToActivate() )
341 ic->Deactivate( anAIS );
346 //=======================================================================
348 // Purpose : Erase presentation
349 //=======================================================================
350 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
352 // try do downcast object
353 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
354 if ( !anOCCPrs || anOCCPrs->IsNull() )
357 // get SALOMEDS Study
358 _PTR(Study) study(getStudyDS());
361 Handle(AIS_InteractiveContext) ic = getAISContext();
363 // get objects to be erased
364 AIS_ListOfInteractive anAISObjects;
365 anOCCPrs->GetObjects( anAISObjects );
367 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
368 for ( ; aIter.More(); aIter.Next() ) {
369 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
370 if ( !anAIS.IsNull() ) {
371 // erase the object from context : move it to collector
372 ic->Erase( anAIS, false, forced ? false : true );
374 // Set visibility flag if necessary
377 Handle(SALOME_InteractiveObject) anObj =
378 Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
379 if ( !anObj.IsNull() && anObj->hasEntry() )
382 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
389 //=======================================================================
391 // Purpose : Erase all presentations
392 //=======================================================================
393 void SOCC_Viewer::EraseAll( const bool forced )
395 // get SALOMEDS Study
396 _PTR(Study) study(getStudyDS());
399 Handle(AIS_InteractiveContext) ic = getAISContext();
401 // check if trihedron is displayed
402 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
404 // get objects to be erased (all currently displayed objects)
405 AIS_ListOfInteractive aList;
406 ic->DisplayedObjects( aList );
407 AIS_ListIteratorOfListOfInteractive anIter( aList );
408 for ( ; anIter.More(); anIter.Next() ) {
409 if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
413 Handle(AIS_InteractiveObject) anIO = anIter.Value();
414 ic->Erase( anIO, false, forced ? false : true );
416 // Set visibility flag if necessary
418 Handle(SALOME_InteractiveObject) anObj =
419 Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
421 if ( !anObj.IsNull() && anObj->hasEntry() ) {
423 ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
428 // display trihedron if necessary
429 if ( isTrihedronDisplayed )
430 getAISContext()->Display( getTrihedron() );
435 //=======================================================================
437 // Purpose : Create presentation corresponding to the entry
438 //=======================================================================
439 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
441 SOCC_Prs* prs = new SOCC_Prs();
445 Handle(AIS_InteractiveContext) ic = getAISContext();
447 // get displayed objects
448 AIS_ListOfInteractive List;
449 ic->DisplayedObjects( List );
450 // get objects in the collector
451 AIS_ListOfInteractive ListCollector;
452 ic->ObjectsInCollector( ListCollector );
453 List.Append( ListCollector );
455 AIS_ListIteratorOfListOfInteractive ite( List );
456 for ( ; ite.More(); ite.Next() )
458 Handle(SALOME_InteractiveObject) anObj =
459 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
461 if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
462 prs->AddObject( ite.Value() );
468 //=======================================================================
469 // name : LocalSelection
470 // Purpose : Activates selection of sub shapes
471 //=======================================================================
472 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
474 Handle(AIS_InteractiveContext) ic = getAISContext();
476 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
480 // Open local context if there is no one
481 bool allObjects = thePrs == 0 || thePrs->IsNull();
482 if ( !ic->HasOpenedContext() ) {
483 ic->ClearCurrents( false );
484 ic->OpenLocalContext( allObjects, true, true );
487 AIS_ListOfInteractive anObjs;
488 // Get objects to be activated
490 ic->DisplayedObjects( anObjs );
492 anOCCPrs->GetObjects( anObjs );
494 // Activate selection of objects from prs
495 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
496 for ( ; aIter.More(); aIter.Next() ) {
497 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
498 if ( !anAIS.IsNull() )
500 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
502 ic->Load( anAIS, -1, false );
503 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
505 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
507 ic->Load( anAIS, -1, false );
508 ic->Activate( anAIS, theMode );
514 //=======================================================================
515 // name : GlobalSelection
516 // Purpose : Deactivates selection of sub shapes
517 //=======================================================================
518 void SOCC_Viewer::GlobalSelection( const bool update ) const
520 Handle(AIS_InteractiveContext) ic = getAISContext();
523 ic->CloseAllContexts( false );
525 ic->CurrentViewer()->Redraw();
529 //=======================================================================
530 // name : BeforeDisplay
531 // Purpose : Axiluary method called before displaying of objects
532 //=======================================================================
533 void SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
535 d->BeforeDisplay( this, SALOME_OCCViewType() );
538 //=======================================================================
539 // name : AfterDisplay
540 // Purpose : Axiluary method called after displaying of objects
541 //=======================================================================
542 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
544 d->AfterDisplay( this, SALOME_OCCViewType() );
547 //=======================================================================
548 // name : getTrihedronSize
549 // Purpose : Get new and current trihedron size corresponding to the
550 // current model size
551 //=======================================================================
552 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
557 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
558 Handle(V3d_Viewer) viewer = getViewer3d();
559 viewer->InitActiveViews();
560 if(!viewer->MoreActiveViews()) return false;
562 Handle(V3d_View) view3d = viewer->ActiveView();
565 if ( view3d.IsNull() )
568 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
571 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
573 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
574 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
577 aMaxSide = Xmax - Xmin;
578 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
579 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
581 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer","TrihedronSize", 105.);
583 static float EPS = 5.0E-3;
584 theSize = getTrihedron()->Size();
585 theNewSize = aMaxSide*aSizeInPercents / 100.0;
587 return fabs( theNewSize - theSize ) > theSize * EPS ||
588 fabs( theNewSize - theSize) > theNewSize * EPS;
591 //=======================================================================
594 //=======================================================================
595 void SOCC_Viewer::Repaint()
597 // onAdjustTrihedron();
598 getViewer3d()->Update();