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