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