1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SOCC_ViewModel.h"
26 #include "SOCC_ViewWindow.h"
28 #include "SUIT_Session.h"
29 #include "SUIT_ResourceMgr.h"
30 //#include "SUIT_Application.h"
32 //#include "ToolsGUI.h"
34 // Temporarily commented to avoid awful dependecy on SALOMEDS
35 // TODO: better mechanism of storing display/erse status in a study
36 // should be provided...
37 //#include <SALOMEconfig.h>
38 //#include CORBA_CLIENT_HEADER(SALOMEDS)
40 #include <AIS_ListIteratorOfListOfInteractive.hxx>
42 #include <SALOME_AISShape.hxx>
43 #include <SALOME_AISObject.hxx>
44 #include <SALOME_InteractiveObject.hxx>
45 #include <SALOME_ListIO.hxx>
47 // Temporarily commented to avoid awful dependecy on SALOMEDS
48 // TODO: better mechanism of storing display/erse status in a study
49 // should be provided...
50 //#include <Utils_ORB_INIT.hxx>
51 //#include <Utils_SINGLETON.hxx>
52 //#include <SALOME_ModuleCatalog_impl.hxx>
53 //#include <SALOME_NamingService.hxx>
55 //#include "SALOMEDSClient.hxx"
56 //#include "SALOMEDS_StudyManager.hxx"
58 #include <AIS_TypeOfIso.hxx>
59 #include <Precision.hxx>
63 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
64 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
65 // SALOMEDS::StudyManager - no linkage with SalomeApp.
67 // Temporarily commented to avoid awful dependecy on SALOMEDS
68 // TODO: better mechanism of storing display/erse status in a study
69 // should be provided...
70 //static _PTR(Study) getStudyDS()
72 // SALOMEDSClient_Study* aStudy = NULL;
73 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
75 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
76 // id of its underlying SALOMEDS::Study
77 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
78 // if ( !app ) return _PTR(Study)(aStudy);
79 // SUIT_Study* stud = app->activeStudy();
80 // if ( !stud ) return _PTR(Study)(aStudy);
81 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
82 // get SALOMEDS_Study with this id from StudyMgr
83 // return aMgr->GetStudyByID( id );
88 \param DisplayTrihedron - is trihedron displayed
90 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
91 : OCCViewer_Viewer( DisplayTrihedron )
98 SOCC_Viewer::~SOCC_Viewer()
103 Hilights/unhilights object in viewer
104 \param obj - object to be updated
105 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
106 \param update - update current viewer
108 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
109 bool hilight, bool upd )
111 bool isInLocal = getAISContext()->HasOpenedContext();
112 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
113 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
115 AIS_ListOfInteractive List;
116 getAISContext()->DisplayedObjects(List);
118 AIS_ListIteratorOfListOfInteractive ite(List);
119 for ( ; ite.More(); ite.Next() )
121 Handle(SALOME_InteractiveObject) anObj =
122 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
124 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
127 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
128 // highlight sub-shapes only when local selection is active
131 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
133 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
134 TColStd_IndexedMapOfInteger MapIndex;
135 Sel->GetIndex( IObject, MapIndex );
136 aSh->highlightSubShapes( MapIndex, highlight );
150 \return true if object is in viewer or in collector
151 \param obj - object to be checked
152 \param onlyInViewer - search object only in viewer (so object must be displayed)
154 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
157 AIS_ListOfInteractive List;
158 getAISContext()->DisplayedObjects(List);
159 AIS_ListIteratorOfListOfInteractive ite(List);
160 for ( ; ite.More(); ite.Next() )
162 Handle(SALOME_InteractiveObject) anObj =
163 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
165 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
172 \return true if object is displayed in viewer
173 \param obj - object to be checked
175 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
178 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
179 if(it != entry2aisobjects.end())
182 Handle (AIS_InteractiveContext) ic = getAISContext();
183 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
184 for( unsigned int ind = 0; ind < List.size(); ind++ )
186 Handle(AIS_InteractiveObject) anAIS=List[ind];
187 if(ic->IsDisplayed(anAIS))
197 \param obj - object to be updated
198 \param color - new color
199 \param update - update current viewer
201 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
202 const QColor& color, bool update )
204 if(obj.IsNull() || !obj->hasEntry() )
207 if(entry2aisobjects.count(obj->getEntry())>0)
210 Handle (AIS_InteractiveContext) ic = getAISContext();
211 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
212 for( unsigned int ind = 0; ind < List.size(); ind++ )
214 Handle(AIS_InteractiveObject) anAIS=List[ind];
215 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
216 OCCViewer_Viewer::setColor( anAIS, color, update );
222 Changes display mode of object
223 \param obj - object to be processed
224 \param mode - new display mode
225 \param update - update current viewer
227 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
228 int mode, bool update )
230 if(obj.IsNull() || !obj->hasEntry() )
233 if(entry2aisobjects.count(obj->getEntry())>0)
236 Handle (AIS_InteractiveContext) ic = getAISContext();
237 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
238 for( unsigned int ind = 0; ind < List.size(); ind++ )
240 Handle(AIS_InteractiveObject) anAIS=List[ind];
241 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
242 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
248 Changes transparency of object
249 \param obj - object to be processed
250 \param trans - new transparency
251 \param update - update current viewer
253 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
254 float trans, bool update )
256 if(obj.IsNull() || !obj->hasEntry() )
259 if(entry2aisobjects.count(obj->getEntry())>0)
262 Handle (AIS_InteractiveContext) ic = getAISContext();
263 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
264 for( unsigned int ind = 0; ind < List.size(); ind++ )
266 Handle(AIS_InteractiveObject) anAIS=List[ind];
267 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
268 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
274 Changes name of object
275 \param obj - object to be renamed
276 \param name - new name
278 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
279 const QString& name )
281 AIS_ListOfInteractive List;
282 getAISContext()->DisplayedObjects(List);
284 AIS_ListIteratorOfListOfInteractive ite(List);
287 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
289 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
293 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
294 if ( IO->isSame( obj ) )
296 aSh->setName( name.toLatin1().data() );
301 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
303 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
305 // Add code here, if someone create a MODULE_AISObject.
314 \param prs - presentation
316 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
318 // try do downcast object
319 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
320 if ( !anOCCPrs || anOCCPrs->IsNull() )
323 // get SALOMEDS Study
324 // Temporarily commented to avoid awful dependecy on SALOMEDS
325 // TODO: better mechanism of storing display/erse status in a study
326 // should be provided...
327 // _PTR(Study) study(getStudyDS());
330 Handle (AIS_InteractiveContext) ic = getAISContext();
332 // get objects to be displayed
333 AIS_ListOfInteractive anAISObjects;
334 anOCCPrs->GetObjects( anAISObjects );
336 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
337 for ( ; aIter.More(); aIter.Next() )
339 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
340 if ( !anAIS.IsNull() )
342 // try to find presentation in the viewer
344 // if the object is already displayed - nothing to do more
345 if(ic->IsDisplayed(anAIS))
347 // Deactivate object if necessary
348 if ( !anOCCPrs->ToActivate() )
349 ic->Deactivate( anAIS );
353 // if object is not displayed and not found in the collector - display it
354 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
356 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
357 double aNewSize = 100, aSize = 100;
358 computeTrihedronSize( aNewSize, aSize );
359 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
362 ic->Display( anAIS, false );
363 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
366 aSh->SetClippable (prs->IsClippable());
367 applyExistingClipPlanesToObject (anAIS);
368 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
369 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
370 if(!aSh->toActivate())
372 ic->Deactivate( aSh );
376 //Register anAIS (if it has an entry) in entry2aisobjects map
377 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
378 if ( !anObj.IsNull() && anObj->hasEntry())
380 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
382 for ( unsigned int ind = 0; ind < List.size(); ind++ )
384 if(List[ind] == anAIS)
392 List.push_back(anAIS);
396 // Set visibility flag
397 // Temporarily commented to avoid awful dependecy on SALOMEDS
398 // TODO: better mechanism of storing display/erse status in a study
399 // should be provided...
400 //Handle(SALOME_InteractiveObject) anObj =
401 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
402 //if ( !anObj.IsNull() && anObj->hasEntry() )
405 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
408 // Deactivate object if necessary
409 if ( !anOCCPrs->ToActivate() )
410 ic->Deactivate( anAIS );
419 \param prs - presentation
420 \param forced - removes object from context
422 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
424 // try do downcast object
425 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
426 if ( !anOCCPrs || anOCCPrs->IsNull() )
429 // get SALOMEDS Study
430 // Temporarily commented to avoid awful dependecy on SALOMEDS
431 // TODO: better mechanism of storing display/erse status in a study
432 // should be provided...
433 // _PTR(Study) study(getStudyDS());
436 Handle(AIS_InteractiveContext) ic = getAISContext();
438 // get objects to be erased
439 AIS_ListOfInteractive anAISObjects;
440 anOCCPrs->GetObjects( anAISObjects );
442 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
443 for ( ; aIter.More(); aIter.Next() ) {
444 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
445 if ( !anAIS.IsNull() ) {
446 // erase the object from context : move it to collector
447 ic->Erase( anAIS, false );
448 // Set visibility flag if necessary
449 // Temporarily commented to avoid awful dependecy on SALOMEDS
450 // TODO: better mechanism of storing display/erse status in a study
451 // should be provided...
454 // Handle(SALOME_InteractiveObject) anObj =
455 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
456 // if ( !anObj.IsNull() && anObj->hasEntry() )
459 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
469 Erase all presentations
470 \param forced - removes all objects from context
472 void SOCC_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
474 // get SALOMEDS Study
475 // Temporarily commented to avoid awful dependecy on SALOMEDS
476 // TODO: better mechanism of storing display/erse status in a study
477 // should be provided...
478 // _PTR(Study) study(getStudyDS());
481 Handle(AIS_InteractiveContext) ic = getAISContext();
483 // check if trihedron is displayed
484 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
486 // get objects to be erased (all currently displayed objects)
487 AIS_ListOfInteractive aList;
488 ic->DisplayedObjects( aList );
489 AIS_ListIteratorOfListOfInteractive anIter( aList );
490 for ( ; anIter.More(); anIter.Next() ) {
491 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) ) )
495 Handle(AIS_InteractiveObject) anIO = anIter.Value();
496 ic->Erase( anIO, false );
498 // Set visibility flag if necessary
499 // Temporarily commented to avoid awful dependecy on SALOMEDS
500 // TODO: better mechanism of storing display/erse status in a study
501 // should be provided...
503 // Handle(SALOME_InteractiveObject) anObj =
504 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
506 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
508 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
513 SALOME_View::EraseAll( d, forced );
520 Create presentation corresponding to the entry
523 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
525 SOCC_Prs* prs = new SOCC_Prs(entry);
528 if(entry2aisobjects.count(entry)>0)
531 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
533 Handle(AIS_InteractiveContext) ic = getAISContext();
535 for ( unsigned int ind = 0; ind < List.size(); ind++ )
537 Handle(AIS_InteractiveObject) anAIS=List[ind];
538 if(ic->IsDisplayed(anAIS))
540 prs->AddObject( anAIS );
549 Activates selection of sub-shapes
551 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const std::list<int> modes )
553 Handle(AIS_InteractiveContext) ic = getAISContext();
555 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
559 // Open local context if there is no one
560 bool allObjects = thePrs == 0 || thePrs->IsNull();
561 if ( !ic->HasOpenedContext() ) {
562 ic->ClearCurrents( false );
563 ic->OpenLocalContext( Standard_False, Standard_True, Standard_True );
566 AIS_ListOfInteractive anObjs;
567 // Get objects to be activated
569 ic->DisplayedObjects( anObjs );
571 anOCCPrs->GetObjects( anObjs );
573 std::list<int> sel_modes;
574 for ( int i = TopAbs_COMPOUND; i < TopAbs_SHAPE; i++ )
575 if ( std::find(modes.begin(), modes.end(), (int)TopAbs_SHAPE) != modes.end() || std::find(modes.begin(), modes.end(), i) != modes.end())
576 sel_modes.push_back(i);
578 // Activate selection of objects from prs
579 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
580 for ( ; aIter.More(); aIter.Next() ) {
581 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
582 if ( !anAIS.IsNull() )
584 std::list<int>::const_iterator it;
585 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
587 ic->Load( anAIS, -1, false );
588 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
589 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)*it ) );
591 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
593 ic->Load( anAIS, -1, false );
594 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
595 ic->Activate( anAIS, *it );
602 Activates selection of sub-shapes
604 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
606 std::list<int> modes;
607 modes.push_back( theMode );
608 LocalSelection( thePrs, modes );
612 Deactivates selection of sub-shapes
614 void SOCC_Viewer::GlobalSelection( const bool update ) const
616 Handle(AIS_InteractiveContext) ic = getAISContext();
619 ic->CloseAllContexts( false );
621 ic->CurrentViewer()->Redraw();
627 \Collect objects visible in viewer
628 \param theList - visible objects collection
630 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
632 AIS_ListOfInteractive List;
633 getAISContext()->DisplayedObjects(List);
635 AIS_ListIteratorOfListOfInteractive ite(List);
636 for ( ; ite.More(); ite.Next() )
638 Handle(SALOME_InteractiveObject) anObj =
639 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
641 if ( !anObj.IsNull() && anObj->hasEntry() )
642 theList.Append( anObj );
647 Updates current viewer
649 void SOCC_Viewer::Repaint()
651 // onAdjustTrihedron();
652 getViewer3d()->Update();
657 create SOCC_ViewWindow
659 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
661 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
663 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
668 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
670 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
672 return new SOCC_ViewWindow( 0, this);