Salome HOME
2569df22c68875fa751bc14005841af50190bf75
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_OCCSelector.cxx
1 // Copyright (C) 2013-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : HEXABLOCKGUI_OCCSelector.cxx
21 //
22
23 #include "HEXABLOCKGUI_OCCSelector.h"
24 #include "HEXABLOCKGUI_SalomeTools.hxx"
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 #include <Basics_OCCTVersion.hxx>
50
51 using namespace HEXABLOCK::GUI;
52
53 //================================================================
54 // Function : HEXABLOCKGUI_OCCSelector
55 // Purpose  :
56 //================================================================
57 HEXABLOCKGUI_OCCSelector::HEXABLOCKGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
58 : LightApp_OCCSelector( viewer, mgr )
59 {
60 }
61
62 //================================================================
63 // Function : ~HEXABLOCKGUI_OCCSelector
64 // Purpose  :
65 //================================================================
66 HEXABLOCKGUI_OCCSelector::~HEXABLOCKGUI_OCCSelector()
67 {
68 }
69
70 //================================================================
71 // Function : getSelection
72 // Purpose  :
73 //================================================================
74 void HEXABLOCKGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
75 {
76   OCCViewer_Viewer* vw = viewer();
77   if (!vw)
78     return;
79
80   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
81 #if OCC_VERSION_LARGE <= 0x07030000
82   if (ic->HasOpenedContext())
83   {
84 #endif
85     TopoDS_Shape curBigShape;
86     TopTools_IndexedMapOfShape subShapes;
87
88     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
89     {
90       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(ic->SelectedInteractive());
91
92       QString entryStr = entry(io);
93       int index = -1;
94
95       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
96
97       if (!anOwner.IsNull() && anOwner->ComesFromDecomposition()) // == Local Selection
98       {
99         TopoDS_Shape subShape = anOwner->Shape();
100         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
101         if (!aisShape.IsNull())
102         {
103           TopoDS_Shape bigShape = aisShape->Shape();
104
105           if (!bigShape.IsEqual(curBigShape))
106           {
107             curBigShape = bigShape;
108             subShapes.Clear();
109             TopExp::MapShapes(bigShape, subShapes);
110           }
111           index = subShapes.FindIndex(subShape);
112         }
113       }
114
115       if (!entryStr.isEmpty())
116       {
117         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
118         LightApp_DataOwner* owner;
119         if (index > -1) // Local Selection
120           owner = new LightApp_DataSubOwner (entryStr, index);
121         else if ( !anIO.IsNull() ) // Global Selection
122           owner = new LightApp_DataOwner( anIO );
123
124         aList.append(SUIT_DataOwnerPtr(owner));
125       }
126     }
127 #if OCC_VERSION_LARGE <= 0x07030000
128   }
129   else
130   {
131     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
132     {
133       Handle(AIS_InteractiveObject) io = ic->Current();
134
135       QString entryStr = entry( io );
136
137       if ( !entryStr.isEmpty() )
138       {
139         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
140         if ( !anIO.IsNull() ) {
141           LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
142           aList.append( SUIT_DataOwnerPtr( owner ) );
143         }
144       }
145     }
146   }
147 #endif
148
149   // add externally selected objects
150   SUIT_DataOwnerPtrList::const_iterator anExtIter, itEnd;
151   for (anExtIter = mySelectedExternals.begin(), itEnd = mySelectedExternals.end(); anExtIter != itEnd; anExtIter++)
152   {
153     aList.append(*anExtIter);
154   }
155 }
156
157 //================================================================
158 // Function : setSelection
159 // Purpose  :
160 //================================================================
161 void HEXABLOCKGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
162 {
163   OCCViewer_Viewer* vw = viewer();
164   if ( !vw )
165     return;
166
167   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
168
169   // "entry - list_of_int" map for LOCAL selection
170   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
171
172   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
173   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
174
175   AIS_ListOfInteractive aDispList;
176   ic->DisplayedObjects( aDispList );
177
178   // build a map of data owner indexes to be selected.
179   // "entry - to - list_of_ids" map
180   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(), itEnd = aList.end(); itr != itEnd; ++itr )
181   {
182     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
183     if ( subOwner )
184     {
185       QString entry = subOwner->entry();
186       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
187       {
188         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
189         subIndexes.Add( subOwner->index() );
190         //indexesMap.replace( entry, subIndexes );
191       }
192       else
193       {
194         TColStd_IndexedMapOfInteger subIndexes;
195         subIndexes.Add( subOwner->index() );
196         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
197       }
198     }
199     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
200     {
201       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
202       if ( owner )
203       {
204         SalomeApp_Study* appStudy =
205           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
206         QString anEntry = appStudy->referencedToEntry( owner->entry() );
207
208         globalSelMap[anEntry] = 1;
209       }
210     }
211   }
212
213   // get all owners.  Fill "entry - list_of_owners" map.
214   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
215   {
216     Handle(AIS_InteractiveObject) io = it.Value();
217     QString entryStr = entry( io );
218     if ( !entryStr.isEmpty() )
219     {
220       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
221       SelectMgr_IndexedMapOfOwner owners;
222       getEntityOwners( io, ic, owners ); // get all owners
223
224       int i, n = owners.Extent();
225
226       // 1. Prepare map of shapes for local selection
227       TopTools_IndexedMapOfShape aMapOfShapes;
228       bool isLocal = false;
229
230       Handle(StdSelect_BRepOwner) anOwner;
231       for (i = 1; i <= n && !isLocal; i++)
232       {
233         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
234         if (!anOwner.IsNull() && anOwner->HasShape())
235         {
236           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
237           {
238             // has a local selection
239             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
240             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
241             {
242               isLocal = true;
243               TopoDS_Shape shape = aisShape->Shape();
244               aMapOfShapes.Clear();
245               TopExp::MapShapes(shape, aMapOfShapes);
246             }
247           }
248         }
249       }
250
251       // 2. Process all owners
252       for (i = 1; i <= n; i++)
253       {
254         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
255
256         if ( anOwner.IsNull() || !anOwner->HasShape() )
257           continue;
258
259         // GLOBAL selection
260         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
261         {
262           ownersmap.Add( anOwner );
263           globalSelMap[entryStr]++;
264         }
265         // LOCAL selection
266         else
267         {
268           if (isLocal)
269           {
270             const TColStd_IndexedMapOfInteger& subIndexes =
271               indexesMap.ChangeFind(entryStr.toLatin1().data());
272
273             const TopoDS_Shape& aSubShape = anOwner->Shape();
274             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
275
276             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
277             // which was passes in the parameter
278             if ( subIndexes.Contains( aSubShapeId ) )
279             {
280               ownersmap.Add( anOwner );
281             }
282           }
283         } // end of LOCAL selection
284       } // end of for(owners)
285     } // end of if(entry)
286   } // end of for(AIS_all_ios)
287
288   vw->unHighlightAll( false );
289
290   // DO the selection
291   int i = 1, n = ownersmap.Extent();
292   bool isAutoHilight = ic->AutomaticHilight();
293   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
294   for  (; i <= n; i++)
295   {
296     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
297     if ( owner->State() )
298       continue;
299
300     if ( owner->ComesFromDecomposition() )
301       ic->AddOrRemoveSelected( owner, false );
302     else
303       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
304   }
305   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
306   if (n < 3000)
307     ic->HilightSelected(/*updateviewer*/Standard_True);
308   else
309     vw->update();
310
311   // fill extra selected
312   mySelectedExternals.clear();
313   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(), itEnd = aList.end(); itr2 != itEnd; ++itr2 )
314   {
315     const LightApp_DataSubOwner* subOwner =
316       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
317     if ( !subOwner )
318     {
319       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
320       if ( owner )
321       {
322         SalomeApp_Study* appStudy =
323           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
324         QString anEntry = appStudy->referencedToEntry( owner->entry() );
325         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
326       }
327     }
328   }
329 }