Salome HOME
0020044: EDF 866 GEOM: Extrusion along a path : impossible to select a Wire
[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 <SUIT_Session.h>
31 #include <SalomeApp_Study.h>
32 #include <SALOME_InteractiveObject.hxx>
33
34 #include <StdSelect_BRepOwner.hxx>
35 #include <SelectMgr_Selection.hxx>
36 #include <SelectMgr_EntityOwner.hxx>
37 #include <SelectMgr_IndexedMapOfOwner.hxx>
38 #include <SelectBasics_SensitiveEntity.hxx>
39
40 #include <AIS_Shape.hxx>
41 #include <AIS_ListOfInteractive.hxx>
42 #include <AIS_ListIteratorOfListOfInteractive.hxx>
43 #include <TopExp.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <NCollection_DataMap.hxx>
48
49
50 //================================================================
51 // Function : GEOMGUI_OCCSelector
52 // Purpose  : 
53 //================================================================
54 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
55 : LightApp_OCCSelector( viewer, mgr )
56 {
57 }
58
59 //================================================================
60 // Function : ~GEOMGUI_OCCSelector
61 // Purpose  : 
62 //================================================================
63 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
64 {
65 }
66
67 //================================================================
68 // Function : getSelection
69 // Purpose  : 
70 //================================================================
71 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
72 {
73   OCCViewer_Viewer* vw = viewer();
74   if (!vw)
75     return;
76
77   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
78
79   if (ic->HasOpenedContext())
80   {
81     TopoDS_Shape curBigShape;
82     TopTools_IndexedMapOfShape subShapes;
83
84     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
85     {
86       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
87       if (anOwner.IsNull())
88         continue;
89
90       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
91
92       QString entryStr = entry(io);
93       int index = -1;
94
95       if (anOwner->ComesFromDecomposition()) // == Local Selection
96       {
97         TopoDS_Shape subShape = anOwner->Shape();
98         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
99         if (!aisShape.IsNull())
100         {
101           TopoDS_Shape bigShape = aisShape->Shape();
102
103           if (!bigShape.IsEqual(curBigShape))
104           {
105             curBigShape = bigShape;
106             subShapes.Clear();
107             TopExp::MapShapes(bigShape, subShapes);
108           }
109           index = subShapes.FindIndex(subShape);
110         }
111       }
112
113       if (!entryStr.isEmpty())
114       {
115         LightApp_DataOwner* owner;
116         if (index > -1) // Local Selection
117           owner = new LightApp_DataSubOwner (entryStr, index);
118         else // Global Selection
119           owner = new LightApp_DataOwner (entryStr);
120
121         aList.append(SUIT_DataOwnerPtr(owner));
122       }
123     }
124   }
125   else
126   {
127     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
128     {
129       Handle(AIS_InteractiveObject) io = ic->Current();
130
131       QString entryStr = entry( io );
132
133       if ( !entryStr.isEmpty() )
134       {
135         LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
136         aList.append( SUIT_DataOwnerPtr( owner ) );
137       }
138     }
139   }
140
141   // add externally selected objects
142   SUIT_DataOwnerPtrList::const_iterator anExtIter;
143   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
144   {
145     aList.append(*anExtIter);
146   }
147 }
148
149 //================================================================
150 // Function : getEntityOwners
151 // Purpose  : 
152 //================================================================
153 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
154                              const Handle(AIS_InteractiveContext)& theIC,
155                              SelectMgr_IndexedMapOfOwner& theMap )
156 {
157   if ( theObj.IsNull() || theIC.IsNull() )
158     return;
159
160   TColStd_ListOfInteger modes;
161   theIC->ActivatedModes( theObj, modes );
162
163   TColStd_ListIteratorOfListOfInteger itr( modes );
164   for (; itr.More(); itr.Next() ) {
165     int m = itr.Value();
166     if ( !theObj->HasSelection( m ) )
167       continue;
168
169     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
170
171     for ( sel->Init(); sel->More(); sel->Next() ) {
172       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
173       if ( entity.IsNull() )
174         continue;
175
176       Handle(SelectMgr_EntityOwner) owner =
177         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
178       if ( !owner.IsNull() )
179         theMap.Add( owner );
180     }
181   }
182 }
183
184 //================================================================
185 // Function : setSelection
186 // Purpose  : 
187 //================================================================
188 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
189 {
190   OCCViewer_Viewer* vw = viewer();
191   if ( !vw )
192     return;
193
194   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
195
196   // "entry - list_of_int" map for LOCAL selection
197 #ifndef WNT
198   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
199 #else
200   NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap;
201 #endif
202
203   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
204   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
205
206   AIS_ListOfInteractive aDispList;
207   ic->DisplayedObjects( aDispList );
208
209   // build a map of data owner indexes to be selected.
210   // "entry - to - list_of_ids" map
211   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
212   {
213     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
214     if ( subOwner )
215     {
216       QString entry = subOwner->entry();
217 #ifndef WNT
218       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
219 #else
220       if ( indexesMap.IsBound( entry.toLatin1().data() ) )
221 #endif
222       {
223         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
224         subIndexes.Add( subOwner->index() );
225         //indexesMap.replace( entry, subIndexes );
226       }
227       else
228       {
229         TColStd_IndexedMapOfInteger subIndexes;
230         subIndexes.Add( subOwner->index() );
231         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
232       }
233     }
234     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
235     {
236       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
237       if ( owner )
238       {
239         SalomeApp_Study* appStudy =
240           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
241         QString anEntry = appStudy->referencedToEntry( owner->entry() );
242
243         globalSelMap[anEntry] = 1;
244       }
245     }
246   }
247
248   // get all owners.  Fill "entry - list_of_owners" map.
249   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
250   {
251     Handle(AIS_InteractiveObject) io = it.Value();
252     QString entryStr = entry( io );
253     if ( !entryStr.isEmpty() )
254     {
255       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
256       SelectMgr_IndexedMapOfOwner owners;
257       getEntityOwners( io, ic, owners ); // get all owners
258
259       int i, n = owners.Extent();
260
261       // 1. Prepare map of shapes for local selection
262       TopTools_IndexedMapOfShape aMapOfShapes;
263       bool isLocal = false;
264
265       Handle(StdSelect_BRepOwner) anOwner;
266       for (i = 1; i <= n && !isLocal; i++)
267       {
268         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
269         if (!anOwner.IsNull() && anOwner->HasShape())
270         {
271           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
272           {
273             // has a local selection
274             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
275             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
276             {
277               isLocal = true;
278               TopoDS_Shape shape = aisShape->Shape();
279               aMapOfShapes.Clear();
280               TopExp::MapShapes(shape, aMapOfShapes);
281             }
282           }
283         }
284       }
285
286       // 2. Process all owners
287       for (i = 1; i <= n; i++)
288       {
289         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
290
291         if ( anOwner.IsNull() || !anOwner->HasShape() )
292           continue;
293
294         // GLOBAL selection
295         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
296         {
297           ownersmap.Add( anOwner );
298           globalSelMap[entryStr]++;
299         }
300         // LOCAL selection
301         else
302         {
303           if (isLocal)
304           {
305             const TColStd_IndexedMapOfInteger& subIndexes =
306               indexesMap.ChangeFind(entryStr.toLatin1().data());
307
308             const TopoDS_Shape& aSubShape = anOwner->Shape();
309             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
310
311             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
312             // which was passes in the parameter
313             if ( subIndexes.Contains( aSubShapeId ) )
314             {
315               ownersmap.Add( anOwner );
316             }
317           }
318         } // end of LOCAL selection
319       } // end of for(owners)
320     } // end of if(entry)
321   } // end of for(AIS_all_ios)
322
323   vw->unHighlightAll( false );
324
325   // DO the selection
326   int i = 1, n = ownersmap.Extent();
327   bool isAutoHilight = ic->AutomaticHilight();
328   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
329   for  (; i <= n; i++)
330   {
331     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
332     if ( owner->State() )
333       continue;
334
335     if ( ic->HasOpenedContext() )
336       ic->AddOrRemoveSelected( owner, false );
337     else
338       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
339   }
340   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
341   if (n < 3000)
342     ic->HilightSelected(/*updateviewer*/Standard_True);
343   else
344     vw->update();
345
346   // fill extra selected
347   mySelectedExternals.clear();
348   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
349   {
350     const LightApp_DataSubOwner* subOwner =
351       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
352     if ( !subOwner )
353     {
354       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
355       if ( owner )
356       {
357         SalomeApp_Study* appStudy =
358           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
359         QString anEntry = appStudy->referencedToEntry( owner->entry() );
360         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
361       }
362     }
363   }
364 }