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 static void AIS_Sel_CurrentSelection (Handle(AIS_Selection)& InputSel)
38 static Handle(AIS_Selection) theCurrentSelection;
39 if(!InputSel.IsNull())
40 theCurrentSelection = InputSel;
42 InputSel = theCurrentSelection;
45 static TColStd_SequenceOfTransient& AIS_Sel_GetSelections()
47 static TColStd_SequenceOfTransient Selections;
51 //=======================================================================
52 //function : AIS_Selection
54 //=======================================================================
55 AIS_Selection::AIS_Selection(const Standard_CString aName) :
56 myName(TCollection_AsciiString(aName)),
58 #if !defined USE_MAP && !defined OCC189
59 myresult(new TColStd_HArray1OfTransient(1,MaxSizeOfResult)),
64 myResultMap.ReSize( MaxSizeOfResult ); // for maximum performnace on medium selections ( < 100000 objects )
68 //=======================================================================
69 //function : CreateSelection
71 //=======================================================================
72 Standard_Boolean AIS_Selection::CreateSelection(const Standard_CString aName)
74 Handle(AIS_Selection) S = AIS_Selection::Selection(aName);
76 return Standard_False;
77 S = new AIS_Selection(aName);
78 AIS_Sel_GetSelections().Prepend(S);
79 AIS_Sel_CurrentSelection(S);
84 //=======================================================================
85 //function : Selection
87 //=======================================================================
88 Handle(AIS_Selection) AIS_Selection::Selection(const Standard_CString aName)
90 Handle(AIS_Selection) S;
91 if(AIS_Sel_GetSelections().IsEmpty()) return S;
93 Handle(Standard_Transient) curobj;
94 Handle(AIS_Selection) Sel;
95 // Standard_Boolean found(Standard_False);
96 for(Standard_Integer I =1; I<= AIS_Sel_GetSelections().Length();I++){
97 curobj = AIS_Sel_GetSelections().Value(I);
98 Sel = *((Handle(AIS_Selection)*)&curobj);
99 if(Sel->myName.IsEqual(aName))
106 //=======================================================================
109 //=======================================================================
110 Standard_Boolean AIS_Selection::Find(const Standard_CString aName)
112 Handle(AIS_Selection) S = AIS_Selection::Selection(aName);
116 //=======================================================================
117 //function : SetCurrentSelection
119 //=======================================================================
120 Standard_Boolean AIS_Selection::SetCurrentSelection (const Standard_CString aName)
122 AIS_Selection::CreateSelection(aName);
125 Handle(AIS_Selection) anAISSelection = AIS_Selection::Selection(aName) ;
126 AIS_Sel_CurrentSelection ( anAISSelection ) ;
127 return Standard_True;
130 //=======================================================================
133 //=======================================================================
134 void AIS_Selection::Select()
136 Handle(AIS_Selection) S;
137 AIS_Sel_CurrentSelection (S);
140 #if defined OCC189 || defined USE_MAP
143 S->myResultMap.Clear();
149 //=======================================================================
150 //function : CurrentSelection
152 //=======================================================================
153 Handle(AIS_Selection) AIS_Selection::CurrentSelection() {
154 Handle(AIS_Selection) S;
155 AIS_Sel_CurrentSelection (S);
158 //=======================================================================
161 //=======================================================================
162 AIS_SelectStatus AIS_Selection::Select(const Handle(Standard_Transient)& anObject)
164 Handle(AIS_Selection) S;
165 AIS_Sel_CurrentSelection (S);
166 if(S.IsNull()) return AIS_SS_NotDone;
167 Handle(AIS_InteractiveObject) anAISObj;
168 Handle(SelectMgr_EntityOwner) owner = Handle(SelectMgr_EntityOwner)::DownCast( anObject );
169 if ( owner.IsNull() )
170 anAISObj = Handle(AIS_InteractiveObject)::DownCast( anObject );
172 TColStd_Array1OfTransient& arr = S->myresult->ChangeArray1();
174 Standard_Integer Found(-1);
179 Standard_Boolean selected = Standard_False;
180 if ( !owner.IsNull() )
181 selected = owner->State() != 0;
183 else if(!anAISObj.IsNull())
184 selected = anAISObj->State();
188 if ( selected ) // looking up index of object
191 for( i=arr.Lower() && Found==-1;i<=S->myNb;i++){
195 // If it is not inside, it is added...
198 if ( !selected || Found == -1 ) {
202 if((S->myNb)+1>arr.Length()){
203 Handle(TColStd_HArray1OfTransient) NiouTab = new TColStd_HArray1OfTransient(1,arr.Length()+MaxSizeOfResult);
204 for(i=arr.Lower();i<=S->myNb;i++){
205 const Handle(Standard_Transient)& T = S->myresult->Value(i);
206 NiouTab->SetValue(i,T);
209 S->myresult = NiouTab;
213 S->myresult->SetValue(S->myNb,anObject);
216 // it was inside and it is removed...
217 for(i=Found;i<=S->myNb;i++)
220 #elif !defined USE_MAP //OCC189
221 AIS_NListTransient::Iterator anIter ( S->myresult );
222 for ( ; anIter.More(); anIter.Next() )
223 if ( anIter.Value() == anObject ) {
224 S->myresult.Remove( anIter );
225 return AIS_SS_Removed;
228 S->myresult.Append( anObject );
231 if ( S->myResultMap.IsBound( anObject ) ){
232 AIS_NListTransient::Iterator aListIter = S->myResultMap.Find( anObject );
233 //skt-----------------------------------------------------------------
234 if( S->myIterator == aListIter )
235 if( S->myIterator.More() )
236 S->myIterator.Next();
238 S->myIterator = AIS_NListTransient::Iterator();
239 //--------------------------------------------------------------------
241 // In the mode of advanced mesh selection only one owner is created
242 // for all selection modes. It is necessary to check the current detected
243 // entity and remove the owner from map only if the detected entity is
244 // the same as previous selected (IsForcedHilight call)
245 if( !anAISObj.IsNull() || ( !owner.IsNull() && !owner->IsForcedHilight() ) )
248 S->myresult.Remove( aListIter );
249 S->myResultMap.UnBind( anObject );
251 // update list iterator for next object in <myresult> list if any
252 if ( aListIter.More() ){
253 const Handle(Standard_Transient)& aNextObject = aListIter.Value();
254 if ( S->myResultMap.IsBound( aNextObject ) )
255 S->myResultMap( aNextObject ) = aListIter;
257 S->myResultMap.Bind( aNextObject, aListIter );
259 return AIS_SS_Removed;
265 AIS_NListTransient::Iterator aListIter;
266 S->myresult.Append( anObject, aListIter );
267 S->myResultMap.Bind( anObject, aListIter );
270 return AIS_SS_Removed;
274 //=======================================================================
275 //function : AddSelect
276 //purpose : Always add int the selection
277 //=======================================================================
278 AIS_SelectStatus AIS_Selection::AddSelect(const Handle(Standard_Transient)& anObject)
280 Handle(AIS_Selection) S;
281 AIS_Sel_CurrentSelection (S);
282 if(S.IsNull()) return AIS_SS_NotDone;
285 TColStd_Array1OfTransient& arr = S->myresult->ChangeArray1();
287 if((S->myNb)+1>arr.Length()){
288 Handle(TColStd_HArray1OfTransient) NiouTab = new TColStd_HArray1OfTransient(1,arr.Length()+MaxSizeOfResult);
289 for(i=arr.Lower();i<=S->myNb;i++){
290 const Handle(Standard_Transient)& T = S->myresult->Value(i);
291 NiouTab->SetValue(i,T);
293 S->myresult = NiouTab;
296 S->myresult->SetValue(S->myNb,anObject);
297 #elif !defined USE_MAP //OCC189
298 S->myresult.Append( anObject );
300 if ( S->myResultMap.IsBound( anObject ) )
301 return AIS_SS_NotDone;
303 AIS_NListTransient::Iterator aListIter;
304 S->myresult.Append( anObject, aListIter );
305 S->myResultMap.Bind( anObject, aListIter );
311 //=======================================================================
312 //function : ClearAndSelect
314 //=======================================================================
316 void AIS_Selection::ClearAndSelect(const Handle(Standard_Transient)& anObject) {
317 Handle(AIS_Selection) S;
318 AIS_Sel_CurrentSelection (S);
319 if(S.IsNull()) return;
327 //=======================================================================
330 //=======================================================================
331 Standard_Integer AIS_Selection::Extent() {
332 Handle(AIS_Selection) S;
333 AIS_Sel_CurrentSelection (S);
334 #if !defined OCC189 && !defined USE_MAP
337 return S->myresult.Extent();
341 //=======================================================================
344 //=======================================================================
345 Handle(Standard_Transient) AIS_Selection::Single()
347 Handle(AIS_Selection) S;
348 AIS_Sel_CurrentSelection (S);
353 //=======================================================================
354 //function : IsSelected
356 //=======================================================================
357 Standard_Boolean AIS_Selection::IsSelected(const Handle(Standard_Transient)& anObject)
359 Handle(AIS_Selection) S;
360 AIS_Sel_CurrentSelection (S);
361 if(S.IsNull()) return Standard_False;
363 for(S->Init();S->More();S->Next()){
364 if(S->Value()==anObject)
365 return Standard_True;
367 return Standard_False;
369 return S->myResultMap.IsBound( anObject );
373 //=======================================================================
376 //=======================================================================
378 Standard_Integer AIS_Selection::Index(const Standard_CString aName)
380 Handle (Standard_Transient) curobj;
381 for(Standard_Integer I =1; I<= AIS_Sel_GetSelections().Length();I++){
382 curobj = AIS_Sel_GetSelections().Value(I);
383 if((*((Handle(AIS_Selection)*)&curobj))->myName.IsEqual(aName))
389 //=======================================================================
392 //=======================================================================
394 void AIS_Selection::Remove(const Standard_CString aName)
396 Standard_Integer I = AIS_Selection::Index(aName);
399 Handle(AIS_Selection) selection = Handle(AIS_Selection)::DownCast( AIS_Sel_GetSelections().Value(I) );
400 Standard_Integer stored = selection->NbStored();
404 AIS_Sel_GetSelections().Remove(I);