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 #include <AIS_TypeOfIso.hxx>
48 #include <Precision.hxx>
54 \param DisplayTrihedron - is trihedron displayed
56 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
57 : OCCViewer_Viewer( DisplayTrihedron )
64 SOCC_Viewer::~SOCC_Viewer()
69 Hilights/unhilights object in viewer
70 \param obj - object to be updated
71 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
72 \param update - update current viewer
74 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
75 bool hilight, bool upd )
77 bool isInLocal = getAISContext()->HasOpenedContext();
78 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
79 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
81 AIS_ListOfInteractive List;
82 getAISContext()->DisplayedObjects(List);
84 AIS_ListIteratorOfListOfInteractive ite(List);
85 for ( ; ite.More(); ite.Next() )
87 Handle(SALOME_InteractiveObject) anObj =
88 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
90 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
93 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
94 // highlight sub-shapes only when local selection is active
97 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
99 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
100 TColStd_IndexedMapOfInteger MapIndex;
101 Sel->GetIndex( IObject, MapIndex );
102 aSh->highlightSubShapes( MapIndex, highlight );
116 \return true if object is in viewer or in collector
117 \param obj - object to be checked
118 \param onlyInViewer - search object only in viewer (so object must be displayed)
120 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
123 AIS_ListOfInteractive List;
124 getAISContext()->DisplayedObjects(List);
125 AIS_ListIteratorOfListOfInteractive ite(List);
126 for ( ; ite.More(); ite.Next() )
128 Handle(SALOME_InteractiveObject) anObj =
129 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
131 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
138 \return true if object is displayed in viewer
139 \param obj - object to be checked
141 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
144 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
145 if(it != entry2aisobjects.end())
148 Handle (AIS_InteractiveContext) ic = getAISContext();
149 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
150 for( unsigned int ind = 0; ind < List.size(); ind++ )
152 Handle(AIS_InteractiveObject) anAIS=List[ind];
153 if(ic->IsDisplayed(anAIS))
163 \param obj - object to be updated
164 \param color - new color
165 \param update - update current viewer
167 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
168 const QColor& color, bool update )
170 if(obj.IsNull() || !obj->hasEntry() )
173 if(entry2aisobjects.count(obj->getEntry())>0)
176 Handle (AIS_InteractiveContext) ic = getAISContext();
177 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
178 for( unsigned int ind = 0; ind < List.size(); ind++ )
180 Handle(AIS_InteractiveObject) anAIS=List[ind];
181 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
182 OCCViewer_Viewer::setColor( anAIS, color, update );
188 Changes display mode of object
189 \param obj - object to be processed
190 \param mode - new display mode
191 \param update - update current viewer
193 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
194 int mode, bool update )
196 if(obj.IsNull() || !obj->hasEntry() )
199 if(entry2aisobjects.count(obj->getEntry())>0)
202 Handle (AIS_InteractiveContext) ic = getAISContext();
203 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
204 for( unsigned int ind = 0; ind < List.size(); ind++ )
206 Handle(AIS_InteractiveObject) anAIS=List[ind];
207 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
208 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
214 Changes transparency of object
215 \param obj - object to be processed
216 \param trans - new transparency
217 \param update - update current viewer
219 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
220 float trans, bool update )
222 if(obj.IsNull() || !obj->hasEntry() )
225 if(entry2aisobjects.count(obj->getEntry())>0)
228 Handle (AIS_InteractiveContext) ic = getAISContext();
229 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
230 for( unsigned int ind = 0; ind < List.size(); ind++ )
232 Handle(AIS_InteractiveObject) anAIS=List[ind];
233 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
234 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
240 Changes name of object
241 \param obj - object to be renamed
242 \param name - new name
244 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
245 const QString& name )
247 AIS_ListOfInteractive List;
248 getAISContext()->DisplayedObjects(List);
250 AIS_ListIteratorOfListOfInteractive ite(List);
253 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
255 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
259 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
260 if ( IO->isSame( obj ) )
262 aSh->setName( name.toLatin1().data() );
267 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
269 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
271 // Add code here, if someone create a MODULE_AISObject.
280 \param prs - presentation
282 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
284 // try do downcast object
285 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
286 if ( !anOCCPrs || anOCCPrs->IsNull() )
290 Handle (AIS_InteractiveContext) ic = getAISContext();
292 // get objects to be displayed
293 AIS_ListOfInteractive anAISObjects;
294 anOCCPrs->GetObjects( anAISObjects );
296 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
297 for ( ; aIter.More(); aIter.Next() )
299 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
300 if ( !anAIS.IsNull() )
302 // try to find presentation in the viewer
304 // if the object is already displayed - nothing to do more
305 if(ic->IsDisplayed(anAIS))
307 // Deactivate object if necessary
308 if ( !anOCCPrs->ToActivate() )
309 ic->Deactivate( anAIS );
313 // if object is not displayed and not found in the collector - display it
314 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
316 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
317 double aNewSize = 100, aSize = 100;
318 computeTrihedronSize( aNewSize, aSize );
319 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
322 ic->Display( anAIS, false );
323 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
326 aSh->SetClippable (prs->IsClippable());
327 applyExistingClipPlanesToObject (anAIS);
328 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
329 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
330 if(!aSh->toActivate())
332 ic->Deactivate( aSh );
336 //Register anAIS (if it has an entry) in entry2aisobjects map
337 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
338 if ( !anObj.IsNull() && anObj->hasEntry())
340 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
342 for ( unsigned int ind = 0; ind < List.size(); ind++ )
344 if(List[ind] == anAIS)
352 List.push_back(anAIS);
356 // Set visibility flag
357 // Temporarily commented to avoid awful dependecy on SALOMEDS
358 // TODO: better mechanism of storing display/erse status in a study
359 // should be provided...
360 //Handle(SALOME_InteractiveObject) anObj =
361 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
362 //if ( !anObj.IsNull() && anObj->hasEntry() )
364 // ToolsGUI::SetVisibility( anObj->getEntry(), true, this );
367 // Deactivate object if necessary
368 if ( !anOCCPrs->ToActivate() )
369 ic->Deactivate( anAIS );
378 \param prs - presentation
379 \param forced - removes object from context
381 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
383 // try do downcast object
384 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
385 if ( !anOCCPrs || anOCCPrs->IsNull() )
389 Handle(AIS_InteractiveContext) ic = getAISContext();
391 // get objects to be erased
392 AIS_ListOfInteractive anAISObjects;
393 anOCCPrs->GetObjects( anAISObjects );
395 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
396 for ( ; aIter.More(); aIter.Next() ) {
397 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
398 if ( !anAIS.IsNull() ) {
399 // erase the object from context : move it to collector
400 ic->Erase( anAIS, false );
401 // Set visibility flag if necessary
402 // Temporarily commented to avoid awful dependecy on SALOMEDS
403 // TODO: better mechanism of storing display/erse status in a study
404 // should be provided...
407 // Handle(SALOME_InteractiveObject) anObj =
408 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
409 // if ( !anObj.IsNull() && anObj->hasEntry() )
411 // ToolsGUI::SetVisibility( anObj->getEntry(), true, this );
421 Erase all presentations
422 \param forced - removes all objects from context
424 void SOCC_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
427 Handle(AIS_InteractiveContext) ic = getAISContext();
429 // check if trihedron is displayed
430 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
432 // get objects to be erased (all currently displayed objects)
433 AIS_ListOfInteractive aList;
434 ic->DisplayedObjects( aList );
435 AIS_ListIteratorOfListOfInteractive anIter( aList );
436 for ( ; anIter.More(); anIter.Next() ) {
437 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) ) )
441 Handle(AIS_InteractiveObject) anIO = anIter.Value();
442 ic->Erase( anIO, false );
444 // Set visibility flag if necessary
445 // Temporarily commented to avoid awful dependecy on SALOMEDS
446 // TODO: better mechanism of storing display/erse status in a study
447 // should be provided...
449 // Handle(SALOME_InteractiveObject) anObj =
450 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
452 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
453 // ToolsGUI::SetVisibility( anObj->getEntry(), true, this );
458 SALOME_View::EraseAll( d, forced );
465 Create presentation corresponding to the entry
468 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
470 SOCC_Prs* prs = new SOCC_Prs(entry);
473 if(entry2aisobjects.count(entry)>0)
476 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
478 Handle(AIS_InteractiveContext) ic = getAISContext();
480 for ( unsigned int ind = 0; ind < List.size(); ind++ )
482 Handle(AIS_InteractiveObject) anAIS=List[ind];
483 if(ic->IsDisplayed(anAIS))
485 prs->AddObject( anAIS );
494 Activates selection of sub-shapes
496 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const std::list<int> modes )
498 Handle(AIS_InteractiveContext) ic = getAISContext();
500 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
504 // Open local context if there is no one
505 bool allObjects = thePrs == 0 || thePrs->IsNull();
506 if ( !ic->HasOpenedContext() ) {
507 ic->ClearCurrents( false );
508 ic->OpenLocalContext( Standard_False, Standard_True, Standard_True );
511 AIS_ListOfInteractive anObjs;
512 // Get objects to be activated
514 ic->DisplayedObjects( anObjs );
516 anOCCPrs->GetObjects( anObjs );
518 std::list<int> sel_modes;
519 for ( int i = TopAbs_COMPOUND; i < TopAbs_SHAPE; i++ )
520 if ( std::find(modes.begin(), modes.end(), (int)TopAbs_SHAPE) != modes.end() || std::find(modes.begin(), modes.end(), i) != modes.end())
521 sel_modes.push_back(i);
523 // Activate selection of objects from prs
524 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
525 for ( ; aIter.More(); aIter.Next() ) {
526 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
527 if ( !anAIS.IsNull() )
529 std::list<int>::const_iterator it;
530 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
532 ic->Load( anAIS, -1, false );
533 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
534 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)*it ) );
536 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
538 ic->Load( anAIS, -1, false );
539 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
540 ic->Activate( anAIS, *it );
547 Activates selection of sub-shapes
549 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
551 std::list<int> modes;
552 modes.push_back( theMode );
553 LocalSelection( thePrs, modes );
557 Deactivates selection of sub-shapes
559 void SOCC_Viewer::GlobalSelection( const bool update ) const
561 Handle(AIS_InteractiveContext) ic = getAISContext();
564 ic->CloseAllContexts( false );
566 ic->CurrentViewer()->Redraw();
572 \Collect objects visible in viewer
573 \param theList - visible objects collection
575 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
577 AIS_ListOfInteractive List;
578 getAISContext()->DisplayedObjects(List);
580 AIS_ListIteratorOfListOfInteractive ite(List);
581 for ( ; ite.More(); ite.Next() )
583 Handle(SALOME_InteractiveObject) anObj =
584 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
586 if ( !anObj.IsNull() && anObj->hasEntry() )
587 theList.Append( anObj );
592 Updates current viewer
594 void SOCC_Viewer::Repaint()
596 // onAdjustTrihedron();
597 getViewer3d()->Update();
602 create SOCC_ViewWindow
604 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
606 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
608 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
613 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
615 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
617 return new SOCC_ViewWindow( 0, this);