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 #include "GEOMGUI_OCCSelector.h"
22 #include <LightApp_DataSubOwner.h>
24 #include <OCCViewer_ViewModel.h>
26 #include <SALOME_InteractiveObject.hxx>
28 #include <AIS_ListOfInteractive.hxx>
29 #include <AIS_ListIteratorOfListOfInteractive.hxx>
30 #include <SelectMgr_EntityOwner.hxx>
31 #include <AIS_Shape.hxx>
32 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <SelectMgr_IndexedMapOfOwner.hxx>
35 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <SelectBasics_SensitiveEntity.hxx>
38 #include <StdSelect_BRepOwner.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <SelectMgr_IndexedMapOfOwner.hxx>
41 #include <NCollection_DataMap.hxx>
43 #include <SUIT_Session.h>
44 #include <SalomeApp_Study.h>
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 for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
80 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
81 if ( anOwner.IsNull() )
84 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
86 QString entryStr = entry( io );
89 if ( anOwner->ComesFromDecomposition() ) // == Local Selection
91 TopoDS_Shape subShape = anOwner->Shape();
92 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
93 if ( !aisShape.IsNull() )
95 TopoDS_Shape bigShape = aisShape->Shape();
97 TopTools_IndexedMapOfShape subShapes;
98 TopExp::MapShapes( bigShape, subShapes );
99 index = subShapes.FindIndex( subShape );
103 if ( !entryStr.isEmpty() )
105 LightApp_DataOwner* owner;
106 if ( index > -1 ) // Local Selection
107 owner = new LightApp_DataSubOwner( entryStr, index );
108 else // Global Selection
109 owner = new LightApp_DataOwner( entryStr );
111 aList.append( SUIT_DataOwnerPtr( owner ) );
117 for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
119 Handle(AIS_InteractiveObject) io = ic->Current();
121 QString entryStr = entry( io );
123 if ( !entryStr.isEmpty() )
125 LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
126 aList.append( SUIT_DataOwnerPtr( owner ) );
130 // add externally selected objects
131 SUIT_DataOwnerPtrList::const_iterator anExtIter;
132 for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
133 aList.append(*anExtIter);
138 //================================================================
139 // Function : getEntityOwners
141 //================================================================
142 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
143 const Handle(AIS_InteractiveContext)& theIC,
144 SelectMgr_IndexedMapOfOwner& theMap )
146 if ( theObj.IsNull() || theIC.IsNull() )
149 TColStd_ListOfInteger modes;
150 theIC->ActivatedModes( theObj, modes );
152 TColStd_ListIteratorOfListOfInteger itr( modes );
153 for (; itr.More(); itr.Next() ) {
155 if ( !theObj->HasSelection( m ) )
158 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
160 for ( sel->Init(); sel->More(); sel->Next() ) {
161 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
162 if ( entity.IsNull() )
165 Handle(SelectMgr_EntityOwner) owner =
166 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
167 if ( !owner.IsNull() )
173 //================================================================
174 // Function : setSelection
176 //================================================================
177 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
179 OCCViewer_Viewer* vw = viewer();
183 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
185 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
186 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
187 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
189 AIS_ListOfInteractive aDispList;
190 ic->DisplayedObjects( aDispList );
192 // build a map of data owner indexes to be selected.
193 // "entry - to - list_of_ids" map
194 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
196 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
199 QString entry = subOwner->entry();
201 if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
203 if ( indexesMap.IsBound( (char*)entry.latin1()))
206 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
207 subIndexes.Add( subOwner->index() );
208 //indexesMap.replace( entry, subIndexes );
212 TColStd_IndexedMapOfInteger subIndexes;
213 subIndexes.Add( subOwner->index() );
214 indexesMap.Bind((char*)entry.latin1(), subIndexes);
217 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
219 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
222 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
223 QString anEntry = appStudy->referencedToEntry( owner->entry() );
225 globalSelMap[anEntry] = 1;
230 // get all owners. Fill "entry - list_of_owners" map.
231 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
233 Handle(AIS_InteractiveObject) io = it.Value();
234 QString entryStr = entry( io );
235 if ( !entryStr.isEmpty() )
237 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
238 SelectMgr_IndexedMapOfOwner owners;
239 getEntityOwners( io, ic, owners ); // get all owners
241 for ( int i = 1, n = owners.Extent(); i <= n; i++ )
244 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
246 if ( anOwner.IsNull() || !anOwner->HasShape() )
250 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
252 ownersmap.Add( anOwner );
253 globalSelMap[entryStr]++;
258 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
260 if ( !aisShape.IsNull() && indexesMap.IsBound( (char*)entryStr.latin1() ) )
262 TopoDS_Shape shape = aisShape->Shape();
263 TopTools_IndexedMapOfShape aMapOfShapes;
264 TopExp::MapShapes( shape, aMapOfShapes );
265 const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entryStr.latin1());
267 const TopoDS_Shape& aSubShape = anOwner->Shape();
268 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
270 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
271 // which was passes in the parameter
272 if ( subIndexes.Contains( aSubShapeId ) )
274 ownersmap.Add( anOwner );
277 } // end of local selection
278 } // end of for(owners)
280 }// end of for(AIS_all_ios)
282 vw->unHighlightAll( false );
285 for ( int i = 1, n = ownersmap.Extent(); i <= n; i++ )
287 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
288 if ( owner->State() )
291 if ( ic->HasOpenedContext() )
292 ic->AddOrRemoveSelected( owner, false );
294 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
299 // fill extra selected
300 mySelectedExternals.clear();
301 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 ) {
302 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
305 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
308 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
309 QString anEntry = appStudy->referencedToEntry( owner->entry() );
310 if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);