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 // File : GEOMGUI_OCCSelector.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
26 #include "GEOMGUI_OCCSelector.h"
27 #include "GEOM_Annotation.hxx"
29 #include <Basics_OCCTVersion.hxx>
31 #include <LightApp_DataSubOwner.h>
33 #include <OCCViewer_ViewModel.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SalomeApp_Study.h>
38 #include <SALOME_InteractiveObject.hxx>
40 #include <StdSelect_BRepOwner.hxx>
41 #include <SelectMgr_Selection.hxx>
42 #include <SelectMgr_EntityOwner.hxx>
43 #include <SelectMgr_IndexedMapOfOwner.hxx>
44 #include <SelectBasics_SensitiveEntity.hxx>
46 #include <AIS_Shape.hxx>
47 #include <AIS_ListOfInteractive.hxx>
48 #include <AIS_ListIteratorOfListOfInteractive.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TColStd_ListIteratorOfListOfInteger.hxx>
52 #include <TColStd_IndexedMapOfInteger.hxx>
53 #include <NCollection_DataMap.hxx>
56 //================================================================
57 // Function : GEOMGUI_OCCSelector
59 //================================================================
60 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
61 : LightApp_OCCSelector( viewer, mgr )
65 //================================================================
66 // Function : ~GEOMGUI_OCCSelector
68 //================================================================
69 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
73 //================================================================
74 // Function : getSelection
76 //================================================================
77 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
79 OCCViewer_Viewer* vw = viewer();
83 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
85 if (ic->HasOpenedContext())
87 TopoDS_Shape curBigShape;
88 TopTools_IndexedMapOfShape subShapes;
90 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
92 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
96 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
98 QString entryStr = entry(io);
101 if (anOwner->ComesFromDecomposition()) // == Local Selection
103 TopoDS_Shape subShape = anOwner->Shape();
104 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
105 if (!aisShape.IsNull())
107 TopoDS_Shape bigShape = aisShape->Shape();
109 if (!bigShape.IsEqual(curBigShape))
111 curBigShape = bigShape;
113 TopExp::MapShapes(bigShape, subShapes);
115 index = subShapes.FindIndex(subShape);
119 if (!entryStr.isEmpty())
121 Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner());
122 LightApp_DataOwner* owner;
123 if (index > -1) // Local Selection
124 owner = new LightApp_DataSubOwner (entryStr, index);
125 else if ( !anIO.IsNull() ) // Global Selection
126 owner = new LightApp_DataOwner( anIO );
128 aList.append(SUIT_DataOwnerPtr(owner));
134 for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
136 Handle(AIS_InteractiveObject) io = ic->Current();
138 QString entryStr = entry( io );
140 if ( !entryStr.isEmpty() )
142 Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner());
143 if ( !anIO.IsNull() ) {
144 LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
145 aList.append( SUIT_DataOwnerPtr( owner ) );
151 // add externally selected objects
152 SUIT_DataOwnerPtrList::const_iterator anExtIter;
153 for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
155 aList.append(*anExtIter);
159 //================================================================
160 // Function : getEntityOwners
162 //================================================================
163 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
164 const Handle(AIS_InteractiveContext)& theIC,
165 SelectMgr_IndexedMapOfOwner& theMap )
167 if ( theObj.IsNull() || theIC.IsNull() )
170 TColStd_ListOfInteger modes;
171 theIC->ActivatedModes( theObj, modes );
173 TColStd_ListIteratorOfListOfInteger itr( modes );
174 for (; itr.More(); itr.Next() ) {
176 if ( !theObj->HasSelection( m ) )
179 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
181 for ( sel->Init(); sel->More(); sel->Next() ) {
182 #if OCC_VERSION_LARGE > 0x06080100
183 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = sel->Sensitive();
184 if( aHSenEntity.IsNull() )
187 Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
189 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
191 if ( entity.IsNull() )
194 Handle(SelectMgr_EntityOwner) owner =
195 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
196 if ( !owner.IsNull() )
202 //================================================================
203 // Function : setSelection
205 //================================================================
206 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
208 OCCViewer_Viewer* vw = viewer();
209 if ( !vw || !vw->isSelectionEnabled())
212 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
213 bool anAutoBringToFront = aResourceMgr->booleanValue( "Geometry", "auto_bring_to_front", false );
215 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
217 // "entry - list_of_int" map for LOCAL selection
218 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
220 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
221 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
223 AIS_ListOfInteractive aDispList;
224 ic->DisplayedObjects( aDispList );
226 // build a map of data owner indexes to be selected.
227 // "entry - to - list_of_ids" map
228 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
230 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
233 QString entry = subOwner->entry();
234 if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
236 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
237 subIndexes.Add( subOwner->index() );
238 //indexesMap.replace( entry, subIndexes );
242 TColStd_IndexedMapOfInteger subIndexes;
243 subIndexes.Add( subOwner->index() );
244 indexesMap.Bind(entry.toLatin1().data(), subIndexes);
247 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
249 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
252 SalomeApp_Study* appStudy =
253 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
254 QString anEntry = appStudy->referencedToEntry( owner->entry() );
256 globalSelMap[anEntry] = 1;
261 // get all owners. Fill "entry - list_of_owners" map.
262 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
264 Handle(AIS_InteractiveObject) io = it.Value();
265 QString entryStr = entry( io );
266 if ( !entryStr.isEmpty() )
268 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
269 SelectMgr_IndexedMapOfOwner owners;
270 getEntityOwners( io, ic, owners ); // get all owners
272 int i, n = owners.Extent();
274 // 1. Prepare map of shapes for local selection
275 TopTools_IndexedMapOfShape aMapOfShapes;
276 bool isLocal = false;
278 Handle(StdSelect_BRepOwner) anOwner;
279 for (i = 1; i <= n && !isLocal; i++)
281 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
282 if (!anOwner.IsNull() && anOwner->HasShape())
284 if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
286 // has a local selection
287 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
288 if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
291 TopoDS_Shape shape = aisShape->Shape();
292 aMapOfShapes.Clear();
293 TopExp::MapShapes(shape, aMapOfShapes);
299 // 2. Process all owners
300 for (i = 1; i <= n; i++)
302 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
304 if ( anOwner.IsNull() || !anOwner->HasShape() ) {
305 if ( globalSelMap.contains( entryStr ) ) {
306 Handle(GEOM_Annotation::GEOM_AnnotationOwner) anAnnotationOwner =
307 Handle(GEOM_Annotation::GEOM_AnnotationOwner)::DownCast(owners( i ));
308 if ( !anAnnotationOwner.IsNull() ) {
309 ownersmap.Add( anAnnotationOwner );
316 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
318 ownersmap.Add( anOwner );
319 globalSelMap[entryStr]++;
326 const TColStd_IndexedMapOfInteger& subIndexes =
327 indexesMap.ChangeFind(entryStr.toLatin1().data());
329 const TopoDS_Shape& aSubShape = anOwner->Shape();
330 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
332 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
333 // which was passes in the parameter
334 if ( subIndexes.Contains( aSubShapeId ) )
336 ownersmap.Add( anOwner );
339 } // end of LOCAL selection
340 } // end of for(owners)
341 } // end of if(entry)
342 } // end of for(AIS_all_ios)
344 vw->unHighlightAll( false );
347 int i = 1, n = ownersmap.Extent();
348 bool isAutoHilight = ic->AutomaticHilight();
349 ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
352 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
353 if ( owner->State() )
356 if ( ic->HasOpenedContext() )
357 ic->AddOrRemoveSelected( owner, false );
359 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
362 ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
364 //rnv: In case Automatic Bring To Front viewer will be updated later
365 if(!anAutoBringToFront) {
367 ic->HilightSelected(Standard_True);
372 // fill extra selected
373 mySelectedExternals.clear();
374 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
376 const LightApp_DataSubOwner* subOwner =
377 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
380 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
383 SalomeApp_Study* appStudy =
384 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
385 QString anEntry = appStudy->referencedToEntry( owner->entry() );
386 if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);