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);
161 AIS_ListIteratorOfListOfInteractive ite(List);
162 for ( ; ite.More(); ite.Next() )
164 Handle(SALOME_InteractiveObject) anObj =
165 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
167 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
174 \return true if object is displayed in viewer
175 \param obj - object to be checked
177 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
180 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
181 if(it != entry2aisobjects.end())
184 Handle (AIS_InteractiveContext) ic = getAISContext();
185 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
186 for( unsigned int ind = 0; ind < List.size(); ind++ )
188 Handle(AIS_InteractiveObject) anAIS=List[ind];
189 if(ic->IsDisplayed(anAIS))
199 \param obj - object to be updated
200 \param color - new color
201 \param update - update current viewer
203 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
204 const QColor& color, bool update )
206 if(obj.IsNull() || !obj->hasEntry() )
209 if(entry2aisobjects.count(obj->getEntry())>0)
212 Handle (AIS_InteractiveContext) ic = getAISContext();
213 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
214 for( unsigned int ind = 0; ind < List.size(); ind++ )
216 Handle(AIS_InteractiveObject) anAIS=List[ind];
217 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
218 OCCViewer_Viewer::setColor( anAIS, color, update );
224 Changes display mode of object
225 \param obj - object to be processed
226 \param mode - new display mode
227 \param update - update current viewer
229 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
230 int mode, bool update )
232 if(obj.IsNull() || !obj->hasEntry() )
235 if(entry2aisobjects.count(obj->getEntry())>0)
238 Handle (AIS_InteractiveContext) ic = getAISContext();
239 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
240 for( unsigned int ind = 0; ind < List.size(); ind++ )
242 Handle(AIS_InteractiveObject) anAIS=List[ind];
243 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
244 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
250 Changes transparency of object
251 \param obj - object to be processed
252 \param trans - new transparency
253 \param update - update current viewer
255 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
256 float trans, bool update )
258 if(obj.IsNull() || !obj->hasEntry() )
261 if(entry2aisobjects.count(obj->getEntry())>0)
264 Handle (AIS_InteractiveContext) ic = getAISContext();
265 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
266 for( unsigned int ind = 0; ind < List.size(); ind++ )
268 Handle(AIS_InteractiveObject) anAIS=List[ind];
269 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
270 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
276 Changes name of object
277 \param obj - object to be renamed
278 \param name - new name
280 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
281 const QString& name )
283 AIS_ListOfInteractive List;
284 getAISContext()->DisplayedObjects(List);
286 AIS_ListIteratorOfListOfInteractive ite(List);
289 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
291 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
295 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
296 if ( IO->isSame( obj ) )
298 aSh->setName( name.toLatin1().data() );
303 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
305 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
307 // Add code here, if someone create a MODULE_AISObject.
316 \param prs - presentation
318 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
320 // try do downcast object
321 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
322 if ( !anOCCPrs || anOCCPrs->IsNull() )
325 // get SALOMEDS Study
326 // Temporarily commented to avoid awful dependecy on SALOMEDS
327 // TODO: better mechanism of storing display/erse status in a study
328 // should be provided...
329 // _PTR(Study) study(getStudyDS());
332 Handle (AIS_InteractiveContext) ic = getAISContext();
334 // get objects to be displayed
335 AIS_ListOfInteractive anAISObjects;
336 anOCCPrs->GetObjects( anAISObjects );
338 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
339 for ( ; aIter.More(); aIter.Next() )
341 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
342 if ( !anAIS.IsNull() )
344 // try to find presentation in the viewer
346 // if the object is already displayed - nothing to do more
347 if(ic->IsDisplayed(anAIS))
349 // Deactivate object if necessary
350 if ( !anOCCPrs->ToActivate() )
351 ic->Deactivate( anAIS );
355 // if object is not displayed and not found in the collector - display it
356 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
358 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
359 double aNewSize = 100, aSize = 100;
360 computeTrihedronSize( aNewSize, aSize );
361 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
364 ic->Display( anAIS, false );
365 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
368 aSh->SetClippable (prs->IsClippable());
369 applyExistingClipPlanesToObject (anAIS);
370 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
371 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
372 if(!aSh->toActivate())
374 ic->Deactivate( aSh );
378 //Register anAIS (if it has an entry) in entry2aisobjects map
379 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
380 if ( !anObj.IsNull() && anObj->hasEntry())
382 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
384 for ( unsigned int ind = 0; ind < List.size(); ind++ )
386 if(List[ind] == anAIS)
394 List.push_back(anAIS);
398 // Set visibility flag
399 // Temporarily commented to avoid awful dependecy on SALOMEDS
400 // TODO: better mechanism of storing display/erse status in a study
401 // should be provided...
402 //Handle(SALOME_InteractiveObject) anObj =
403 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
404 //if ( !anObj.IsNull() && anObj->hasEntry() )
407 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
410 // Deactivate object if necessary
411 if ( !anOCCPrs->ToActivate() )
412 ic->Deactivate( anAIS );
421 \param prs - presentation
422 \param forced - removes object from context
424 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
426 // try do downcast object
427 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
428 if ( !anOCCPrs || anOCCPrs->IsNull() )
431 // get SALOMEDS Study
432 // Temporarily commented to avoid awful dependecy on SALOMEDS
433 // TODO: better mechanism of storing display/erse status in a study
434 // should be provided...
435 // _PTR(Study) study(getStudyDS());
438 Handle(AIS_InteractiveContext) ic = getAISContext();
440 // get objects to be erased
441 AIS_ListOfInteractive anAISObjects;
442 anOCCPrs->GetObjects( anAISObjects );
444 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
445 for ( ; aIter.More(); aIter.Next() ) {
446 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
447 if ( !anAIS.IsNull() ) {
448 // erase the object from context : move it to collector
449 ic->Erase( anAIS, false );
450 // Set visibility flag if necessary
451 // Temporarily commented to avoid awful dependecy on SALOMEDS
452 // TODO: better mechanism of storing display/erse status in a study
453 // should be provided...
456 // Handle(SALOME_InteractiveObject) anObj =
457 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
458 // if ( !anObj.IsNull() && anObj->hasEntry() )
461 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
471 Erase all presentations
472 \param forced - removes all objects from context
474 void SOCC_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
476 // get SALOMEDS Study
477 // Temporarily commented to avoid awful dependecy on SALOMEDS
478 // TODO: better mechanism of storing display/erse status in a study
479 // should be provided...
480 // _PTR(Study) study(getStudyDS());
483 Handle(AIS_InteractiveContext) ic = getAISContext();
485 // check if trihedron is displayed
486 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
488 // get objects to be erased (all currently displayed objects)
489 AIS_ListOfInteractive aList;
490 ic->DisplayedObjects( aList );
491 AIS_ListIteratorOfListOfInteractive anIter( aList );
492 for ( ; anIter.More(); anIter.Next() ) {
493 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron )) ||
494 anIter.Value()->DynamicType() == STANDARD_TYPE( OCCViewer_Trihedron ))
498 Handle(AIS_InteractiveObject) anIO = anIter.Value();
499 ic->Erase( anIO, false );
501 // Set visibility flag if necessary
502 // Temporarily commented to avoid awful dependecy on SALOMEDS
503 // TODO: better mechanism of storing display/erse status in a study
504 // should be provided...
506 // Handle(SALOME_InteractiveObject) anObj =
507 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
509 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
511 // ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
516 SALOME_View::EraseAll( d, forced );
523 Create presentation corresponding to the entry
526 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
528 SOCC_Prs* prs = new SOCC_Prs(entry);
531 if(entry2aisobjects.count(entry)>0)
534 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
536 Handle(AIS_InteractiveContext) ic = getAISContext();
538 for ( unsigned int ind = 0; ind < List.size(); ind++ )
540 Handle(AIS_InteractiveObject) anAIS=List[ind];
541 if(ic->IsDisplayed(anAIS))
543 prs->AddObject( anAIS );
552 Activates selection of sub-shapes
554 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
556 Handle(AIS_InteractiveContext) ic = getAISContext();
558 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
562 // Open local context if there is no one
563 bool allObjects = thePrs == 0 || thePrs->IsNull();
564 if ( !ic->HasOpenedContext() ) {
565 ic->ClearCurrents( false );
566 ic->OpenLocalContext( allObjects, true, true );
569 AIS_ListOfInteractive anObjs;
570 // Get objects to be activated
572 ic->DisplayedObjects( anObjs );
574 anOCCPrs->GetObjects( anObjs );
576 // Activate selection of objects from prs
577 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
578 for ( ; aIter.More(); aIter.Next() ) {
579 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
580 if ( !anAIS.IsNull() )
582 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
584 ic->Load( anAIS, -1, false );
585 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
587 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
589 ic->Load( anAIS, -1, false );
590 ic->Activate( anAIS, theMode );
597 Deactivates selection of sub-shapes
599 void SOCC_Viewer::GlobalSelection( const bool update ) const
601 Handle(AIS_InteractiveContext) ic = getAISContext();
604 ic->CloseAllContexts( false );
606 ic->CurrentViewer()->Redraw();
612 \Collect objects visible in viewer
613 \param theList - visible objects collection
615 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
617 AIS_ListOfInteractive List;
618 getAISContext()->DisplayedObjects(List);
620 AIS_ListIteratorOfListOfInteractive ite(List);
621 for ( ; ite.More(); ite.Next() )
623 Handle(SALOME_InteractiveObject) anObj =
624 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
626 if ( !anObj.IsNull() && anObj->hasEntry() )
627 theList.Append( anObj );
632 Updates current viewer
634 void SOCC_Viewer::Repaint()
636 // onAdjustTrihedron();
637 getViewer3d()->Update();
642 create SOCC_ViewWindow
644 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
646 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
648 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
653 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
655 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
657 return new SOCC_ViewWindow( 0, this);