Salome HOME
*** empty log message ***
[modules/geom.git] / src / GEOMGUI / GEOMGUI_OCCSelector.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : GEOMGUI_OCCSelector.cxx
21 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
22 //
23
24 #include "GEOMGUI_OCCSelector.h"
25
26 #include <LightApp_DataSubOwner.h>
27
28 #include <OCCViewer_ViewModel.h>
29
30 #include <AIS_ListOfInteractive.hxx>
31 #include <AIS_ListIteratorOfListOfInteractive.hxx>
32 #include <SelectMgr_EntityOwner.hxx>
33 #include <AIS_Shape.hxx>
34 #include <TopTools_IndexedMapOfShape.hxx>
35 #include <TopExp.hxx>
36 #include <SelectMgr_IndexedMapOfOwner.hxx>
37 #include <TColStd_ListIteratorOfListOfInteger.hxx>
38 #include <SelectMgr_Selection.hxx>
39 #include <SelectBasics_SensitiveEntity.hxx>
40 #include <StdSelect_BRepOwner.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <NCollection_DataMap.hxx>
43
44 #include <SUIT_Session.h>
45 #include <SalomeApp_Study.h>
46
47
48 //================================================================
49 // Function : GEOMGUI_OCCSelector
50 // Purpose  : 
51 //================================================================
52 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
53 : LightApp_OCCSelector( viewer, mgr )
54 {
55 }
56
57 //================================================================
58 // Function : ~GEOMGUI_OCCSelector
59 // Purpose  : 
60 //================================================================
61 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
62 {
63 }
64
65 //================================================================
66 // Function : getSelection
67 // Purpose  : 
68 //================================================================
69 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
70 {
71   OCCViewer_Viewer* vw = viewer();
72   if ( !vw )
73     return;
74
75   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
76   
77   if ( ic->HasOpenedContext() )
78     {
79       for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
80         {
81           Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
82           if ( anOwner.IsNull() )
83             continue;
84           
85           Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
86           
87           QString entryStr = entry( io );
88           int index = -1; 
89           
90           if ( anOwner->ComesFromDecomposition() ) // == Local Selection
91             {
92               TopoDS_Shape subShape = anOwner->Shape();
93               Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
94               if ( !aisShape.IsNull() )
95                 {
96                   TopoDS_Shape bigShape = aisShape->Shape();
97                   
98                   TopTools_IndexedMapOfShape subShapes;
99                   TopExp::MapShapes( bigShape, subShapes );
100                   index = subShapes.FindIndex( subShape );
101                 }
102             }
103           
104           if ( !entryStr.isEmpty() )
105             {
106               LightApp_DataOwner* owner;
107               if ( index > -1 ) // Local Selection
108                 owner = new LightApp_DataSubOwner( entryStr, index );
109               else // Global Selection
110                 owner = new LightApp_DataOwner( entryStr );
111
112               aList.append( SUIT_DataOwnerPtr( owner ) );
113             }
114         }
115     }
116   else
117     {
118       for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
119         {
120           Handle(AIS_InteractiveObject) io = ic->Current();
121           
122           QString entryStr = entry( io );
123           
124           if ( !entryStr.isEmpty() )
125             {
126               LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
127               aList.append( SUIT_DataOwnerPtr( owner ) );
128             }
129         }
130     }
131   // add externally selected objects
132   SUIT_DataOwnerPtrList::const_iterator anExtIter;
133   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
134     aList.append(*anExtIter);
135   }
136   
137 }
138
139 //================================================================
140 // Function : getEntityOwners
141 // Purpose  : 
142 //================================================================
143 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
144                              const Handle(AIS_InteractiveContext)& theIC,
145                              SelectMgr_IndexedMapOfOwner& theMap )
146 {
147   if ( theObj.IsNull() || theIC.IsNull() )
148     return;
149
150   TColStd_ListOfInteger modes;
151   theIC->ActivatedModes( theObj, modes );
152
153   TColStd_ListIteratorOfListOfInteger itr( modes );
154   for (; itr.More(); itr.Next() ) {
155     int m = itr.Value();
156     if ( !theObj->HasSelection( m ) )
157       continue;
158
159     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
160
161     for ( sel->Init(); sel->More(); sel->Next() ) {
162       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
163       if ( entity.IsNull() )
164         continue;
165
166       Handle(SelectMgr_EntityOwner) owner =
167         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
168       if ( !owner.IsNull() )
169         theMap.Add( owner );
170     }
171   }
172 }
173
174 //================================================================
175 // Function : setSelection
176 // Purpose  : 
177 //================================================================
178 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
179 {
180   OCCViewer_Viewer* vw = viewer();
181   if ( !vw )
182     return;
183
184   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
185
186   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
187   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
188   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
189   
190   AIS_ListOfInteractive aDispList;
191   ic->DisplayedObjects( aDispList );
192
193   // build a map of data owner indexes to be selected.
194   // "entry - to - list_of_ids" map
195   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
196   {
197     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
198     if ( subOwner )
199     {
200       QString entry = subOwner->entry();
201 #ifndef WNT
202       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
203 #else
204           if ( indexesMap.IsBound( (char*)entry.toLatin1()))
205 #endif
206       {
207         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
208         subIndexes.Add( subOwner->index() );
209         //indexesMap.replace( entry, subIndexes );
210       }
211       else
212       {
213         TColStd_IndexedMapOfInteger subIndexes;
214         subIndexes.Add( subOwner->index() );
215         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
216       }
217     } 
218     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
219     {
220       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
221       if ( owner )
222       {
223         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
224         QString anEntry = appStudy->referencedToEntry( owner->entry() );
225         
226         globalSelMap[anEntry] = 1;
227       }
228     }
229   }
230
231   // get all owners.  Fill "entry - list_of_owners" map.
232   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
233   {
234     Handle(AIS_InteractiveObject) io = it.Value();
235     QString entryStr = entry( io );
236     if ( !entryStr.isEmpty() )
237     {
238       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
239       SelectMgr_IndexedMapOfOwner owners;
240       getEntityOwners( io, ic, owners ); // get all owners
241
242       for  ( int i = 1, n = owners.Extent(); i <= n; i++ ) 
243       {
244
245         Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
246
247         if ( anOwner.IsNull() || !anOwner->HasShape() )
248           continue;
249
250         // GLOBAL selection
251         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) ) 
252         {
253           ownersmap.Add( anOwner );
254           globalSelMap[entryStr]++;
255         }
256         // LOCAL selection
257         else
258         {
259           Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
260
261           if ( !aisShape.IsNull() && indexesMap.IsBound( entryStr.toLatin1().data() ) )
262           {
263             TopoDS_Shape shape = aisShape->Shape();
264             TopTools_IndexedMapOfShape aMapOfShapes;
265             TopExp::MapShapes( shape, aMapOfShapes );
266             const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entryStr.toLatin1().data());
267
268             const TopoDS_Shape& aSubShape = anOwner->Shape();
269             int  aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
270
271             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
272             // which was passes in the parameter
273             if ( subIndexes.Contains( aSubShapeId ) )
274             {
275               ownersmap.Add( anOwner );
276             }
277           }
278         } // end of local selection
279       } // end of for(owners)
280     }// end of if(entry)
281   }// end of for(AIS_all_ios)
282
283   vw->unHighlightAll( false );
284
285   // DO the selection
286   for  ( int i = 1, n = ownersmap.Extent(); i <= n; i++ ) 
287   {
288     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
289     if ( owner->State() )
290       continue;
291
292     if ( ic->HasOpenedContext() )
293       ic->AddOrRemoveSelected( owner, false );
294     else
295       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
296   }
297
298   vw->update();
299   
300   // fill extra selected
301   mySelectedExternals.clear();
302   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 ) {
303     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
304     if ( !subOwner )
305     {
306       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
307       if ( owner )
308       {
309         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
310         QString anEntry = appStudy->referencedToEntry( owner->entry() );
311         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
312       }
313     }
314   }
315 }