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;
104 TopExp::MapShapes(bigShape, subShapes);
106 index = subShapes.FindIndex(subShape);
110 if (!entryStr.isEmpty())
112 LightApp_DataOwner* owner;
113 if (index > -1) // Local Selection
114 owner = new LightApp_DataSubOwner (entryStr, index);
115 else // Global Selection
116 owner = new LightApp_DataOwner (entryStr);
118 aList.append(SUIT_DataOwnerPtr(owner));
124 for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
126 Handle(AIS_InteractiveObject) io = ic->Current();
128 QString entryStr = entry( io );
130 if ( !entryStr.isEmpty() )
132 LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
133 aList.append( SUIT_DataOwnerPtr( owner ) );
138 // add externally selected objects
139 SUIT_DataOwnerPtrList::const_iterator anExtIter;
140 for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
142 aList.append(*anExtIter);
146 //================================================================
147 // Function : getEntityOwners
149 //================================================================
150 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
151 const Handle(AIS_InteractiveContext)& theIC,
152 SelectMgr_IndexedMapOfOwner& theMap )
154 if ( theObj.IsNull() || theIC.IsNull() )
157 TColStd_ListOfInteger modes;
158 theIC->ActivatedModes( theObj, modes );
160 TColStd_ListIteratorOfListOfInteger itr( modes );
161 for (; itr.More(); itr.Next() ) {
163 if ( !theObj->HasSelection( m ) )
166 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
168 for ( sel->Init(); sel->More(); sel->Next() ) {
169 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
170 if ( entity.IsNull() )
173 Handle(SelectMgr_EntityOwner) owner =
174 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
175 if ( !owner.IsNull() )
181 //================================================================
182 // Function : setSelection
184 //================================================================
185 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
187 OCCViewer_Viewer* vw = viewer();
191 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
193 // "entry - list_of_int" map for LOCAL selection
195 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
197 NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap;
200 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
201 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
203 AIS_ListOfInteractive aDispList;
204 ic->DisplayedObjects( aDispList );
206 // build a map of data owner indexes to be selected.
207 // "entry - to - list_of_ids" map
208 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
210 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
213 QString entry = subOwner->entry();
215 if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
217 if ( indexesMap.IsBound( (char*)entry.latin1()))
220 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
221 subIndexes.Add( subOwner->index() );
222 //indexesMap.replace( entry, subIndexes );
226 TColStd_IndexedMapOfInteger subIndexes;
227 subIndexes.Add( subOwner->index() );
228 indexesMap.Bind((char*)entry.latin1(), subIndexes);
231 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
233 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
236 SalomeApp_Study* appStudy =
237 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
238 QString anEntry = appStudy->referencedToEntry( owner->entry() );
240 globalSelMap[anEntry] = 1;
245 // get all owners. Fill "entry - list_of_owners" map.
246 for (AIS_ListIteratorOfListOfInteractive it (aDispList); it.More(); it.Next())
248 Handle(AIS_InteractiveObject) io = it.Value();
249 QString entryStr = entry( io );
250 if ( !entryStr.isEmpty() )
252 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
253 SelectMgr_IndexedMapOfOwner owners;
254 getEntityOwners( io, ic, owners ); // get all owners
256 int i, n = owners.Extent();
258 // 1. Prepare map of shapes for local selection
259 TopTools_IndexedMapOfShape aMapOfShapes;
260 bool isLocal = false;
262 Handle(StdSelect_BRepOwner) anOwner;
263 for (i = 1; i <= n && !isLocal; i++)
265 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
266 if (!anOwner.IsNull() && anOwner->HasShape())
268 if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
270 // has a local selection
271 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
272 if (!aisShape.IsNull() && indexesMap.IsBound((char*)entryStr.latin1()))
275 TopoDS_Shape shape = aisShape->Shape();
276 aMapOfShapes.Clear();
277 TopExp::MapShapes(shape, aMapOfShapes);
283 // 2. Process all owners
284 for (i = 1; i <= n; i++)
286 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
288 if ( anOwner.IsNull() || !anOwner->HasShape() )
292 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
294 ownersmap.Add( anOwner );
295 globalSelMap[entryStr]++;
302 const TColStd_IndexedMapOfInteger& subIndexes =
303 indexesMap.ChangeFind((char*)entryStr.latin1());
305 const TopoDS_Shape& aSubShape = anOwner->Shape();
306 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
308 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
309 // which was passes in the parameter
310 if ( subIndexes.Contains( aSubShapeId ) )
312 ownersmap.Add( anOwner );
315 } // end of LOCAL selection
316 } // end of for(owners)
317 } // end of if(entry)
318 } // end of for(AIS_all_ios)
320 vw->unHighlightAll( false );
323 int i = 1, n = ownersmap.Extent();
324 bool isAutoHilight = ic->AutomaticHilight();
325 ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
328 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
329 if ( owner->State() )
332 if ( ic->HasOpenedContext() )
333 ic->AddOrRemoveSelected( owner, false );
335 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
337 ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
339 ic->HilightSelected(/*updateviewer*/Standard_True);
343 // fill extra selected
344 mySelectedExternals.clear();
345 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
347 const LightApp_DataSubOwner* subOwner =
348 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
351 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
354 SalomeApp_Study* appStudy =
355 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
356 QString anEntry = appStudy->referencedToEntry( owner->entry() );
357 if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);