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