1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : GEOMGUI_OCCSelector.cxx
21 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
24 #include "GEOMGUI_OCCSelector.h"
26 #include <LightApp_DataSubOwner.h>
28 #include <OCCViewer_ViewModel.h>
30 #include <SUIT_Session.h>
31 #include <SalomeApp_Study.h>
32 #include <SALOME_InteractiveObject.hxx>
34 #include <StdSelect_BRepOwner.hxx>
35 #include <SelectMgr_Selection.hxx>
36 #include <SelectMgr_EntityOwner.hxx>
37 #include <SelectMgr_IndexedMapOfOwner.hxx>
38 #include <SelectBasics_SensitiveEntity.hxx>
40 #include <AIS_Shape.hxx>
41 #include <AIS_ListOfInteractive.hxx>
42 #include <AIS_ListIteratorOfListOfInteractive.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <NCollection_DataMap.hxx>
50 //================================================================
51 // Function : GEOMGUI_OCCSelector
53 //================================================================
54 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
55 : LightApp_OCCSelector( viewer, mgr )
59 //================================================================
60 // Function : ~GEOMGUI_OCCSelector
62 //================================================================
63 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
67 //================================================================
68 // Function : getSelection
70 //================================================================
71 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
73 OCCViewer_Viewer* vw = viewer();
77 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
79 if (ic->HasOpenedContext())
81 TopoDS_Shape curBigShape;
82 TopTools_IndexedMapOfShape subShapes;
84 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
86 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
90 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
92 QString entryStr = entry(io);
95 if (anOwner->ComesFromDecomposition()) // == Local Selection
97 TopoDS_Shape subShape = anOwner->Shape();
98 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
99 if (!aisShape.IsNull())
101 TopoDS_Shape bigShape = aisShape->Shape();
103 if (!bigShape.IsEqual(curBigShape))
105 curBigShape = bigShape;
106 TopExp::MapShapes(bigShape, subShapes);
108 index = subShapes.FindIndex(subShape);
112 if (!entryStr.isEmpty())
114 LightApp_DataOwner* owner;
115 if (index > -1) // Local Selection
116 owner = new LightApp_DataSubOwner (entryStr, index);
117 else // Global Selection
118 owner = new LightApp_DataOwner (entryStr);
120 aList.append(SUIT_DataOwnerPtr(owner));
126 for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
128 Handle(AIS_InteractiveObject) io = ic->Current();
130 QString entryStr = entry( io );
132 if ( !entryStr.isEmpty() )
134 LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
135 aList.append( SUIT_DataOwnerPtr( owner ) );
140 // add externally selected objects
141 SUIT_DataOwnerPtrList::const_iterator anExtIter;
142 for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
144 aList.append(*anExtIter);
148 //================================================================
149 // Function : getEntityOwners
151 //================================================================
152 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
153 const Handle(AIS_InteractiveContext)& theIC,
154 SelectMgr_IndexedMapOfOwner& theMap )
156 if ( theObj.IsNull() || theIC.IsNull() )
159 TColStd_ListOfInteger modes;
160 theIC->ActivatedModes( theObj, modes );
162 TColStd_ListIteratorOfListOfInteger itr( modes );
163 for (; itr.More(); itr.Next() ) {
165 if ( !theObj->HasSelection( m ) )
168 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
170 for ( sel->Init(); sel->More(); sel->Next() ) {
171 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
172 if ( entity.IsNull() )
175 Handle(SelectMgr_EntityOwner) owner =
176 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
177 if ( !owner.IsNull() )
183 //================================================================
184 // Function : setSelection
186 //================================================================
187 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
189 OCCViewer_Viewer* vw = viewer();
193 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
195 // "entry - list_of_int" map for LOCAL selection
197 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
199 NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap;
202 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
203 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
205 AIS_ListOfInteractive aDispList;
206 ic->DisplayedObjects( aDispList );
208 // build a map of data owner indexes to be selected.
209 // "entry - to - list_of_ids" map
210 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
212 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
215 QString entry = subOwner->entry();
217 if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
219 if ( indexesMap.IsBound( entry.toLatin1().data() ) )
222 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
223 subIndexes.Add( subOwner->index() );
224 //indexesMap.replace( entry, subIndexes );
228 TColStd_IndexedMapOfInteger subIndexes;
229 subIndexes.Add( subOwner->index() );
230 indexesMap.Bind(entry.toLatin1().data(), subIndexes);
233 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
235 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
238 SalomeApp_Study* appStudy =
239 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
240 QString anEntry = appStudy->referencedToEntry( owner->entry() );
242 globalSelMap[anEntry] = 1;
247 // get all owners. Fill "entry - list_of_owners" map.
248 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
250 Handle(AIS_InteractiveObject) io = it.Value();
251 QString entryStr = entry( io );
252 if ( !entryStr.isEmpty() )
254 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
255 SelectMgr_IndexedMapOfOwner owners;
256 getEntityOwners( io, ic, owners ); // get all owners
258 int i, n = owners.Extent();
260 // 1. Prepare map of shapes for local selection
261 TopTools_IndexedMapOfShape aMapOfShapes;
262 bool isLocal = false;
264 Handle(StdSelect_BRepOwner) anOwner;
265 for (i = 1; i <= n && !isLocal; i++)
267 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
268 if (!anOwner.IsNull() && anOwner->HasShape())
270 if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
272 // has a local selection
273 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
274 if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
277 TopoDS_Shape shape = aisShape->Shape();
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);