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
21 #include "GEOMGUI_OCCSelector.h"
23 #include <LightApp_DataSubOwner.h>
25 #include <OCCViewer_ViewModel.h>
27 #include <SUIT_Session.h>
28 #include <SalomeApp_Study.h>
29 #include <SALOME_InteractiveObject.hxx>
31 #include <StdSelect_BRepOwner.hxx>
32 #include <SelectMgr_Selection.hxx>
33 #include <SelectMgr_EntityOwner.hxx>
34 #include <SelectMgr_IndexedMapOfOwner.hxx>
35 #include <SelectBasics_SensitiveEntity.hxx>
37 #include <AIS_Shape.hxx>
38 #include <AIS_ListOfInteractive.hxx>
39 #include <AIS_ListIteratorOfListOfInteractive.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TColStd_ListIteratorOfListOfInteger.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44 #include <NCollection_DataMap.hxx>
47 //================================================================
48 // Function : GEOMGUI_OCCSelector
50 //================================================================
51 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
52 : LightApp_OCCSelector( viewer, mgr )
56 //================================================================
57 // Function : ~GEOMGUI_OCCSelector
59 //================================================================
60 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
64 //================================================================
65 // Function : getSelection
67 //================================================================
68 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
70 OCCViewer_Viewer* vw = viewer();
74 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
76 if (ic->HasOpenedContext())
78 TopoDS_Shape curBigShape;
79 TopTools_IndexedMapOfShape subShapes;
81 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
83 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
87 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
89 QString entryStr = entry(io);
92 if (anOwner->ComesFromDecomposition()) // == Local Selection
94 TopoDS_Shape subShape = anOwner->Shape();
95 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
96 if (!aisShape.IsNull())
98 TopoDS_Shape bigShape = aisShape->Shape();
100 if (!bigShape.IsEqual(curBigShape))
102 curBigShape = bigShape;
103 TopExp::MapShapes(bigShape, subShapes);
105 index = subShapes.FindIndex(subShape);
109 if (!entryStr.isEmpty())
111 LightApp_DataOwner* owner;
112 if (index > -1) // Local Selection
113 owner = new LightApp_DataSubOwner (entryStr, index);
114 else // Global Selection
115 owner = new LightApp_DataOwner (entryStr);
117 aList.append(SUIT_DataOwnerPtr(owner));
123 for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
125 Handle(AIS_InteractiveObject) io = ic->Current();
127 QString entryStr = entry( io );
129 if ( !entryStr.isEmpty() )
131 LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
132 aList.append( SUIT_DataOwnerPtr( owner ) );
137 // add externally selected objects
138 SUIT_DataOwnerPtrList::const_iterator anExtIter;
139 for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
141 aList.append(*anExtIter);
145 //================================================================
146 // Function : getEntityOwners
148 //================================================================
149 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
150 const Handle(AIS_InteractiveContext)& theIC,
151 SelectMgr_IndexedMapOfOwner& theMap )
153 if ( theObj.IsNull() || theIC.IsNull() )
156 TColStd_ListOfInteger modes;
157 theIC->ActivatedModes( theObj, modes );
159 TColStd_ListIteratorOfListOfInteger itr( modes );
160 for (; itr.More(); itr.Next() ) {
162 if ( !theObj->HasSelection( m ) )
165 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
167 for ( sel->Init(); sel->More(); sel->Next() ) {
168 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
169 if ( entity.IsNull() )
172 Handle(SelectMgr_EntityOwner) owner =
173 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
174 if ( !owner.IsNull() )
180 //================================================================
181 // Function : setSelection
183 //================================================================
184 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
186 OCCViewer_Viewer* vw = viewer();
190 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
192 // "entry - list_of_int" map for LOCAL selection
193 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
195 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
196 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
198 AIS_ListOfInteractive aDispList;
199 ic->DisplayedObjects( aDispList );
201 // build a map of data owner indexes to be selected.
202 // "entry - to - list_of_ids" map
203 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
205 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
208 QString entry = subOwner->entry();
210 if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
212 if ( indexesMap.IsBound( (char*)entry.latin1()))
215 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
216 subIndexes.Add( subOwner->index() );
217 //indexesMap.replace( entry, subIndexes );
221 TColStd_IndexedMapOfInteger subIndexes;
222 subIndexes.Add( subOwner->index() );
223 indexesMap.Bind((char*)entry.latin1(), subIndexes);
226 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
228 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
231 SalomeApp_Study* appStudy =
232 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
233 QString anEntry = appStudy->referencedToEntry( owner->entry() );
235 globalSelMap[anEntry] = 1;
240 // get all owners. Fill "entry - list_of_owners" map.
241 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
243 Handle(AIS_InteractiveObject) io = it.Value();
244 QString entryStr = entry( io );
245 if ( !entryStr.isEmpty() )
247 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
248 SelectMgr_IndexedMapOfOwner owners;
249 getEntityOwners( io, ic, owners ); // get all owners
251 int i, n = owners.Extent();
253 // 1. Prepare map of shapes for local selection
254 TopTools_IndexedMapOfShape aMapOfShapes;
255 bool isLocal = false;
257 Handle(StdSelect_BRepOwner) anOwner;
258 for (i = 1; i <= n && !isLocal; i++)
260 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
261 if (!anOwner.IsNull() && anOwner->HasShape())
263 if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
265 // has a local selection
266 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
267 if (!aisShape.IsNull() && indexesMap.IsBound((char*)entryStr.latin1()))
270 TopoDS_Shape shape = aisShape->Shape();
271 TopExp::MapShapes(shape, aMapOfShapes);
277 // 2. Process all owners
278 for (i = 1; i <= n; i++)
280 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
282 if ( anOwner.IsNull() || !anOwner->HasShape() )
286 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
288 ownersmap.Add( anOwner );
289 globalSelMap[entryStr]++;
296 const TColStd_IndexedMapOfInteger& subIndexes =
297 indexesMap.ChangeFind((char*)entryStr.latin1());
299 const TopoDS_Shape& aSubShape = anOwner->Shape();
300 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
302 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
303 // which was passes in the parameter
304 if ( subIndexes.Contains( aSubShapeId ) )
306 ownersmap.Add( anOwner );
309 } // end of LOCAL selection
310 } // end of for(owners)
311 } // end of if(entry)
312 } // end of for(AIS_all_ios)
314 vw->unHighlightAll( false );
317 for ( int i = 1, n = ownersmap.Extent(); i <= n; i++ )
319 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
320 if ( owner->State() )
323 if ( ic->HasOpenedContext() )
324 ic->AddOrRemoveSelected( owner, false );
326 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
331 // fill extra selected
332 mySelectedExternals.clear();
333 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
335 const LightApp_DataSubOwner* subOwner =
336 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
339 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
342 SalomeApp_Study* appStudy =
343 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
344 QString anEntry = appStudy->referencedToEntry( owner->entry() );
345 if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);