1 // Copyright (C) 2007-2019 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 #if OCC_VERSION_LARGE <= 0x07030000
78 bool isInLocal = getAISContext()->HasOpenedContext();
80 //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
81 //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
83 AIS_ListOfInteractive List;
84 getAISContext()->DisplayedObjects(List);
86 AIS_ListIteratorOfListOfInteractive ite(List);
87 for ( ; ite.More(); ite.Next() )
89 Handle(SALOME_InteractiveObject) anObj =
90 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
92 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
94 #if OCC_VERSION_LARGE <= 0x07030000
97 OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
98 // highlight sub-shapes only when local selection is active
99 #if OCC_VERSION_LARGE <= 0x07030000
102 /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
104 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
105 TColStd_IndexedMapOfInteger MapIndex;
106 Sel->GetIndex( IObject, MapIndex );
107 aSh->highlightSubShapes( MapIndex, highlight );
122 \return true if object is in viewer or in collector
123 \param obj - object to be checked
124 \param onlyInViewer - search object only in viewer (so object must be displayed)
126 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
129 AIS_ListOfInteractive List;
130 getAISContext()->DisplayedObjects(List);
131 AIS_ListIteratorOfListOfInteractive ite(List);
132 for ( ; ite.More(); ite.Next() )
134 Handle(SALOME_InteractiveObject) anObj =
135 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
137 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
144 \return true if object is displayed in viewer
145 \param obj - object to be checked
147 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
150 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator it=entry2aisobjects.find(obj->getEntry());
151 if(it != entry2aisobjects.end())
154 Handle (AIS_InteractiveContext) ic = getAISContext();
155 std::vector<Handle(AIS_InteractiveObject)>& List = it->second;
156 for( unsigned int ind = 0; ind < List.size(); ind++ )
158 Handle(AIS_InteractiveObject) anAIS=List[ind];
159 if(ic->IsDisplayed(anAIS))
169 \param obj - object to be updated
170 \param color - new color
171 \param update - update current viewer
173 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
174 const QColor& color, bool update )
176 if(obj.IsNull() || !obj->hasEntry() )
179 if(entry2aisobjects.count(obj->getEntry())>0)
182 Handle (AIS_InteractiveContext) ic = getAISContext();
183 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
184 for( unsigned int ind = 0; ind < List.size(); ind++ )
186 Handle(AIS_InteractiveObject) anAIS=List[ind];
187 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
188 OCCViewer_Viewer::setColor( anAIS, color, update );
194 Changes display mode of object
195 \param obj - object to be processed
196 \param mode - new display mode
197 \param update - update current viewer
199 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
200 int mode, bool update )
202 if(obj.IsNull() || !obj->hasEntry() )
205 if(entry2aisobjects.count(obj->getEntry())>0)
208 Handle (AIS_InteractiveContext) ic = getAISContext();
209 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
210 for( unsigned int ind = 0; ind < List.size(); ind++ )
212 Handle(AIS_InteractiveObject) anAIS=List[ind];
213 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
214 OCCViewer_Viewer::switchRepresentation( anAIS, mode, update );
220 Changes transparency of object
221 \param obj - object to be processed
222 \param trans - new transparency
223 \param update - update current viewer
225 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
226 float trans, bool update )
228 if(obj.IsNull() || !obj->hasEntry() )
231 if(entry2aisobjects.count(obj->getEntry())>0)
234 Handle (AIS_InteractiveContext) ic = getAISContext();
235 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[obj->getEntry()];
236 for( unsigned int ind = 0; ind < List.size(); ind++ )
238 Handle(AIS_InteractiveObject) anAIS=List[ind];
239 if( !anAIS.IsNull() && ic->IsDisplayed(anAIS))
240 OCCViewer_Viewer::setTransparency( anAIS, trans, update );
246 Changes name of object
247 \param obj - object to be renamed
248 \param name - new name
250 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
251 const QString& name )
253 AIS_ListOfInteractive List;
254 getAISContext()->DisplayedObjects(List);
256 AIS_ListIteratorOfListOfInteractive ite(List);
259 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
261 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
265 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
266 if ( IO->isSame( obj ) )
268 aSh->setName( name.toUtf8().data() );
273 else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
275 Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
277 // Add code here, if someone create a MODULE_AISObject.
286 \param prs - presentation
288 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
290 // try do downcast object
291 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
292 if ( !anOCCPrs || anOCCPrs->IsNull() )
296 Handle (AIS_InteractiveContext) ic = getAISContext();
298 // get objects to be displayed
299 AIS_ListOfInteractive anAISObjects;
300 anOCCPrs->GetObjects( anAISObjects );
302 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
303 for ( ; aIter.More(); aIter.Next() )
305 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
306 if ( !anAIS.IsNull() )
308 // try to find presentation in the viewer
310 // if the object is already displayed - nothing to do more
311 if(ic->IsDisplayed(anAIS))
313 // Deactivate object if necessary
314 if ( !anOCCPrs->ToActivate() )
315 ic->Deactivate( anAIS );
319 // if object is not displayed and not found in the collector - display it
320 if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
322 Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
323 double aNewSize = 100, aSize = 100;
324 computeTrihedronSize( aNewSize, aSize );
325 aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
328 ic->Display( anAIS, false );
329 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast (anAIS);
332 aSh->SetClippable (prs->IsClippable());
333 applyExistingClipPlanesToObject (anAIS);
334 bool top = (aSh->isTopLevel() && aSh->switchTopLevel());
335 ic->SetZLayer( aSh, top ? getTopLayerId() : 0 );
336 if(!aSh->toActivate())
338 ic->Deactivate( aSh );
342 //Register anAIS (if it has an entry) in entry2aisobjects map
343 Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
344 if ( !anObj.IsNull() && anObj->hasEntry())
346 std::vector<Handle(AIS_InteractiveObject)>& List = entry2aisobjects[anObj->getEntry()];
348 for ( unsigned int ind = 0; ind < List.size(); ind++ )
350 if(List[ind] == anAIS)
358 List.push_back(anAIS);
362 // Set visibility flag
363 // Temporarily commented to avoid awful dependecy on SALOMEDS
364 // TODO: better mechanism of storing display/erse status in a study
365 // should be provided...
366 //Handle(SALOME_InteractiveObject) anObj =
367 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
368 //if ( !anObj.IsNull() && anObj->hasEntry() )
370 // ToolsGUI::SetVisibility( anObj->getEntry(), true, this );
373 // Deactivate object if necessary
374 if ( !anOCCPrs->ToActivate() )
375 ic->Deactivate( anAIS );
384 \param prs - presentation
385 \param forced - removes object from context
387 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
389 // try do downcast object
390 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
391 if ( !anOCCPrs || anOCCPrs->IsNull() )
395 Handle(AIS_InteractiveContext) ic = getAISContext();
397 // get objects to be erased
398 AIS_ListOfInteractive anAISObjects;
399 anOCCPrs->GetObjects( anAISObjects );
401 AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
402 for ( ; aIter.More(); aIter.Next() ) {
403 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
404 if ( !anAIS.IsNull() ) {
405 // erase the object from context : move it to collector
406 ic->Erase( anAIS, false );
407 // Set visibility flag if necessary
408 // Temporarily commented to avoid awful dependecy on SALOMEDS
409 // TODO: better mechanism of storing display/erse status in a study
410 // should be provided...
413 // Handle(SALOME_InteractiveObject) anObj =
414 // Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
415 // if ( !anObj.IsNull() && anObj->hasEntry() )
417 // ToolsGUI::SetVisibility( anObj->getEntry(), true, this );
427 Erase all presentations
428 \param forced - removes all objects from context
430 void SOCC_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
433 Handle(AIS_InteractiveContext) ic = getAISContext();
435 // check if trihedron is displayed
436 Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
438 // get objects to be erased (all currently displayed objects)
439 AIS_ListOfInteractive aList;
440 ic->DisplayedObjects( aList );
441 AIS_ListIteratorOfListOfInteractive anIter( aList );
442 for ( ; anIter.More(); anIter.Next() ) {
443 if ( (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) ) )
447 Handle(AIS_InteractiveObject) anIO = anIter.Value();
448 ic->Erase( anIO, 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...
455 // Handle(SALOME_InteractiveObject) anObj =
456 // Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
458 // if ( !anObj.IsNull() && anObj->hasEntry() ) {
459 // ToolsGUI::SetVisibility( anObj->getEntry(), true, this );
464 SALOME_View::EraseAll( d, forced );
471 Create presentation corresponding to the entry
474 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
476 SOCC_Prs* prs = new SOCC_Prs(entry);
479 if(entry2aisobjects.count(entry)>0)
482 std::vector<Handle(AIS_InteractiveObject)> List = entry2aisobjects[entry];
484 Handle(AIS_InteractiveContext) ic = getAISContext();
486 for ( unsigned int ind = 0; ind < List.size(); ind++ )
488 Handle(AIS_InteractiveObject) anAIS=List[ind];
489 if(ic->IsDisplayed(anAIS))
491 prs->AddObject( anAIS );
500 Activates selection of sub-shapes
502 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const std::list<int> modes )
504 Handle(AIS_InteractiveContext) ic = getAISContext();
506 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
510 // Open local context if there is no one
511 bool allObjects = thePrs == 0 || thePrs->IsNull();
512 #if OCC_VERSION_LARGE <= 0x07030000
513 if ( !ic->HasOpenedContext() ) {
514 ic->ClearCurrents( false );
515 ic->OpenLocalContext( Standard_False, Standard_True, Standard_True );
521 AIS_ListOfInteractive anObjs;
522 // Get objects to be activated
524 ic->DisplayedObjects( anObjs );
526 anOCCPrs->GetObjects( anObjs );
528 std::list<int> sel_modes;
529 for ( int i = TopAbs_COMPOUND; i < TopAbs_SHAPE; i++ )
530 if ( std::find(modes.begin(), modes.end(), (int)TopAbs_SHAPE) != modes.end() || std::find(modes.begin(), modes.end(), i) != modes.end())
531 sel_modes.push_back(i);
533 // Activate selection of objects from prs
534 AIS_ListIteratorOfListOfInteractive aIter( anObjs );
535 for ( ; aIter.More(); aIter.Next() ) {
536 Handle(AIS_InteractiveObject) anAIS = aIter.Value();
537 if ( !anAIS.IsNull() )
539 std::list<int>::const_iterator it;
540 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
542 ic->Load( anAIS, -1, false );
543 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
544 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)*it ) );
546 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
548 ic->Load( anAIS, -1, false );
549 for( it = sel_modes.begin(); it != sel_modes.end(); ++it )
550 ic->Activate( anAIS, *it );
557 Activates selection of sub-shapes
559 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
561 std::list<int> modes;
562 modes.push_back( theMode );
563 LocalSelection( thePrs, modes );
567 Deactivates selection of sub-shapes
569 void SOCC_Viewer::GlobalSelection( const bool update ) const
571 Handle(AIS_InteractiveContext) ic = getAISContext();
574 #if OCC_VERSION_LARGE <= 0x07030000
575 ic->CloseAllContexts( false );
581 ic->CurrentViewer()->Redraw();
587 \Collect objects visible in viewer
588 \param theList - visible objects collection
590 void SOCC_Viewer::GetVisible( SALOME_ListIO& theList )
592 AIS_ListOfInteractive List;
593 getAISContext()->DisplayedObjects(List);
595 AIS_ListIteratorOfListOfInteractive ite(List);
596 for ( ; ite.More(); ite.Next() )
598 Handle(SALOME_InteractiveObject) anObj =
599 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
601 if ( !anObj.IsNull() && anObj->hasEntry() )
602 theList.Append( anObj );
607 Updates current viewer
609 void SOCC_Viewer::Repaint()
611 // onAdjustTrihedron();
612 getViewer3d()->Update();
617 create SOCC_ViewWindow
619 /*SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
621 SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
623 initView( view->getView(OCCViewer_ViewFrame::MAIN_VIEW) );
628 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
630 OCCViewer_ViewWindow* SOCC_Viewer::createSubWindow()
632 return new SOCC_ViewWindow( 0, this);