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