1 // Copyright (C) 2007-2015 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>
66 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
67 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
68 // SALOMEDS::StudyManager - no linkage with SalomeApp.
70 // Temporarily commented to avoid awful dependecy on SALOMEDS
71 // TODO: better mechanism of storing display/erse status in a study
72 // should be provided...
73 //static _PTR(Study) getStudyDS()
75 // SALOMEDSClient_Study* aStudy = NULL;
76 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
78 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
79 // id of its underlying SALOMEDS::Study
80 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
81 // if ( !app ) return _PTR(Study)(aStudy);
82 // SUIT_Study* stud = app->activeStudy();
83 // if ( !stud ) return _PTR(Study)(aStudy);
84 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
85 // get SALOMEDS_Study with this id from StudyMgr
86 // return aMgr->GetStudyByID( id );
91 \param DisplayTrihedron - is trihedron displayed
93 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
94 : OCCViewer_Viewer( DisplayTrihedron )
101 SOCC_Viewer::~SOCC_Viewer()
106 Hilights/unhilights object in viewer
107 \param obj - object to be updated
108 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
109 \param update - update current viewer
111 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
112 bool hilight, bool upd )
114 bool isInLocal = getAISContext()->HasOpenedContext();
115 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
116 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
118 AIS_ListOfInteractive List;
119 getAISContext()->DisplayedObjects(List);
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 ) )
130 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
131 // highlight sub-shapes only when local selection is active
134 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
136 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
137 TColStd_IndexedMapOfInteger MapIndex;
138 Sel->GetIndex( IObject, MapIndex );
139 aSh->highlightSubShapes( MapIndex, highlight );
153 \return true if object is in viewer or in collector
154 \param obj - object to be checked
155 \param onlyInViewer - search object only in viewer (so object must be displayed)
157 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
160 AIS_ListOfInteractive List;
161 getAISContext()->DisplayedObjects(List);
162 AIS_ListIteratorOfListOfInteractive ite(List);
163 for ( ; ite.More(); ite.Next() )
165 Handle(SALOME_InteractiveObject) anObj =
166 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
168 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
175 \return true if object is displayed in viewer
176 \param obj - object to be checked
178 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
181 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
182 if(it != entry2aisobjects.end())
185 Handle (AIS_InteractiveContext) ic = getAISContext();
186 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
187 for( unsigned int ind = 0; ind < List.size(); ind++ )
189 Handle(AIS_InteractiveObject) anAIS=List[ind];
190 if(ic->IsDisplayed(anAIS))
200 \param obj - object to be updated
201 \param color - new color
202 \param update - update current viewer
204 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
205 const QColor& color, bool update )
207 if(obj.IsNull() || !obj->hasEntry() )
210 if(entry2aisobjects.count(obj->getEntry())>0)
213 Handle (AIS_InteractiveContext) ic = getAISContext();
214 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
215 for( unsigned int ind = 0; ind < List.size(); ind++ )
217 Handle(AIS_InteractiveObject) anAIS=List[ind];
218 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
219 OCCViewer_Viewer::setColor( anAIS, color, update );
225 Changes display mode of object
226 \param obj - object to be processed
227 \param mode - new display mode
228 \param update - update current viewer
230 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
231 int mode, bool update )
233 if(obj.IsNull() || !obj->hasEntry() )
236 if(entry2aisobjects.count(obj->getEntry())>0)
239 Handle (AIS_InteractiveContext) ic = getAISContext();
240 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
241 for( unsigned int ind = 0; ind < List.size(); ind++ )
243 Handle(AIS_InteractiveObject) anAIS=List[ind];
244 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
245 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
251 Changes transparency of object
252 \param obj - object to be processed
253 \param trans - new transparency
254 \param update - update current viewer
256 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
257 float trans, bool update )
259 if(obj.IsNull() || !obj->hasEntry() )
262 if(entry2aisobjects.count(obj->getEntry())>0)
265 Handle (AIS_InteractiveContext) ic = getAISContext();
266 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
267 for( unsigned int ind = 0; ind < List.size(); ind++ )
269 Handle(AIS_InteractiveObject) anAIS=List[ind];
270 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
271 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
277 Changes name of object
278 \param obj - object to be renamed
279 \param name - new name
281 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
282 const QString& name )
284 AIS_ListOfInteractive List;
285 getAISContext()->DisplayedObjects(List);
287 AIS_ListIteratorOfListOfInteractive ite(List);
290 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
292 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
296 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
297 if ( IO->isSame( obj ) )
299 aSh->setName( name.toLatin1().data() );
304 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
306 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
308 // Add code here, if someone create a MODULE_AISObject.
317 \param prs - presentation
319 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
321 // try do downcast object
322 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
323 if ( !anOCCPrs || anOCCPrs->IsNull() )
326 // get SALOMEDS Study
327 // Temporarily commented to avoid awful dependecy on SALOMEDS
328 // TODO: better mechanism of storing display/erse status in a study
329 // should be provided...
330 // _PTR(Study) study(getStudyDS());
333 Handle (AIS_InteractiveContext) ic = getAISContext();
335 // get objects to be displayed
336 AIS_ListOfInteractive anAISObjects;
337 anOCCPrs->GetObjects( anAISObjects );
339 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
340 for ( ; aIter.More(); aIter.Next() )
342 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
343 if ( !anAIS.IsNull() )
345 // try to find presentation in the viewer
347 // if the object is already displayed - nothing to do more
348 if(ic->IsDisplayed(anAIS))
350 // Deactivate object if necessary
351 if ( !anOCCPrs->ToActivate() )
352 ic->Deactivate( anAIS );
356 // if object is not displayed and not found in the collector - display it
357 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
359 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
360 double aNewSize = 100, aSize = 100;
361 computeTrihedronSize( aNewSize, aSize );
362 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
365 ic->Display( anAIS, false );
366 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
369 aSh->SetClippable (prs->IsClippable());
370 applyExistingClipPlanesToObject (anAIS);
371 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
372 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
373 if(!aSh->toActivate())
375 ic->Deactivate( aSh );
379 //Register anAIS (if it has an entry) in entry2aisobjects map
380 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
381 if ( !anObj.IsNull() && anObj->hasEntry())
383 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
385 for ( unsigned int ind = 0; ind < List.size(); ind++ )
387 if(List[ind] == anAIS)
395 List.push_back(anAIS);
399 // Set visibility flag
400 // Temporarily commented to avoid awful dependecy on SALOMEDS
401 // TODO: better mechanism of storing display/erse status in a study
402 // should be provided...
403 //Handle(SALOME_InteractiveObject) anObj =
404 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
405 //if ( !anObj.IsNull() && anObj->hasEntry() )
408 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
411 // Deactivate object if necessary
412 if ( !anOCCPrs->ToActivate() )
413 ic->Deactivate( anAIS );
422 \param prs - presentation
423 \param forced - removes object from context
425 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
427 // try do downcast object
428 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
429 if ( !anOCCPrs || anOCCPrs->IsNull() )
432 // get SALOMEDS Study
433 // Temporarily commented to avoid awful dependecy on SALOMEDS
434 // TODO: better mechanism of storing display/erse status in a study
435 // should be provided...
436 // _PTR(Study) study(getStudyDS());
439 Handle(AIS_InteractiveContext) ic = getAISContext();
441 // get objects to be erased
442 AIS_ListOfInteractive anAISObjects;
443 anOCCPrs->GetObjects( anAISObjects );
445 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
446 for ( ; aIter.More(); aIter.Next() ) {
447 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
448 if ( !anAIS.IsNull() ) {
449 // erase the object from context : move it to collector
450 ic->Erase( anAIS, false );
451 // Set visibility flag if necessary
452 // Temporarily commented to avoid awful dependecy on SALOMEDS
453 // TODO: better mechanism of storing display/erse status in a study
454 // should be provided...
457 // Handle(SALOME_InteractiveObject) anObj =
458 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
459 // if ( !anObj.IsNull() && anObj->hasEntry() )
462 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
472 Erase all presentations
473 \param forced - removes all objects from context
475 void SOCC_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
477 // get SALOMEDS Study
478 // Temporarily commented to avoid awful dependecy on SALOMEDS
479 // TODO: better mechanism of storing display/erse status in a study
480 // should be provided...
481 // _PTR(Study) study(getStudyDS());
484 Handle(AIS_InteractiveContext) ic = getAISContext();
486 // check if trihedron is displayed
487 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
489 // get objects to be erased (all currently displayed objects)
490 AIS_ListOfInteractive aList;
491 ic->DisplayedObjects( aList );
492 AIS_ListIteratorOfListOfInteractive anIter( aList );
493 for ( ; anIter.More(); anIter.Next() ) {
494 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron )) ||
495 anIter.Value()->DynamicType() == STANDARD_TYPE( OCCViewer_Trihedron ))
499 Handle(AIS_InteractiveObject) anIO = anIter.Value();
500 ic->Erase( anIO, false );
502 // Set visibility flag if necessary
503 // Temporarily commented to avoid awful dependecy on SALOMEDS
504 // TODO: better mechanism of storing display/erse status in a study
505 // should be provided...
507 // Handle(SALOME_InteractiveObject) anObj =
508 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
510 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
512 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
517 SALOME_View::EraseAll( d, forced );
524 Create presentation corresponding to the entry
527 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
529 SOCC_Prs* prs = new SOCC_Prs(entry);
532 if(entry2aisobjects.count(entry)>0)
535 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
537 Handle(AIS_InteractiveContext) ic = getAISContext();
539 for ( unsigned int ind = 0; ind < List.size(); ind++ )
541 Handle(AIS_InteractiveObject) anAIS=List[ind];
542 if(ic->IsDisplayed(anAIS))
544 prs->AddObject( anAIS );
553 Activates selection of sub-shapes
555 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const std::list<int> modes )
557 Handle(AIS_InteractiveContext) ic = getAISContext();
559 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
563 // Open local context if there is no one
564 bool allObjects = thePrs == 0 || thePrs->IsNull();
565 if ( !ic->HasOpenedContext() ) {
566 ic->ClearCurrents( false );
567 ic->OpenLocalContext( allObjects, true, true );
570 AIS_ListOfInteractive anObjs;
571 // Get objects to be activated
573 ic->DisplayedObjects( anObjs );
575 anOCCPrs->GetObjects( anObjs );
577 std::list<int> sel_modes;
578 for ( int i = TopAbs_COMPOUND; i < TopAbs_SHAPE; i++ )
579 if ( std::find(modes.begin(), modes.end(), (int)TopAbs_SHAPE) != modes.end() || std::find(modes.begin(), modes.end(), i) != modes.end())
580 sel_modes.push_back(i);
582 // Activate selection of objects from prs
583 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
584 for ( ; aIter.More(); aIter.Next() ) {
585 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
586 if ( !anAIS.IsNull() )
588 std::list<int>::const_iterator it;
589 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
591 ic->Load( anAIS, -1, false );
592 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
593 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)*it ) );
595 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
597 ic->Load( anAIS, -1, false );
598 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
599 ic->Activate( anAIS, *it );
606 Activates selection of sub-shapes
608 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
610 std::list<int> modes;
611 modes.push_back( theMode );
612 LocalSelection( thePrs, modes );
616 Deactivates selection of sub-shapes
618 void SOCC_Viewer::GlobalSelection( const bool update ) const
620 Handle(AIS_InteractiveContext) ic = getAISContext();
623 ic->CloseAllContexts( false );
625 ic->CurrentViewer()->Redraw();
631 \Collect objects visible in viewer
632 \param theList - visible objects collection
634 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
636 AIS_ListOfInteractive List;
637 getAISContext()->DisplayedObjects(List);
639 AIS_ListIteratorOfListOfInteractive ite(List);
640 for ( ; ite.More(); ite.Next() )
642 Handle(SALOME_InteractiveObject) anObj =
643 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
645 if ( !anObj.IsNull() && anObj->hasEntry() )
646 theList.Append( anObj );
651 Updates current viewer
653 void SOCC_Viewer::Repaint()
655 // onAdjustTrihedron();
656 getViewer3d()->Update();
661 create SOCC_ViewWindow
663 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
665 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
667 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
672 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
674 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
676 return new SOCC_ViewWindow( 0, this);