1 // Copyright (C) 2007-2014 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 "OCCViewer_Trihedron.h"
30 #include "SUIT_Session.h"
31 #include "SUIT_ResourceMgr.h"
32 //#include "SUIT_Application.h"
34 //#include "ToolsGUI.h"
36 // Temporarily commented to avoid awful dependecy on SALOMEDS
37 // TODO: better mechanism of storing display/erse status in a study
38 // should be provided...
39 //#include <SALOMEconfig.h>
40 //#include CORBA_CLIENT_HEADER(SALOMEDS)
42 #include <AIS_ListIteratorOfListOfInteractive.hxx>
43 #include <Visual3d_View.hxx>
45 #include <SALOME_AISShape.hxx>
46 #include <SALOME_AISObject.hxx>
47 #include <SALOME_InteractiveObject.hxx>
48 #include <SALOME_ListIO.hxx>
50 // Temporarily commented to avoid awful dependecy on SALOMEDS
51 // TODO: better mechanism of storing display/erse status in a study
52 // should be provided...
53 //#include <Utils_ORB_INIT.hxx>
54 //#include <Utils_SINGLETON.hxx>
55 //#include <SALOME_ModuleCatalog_impl.hxx>
56 //#include <SALOME_NamingService.hxx>
58 //#include "SALOMEDSClient.hxx"
59 //#include "SALOMEDS_StudyManager.hxx"
61 #include <AIS_TypeOfIso.hxx>
62 #include <Precision.hxx>
64 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
65 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
66 // SALOMEDS::StudyManager - no linkage with SalomeApp.
68 // Temporarily commented to avoid awful dependecy on SALOMEDS
69 // TODO: better mechanism of storing display/erse status in a study
70 // should be provided...
71 //static _PTR(Study) getStudyDS()
73 // SALOMEDSClient_Study* aStudy = NULL;
74 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
76 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
77 // id of its underlying SALOMEDS::Study
78 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
79 // if ( !app ) return _PTR(Study)(aStudy);
80 // SUIT_Study* stud = app->activeStudy();
81 // if ( !stud ) return _PTR(Study)(aStudy);
82 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
83 // get SALOMEDS_Study with this id from StudyMgr
84 // return aMgr->GetStudyByID( id );
89 \param DisplayTrihedron - is trihedron displayed
91 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
92 : OCCViewer_Viewer( DisplayTrihedron )
99 SOCC_Viewer::~SOCC_Viewer()
104 Hilights/unhilights object in viewer
105 \param obj - object to be updated
106 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
107 \param update - update current viewer
109 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
110 bool hilight, bool upd )
112 bool isInLocal = getAISContext()->HasOpenedContext();
113 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
114 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
116 AIS_ListOfInteractive List;
117 getAISContext()->DisplayedObjects(List);
119 AIS_ListIteratorOfListOfInteractive ite(List);
120 for ( ; ite.More(); ite.Next() )
122 Handle(SALOME_InteractiveObject) anObj =
123 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
125 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
128 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
129 // highlight sub-shapes only when local selection is active
132 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
134 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
135 TColStd_IndexedMapOfInteger MapIndex;
136 Sel->GetIndex( IObject, MapIndex );
137 aSh->highlightSubShapes( MapIndex, highlight );
151 \return true if object is in viewer or in collector
152 \param obj - object to be checked
153 \param onlyInViewer - search object only in viewer (so object must be displayed)
155 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
158 AIS_ListOfInteractive List;
159 getAISContext()->DisplayedObjects(List);
160 AIS_ListIteratorOfListOfInteractive ite(List);
161 for ( ; ite.More(); ite.Next() )
163 Handle(SALOME_InteractiveObject) anObj =
164 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
166 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
173 \return true if object is displayed in viewer
174 \param obj - object to be checked
176 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
179 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
180 if(it != entry2aisobjects.end())
183 Handle (AIS_InteractiveContext) ic = getAISContext();
184 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
185 for( unsigned int ind = 0; ind < List.size(); ind++ )
187 Handle(AIS_InteractiveObject) anAIS=List[ind];
188 if(ic->IsDisplayed(anAIS))
198 \param obj - object to be updated
199 \param color - new color
200 \param update - update current viewer
202 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
203 const QColor& color, bool update )
205 if(obj.IsNull() || !obj->hasEntry() )
208 if(entry2aisobjects.count(obj->getEntry())>0)
211 Handle (AIS_InteractiveContext) ic = getAISContext();
212 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
213 for( unsigned int ind = 0; ind < List.size(); ind++ )
215 Handle(AIS_InteractiveObject) anAIS=List[ind];
216 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
217 OCCViewer_Viewer::setColor( anAIS, color, update );
223 Changes display mode of object
224 \param obj - object to be processed
225 \param mode - new display mode
226 \param update - update current viewer
228 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
229 int mode, bool update )
231 if(obj.IsNull() || !obj->hasEntry() )
234 if(entry2aisobjects.count(obj->getEntry())>0)
237 Handle (AIS_InteractiveContext) ic = getAISContext();
238 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
239 for( unsigned int ind = 0; ind < List.size(); ind++ )
241 Handle(AIS_InteractiveObject) anAIS=List[ind];
242 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
243 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
249 Changes transparency of object
250 \param obj - object to be processed
251 \param trans - new transparency
252 \param update - update current viewer
254 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
255 float trans, bool update )
257 if(obj.IsNull() || !obj->hasEntry() )
260 if(entry2aisobjects.count(obj->getEntry())>0)
263 Handle (AIS_InteractiveContext) ic = getAISContext();
264 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
265 for( unsigned int ind = 0; ind < List.size(); ind++ )
267 Handle(AIS_InteractiveObject) anAIS=List[ind];
268 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
269 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
275 Changes name of object
276 \param obj - object to be renamed
277 \param name - new name
279 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
280 const QString& name )
282 AIS_ListOfInteractive List;
283 getAISContext()->DisplayedObjects(List);
285 AIS_ListIteratorOfListOfInteractive ite(List);
288 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
290 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
294 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
295 if ( IO->isSame( obj ) )
297 aSh->setName( name.toLatin1().data() );
302 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
304 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
306 // Add code here, if someone create a MODULE_AISObject.
315 \param prs - presentation
317 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
319 // try do downcast object
320 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
321 if ( !anOCCPrs || anOCCPrs->IsNull() )
324 // get SALOMEDS Study
325 // Temporarily commented to avoid awful dependecy on SALOMEDS
326 // TODO: better mechanism of storing display/erse status in a study
327 // should be provided...
328 // _PTR(Study) study(getStudyDS());
331 Handle (AIS_InteractiveContext) ic = getAISContext();
333 // get objects to be displayed
334 AIS_ListOfInteractive anAISObjects;
335 anOCCPrs->GetObjects( anAISObjects );
337 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
338 for ( ; aIter.More(); aIter.Next() )
340 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
341 if ( !anAIS.IsNull() )
343 // try to find presentation in the viewer
345 // if the object is already displayed - nothing to do more
346 if(ic->IsDisplayed(anAIS))
348 // Deactivate object if necessary
349 if ( !anOCCPrs->ToActivate() )
350 ic->Deactivate( anAIS );
354 // if object is not displayed and not found in the collector - display it
355 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
357 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
358 double aNewSize = 100, aSize = 100;
359 computeTrihedronSize( aNewSize, aSize );
360 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
363 ic->Display( anAIS, false );
364 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
367 aSh->SetClippable (prs->IsClippable());
368 applyExistingClipPlanesToObject (anAIS);
369 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
370 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
371 if(!aSh->toActivate())
373 ic->Deactivate( aSh );
377 //Register anAIS (if it has an entry) in entry2aisobjects map
378 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
379 if ( !anObj.IsNull() && anObj->hasEntry())
381 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
383 for ( unsigned int ind = 0; ind < List.size(); ind++ )
385 if(List[ind] == anAIS)
393 List.push_back(anAIS);
397 // Set visibility flag
398 // Temporarily commented to avoid awful dependecy on SALOMEDS
399 // TODO: better mechanism of storing display/erse status in a study
400 // 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 );
409 // Deactivate object if necessary
410 if ( !anOCCPrs->ToActivate() )
411 ic->Deactivate( anAIS );
420 \param prs - presentation
421 \param forced - removes object from context
423 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
425 // try do downcast object
426 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
427 if ( !anOCCPrs || anOCCPrs->IsNull() )
430 // get SALOMEDS Study
431 // Temporarily commented to avoid awful dependecy on SALOMEDS
432 // TODO: better mechanism of storing display/erse status in a study
433 // should be provided...
434 // _PTR(Study) study(getStudyDS());
437 Handle(AIS_InteractiveContext) ic = getAISContext();
439 // get objects to be erased
440 AIS_ListOfInteractive anAISObjects;
441 anOCCPrs->GetObjects( anAISObjects );
443 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
444 for ( ; aIter.More(); aIter.Next() ) {
445 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
446 if ( !anAIS.IsNull() ) {
447 // erase the object from context : move it to collector
448 ic->Erase( anAIS, false );
449 // Set visibility flag if necessary
450 // Temporarily commented to avoid awful dependecy on SALOMEDS
451 // TODO: better mechanism of storing display/erse status in a study
452 // should be provided...
455 // Handle(SALOME_InteractiveObject) anObj =
456 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
457 // if ( !anObj.IsNull() && anObj->hasEntry() )
460 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
470 Erase all presentations
471 \param forced - removes all objects from context
473 void SOCC_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
475 // get SALOMEDS Study
476 // Temporarily commented to avoid awful dependecy on SALOMEDS
477 // TODO: better mechanism of storing display/erse status in a study
478 // should be provided...
479 // _PTR(Study) study(getStudyDS());
482 Handle(AIS_InteractiveContext) ic = getAISContext();
484 // check if trihedron is displayed
485 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
487 // get objects to be erased (all currently displayed objects)
488 AIS_ListOfInteractive aList;
489 ic->DisplayedObjects( aList );
490 AIS_ListIteratorOfListOfInteractive anIter( aList );
491 for ( ; anIter.More(); anIter.Next() ) {
492 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron )) ||
493 anIter.Value()->DynamicType() == STANDARD_TYPE( OCCViewer_Trihedron ))
497 Handle(AIS_InteractiveObject) anIO = anIter.Value();
498 ic->Erase( anIO, false );
500 // Set visibility flag if necessary
501 // Temporarily commented to avoid awful dependecy on SALOMEDS
502 // TODO: better mechanism of storing display/erse status in a study
503 // should be provided...
505 // Handle(SALOME_InteractiveObject) anObj =
506 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
508 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
510 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
515 SALOME_View::EraseAll( d, forced );
522 Create presentation corresponding to the entry
525 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
527 SOCC_Prs* prs = new SOCC_Prs(entry);
530 if(entry2aisobjects.count(entry)>0)
533 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
535 Handle(AIS_InteractiveContext) ic = getAISContext();
537 for ( unsigned int ind = 0; ind < List.size(); ind++ )
539 Handle(AIS_InteractiveObject) anAIS=List[ind];
540 if(ic->IsDisplayed(anAIS))
542 prs->AddObject( anAIS );
551 Activates selection of sub-shapes
553 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
555 Handle(AIS_InteractiveContext) ic = getAISContext();
557 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
561 // Open local context if there is no one
562 bool allObjects = thePrs == 0 || thePrs->IsNull();
563 if ( !ic->HasOpenedContext() ) {
564 ic->ClearCurrents( false );
565 ic->OpenLocalContext( allObjects, true, true );
568 AIS_ListOfInteractive anObjs;
569 // Get objects to be activated
571 ic->DisplayedObjects( anObjs );
573 anOCCPrs->GetObjects( anObjs );
575 // Activate selection of objects from prs
576 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
577 for ( ; aIter.More(); aIter.Next() ) {
578 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
579 if ( !anAIS.IsNull() )
581 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
583 ic->Load( anAIS, -1, false );
584 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
586 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
588 ic->Load( anAIS, -1, false );
589 ic->Activate( anAIS, theMode );
596 Deactivates selection of sub-shapes
598 void SOCC_Viewer::GlobalSelection( const bool update ) const
600 Handle(AIS_InteractiveContext) ic = getAISContext();
603 ic->CloseAllContexts( false );
605 ic->CurrentViewer()->Redraw();
611 \Collect objects visible in viewer
612 \param theList - visible objects collection
614 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
616 AIS_ListOfInteractive List;
617 getAISContext()->DisplayedObjects(List);
619 AIS_ListIteratorOfListOfInteractive ite(List);
620 for ( ; ite.More(); ite.Next() )
622 Handle(SALOME_InteractiveObject) anObj =
623 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
625 if ( !anObj.IsNull() && anObj->hasEntry() )
626 theList.Append( anObj );
631 Updates current viewer
633 void SOCC_Viewer::Repaint()
635 // onAdjustTrihedron();
636 getViewer3d()->Update();
641 create SOCC_ViewWindow
643 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
645 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
647 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
652 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
654 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
656 return new SOCC_ViewWindow( 0, this);