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"
60 #include <Basics_OCCTVersion.hxx>
62 #include <AIS_TypeOfIso.hxx>
63 #include <Precision.hxx>
65 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
66 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
67 // SALOMEDS::StudyManager - no linkage with SalomeApp.
69 // Temporarily commented to avoid awful dependecy on SALOMEDS
70 // TODO: better mechanism of storing display/erse status in a study
71 // should be provided...
72 //static _PTR(Study) getStudyDS()
74 // SALOMEDSClient_Study* aStudy = NULL;
75 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
77 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
78 // id of its underlying SALOMEDS::Study
79 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
80 // if ( !app ) return _PTR(Study)(aStudy);
81 // SUIT_Study* stud = app->activeStudy();
82 // if ( !stud ) return _PTR(Study)(aStudy);
83 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
84 // get SALOMEDS_Study with this id from StudyMgr
85 // return aMgr->GetStudyByID( id );
90 \param DisplayTrihedron - is trihedron displayed
92 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
93 : OCCViewer_Viewer( DisplayTrihedron )
100 SOCC_Viewer::~SOCC_Viewer()
105 Hilights/unhilights object in viewer
106 \param obj - object to be updated
107 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
108 \param update - update current viewer
110 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
111 bool hilight, bool upd )
113 bool isInLocal = getAISContext()->HasOpenedContext();
114 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
115 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
117 AIS_ListOfInteractive List;
118 getAISContext()->DisplayedObjects(List);
120 AIS_ListIteratorOfListOfInteractive ite(List);
121 for ( ; ite.More(); ite.Next() )
123 Handle(SALOME_InteractiveObject) anObj =
124 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
126 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
129 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
130 // highlight sub-shapes only when local selection is active
133 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
135 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
136 TColStd_IndexedMapOfInteger MapIndex;
137 Sel->GetIndex( IObject, MapIndex );
138 aSh->highlightSubShapes( MapIndex, highlight );
152 \return true if object is in viewer or in collector
153 \param obj - object to be checked
154 \param onlyInViewer - search object only in viewer (so object must be displayed)
156 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
159 AIS_ListOfInteractive List;
160 getAISContext()->DisplayedObjects(List);
162 #if OCC_VERSION_LARGE <= 0x06060000
163 if( !onlyInViewer ) {
164 AIS_ListOfInteractive List1;
165 getAISContext()->ObjectsInCollector(List1);
171 AIS_ListIteratorOfListOfInteractive ite(List);
172 for ( ; ite.More(); ite.Next() )
174 Handle(SALOME_InteractiveObject) anObj =
175 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
177 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
184 \return true if object is displayed in viewer
185 \param obj - object to be checked
187 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
190 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
191 if(it != entry2aisobjects.end())
194 Handle (AIS_InteractiveContext) ic = getAISContext();
195 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
196 for( unsigned int ind = 0; ind < List.size(); ind++ )
198 Handle(AIS_InteractiveObject) anAIS=List[ind];
199 if(ic->IsDisplayed(anAIS))
209 \param obj - object to be updated
210 \param color - new color
211 \param update - update current viewer
213 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
214 const QColor& color, bool update )
216 if(obj.IsNull() || !obj->hasEntry() )
219 if(entry2aisobjects.count(obj->getEntry())>0)
222 Handle (AIS_InteractiveContext) ic = getAISContext();
223 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
224 for( unsigned int ind = 0; ind < List.size(); ind++ )
226 Handle(AIS_InteractiveObject) anAIS=List[ind];
227 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
228 OCCViewer_Viewer::setColor( anAIS, color, update );
234 Changes display mode of object
235 \param obj - object to be processed
236 \param mode - new display mode
237 \param update - update current viewer
239 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
240 int mode, bool update )
242 if(obj.IsNull() || !obj->hasEntry() )
245 if(entry2aisobjects.count(obj->getEntry())>0)
248 Handle (AIS_InteractiveContext) ic = getAISContext();
249 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
250 for( unsigned int ind = 0; ind < List.size(); ind++ )
252 Handle(AIS_InteractiveObject) anAIS=List[ind];
253 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
254 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
260 Changes transparency of object
261 \param obj - object to be processed
262 \param trans - new transparency
263 \param update - update current viewer
265 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
266 float trans, bool update )
268 if(obj.IsNull() || !obj->hasEntry() )
271 if(entry2aisobjects.count(obj->getEntry())>0)
274 Handle (AIS_InteractiveContext) ic = getAISContext();
275 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
276 for( unsigned int ind = 0; ind < List.size(); ind++ )
278 Handle(AIS_InteractiveObject) anAIS=List[ind];
279 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
280 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
286 Changes name of object
287 \param obj - object to be renamed
288 \param name - new name
290 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
291 const QString& name )
293 AIS_ListOfInteractive List;
294 getAISContext()->DisplayedObjects(List);
296 AIS_ListIteratorOfListOfInteractive ite(List);
299 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
301 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
305 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
306 if ( IO->isSame( obj ) )
308 aSh->setName( name.toLatin1().data() );
313 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
315 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
317 // Add code here, if someone create a MODULE_AISObject.
326 \param prs - presentation
328 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
330 // try do downcast object
331 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
332 if ( !anOCCPrs || anOCCPrs->IsNull() )
335 // get SALOMEDS Study
336 // Temporarily commented to avoid awful dependecy on SALOMEDS
337 // TODO: better mechanism of storing display/erse status in a study
338 // should be provided...
339 // _PTR(Study) study(getStudyDS());
342 Handle (AIS_InteractiveContext) ic = getAISContext();
344 // get objects to be displayed
345 AIS_ListOfInteractive anAISObjects;
346 anOCCPrs->GetObjects( anAISObjects );
348 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
349 for ( ; aIter.More(); aIter.Next() )
351 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
352 if ( !anAIS.IsNull() )
354 // try to find presentation in the viewer
356 // if the object is already displayed - nothing to do more
357 if(ic->IsDisplayed(anAIS))
359 // Deactivate object if necessary
360 if ( !anOCCPrs->ToActivate() )
361 ic->Deactivate( anAIS );
365 #if OCC_VERSION_LARGE <= 0x06060000
366 // then try to find presentation in the collector
367 if(ic->IsInCollector(anAIS))
369 ic->DisplayFromCollector( anAIS, false );
370 // Deactivate object if necessary
371 if ( !anOCCPrs->ToActivate() )
372 ic->Deactivate( anAIS );
374 // Set visibility flag
375 // Temporarily commented to avoid awful dependecy on SALOMEDS
376 // TODO: better mechanism of storing display/erse status in a study
377 // should be provided...
378 //Handle(SALOME_InteractiveObject) anObj =
379 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
380 //if ( !anObj.IsNull() && anObj->hasEntry() )
383 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
388 // if object is not displayed and not found in the collector - display it
389 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
391 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
392 double aNewSize = 100, aSize = 100;
393 computeTrihedronSize( aNewSize, aSize );
394 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
397 ic->Display( anAIS, false );
398 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
401 aSh->SetClippable (prs->IsClippable());
402 applyExistingClipPlanesToObject (anAIS);
403 #if OCC_VERSION_LARGE > 0x06050200
404 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
405 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
406 if(!aSh->toActivate())
408 ic->Deactivate( aSh );
413 //Register anAIS (if it has an entry) in entry2aisobjects map
414 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
415 if ( !anObj.IsNull() && anObj->hasEntry())
417 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
419 for ( unsigned int ind = 0; ind < List.size(); ind++ )
421 if(List[ind] == anAIS)
429 List.push_back(anAIS);
433 // Set visibility flag
434 // Temporarily commented to avoid awful dependecy on SALOMEDS
435 // TODO: better mechanism of storing display/erse status in a study
436 // should be provided...
437 //Handle(SALOME_InteractiveObject) anObj =
438 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
439 //if ( !anObj.IsNull() && anObj->hasEntry() )
442 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
445 // Deactivate object if necessary
446 if ( !anOCCPrs->ToActivate() )
447 ic->Deactivate( anAIS );
456 \param prs - presentation
457 \param forced - removes object from context
459 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
461 // try do downcast object
462 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
463 if ( !anOCCPrs || anOCCPrs->IsNull() )
466 // get SALOMEDS Study
467 // Temporarily commented to avoid awful dependecy on SALOMEDS
468 // TODO: better mechanism of storing display/erse status in a study
469 // should be provided...
470 // _PTR(Study) study(getStudyDS());
473 Handle(AIS_InteractiveContext) ic = getAISContext();
475 // get objects to be erased
476 AIS_ListOfInteractive anAISObjects;
477 anOCCPrs->GetObjects( anAISObjects );
479 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
480 for ( ; aIter.More(); aIter.Next() ) {
481 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
482 if ( !anAIS.IsNull() ) {
483 // erase the object from context : move it to collector
484 #if OCC_VERSION_LARGE <= 0x06060000
485 ic->Erase( anAIS, false, forced ? false : true );
487 ic->Erase( anAIS, false );
489 // Set visibility flag if necessary
490 // Temporarily commented to avoid awful dependecy on SALOMEDS
491 // TODO: better mechanism of storing display/erse status in a study
492 // should be provided...
495 // Handle(SALOME_InteractiveObject) anObj =
496 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
497 // if ( !anObj.IsNull() && anObj->hasEntry() )
500 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
510 Erase all presentations
511 \param forced - removes all objects from context
513 void SOCC_Viewer::EraseAll( const bool forced )
515 // get SALOMEDS Study
516 // Temporarily commented to avoid awful dependecy on SALOMEDS
517 // TODO: better mechanism of storing display/erse status in a study
518 // should be provided...
519 // _PTR(Study) study(getStudyDS());
522 Handle(AIS_InteractiveContext) ic = getAISContext();
524 // check if trihedron is displayed
525 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
527 // get objects to be erased (all currently displayed objects)
528 AIS_ListOfInteractive aList;
529 ic->DisplayedObjects( aList );
530 AIS_ListIteratorOfListOfInteractive anIter( aList );
531 for ( ; anIter.More(); anIter.Next() ) {
532 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron )) ||
533 anIter.Value()->DynamicType() == STANDARD_TYPE( OCCViewer_Trihedron ))
537 Handle(AIS_InteractiveObject) anIO = anIter.Value();
538 #if OCC_VERSION_LARGE <= 0x06060000
539 ic->Erase( anIO, false, forced ? false : true );
541 ic->Erase( anIO, false );
544 // Set visibility flag if necessary
545 // Temporarily commented to avoid awful dependecy on SALOMEDS
546 // TODO: better mechanism of storing display/erse status in a study
547 // should be provided...
549 // Handle(SALOME_InteractiveObject) anObj =
550 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
552 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
554 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
564 Create presentation corresponding to the entry
567 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
569 SOCC_Prs* prs = new SOCC_Prs();
572 if(entry2aisobjects.count(entry)>0)
575 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
577 Handle(AIS_InteractiveContext) ic = getAISContext();
579 for ( unsigned int ind = 0; ind < List.size(); ind++ )
581 Handle(AIS_InteractiveObject) anAIS=List[ind];
582 if(ic->IsDisplayed(anAIS)
583 #if OCC_VERSION_LARGE <= 0x06060000
584 || ic->IsInCollector(anAIS)
588 prs->AddObject( anAIS );
597 Activates selection of sub-shapes
599 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
601 Handle(AIS_InteractiveContext) ic = getAISContext();
603 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
607 // Open local context if there is no one
608 bool allObjects = thePrs == 0 || thePrs->IsNull();
609 if ( !ic->HasOpenedContext() ) {
610 ic->ClearCurrents( false );
611 ic->OpenLocalContext( allObjects, true, true );
614 AIS_ListOfInteractive anObjs;
615 // Get objects to be activated
617 ic->DisplayedObjects( anObjs );
619 anOCCPrs->GetObjects( anObjs );
621 // Activate selection of objects from prs
622 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
623 for ( ; aIter.More(); aIter.Next() ) {
624 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
625 if ( !anAIS.IsNull() )
627 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
629 ic->Load( anAIS, -1, false );
630 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
632 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
634 ic->Load( anAIS, -1, false );
635 ic->Activate( anAIS, theMode );
642 Deactivates selection of sub-shapes
644 void SOCC_Viewer::GlobalSelection( const bool update ) const
646 Handle(AIS_InteractiveContext) ic = getAISContext();
649 ic->CloseAllContexts( false );
651 ic->CurrentViewer()->Redraw();
657 \Collect objects visible in viewer
658 \param theList - visible objects collection
660 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
662 AIS_ListOfInteractive List;
663 getAISContext()->DisplayedObjects(List);
665 AIS_ListIteratorOfListOfInteractive ite(List);
666 for ( ; ite.More(); ite.Next() )
668 Handle(SALOME_InteractiveObject) anObj =
669 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
671 if ( !anObj.IsNull() && anObj->hasEntry() )
672 theList.Append( anObj );
677 Updates current viewer
679 void SOCC_Viewer::Repaint()
681 // onAdjustTrihedron();
682 getViewer3d()->Update();
687 create SOCC_ViewWindow
689 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
691 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
693 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
698 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
700 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
702 return new SOCC_ViewWindow( 0, this);