0027957: Visualization, AIS_InteractiveContext - protect from displaying the same...
[occt.git] / src / AIS / AIS_Selection.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <AIS_Selection.hxx>
16
17 #include <AIS_InteractiveObject.hxx>
18
19 IMPLEMENT_STANDARD_RTTIEXT(AIS_Selection, Standard_Transient)
20
21 namespace
22 {
23   static const Standard_Integer THE_MaxSizeOfResult = 100000;
24 }
25
26 //=======================================================================
27 //function : AIS_Selection
28 //purpose  :
29 //=======================================================================
30 AIS_Selection::AIS_Selection()
31 {
32   // for maximum performance on medium selections (< 100000 objects)
33   myResultMap.ReSize (THE_MaxSizeOfResult);
34 }
35
36 //=======================================================================
37 //function : Clear
38 //purpose  :
39 //=======================================================================
40 void AIS_Selection::Clear()
41 {
42   myresult.Clear();
43   myResultMap.Clear();
44 }
45
46 //=======================================================================
47 //function : Select
48 //purpose  :
49 //=======================================================================
50 AIS_SelectStatus AIS_Selection::Select (const Handle(SelectMgr_EntityOwner)& theObject)
51 {
52   if (theObject.IsNull()
53   || !theObject->HasSelectable())
54   {
55     return AIS_SS_NotDone;
56   }
57
58   if (!myResultMap.IsBound (theObject))
59   {
60     AIS_NListOfEntityOwner::Iterator aListIter;
61     myresult.Append  (theObject, aListIter);
62     myResultMap.Bind (theObject, aListIter);
63     return AIS_SS_Added;
64   }
65
66   AIS_NListOfEntityOwner::Iterator aListIter = myResultMap.Find (theObject);
67   if (myIterator == aListIter)
68   {
69     if (myIterator.More())
70     {
71       myIterator.Next();
72     }
73     else
74     {
75       myIterator = AIS_NListOfEntityOwner::Iterator();
76     }
77   }
78
79   // In the mode of advanced mesh selection only one owner is created for all selection modes.
80   // It is necessary to check the current detected entity
81   // and remove the owner from map only if the detected entity is the same as previous selected (IsForcedHilight call)
82   if (theObject->IsForcedHilight())
83   {
84     return AIS_SS_Added;
85   }
86
87   myresult.Remove (aListIter);
88   myResultMap.UnBind (theObject);
89
90   // update list iterator for next object in <myresult> list if any
91   if (aListIter.More())
92   {
93     const Handle(SelectMgr_EntityOwner)& aNextObject = aListIter.Value();
94     if (myResultMap.IsBound (aNextObject))
95     {
96       myResultMap (aNextObject) = aListIter;
97     }
98     else
99     {
100       myResultMap.Bind (aNextObject, aListIter);
101     }
102   }
103   return AIS_SS_Removed;
104 }
105
106 //=======================================================================
107 //function : AddSelect
108 //purpose  :
109 //=======================================================================
110 AIS_SelectStatus AIS_Selection::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
111 {
112   if (theObject.IsNull()
113   || !theObject->HasSelectable()
114   ||  myResultMap.IsBound (theObject))
115   {
116     return AIS_SS_NotDone;
117   }
118
119   AIS_NListOfEntityOwner::Iterator aListIter;
120   myresult.Append  (theObject, aListIter);
121   myResultMap.Bind (theObject, aListIter);
122   return AIS_SS_Added;
123 }