1 #include <AIS_Selection.ixx>
2 #include <TCollection_AsciiString.hxx>
3 #include <TColStd_SequenceOfTransient.hxx>
5 #define BUC60953 // SAV_050701 : the array of selected objects has always the same length
6 // independently of number of objects selected. Thus, if there were selected
7 // more than MaxSizeOfResult objects we have got an exception.
9 // Moreover, Select method was optimized a little bit.
10 // Now it checks the state of incoming owner. If the state is 0
11 // there is no searching for object in <myresult> array.
15 #define OCC189 //SAV: 18//03/02 array was replaced with list.
17 #define OCC1039 //SAV: 25/11/02 clearing selected objects if any on the AIS_Selection remove.
19 #define USE_MAP //san : 18/04/03 USE_MAP - additional datamap is used to speed up access
20 //to certain owners in <myresult> list
23 #include <SelectMgr_EntityOwner.hxx>
26 #ifdef OCC138 //VTN Avoding infinit loop in AddOrRemoveSelected and AddOrRemoveCurrentObject methods.
27 #include <AIS_InteractiveObject.hxx>
31 #define MaxSizeOfResult 10000
33 #define MaxSizeOfResult 100000
36 // beurk mais moins qu''avant.... rob
37 static void AIS_Sel_CurrentSelection (Handle(AIS_Selection)& InputSel)
39 static Handle(AIS_Selection) theCurrentSelection;
40 if(!InputSel.IsNull())
41 theCurrentSelection = InputSel;
43 InputSel = theCurrentSelection;
46 static TColStd_SequenceOfTransient& AIS_Sel_GetSelections()
48 static TColStd_SequenceOfTransient Selections;
52 //=======================================================================
53 //function : AIS_Selection
55 //=======================================================================
56 AIS_Selection::AIS_Selection(const Standard_CString aName) :
57 myName(TCollection_AsciiString(aName)),
59 #if !defined USE_MAP && !defined OCC189
60 myresult(new TColStd_HArray1OfTransient(1,MaxSizeOfResult)),
65 myResultMap.ReSize( MaxSizeOfResult ); // for maximum performnace on medium selections ( < 100000 objects )
69 //=======================================================================
70 //function : CreateSelection
72 //=======================================================================
73 Standard_Boolean AIS_Selection::CreateSelection(const Standard_CString aName)
75 Handle(AIS_Selection) S = AIS_Selection::Selection(aName);
77 return Standard_False;
78 S = new AIS_Selection(aName);
79 AIS_Sel_GetSelections().Prepend(S);
80 AIS_Sel_CurrentSelection(S);
85 //=======================================================================
86 //function : Selection
88 //=======================================================================
89 Handle(AIS_Selection) AIS_Selection::Selection(const Standard_CString aName)
91 Handle(AIS_Selection) S;
92 if(AIS_Sel_GetSelections().IsEmpty()) return S;
94 Handle(Standard_Transient) curobj;
95 Handle(AIS_Selection) Sel;
96 // Standard_Boolean found(Standard_False);
97 for(Standard_Integer I =1; I<= AIS_Sel_GetSelections().Length();I++){
98 curobj = AIS_Sel_GetSelections().Value(I);
99 Sel = *((Handle(AIS_Selection)*)&curobj);
100 if(Sel->myName.IsEqual(aName))
107 //=======================================================================
110 //=======================================================================
111 Standard_Boolean AIS_Selection::Find(const Standard_CString aName)
113 Handle(AIS_Selection) S = AIS_Selection::Selection(aName);
117 //=======================================================================
118 //function : SetCurrentSelection
120 //=======================================================================
121 Standard_Boolean AIS_Selection::SetCurrentSelection (const Standard_CString aName)
123 AIS_Selection::CreateSelection(aName); // juste pour voir... ?????????????
126 Handle(AIS_Selection) anAISSelection = AIS_Selection::Selection(aName) ;
127 AIS_Sel_CurrentSelection ( anAISSelection ) ;
128 return Standard_True;
131 //=======================================================================
134 //=======================================================================
135 void AIS_Selection::Select()
137 Handle(AIS_Selection) S;
138 AIS_Sel_CurrentSelection (S);
141 #if defined OCC189 || defined USE_MAP
144 S->myResultMap.Clear();
150 //=======================================================================
151 //function : CurrentSelection
153 //=======================================================================
154 Handle(AIS_Selection) AIS_Selection::CurrentSelection() {
155 Handle(AIS_Selection) S;
156 AIS_Sel_CurrentSelection (S);
159 //=======================================================================
162 //=======================================================================
163 AIS_SelectStatus AIS_Selection::Select(const Handle(Standard_Transient)& anObject)
165 Handle(AIS_Selection) S;
166 AIS_Sel_CurrentSelection (S);
167 if(S.IsNull()) return AIS_SS_NotDone;
168 Handle(AIS_InteractiveObject) anAISObj;
169 Handle(SelectMgr_EntityOwner) owner = Handle(SelectMgr_EntityOwner)::DownCast( anObject );
170 if ( owner.IsNull() )
171 anAISObj = Handle(AIS_InteractiveObject)::DownCast( anObject );
173 TColStd_Array1OfTransient& arr = S->myresult->ChangeArray1();
175 Standard_Integer Found(-1);
180 Standard_Boolean selected = Standard_False;
181 if ( !owner.IsNull() )
182 selected = owner->State() != 0;
184 else if(!anAISObj.IsNull())
185 selected = anAISObj->State();
189 if ( selected ) // looking up index of object
192 for( i=arr.Lower() && Found==-1;i<=S->myNb;i++){
196 // Il n''est pas dedans, on le rajoute....
199 if ( !selected || Found == -1 ) {
203 if((S->myNb)+1>arr.Length()){
204 Handle(TColStd_HArray1OfTransient) NiouTab = new TColStd_HArray1OfTransient(1,arr.Length()+MaxSizeOfResult);
206 for(i=arr.Lower();i<=S->myNb;i++){
207 const Handle(Standard_Transient)& T = S->myresult->Value(i);
208 NiouTab->SetValue(i,T);
211 S->myresult = NiouTab;
215 S->myresult->SetValue(S->myNb,anObject);
218 // il etait dedans on l''enleve...
219 for(i=Found;i<=S->myNb;i++)
222 #elif !defined USE_MAP //OCC189
223 AIS_NListTransient::Iterator anIter ( S->myresult );
224 for ( ; anIter.More(); anIter.Next() )
225 if ( anIter.Value() == anObject ) {
226 S->myresult.Remove( anIter );
227 return AIS_SS_Removed;
230 S->myresult.Append( anObject );
233 if ( S->myResultMap.IsBound( anObject ) ){
234 AIS_NListTransient::Iterator aListIter = S->myResultMap.Find( anObject );
235 //skt-----------------------------------------------------------------
236 if( S->myIterator == aListIter )
237 if( S->myIterator.More() )
238 S->myIterator.Next();
240 S->myIterator = AIS_NListTransient::Iterator();
241 //--------------------------------------------------------------------
243 // In the mode of advanced mesh selection only one owner is created
244 // for all selection modes. It is necessary to check the current detected
245 // entity and remove the owner from map only if the detected entity is
246 // the same as previous selected (IsForcedHilight call)
247 if( !anAISObj.IsNull() || ( !owner.IsNull() && !owner->IsForcedHilight() ) )
250 S->myresult.Remove( aListIter );
251 S->myResultMap.UnBind( anObject );
253 // update list iterator for next object in <myresult> list if any
254 if ( aListIter.More() ){
255 const Handle(Standard_Transient)& aNextObject = aListIter.Value();
256 if ( S->myResultMap.IsBound( aNextObject ) )
257 S->myResultMap( aNextObject ) = aListIter;
259 S->myResultMap.Bind( aNextObject, aListIter );
261 return AIS_SS_Removed;
267 AIS_NListTransient::Iterator aListIter;
268 S->myresult.Append( anObject, aListIter );
269 S->myResultMap.Bind( anObject, aListIter );
272 return AIS_SS_Removed;
276 //=======================================================================
277 //function : AddSelect
278 //purpose : Always add int the selection
279 //=======================================================================
280 AIS_SelectStatus AIS_Selection::AddSelect(const Handle(Standard_Transient)& anObject)
282 Handle(AIS_Selection) S;
283 AIS_Sel_CurrentSelection (S);
284 if(S.IsNull()) return AIS_SS_NotDone;
287 TColStd_Array1OfTransient& arr = S->myresult->ChangeArray1();
289 if((S->myNb)+1>arr.Length()){
290 Handle(TColStd_HArray1OfTransient) NiouTab = new TColStd_HArray1OfTransient(1,arr.Length()+MaxSizeOfResult);
292 for(i=arr.Lower();i<=S->myNb;i++){
293 const Handle(Standard_Transient)& T = S->myresult->Value(i);
294 NiouTab->SetValue(i,T);
296 S->myresult = NiouTab;
299 S->myresult->SetValue(S->myNb,anObject);
300 #elif !defined USE_MAP //OCC189
301 S->myresult.Append( anObject );
303 if ( S->myResultMap.IsBound( anObject ) )
304 return AIS_SS_NotDone;
306 AIS_NListTransient::Iterator aListIter;
307 S->myresult.Append( anObject, aListIter );
308 S->myResultMap.Bind( anObject, aListIter );
314 //=======================================================================
315 //function : ClearAndSelect
317 //=======================================================================
319 void AIS_Selection::ClearAndSelect(const Handle(Standard_Transient)& anObject) {
320 Handle(AIS_Selection) S;
321 AIS_Sel_CurrentSelection (S);
322 if(S.IsNull()) return;
330 //=======================================================================
333 //=======================================================================
334 Standard_Integer AIS_Selection::Extent() {
335 Handle(AIS_Selection) S;
336 AIS_Sel_CurrentSelection (S);
337 #if !defined OCC189 && !defined USE_MAP
340 return S->myresult.Extent();
344 //=======================================================================
347 //=======================================================================
348 Handle(Standard_Transient) AIS_Selection::Single()
350 Handle(AIS_Selection) S;
351 AIS_Sel_CurrentSelection (S);
356 //=======================================================================
357 //function : IsSelected
359 //=======================================================================
360 Standard_Boolean AIS_Selection::IsSelected(const Handle(Standard_Transient)& anObject)
362 Handle(AIS_Selection) S;
363 AIS_Sel_CurrentSelection (S);
364 if(S.IsNull()) return Standard_False;
366 for(S->Init();S->More();S->Next()){
367 if(S->Value()==anObject)
368 return Standard_True;
370 return Standard_False;
372 return S->myResultMap.IsBound( anObject );
376 //=======================================================================
379 //=======================================================================
381 Standard_Integer AIS_Selection::Index(const Standard_CString aName)
383 Handle (Standard_Transient) curobj;
384 for(Standard_Integer I =1; I<= AIS_Sel_GetSelections().Length();I++){
385 curobj = AIS_Sel_GetSelections().Value(I);
386 if((*((Handle(AIS_Selection)*)&curobj))->myName.IsEqual(aName))
392 //=======================================================================
395 //=======================================================================
397 void AIS_Selection::Remove(const Standard_CString aName)
399 Standard_Integer I = AIS_Selection::Index(aName);
402 Handle(AIS_Selection) selection = Handle(AIS_Selection)::DownCast( AIS_Sel_GetSelections().Value(I) );
403 Standard_Integer stored = selection->NbStored();
407 AIS_Sel_GetSelections().Remove(I);