1 // Copyright (C) 2007-2011 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 // File : GEOMGUI_OCCSelector.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
26 #include "GEOMGUI_OCCSelector.h"
28 #include <LightApp_DataSubOwner.h>
30 #include <OCCViewer_ViewModel.h>
32 #include <SUIT_Session.h>
33 #include <SalomeApp_Study.h>
34 #include <SALOME_InteractiveObject.hxx>
36 #include <StdSelect_BRepOwner.hxx>
37 #include <SelectMgr_Selection.hxx>
38 #include <SelectMgr_EntityOwner.hxx>
39 #include <SelectMgr_IndexedMapOfOwner.hxx>
40 #include <SelectBasics_SensitiveEntity.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_ListOfInteractive.hxx>
44 #include <AIS_ListIteratorOfListOfInteractive.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TColStd_ListIteratorOfListOfInteger.hxx>
48 #include <TColStd_IndexedMapOfInteger.hxx>
49 #include <NCollection_DataMap.hxx>
52 //================================================================
53 // Function : GEOMGUI_OCCSelector
55 //================================================================
56 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
57 : LightApp_OCCSelector( viewer, mgr )
61 //================================================================
62 // Function : ~GEOMGUI_OCCSelector
64 //================================================================
65 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
69 //================================================================
70 // Function : getSelection
72 //================================================================
73 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
75 OCCViewer_Viewer* vw = viewer();
79 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
81 if (ic->HasOpenedContext())
83 TopoDS_Shape curBigShape;
84 TopTools_IndexedMapOfShape subShapes;
86 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
88 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
92 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
94 QString entryStr = entry(io);
97 if (anOwner->ComesFromDecomposition()) // == Local Selection
99 TopoDS_Shape subShape = anOwner->Shape();
100 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
101 if (!aisShape.IsNull())
103 TopoDS_Shape bigShape = aisShape->Shape();
105 if (!bigShape.IsEqual(curBigShape))
107 curBigShape = bigShape;
109 TopExp::MapShapes(bigShape, subShapes);
111 index = subShapes.FindIndex(subShape);
115 if (!entryStr.isEmpty())
117 Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner());
118 LightApp_DataOwner* owner;
119 if (index > -1) // Local Selection
120 owner = new LightApp_DataSubOwner (entryStr, index);
121 else if ( !anIO.IsNull() ) // Global Selection
122 owner = new LightApp_DataOwner( anIO );
124 aList.append(SUIT_DataOwnerPtr(owner));
130 for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
132 Handle(AIS_InteractiveObject) io = ic->Current();
134 QString entryStr = entry( io );
136 if ( !entryStr.isEmpty() )
138 Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner());
139 if ( !anIO.IsNull() ) {
140 LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
141 aList.append( SUIT_DataOwnerPtr( owner ) );
147 // add externally selected objects
148 SUIT_DataOwnerPtrList::const_iterator anExtIter;
149 for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
151 aList.append(*anExtIter);
155 //================================================================
156 // Function : getEntityOwners
158 //================================================================
159 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
160 const Handle(AIS_InteractiveContext)& theIC,
161 SelectMgr_IndexedMapOfOwner& theMap )
163 if ( theObj.IsNull() || theIC.IsNull() )
166 TColStd_ListOfInteger modes;
167 theIC->ActivatedModes( theObj, modes );
169 TColStd_ListIteratorOfListOfInteger itr( modes );
170 for (; itr.More(); itr.Next() ) {
172 if ( !theObj->HasSelection( m ) )
175 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
177 for ( sel->Init(); sel->More(); sel->Next() ) {
178 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
179 if ( entity.IsNull() )
182 Handle(SelectMgr_EntityOwner) owner =
183 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
184 if ( !owner.IsNull() )
190 //================================================================
191 // Function : setSelection
193 //================================================================
194 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
196 OCCViewer_Viewer* vw = viewer();
200 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
202 // "entry - list_of_int" map for LOCAL selection
203 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
205 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
206 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
208 AIS_ListOfInteractive aDispList;
209 ic->DisplayedObjects( aDispList );
211 // build a map of data owner indexes to be selected.
212 // "entry - to - list_of_ids" map
213 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
215 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
218 QString entry = subOwner->entry();
219 if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
221 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
222 subIndexes.Add( subOwner->index() );
223 //indexesMap.replace( entry, subIndexes );
227 TColStd_IndexedMapOfInteger subIndexes;
228 subIndexes.Add( subOwner->index() );
229 indexesMap.Bind(entry.toLatin1().data(), subIndexes);
232 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
234 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
237 SalomeApp_Study* appStudy =
238 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
239 QString anEntry = appStudy->referencedToEntry( owner->entry() );
241 globalSelMap[anEntry] = 1;
246 // get all owners. Fill "entry - list_of_owners" map.
247 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
249 Handle(AIS_InteractiveObject) io = it.Value();
250 QString entryStr = entry( io );
251 if ( !entryStr.isEmpty() )
253 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
254 SelectMgr_IndexedMapOfOwner owners;
255 getEntityOwners( io, ic, owners ); // get all owners
257 int i, n = owners.Extent();
259 // 1. Prepare map of shapes for local selection
260 TopTools_IndexedMapOfShape aMapOfShapes;
261 bool isLocal = false;
263 Handle(StdSelect_BRepOwner) anOwner;
264 for (i = 1; i <= n && !isLocal; i++)
266 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
267 if (!anOwner.IsNull() && anOwner->HasShape())
269 if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
271 // has a local selection
272 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
273 if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
276 TopoDS_Shape shape = aisShape->Shape();
277 aMapOfShapes.Clear();
278 TopExp::MapShapes(shape, aMapOfShapes);
284 // 2. Process all owners
285 for (i = 1; i <= n; i++)
287 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
289 if ( anOwner.IsNull() || !anOwner->HasShape() )
293 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
295 ownersmap.Add( anOwner );
296 globalSelMap[entryStr]++;
303 const TColStd_IndexedMapOfInteger& subIndexes =
304 indexesMap.ChangeFind(entryStr.toLatin1().data());
306 const TopoDS_Shape& aSubShape = anOwner->Shape();
307 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
309 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
310 // which was passes in the parameter
311 if ( subIndexes.Contains( aSubShapeId ) )
313 ownersmap.Add( anOwner );
316 } // end of LOCAL selection
317 } // end of for(owners)
318 } // end of if(entry)
319 } // end of for(AIS_all_ios)
321 vw->unHighlightAll( false );
324 int i = 1, n = ownersmap.Extent();
325 bool isAutoHilight = ic->AutomaticHilight();
326 ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
329 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
330 if ( owner->State() )
333 if ( ic->HasOpenedContext() )
334 ic->AddOrRemoveSelected( owner, false );
336 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
338 ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
340 ic->HilightSelected(/*updateviewer*/Standard_True);
344 // fill extra selected
345 mySelectedExternals.clear();
346 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
348 const LightApp_DataSubOwner* subOwner =
349 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
352 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
355 SalomeApp_Study* appStudy =
356 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
357 QString anEntry = appStudy->referencedToEntry( owner->entry() );
358 if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);