1 // Copyright (C) 2007-2013 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.
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( !onlyInViewer ) {
163 AIS_ListOfInteractive List1;
164 getAISContext()->ObjectsInCollector(List1);
168 AIS_ListIteratorOfListOfInteractive ite(List);
169 for ( ; ite.More(); ite.Next() )
171 Handle(SALOME_InteractiveObject) anObj =
172 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
174 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
181 \return true if object is displayed in viewer
182 \param obj - object to be checked
184 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
187 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
188 if(it != entry2aisobjects.end())
191 Handle (AIS_InteractiveContext) ic = getAISContext();
192 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
193 for( unsigned int ind = 0; ind < List.size(); ind++ )
195 Handle(AIS_InteractiveObject) anAIS=List[ind];
196 if(ic->IsDisplayed(anAIS))
206 \param obj - object to be updated
207 \param color - new color
208 \param update - update current viewer
210 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
211 const QColor& color, bool update )
213 if(obj.IsNull() || !obj->hasEntry() )
216 if(entry2aisobjects.count(obj->getEntry())>0)
219 Handle (AIS_InteractiveContext) ic = getAISContext();
220 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
221 for( unsigned int ind = 0; ind < List.size(); ind++ )
223 Handle(AIS_InteractiveObject) anAIS=List[ind];
224 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
225 OCCViewer_Viewer::setColor( anAIS, color, update );
231 Changes display mode of object
232 \param obj - object to be processed
233 \param mode - new display mode
234 \param update - update current viewer
236 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
237 int mode, bool update )
239 if(obj.IsNull() || !obj->hasEntry() )
242 if(entry2aisobjects.count(obj->getEntry())>0)
245 Handle (AIS_InteractiveContext) ic = getAISContext();
246 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
247 for( unsigned int ind = 0; ind < List.size(); ind++ )
249 Handle(AIS_InteractiveObject) anAIS=List[ind];
250 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
251 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
257 Changes transparency of object
258 \param obj - object to be processed
259 \param trans - new transparency
260 \param update - update current viewer
262 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
263 float trans, bool update )
265 if(obj.IsNull() || !obj->hasEntry() )
268 if(entry2aisobjects.count(obj->getEntry())>0)
271 Handle (AIS_InteractiveContext) ic = getAISContext();
272 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
273 for( unsigned int ind = 0; ind < List.size(); ind++ )
275 Handle(AIS_InteractiveObject) anAIS=List[ind];
276 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
277 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
283 Changes name of object
284 \param obj - object to be renamed
285 \param name - new name
287 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
288 const QString& name )
290 AIS_ListOfInteractive List;
291 getAISContext()->DisplayedObjects(List);
293 AIS_ListIteratorOfListOfInteractive ite(List);
296 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
298 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
302 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
303 if ( IO->isSame( obj ) )
305 aSh->setName( name.toLatin1().data() );
310 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
312 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
314 // Add code here, if someone create a MODULE_AISObject.
323 \param prs - presentation
325 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
327 // try do downcast object
328 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
329 if ( !anOCCPrs || anOCCPrs->IsNull() )
332 // get SALOMEDS Study
333 // Temporarily commented to avoid awful dependecy on SALOMEDS
334 // TODO: better mechanism of storing display/erse status in a study
335 // should be provided...
336 // _PTR(Study) study(getStudyDS());
339 Handle (AIS_InteractiveContext) ic = getAISContext();
341 // get objects to be displayed
342 AIS_ListOfInteractive anAISObjects;
343 anOCCPrs->GetObjects( anAISObjects );
345 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
346 for ( ; aIter.More(); aIter.Next() )
348 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
349 if ( !anAIS.IsNull() )
351 // try to find presentation in the viewer
353 // if the object is already displayed - nothing to do more
354 if(ic->IsDisplayed(anAIS))
356 // Deactivate object if necessary
357 if ( !anOCCPrs->ToActivate() )
358 ic->Deactivate( anAIS );
362 // then try to find presentation in the collector
363 if(ic->IsInCollector(anAIS))
365 ic->DisplayFromCollector( anAIS, false );
366 // Deactivate object if necessary
367 if ( !anOCCPrs->ToActivate() )
368 ic->Deactivate( anAIS );
370 // Set visibility flag
371 // Temporarily commented to avoid awful dependecy on SALOMEDS
372 // TODO: better mechanism of storing display/erse status in a study
373 // should be provided...
374 //Handle(SALOME_InteractiveObject) anObj =
375 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
376 //if ( !anObj.IsNull() && anObj->hasEntry() )
379 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
384 // if object is not displayed and not found in the collector - display it
385 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
387 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
388 double aNewSize = 100, aSize = 100;
389 computeTrihedronSize( aNewSize, aSize );
390 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
393 ic->Display( anAIS, false );
395 #if OCC_VERSION_LARGE > 0x06050200
396 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(anAIS);
397 if ( !aSh.IsNull() ) {
398 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
399 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
400 if(!aSh->toActivate()) {
401 ic->Deactivate( aSh );
405 //Register anAIS (if it has an entry) in entry2aisobjects map
406 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
407 if ( !anObj.IsNull() && anObj->hasEntry())
409 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
411 for ( unsigned int ind = 0; ind < List.size(); ind++ )
413 if(List[ind] == anAIS)
421 List.push_back(anAIS);
425 // Set visibility flag
426 // Temporarily commented to avoid awful dependecy on SALOMEDS
427 // TODO: better mechanism of storing display/erse status in a study
428 // should be provided...
429 //Handle(SALOME_InteractiveObject) anObj =
430 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
431 //if ( !anObj.IsNull() && anObj->hasEntry() )
434 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
437 // Deactivate object if necessary
438 if ( !anOCCPrs->ToActivate() )
439 ic->Deactivate( anAIS );
448 \param prs - presentation
449 \param forced - removes object from context
451 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
453 // try do downcast object
454 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
455 if ( !anOCCPrs || anOCCPrs->IsNull() )
458 // get SALOMEDS Study
459 // Temporarily commented to avoid awful dependecy on SALOMEDS
460 // TODO: better mechanism of storing display/erse status in a study
461 // should be provided...
462 // _PTR(Study) study(getStudyDS());
465 Handle(AIS_InteractiveContext) ic = getAISContext();
467 // get objects to be erased
468 AIS_ListOfInteractive anAISObjects;
469 anOCCPrs->GetObjects( anAISObjects );
471 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
472 for ( ; aIter.More(); aIter.Next() ) {
473 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
474 if ( !anAIS.IsNull() ) {
475 // erase the object from context : move it to collector
476 ic->Erase( anAIS, false, forced ? false : true );
478 // Set visibility flag if necessary
479 // Temporarily commented to avoid awful dependecy on SALOMEDS
480 // TODO: better mechanism of storing display/erse status in a study
481 // should be provided...
484 // Handle(SALOME_InteractiveObject) anObj =
485 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
486 // if ( !anObj.IsNull() && anObj->hasEntry() )
489 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
499 Erase all presentations
500 \param forced - removes all objects from context
502 void SOCC_Viewer::EraseAll( const bool forced )
504 // get SALOMEDS Study
505 // Temporarily commented to avoid awful dependecy on SALOMEDS
506 // TODO: better mechanism of storing display/erse status in a study
507 // should be provided...
508 // _PTR(Study) study(getStudyDS());
511 Handle(AIS_InteractiveContext) ic = getAISContext();
513 // check if trihedron is displayed
514 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
516 // get objects to be erased (all currently displayed objects)
517 AIS_ListOfInteractive aList;
518 ic->DisplayedObjects( aList );
519 AIS_ListIteratorOfListOfInteractive anIter( aList );
520 for ( ; anIter.More(); anIter.Next() ) {
521 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron )) ||
522 anIter.Value()->DynamicType() == STANDARD_TYPE( OCCViewer_Trihedron ))
526 Handle(AIS_InteractiveObject) anIO = anIter.Value();
527 ic->Erase( anIO, false, forced ? false : true );
529 // Set visibility flag if necessary
530 // Temporarily commented to avoid awful dependecy on SALOMEDS
531 // TODO: better mechanism of storing display/erse status in a study
532 // should be provided...
534 // Handle(SALOME_InteractiveObject) anObj =
535 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
537 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
539 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
549 Create presentation corresponding to the entry
552 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
554 SOCC_Prs* prs = new SOCC_Prs();
557 if(entry2aisobjects.count(entry)>0)
560 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
562 Handle(AIS_InteractiveContext) ic = getAISContext();
564 for ( unsigned int ind = 0; ind < List.size(); ind++ )
566 Handle(AIS_InteractiveObject) anAIS=List[ind];
567 if(ic->IsDisplayed(anAIS)||ic->IsInCollector(anAIS))
569 prs->AddObject( anAIS );
578 Activates selection of sub-shapes
580 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
582 Handle(AIS_InteractiveContext) ic = getAISContext();
584 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
588 // Open local context if there is no one
589 bool allObjects = thePrs == 0 || thePrs->IsNull();
590 if ( !ic->HasOpenedContext() ) {
591 ic->ClearCurrents( false );
592 ic->OpenLocalContext( allObjects, true, true );
595 AIS_ListOfInteractive anObjs;
596 // Get objects to be activated
598 ic->DisplayedObjects( anObjs );
600 anOCCPrs->GetObjects( anObjs );
602 // Activate selection of objects from prs
603 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
604 for ( ; aIter.More(); aIter.Next() ) {
605 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
606 if ( !anAIS.IsNull() )
608 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
610 ic->Load( anAIS, -1, false );
611 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
613 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
615 ic->Load( anAIS, -1, false );
616 ic->Activate( anAIS, theMode );
623 Deactivates selection of sub-shapes
625 void SOCC_Viewer::GlobalSelection( const bool update ) const
627 Handle(AIS_InteractiveContext) ic = getAISContext();
630 ic->CloseAllContexts( false );
632 ic->CurrentViewer()->Redraw();
638 \Collect objects visible in viewer
639 \param theList - visible objects collection
641 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
643 AIS_ListOfInteractive List;
644 getAISContext()->DisplayedObjects(List);
646 AIS_ListIteratorOfListOfInteractive ite(List);
647 for ( ; ite.More(); ite.Next() )
649 Handle(SALOME_InteractiveObject) anObj =
650 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
652 if ( !anObj.IsNull() && anObj->hasEntry() )
653 theList.Append( anObj );
658 Updates current viewer
660 void SOCC_Viewer::Repaint()
662 // onAdjustTrihedron();
663 getViewer3d()->Update();
668 create SOCC_ViewWindow
670 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
672 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
674 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
679 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
681 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
683 return new SOCC_ViewWindow( 0, this);