summaryrefslogtreecommitdiff
path: root/src/AIS/AIS_Selection.cxx
blob: 328133aeadad5bb09f416a0ab56886bfc8fca02a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <AIS_Selection.ixx>
#include <TCollection_AsciiString.hxx>
#include <TColStd_SequenceOfTransient.hxx>

#define BUC60953   // SAV_050701 : the array of selected objects has always the same length
                   // independently of number of objects selected. Thus, if there were selected
                   // more than MaxSizeOfResult objects we have got an exception.
                   // 
                   // Moreover, Select method was optimized a little bit.
                   // Now it checks the state of incoming owner. If the state is 0
                   // there is no searching for object in <myresult> array.

#define OCC138

#define OCC189     //SAV: 18//03/02 array was replaced with list.

#define OCC1039    //SAV: 25/11/02 clearing selected objects if any on the AIS_Selection remove.

#define USE_MAP    //san : 18/04/03 USE_MAP - additional datamap is used to speed up access 
                   //to certain owners in <myresult> list

#ifdef BUC60953
#include <SelectMgr_EntityOwner.hxx>
#endif

#ifdef OCC138      //VTN Avoding infinit loop in AddOrRemoveSelected and AddOrRemoveCurrentObject methods.
#include <AIS_InteractiveObject.hxx>
#endif

#ifndef USE_MAP
#define MaxSizeOfResult 10000
#else
#define MaxSizeOfResult 100000
#endif

static void AIS_Sel_CurrentSelection (Handle(AIS_Selection)& InputSel)     
{
  static Handle(AIS_Selection) theCurrentSelection;
  if(!InputSel.IsNull())
    theCurrentSelection = InputSel;
  else
    InputSel = theCurrentSelection;
}

static TColStd_SequenceOfTransient&  AIS_Sel_GetSelections()
{
  static TColStd_SequenceOfTransient Selections;
  return Selections;
}

//=======================================================================
//function : AIS_Selection
//purpose  : 
//=======================================================================
AIS_Selection::AIS_Selection(const Standard_CString aName) :
myName(TCollection_AsciiString(aName)),
mycuri(0), 
#if !defined USE_MAP && !defined OCC189
myresult(new TColStd_HArray1OfTransient(1,MaxSizeOfResult)),
#endif
myNb(0)
{
#ifdef USE_MAP 
  myResultMap.ReSize( MaxSizeOfResult ); // for maximum performnace on medium selections ( < 100000 objects )
#endif
}

//=======================================================================
//function : CreateSelection
//purpose  : 
//=======================================================================
Standard_Boolean AIS_Selection::CreateSelection(const Standard_CString aName)
{ 
  Handle(AIS_Selection) S = AIS_Selection::Selection(aName);
  if(!S.IsNull())
    return Standard_False;
  S = new AIS_Selection(aName);
  AIS_Sel_GetSelections().Prepend(S);
  AIS_Sel_CurrentSelection(S);
  return Standard_True;
}


//=======================================================================
//function : Selection
//purpose  : 
//=======================================================================
Handle(AIS_Selection) AIS_Selection::Selection(const Standard_CString aName) 
{
  Handle(AIS_Selection) S;
  if(AIS_Sel_GetSelections().IsEmpty()) return S;
  
  Handle(Standard_Transient) curobj;
  Handle(AIS_Selection) Sel;
//  Standard_Boolean found(Standard_False);
  for(Standard_Integer I =1; I<= AIS_Sel_GetSelections().Length();I++){
    curobj = AIS_Sel_GetSelections().Value(I);
    Sel = *((Handle(AIS_Selection)*)&curobj);
    if(Sel->myName.IsEqual(aName))
      return Sel;
  }
  
  return S;
}

//=======================================================================
//function : Find
//purpose  : 
//=======================================================================
Standard_Boolean AIS_Selection::Find(const Standard_CString aName) 
{
  Handle(AIS_Selection) S = AIS_Selection::Selection(aName);
  return !S.IsNull();
}

//=======================================================================
//function : SetCurrentSelection
//purpose  : 
//=======================================================================
Standard_Boolean AIS_Selection::SetCurrentSelection (const Standard_CString aName) 
{  
  AIS_Selection::CreateSelection(aName); 


  Handle(AIS_Selection) anAISSelection = AIS_Selection::Selection(aName) ;
  AIS_Sel_CurrentSelection ( anAISSelection ) ;
  return Standard_True;
}

//=======================================================================
//function : Select
//purpose  : 
//=======================================================================
void AIS_Selection::Select() 
{
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  if(!S.IsNull()){
    S->myNb=0;
#if defined OCC189 || defined USE_MAP
    S->myresult.Clear();
#ifdef USE_MAP
    S->myResultMap.Clear();
#endif
#endif
  }
}

//=======================================================================
//function : CurrentSelection
//purpose  : 
//=======================================================================
Handle(AIS_Selection) AIS_Selection::CurrentSelection() {
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  return S;
}
//=======================================================================
//function : Select
//purpose  : 
//=======================================================================
AIS_SelectStatus AIS_Selection::Select(const Handle(Standard_Transient)& anObject) 
{
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  if(S.IsNull()) return AIS_SS_NotDone;
  Handle(AIS_InteractiveObject) anAISObj;
  Handle(SelectMgr_EntityOwner) owner = Handle(SelectMgr_EntityOwner)::DownCast( anObject );
  if ( owner.IsNull() )
    anAISObj = Handle(AIS_InteractiveObject)::DownCast( anObject );
#ifndef OCC189
  TColStd_Array1OfTransient& arr = S->myresult->ChangeArray1();
  
  Standard_Integer Found(-1);
  Standard_Integer i ;

#ifdef BUC60953
  
  Standard_Boolean selected = Standard_False;
  if ( !owner.IsNull() )
    selected = owner->State() != 0;
#ifdef OCC138
  else if(!anAISObj.IsNull())
    selected = anAISObj->State();
  }
#endif

  if ( selected ) // looking up index of object
#endif

  for( i=arr.Lower() && Found==-1;i<=S->myNb;i++){
    if(arr(i)==anObject)
      Found=i;
  }
  // If it is not inside, it is added...

#ifdef BUC60953
  if ( !selected || Found == -1 ) {
#else
  if(Found==-1){
#endif  
    if((S->myNb)+1>arr.Length()){
      Handle(TColStd_HArray1OfTransient) NiouTab = new TColStd_HArray1OfTransient(1,arr.Length()+MaxSizeOfResult);
      for(i=arr.Lower();i<=S->myNb;i++){
	const Handle(Standard_Transient)& T = S->myresult->Value(i);
	NiouTab->SetValue(i,T);
      }
#ifdef BUC60953
      S->myresult = NiouTab;
#endif
    }
    (S->myNb)++;
    S->myresult->SetValue(S->myNb,anObject);
    return AIS_SS_Added;
  }
  // it was inside and it is removed...
  for(i=Found;i<=S->myNb;i++)
    arr(i)=arr(i+1);
  S->myNb--;
#elif !defined USE_MAP //OCC189
  AIS_NListTransient::Iterator anIter ( S->myresult );
  for ( ; anIter.More(); anIter.Next() )
    if ( anIter.Value() == anObject ) {
      S->myresult.Remove( anIter );
      return AIS_SS_Removed;
    }
      
  S->myresult.Append( anObject );
  return AIS_SS_Added;
#else //USE_MAP
  if ( S->myResultMap.IsBound( anObject ) ){
    AIS_NListTransient::Iterator aListIter = S->myResultMap.Find( anObject );
//skt-----------------------------------------------------------------
    if( S->myIterator == aListIter ) {
	if( S->myIterator.More() )
	    S->myIterator.Next();
	else
	    S->myIterator = AIS_NListTransient::Iterator();
    }
//--------------------------------------------------------------------
#ifdef BUC60953
    // In the mode of advanced mesh selection only one owner is created
    // for all selection modes. It is necessary to check the current detected
    // entity and remove the owner from map only if the detected entity is
    // the same as previous selected (IsForcedHilight call)
    if( !anAISObj.IsNull() || ( !owner.IsNull() && !owner->IsForcedHilight() ) )
    {
#endif  
      S->myresult.Remove( aListIter );
      S->myResultMap.UnBind( anObject );
    
      // update list iterator for next object in <myresult> list if any
      if ( aListIter.More() ){
	const Handle(Standard_Transient)& aNextObject = aListIter.Value();
	if ( S->myResultMap.IsBound( aNextObject ) )
	  S->myResultMap( aNextObject ) = aListIter;
	else
	  S->myResultMap.Bind( aNextObject, aListIter );
      }
      return AIS_SS_Removed;
    }
    else
      return AIS_SS_Added;
  }
  
  AIS_NListTransient::Iterator aListIter;
  S->myresult.Append( anObject, aListIter );
  S->myResultMap.Bind( anObject, aListIter );
  return AIS_SS_Added;
#endif //USE_MAP

#ifndef _MSC_VER
  return AIS_SS_Removed;  
#endif
}

//=======================================================================
//function : AddSelect
//purpose  : Always add int the selection
//=======================================================================
AIS_SelectStatus AIS_Selection::AddSelect(const Handle(Standard_Transient)& anObject) 
{
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  if(S.IsNull()) return AIS_SS_NotDone;

#ifndef OCC189
  TColStd_Array1OfTransient& arr = S->myresult->ChangeArray1();
  Standard_Integer i;
  if((S->myNb)+1>arr.Length()){
      Handle(TColStd_HArray1OfTransient) NiouTab = new TColStd_HArray1OfTransient(1,arr.Length()+MaxSizeOfResult);
      for(i=arr.Lower();i<=S->myNb;i++){
	const Handle(Standard_Transient)& T = S->myresult->Value(i);
	NiouTab->SetValue(i,T);
      }
      S->myresult = NiouTab;
  }
  (S->myNb)++;
  S->myresult->SetValue(S->myNb,anObject);
#elif !defined USE_MAP //OCC189
  S->myresult.Append( anObject );
#else //USE_MAP
  if ( S->myResultMap.IsBound( anObject ) )
    return AIS_SS_NotDone;
      
  AIS_NListTransient::Iterator aListIter;
  S->myresult.Append( anObject, aListIter );
  S->myResultMap.Bind( anObject, aListIter );
#endif
  return AIS_SS_Added;
}


//=======================================================================
//function : ClearAndSelect
//purpose  : 
//=======================================================================

void AIS_Selection::ClearAndSelect(const Handle(Standard_Transient)& anObject) {
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  if(S.IsNull()) return;
  
  Select();
  Select(anObject);

}


//=======================================================================
//function : Extent
//purpose  : 
//=======================================================================
Standard_Integer AIS_Selection::Extent() {
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
#if !defined OCC189 && !defined USE_MAP
  return S->myNb;
#else
  return S->myresult.Extent();
#endif
}

//=======================================================================
//function : Single
//purpose  : 
//=======================================================================
Handle(Standard_Transient)  AIS_Selection::Single() 
{
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  
  S->Init();
  return S->Value();
}
//=======================================================================
//function : IsSelected
//purpose  : 
//=======================================================================
Standard_Boolean AIS_Selection::IsSelected(const Handle(Standard_Transient)& anObject)
{
  Handle(AIS_Selection) S;
  AIS_Sel_CurrentSelection (S);
  if(S.IsNull()) return Standard_False;
#ifndef USE_MAP  
  for(S->Init();S->More();S->Next()){
    if(S->Value()==anObject)
      return Standard_True;
  }
  return Standard_False;
#else
  return S->myResultMap.IsBound( anObject );
#endif
}

//=======================================================================
//function : Index
//purpose  : 
//=======================================================================

Standard_Integer AIS_Selection::Index(const Standard_CString aName)
{
  Handle (Standard_Transient) curobj;
  for(Standard_Integer I =1; I<= AIS_Sel_GetSelections().Length();I++){
    curobj = AIS_Sel_GetSelections().Value(I);
    if((*((Handle(AIS_Selection)*)&curobj))->myName.IsEqual(aName))
      return I;
  }
  return 0;
}

//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

void AIS_Selection::Remove(const Standard_CString aName) 
{
  Standard_Integer I = AIS_Selection::Index(aName);
  if(I!=0) {
#ifdef OCC1039
    Handle(AIS_Selection) selection = Handle(AIS_Selection)::DownCast( AIS_Sel_GetSelections().Value(I) );
    Standard_Integer stored = selection->NbStored();
    if ( stored )
      selection->Select();
#endif
    AIS_Sel_GetSelections().Remove(I);
  }
  
}