]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMGUI/GEOMGUI_OCCSelector.cxx
Salome HOME
Porting to OCCT 7.4 dev
[modules/geom.git] / src / GEOMGUI / GEOMGUI_OCCSelector.cxx
1 // Copyright (C) 2007-2019  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 #if OCC_VERSION_LARGE <= 0x07030000
84   if (ic->HasOpenedContext())
85   {
86 #endif
87     TopoDS_Shape curBigShape;
88     TopTools_IndexedMapOfShape subShapes;
89
90     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
91     {
92
93       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( ic->SelectedInteractive() );
94
95       QString entryStr = entry(io);
96       int index = -1;
97
98       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast( ic->SelectedOwner() );
99
100       if (!anOwner.IsNull() && 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 #if OCC_VERSION_LARGE <= 0x07030000
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 #endif
151
152   // add externally selected objects
153   SUIT_DataOwnerPtrList::const_iterator anExtIter;
154   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
155   {
156     aList.append(*anExtIter);
157   }
158 }
159
160 //================================================================
161 // Function : getEntityOwners
162 // Purpose  :
163 //================================================================
164 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
165                              const Handle(AIS_InteractiveContext)& theIC,
166                              SelectMgr_IndexedMapOfOwner& theMap )
167 {
168   if ( theObj.IsNull() || theIC.IsNull() )
169     return;
170
171   TColStd_ListOfInteger modes;
172   theIC->ActivatedModes( theObj, modes );
173
174   TColStd_ListIteratorOfListOfInteger itr( modes );
175   for (; itr.More(); itr.Next() ) {
176     int m = itr.Value();
177     if ( !theObj->HasSelection( m ) )
178       continue;
179
180     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
181
182     for ( sel->Init(); sel->More(); sel->Next() ) {
183       const Handle(SelectMgr_SensitiveEntity) aHSenEntity = sel->Sensitive();
184       if( aHSenEntity.IsNull() )
185         continue;
186
187       Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
188       if ( entity.IsNull() )
189         continue;
190
191       Handle(SelectMgr_EntityOwner) owner =
192         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
193       if ( !owner.IsNull() )
194         theMap.Add( owner );
195     }
196   }
197 }
198
199 //================================================================
200 // Function : setSelection
201 // Purpose  :
202 //================================================================
203 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
204 {
205   OCCViewer_Viewer* vw = viewer();
206   if ( !vw || !vw->isSelectionEnabled())
207     return;
208
209   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
210   bool anAutoBringToFront = aResourceMgr->booleanValue( "Geometry", "auto_bring_to_front", false );
211
212   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
213
214   // "entry - list_of_int" map for LOCAL selection
215   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
216
217   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
218   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
219
220   AIS_ListOfInteractive aDispList;
221   ic->DisplayedObjects( aDispList );
222
223   // build a map of data owner indexes to be selected.
224   // "entry - to - list_of_ids" map
225   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
226   {
227     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
228     if ( subOwner )
229     {
230       QString entry = subOwner->entry();
231       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toUtf8().data())))
232       {
233         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toUtf8().data());
234         subIndexes.Add( subOwner->index() );
235         //indexesMap.replace( entry, subIndexes );
236       }
237       else
238       {
239         TColStd_IndexedMapOfInteger subIndexes;
240         subIndexes.Add( subOwner->index() );
241         indexesMap.Bind(entry.toUtf8().data(), subIndexes);
242       }
243     }
244     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
245     {
246       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
247       if ( owner )
248       {
249         SalomeApp_Study* appStudy =
250           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
251         QString anEntry = appStudy->referencedToEntry( owner->entry() );
252
253         globalSelMap[anEntry] = 1;
254       }
255     }
256   }
257
258   // get all owners.  Fill "entry - list_of_owners" map.
259   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
260   {
261     Handle(AIS_InteractiveObject) io = it.Value();
262     QString entryStr = entry( io );
263     if ( !entryStr.isEmpty() )
264     {
265       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
266       SelectMgr_IndexedMapOfOwner owners;
267       getEntityOwners( io, ic, owners ); // get all owners
268
269       int i, n = owners.Extent();
270
271       // 1. Prepare map of shapes for local selection
272       TopTools_IndexedMapOfShape aMapOfShapes;
273       bool isLocal = false;
274
275       Handle(StdSelect_BRepOwner) anOwner;
276       for (i = 1; i <= n && !isLocal; i++)
277       {
278         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
279         if (!anOwner.IsNull() && anOwner->HasShape())
280         {
281           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
282           {
283             // has a local selection
284             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
285             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toUtf8().data()))
286             {
287               isLocal = true;
288               TopoDS_Shape shape = aisShape->Shape();
289               aMapOfShapes.Clear();
290               TopExp::MapShapes(shape, aMapOfShapes);
291             }
292           }
293         }
294       }
295
296       // 2. Process all owners
297       for (i = 1; i <= n; i++)
298       {
299         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
300
301         if ( anOwner.IsNull() || !anOwner->HasShape() ) {
302           if ( globalSelMap.contains( entryStr ) ) {
303             Handle(GEOM_Annotation::GEOM_AnnotationOwner) anAnnotationOwner =
304                                Handle(GEOM_Annotation::GEOM_AnnotationOwner)::DownCast(owners( i ));
305             if ( !anAnnotationOwner.IsNull() ) {
306               ownersmap.Add( anAnnotationOwner );
307             }
308           }
309           continue;
310         }
311
312         // GLOBAL selection
313         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
314         {
315           ownersmap.Add( anOwner );
316           globalSelMap[entryStr]++;
317         }
318         // LOCAL selection
319         else
320         {
321           if (isLocal)
322           {
323             const TColStd_IndexedMapOfInteger& subIndexes =
324               indexesMap.ChangeFind(entryStr.toUtf8().data());
325
326             const TopoDS_Shape& aSubShape = anOwner->Shape();
327             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
328
329             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
330             // which was passes in the parameter
331             if ( subIndexes.Contains( aSubShapeId ) )
332             {
333               ownersmap.Add( anOwner );
334             }
335           }
336         } // end of LOCAL selection
337       } // end of for(owners)
338     } // end of if(entry)
339   } // end of for(AIS_all_ios)
340
341   vw->unHighlightAll( false );
342
343   // DO the selection
344   int i = 1, n = ownersmap.Extent();
345   bool isAutoHilight = ic->AutomaticHilight();
346   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
347   for  (; i <= n; i++)
348   {
349     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
350     if ( owner->State() )
351       continue;
352
353     if ( owner->ComesFromDecomposition() )
354       ic->AddOrRemoveSelected( owner, false ); 
355     else
356       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
357   }
358
359   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
360
361   //rnv: In case Automatic Bring To Front viewer will be updated later
362   if(!anAutoBringToFront) {
363     if (n < 3000)
364       ic->HilightSelected(Standard_True);
365     else
366       vw->update();
367   }
368
369   // fill extra selected
370   mySelectedExternals.clear();
371   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
372   {
373     const LightApp_DataSubOwner* subOwner =
374       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
375     if ( !subOwner )
376     {
377       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
378       if ( owner )
379       {
380         SalomeApp_Study* appStudy =
381           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
382         QString anEntry = appStudy->referencedToEntry( owner->entry() );
383         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
384       }
385     }
386   }
387 }