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