Salome HOME
Update copyright: 2016
[modules/geom.git] / src / GEOMGUI / GEOMGUI_OCCSelector.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : GEOMGUI_OCCSelector.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
25 //
26 #include "GEOMGUI_OCCSelector.h"
27
28 #include <Basics_OCCTVersion.hxx>
29
30 #include <LightApp_DataSubOwner.h>
31
32 #include <OCCViewer_ViewModel.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SalomeApp_Study.h>
37 #include <SALOME_InteractiveObject.hxx>
38
39 #include <StdSelect_BRepOwner.hxx>
40 #include <SelectMgr_Selection.hxx>
41 #include <SelectMgr_EntityOwner.hxx>
42 #include <SelectMgr_IndexedMapOfOwner.hxx>
43 #include <SelectBasics_SensitiveEntity.hxx>
44
45 #include <AIS_Shape.hxx>
46 #include <AIS_ListOfInteractive.hxx>
47 #include <AIS_ListIteratorOfListOfInteractive.hxx>
48 #include <TopExp.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <TColStd_IndexedMapOfInteger.hxx>
52 #include <NCollection_DataMap.hxx>
53
54
55 //================================================================
56 // Function : GEOMGUI_OCCSelector
57 // Purpose  :
58 //================================================================
59 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
60 : LightApp_OCCSelector( viewer, mgr )
61 {
62 }
63
64 //================================================================
65 // Function : ~GEOMGUI_OCCSelector
66 // Purpose  :
67 //================================================================
68 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
69 {
70 }
71
72 //================================================================
73 // Function : getSelection
74 // Purpose  :
75 //================================================================
76 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
77 {
78   OCCViewer_Viewer* vw = viewer();
79   if (!vw)
80     return;
81
82   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
83
84   if (ic->HasOpenedContext())
85   {
86     TopoDS_Shape curBigShape;
87     TopTools_IndexedMapOfShape subShapes;
88
89     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
90     {
91       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
92       if (anOwner.IsNull())
93         continue;
94
95       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
96
97       QString entryStr = entry(io);
98       int index = -1;
99
100       if (anOwner->ComesFromDecomposition()) // == Local Selection
101       {
102         TopoDS_Shape subShape = anOwner->Shape();
103         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
104         if (!aisShape.IsNull())
105         {
106           TopoDS_Shape bigShape = aisShape->Shape();
107
108           if (!bigShape.IsEqual(curBigShape))
109           {
110             curBigShape = bigShape;
111             subShapes.Clear();
112             TopExp::MapShapes(bigShape, subShapes);
113           }
114           index = subShapes.FindIndex(subShape);
115         }
116       }
117
118       if (!entryStr.isEmpty())
119       {
120         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
121         LightApp_DataOwner* owner;
122         if (index > -1) // Local Selection
123           owner = new LightApp_DataSubOwner (entryStr, index);
124         else if ( !anIO.IsNull() ) // Global Selection
125           owner = new LightApp_DataOwner( anIO );
126
127         aList.append(SUIT_DataOwnerPtr(owner));
128       }
129     }
130   }
131   else
132   {
133     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
134     {
135       Handle(AIS_InteractiveObject) io = ic->Current();
136
137       QString entryStr = entry( io );
138
139       if ( !entryStr.isEmpty() )
140       {
141         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
142         if ( !anIO.IsNull() ) {
143           LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
144           aList.append( SUIT_DataOwnerPtr( owner ) );
145         }
146       }
147     }
148   }
149
150   // add externally selected objects
151   SUIT_DataOwnerPtrList::const_iterator anExtIter;
152   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
153   {
154     aList.append(*anExtIter);
155   }
156 }
157
158 //================================================================
159 // Function : getEntityOwners
160 // Purpose  :
161 //================================================================
162 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
163                              const Handle(AIS_InteractiveContext)& theIC,
164                              SelectMgr_IndexedMapOfOwner& theMap )
165 {
166   if ( theObj.IsNull() || theIC.IsNull() )
167     return;
168
169   TColStd_ListOfInteger modes;
170   theIC->ActivatedModes( theObj, modes );
171
172   TColStd_ListIteratorOfListOfInteger itr( modes );
173   for (; itr.More(); itr.Next() ) {
174     int m = itr.Value();
175     if ( !theObj->HasSelection( m ) )
176       continue;
177
178     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
179
180     for ( sel->Init(); sel->More(); sel->Next() ) {
181 #if OCC_VERSION_LARGE > 0x06080100
182       const Handle(SelectMgr_SensitiveEntity) aHSenEntity = sel->Sensitive();
183       if( aHSenEntity.IsNull() )
184         continue;
185
186       Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
187 #else
188       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
189 #endif
190       if ( entity.IsNull() )
191         continue;
192
193       Handle(SelectMgr_EntityOwner) owner =
194         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
195       if ( !owner.IsNull() )
196         theMap.Add( owner );
197     }
198   }
199 }
200
201 //================================================================
202 // Function : setSelection
203 // Purpose  :
204 //================================================================
205 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
206 {
207   OCCViewer_Viewer* vw = viewer();
208   if ( !vw || !vw->isSelectionEnabled())
209     return;
210
211   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
212   bool anAutoBringToFront = aResourceMgr->booleanValue( "Geometry", "auto_bring_to_front", false );
213
214   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
215
216   // "entry - list_of_int" map for LOCAL selection
217   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
218
219   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
220   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
221
222   AIS_ListOfInteractive aDispList;
223   ic->DisplayedObjects( aDispList );
224
225   // build a map of data owner indexes to be selected.
226   // "entry - to - list_of_ids" map
227   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
228   {
229     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
230     if ( subOwner )
231     {
232       QString entry = subOwner->entry();
233       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
234       {
235         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
236         subIndexes.Add( subOwner->index() );
237         //indexesMap.replace( entry, subIndexes );
238       }
239       else
240       {
241         TColStd_IndexedMapOfInteger subIndexes;
242         subIndexes.Add( subOwner->index() );
243         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
244       }
245     }
246     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
247     {
248       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
249       if ( owner )
250       {
251         SalomeApp_Study* appStudy =
252           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
253         QString anEntry = appStudy->referencedToEntry( owner->entry() );
254
255         globalSelMap[anEntry] = 1;
256       }
257     }
258   }
259
260   // get all owners.  Fill "entry - list_of_owners" map.
261   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
262   {
263     Handle(AIS_InteractiveObject) io = it.Value();
264     QString entryStr = entry( io );
265     if ( !entryStr.isEmpty() )
266     {
267       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
268       SelectMgr_IndexedMapOfOwner owners;
269       getEntityOwners( io, ic, owners ); // get all owners
270
271       int i, n = owners.Extent();
272
273       // 1. Prepare map of shapes for local selection
274       TopTools_IndexedMapOfShape aMapOfShapes;
275       bool isLocal = false;
276
277       Handle(StdSelect_BRepOwner) anOwner;
278       for (i = 1; i <= n && !isLocal; i++)
279       {
280         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
281         if (!anOwner.IsNull() && anOwner->HasShape())
282         {
283           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
284           {
285             // has a local selection
286             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
287             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
288             {
289               isLocal = true;
290               TopoDS_Shape shape = aisShape->Shape();
291               aMapOfShapes.Clear();
292               TopExp::MapShapes(shape, aMapOfShapes);
293             }
294           }
295         }
296       }
297
298       // 2. Process all owners
299       for (i = 1; i <= n; i++)
300       {
301         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
302
303         if ( anOwner.IsNull() || !anOwner->HasShape() )
304           continue;
305
306         // GLOBAL selection
307         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
308         {
309           ownersmap.Add( anOwner );
310           globalSelMap[entryStr]++;
311         }
312         // LOCAL selection
313         else
314         {
315           if (isLocal)
316           {
317             const TColStd_IndexedMapOfInteger& subIndexes =
318               indexesMap.ChangeFind(entryStr.toLatin1().data());
319
320             const TopoDS_Shape& aSubShape = anOwner->Shape();
321             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
322
323             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
324             // which was passes in the parameter
325             if ( subIndexes.Contains( aSubShapeId ) )
326             {
327               ownersmap.Add( anOwner );
328             }
329           }
330         } // end of LOCAL selection
331       } // end of for(owners)
332     } // end of if(entry)
333   } // end of for(AIS_all_ios)
334
335   vw->unHighlightAll( false );
336
337   // DO the selection
338   int i = 1, n = ownersmap.Extent();
339   bool isAutoHilight = ic->AutomaticHilight();
340   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
341   for  (; i <= n; i++)
342   {
343     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
344     if ( owner->State() )
345       continue;
346
347     if ( ic->HasOpenedContext() )
348       ic->AddOrRemoveSelected( owner, false );
349     else
350       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
351   }
352
353   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
354
355   //rnv: In case Automatic Bring To Front viewer will be updated later
356   if(!anAutoBringToFront) {
357     if (n < 3000)
358       ic->HilightSelected(Standard_True);
359     else
360       vw->update();
361   }
362
363   // fill extra selected
364   mySelectedExternals.clear();
365   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
366   {
367     const LightApp_DataSubOwner* subOwner =
368       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
369     if ( !subOwner )
370     {
371       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
372       if ( owner )
373       {
374         SalomeApp_Study* appStudy =
375           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
376         QString anEntry = appStudy->referencedToEntry( owner->entry() );
377         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
378       }
379     }
380   }
381 }