1 #include "GEOMGUI_OCCSelector.h"
3 #include <SalomeApp_DataSubOwner.h>
5 #include <OCCViewer_ViewModel.h>
7 #include <SALOME_InteractiveObject.hxx>
9 #include <AIS_ListOfInteractive.hxx>
10 #include <AIS_ListIteratorOfListOfInteractive.hxx>
11 #include <SelectMgr_EntityOwner.hxx>
12 #include <AIS_Shape.hxx>
13 #include <TopTools_IndexedMapOfShape.hxx>
15 #include <SelectMgr_IndexedMapOfOwner.hxx>
16 #include <TColStd_ListIteratorOfListOfInteger.hxx>
17 #include <SelectMgr_Selection.hxx>
18 #include <SelectBasics_SensitiveEntity.hxx>
19 #include <TColStd_IndexedMapOfInteger.hxx>
20 #include <SelectMgr_IndexedMapOfOwner.hxx>
21 #include <NCollection_DataMap.hxx>
24 //================================================================
25 // Function : GEOMGUI_OCCSelector
27 //================================================================
28 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
29 : SalomeApp_OCCSelector( viewer, mgr )
33 //================================================================
34 // Function : ~GEOMGUI_OCCSelector
36 //================================================================
37 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
41 //================================================================
42 // Function : getSelection
44 //================================================================
45 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
47 OCCViewer_Viewer* vw = viewer();
51 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
53 if ( ic->HasOpenedContext() )
55 for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
57 Handle(SelectMgr_EntityOwner) anOwner = ic->SelectedOwner();
58 if ( anOwner.IsNull() )
61 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
63 QString entryStr = entry( io );
66 if ( anOwner->ComesFromDecomposition() ) // == Local Selection
68 TopoDS_Shape subShape = anOwner->Shape();
69 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
70 if ( !aisShape.IsNull() )
72 TopoDS_Shape bigShape = aisShape->Shape();
74 TopTools_IndexedMapOfShape subShapes;
75 TopExp::MapShapes( bigShape, subShapes );
76 index = subShapes.FindIndex( subShape );
80 if ( !entryStr.isEmpty() )
82 SalomeApp_DataOwner* owner;
83 if ( index > -1 ) // Local Selection
84 owner = new SalomeApp_DataSubOwner( entryStr, index );
85 else // Global Selection
86 owner = new SalomeApp_DataOwner( entryStr );
88 aList.append( SUIT_DataOwnerPtr( owner ) );
94 for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
96 Handle(AIS_InteractiveObject) io = ic->Current();
98 QString entryStr = entry( io );
100 if ( !entryStr.isEmpty() )
102 SalomeApp_DataOwner* owner = new SalomeApp_DataOwner( entryStr );
103 aList.append( SUIT_DataOwnerPtr( owner ) );
109 //================================================================
110 // Function : getEntityOwners
112 //================================================================
113 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
114 const Handle(AIS_InteractiveContext)& theIC,
115 SelectMgr_IndexedMapOfOwner& theMap )
117 if ( theObj.IsNull() || theIC.IsNull() )
120 TColStd_ListOfInteger modes;
121 theIC->ActivatedModes( theObj, modes );
123 TColStd_ListIteratorOfListOfInteger itr( modes );
124 for (; itr.More(); itr.Next() ) {
126 if ( !theObj->HasSelection( m ) )
129 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
131 for ( sel->Init(); sel->More(); sel->Next() ) {
132 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
133 if ( entity.IsNull() )
136 Handle(SelectMgr_EntityOwner) owner =
137 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
138 if ( !owner.IsNull() )
144 //================================================================
145 // Function : setSelection
147 //================================================================
148 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
150 OCCViewer_Viewer* vw = viewer();
154 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
156 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
157 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
158 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
160 AIS_ListOfInteractive aDispList;
161 ic->DisplayedObjects( aDispList );
163 // build a map of data owner indexes to be selected.
164 // "entry - to - list_of_ids" map
165 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
167 const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
170 QString entry = subOwner->entry();
171 if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
173 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
174 subIndexes.Add( subOwner->index() );
175 //indexesMap.replace( entry, subIndexes );
179 TColStd_IndexedMapOfInteger subIndexes;
180 subIndexes.Add( subOwner->index() );
181 indexesMap.Bind((char*)entry.latin1(), subIndexes);
184 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
186 const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
189 globalSelMap[owner->entry()] = 1;
194 // get all owners. Fill "entry - list_of_owners" map.
195 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
197 Handle(AIS_InteractiveObject) io = it.Value();
198 QString entryStr = entry( io );
199 if ( !entryStr.isEmpty() )
201 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
202 SelectMgr_IndexedMapOfOwner owners;
203 getEntityOwners( io, ic, owners ); // get all owners
205 for ( int i = 1, n = owners.Extent(); i <= n; i++ )
207 Handle(SelectMgr_EntityOwner) anOwner = owners( i );
208 if ( anOwner.IsNull() || !anOwner->HasShape() )
212 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
214 ownersmap.Add( anOwner );
219 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
221 if ( !aisShape.IsNull() && indexesMap.IsBound( (char*)entryStr.latin1() ) )
223 TopoDS_Shape shape = aisShape->Shape();
224 TopTools_IndexedMapOfShape aMapOfShapes;
225 TopExp::MapShapes( shape, aMapOfShapes );
226 const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entryStr.latin1());
228 const TopoDS_Shape& aSubShape = anOwner->Shape();
229 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
231 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
232 // which was passes in the parameter
233 if ( subIndexes.Contains( aSubShapeId ) )
235 ownersmap.Add( anOwner );
238 } // end of local selection
239 } // end of for(owners)
241 }// end of for(AIS_all_ios)
243 vw->unHighlightAll( false );
246 for ( int i = 1, n = ownersmap.Extent(); i <= n; i++ )
248 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
249 if ( owner->State() )
252 if ( ic->HasOpenedContext() )
253 ic->AddOrRemoveSelected( owner, false );
255 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );