b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
02974a19 |
15 | #include <AIS_Selection.hxx> |
7fd59977 |
16 | |
7fd59977 |
17 | #include <AIS_InteractiveObject.hxx> |
75cf8250 |
18 | #include <AIS_SelectionScheme.hxx> |
19 | #include <SelectMgr_Filter.hxx> |
7fd59977 |
20 | |
02974a19 |
21 | IMPLEMENT_STANDARD_RTTIEXT(AIS_Selection, Standard_Transient) |
92efcf78 |
22 | |
02974a19 |
23 | namespace |
24 | { |
25 | static const Standard_Integer THE_MaxSizeOfResult = 100000; |
26 | } |
7fd59977 |
27 | |
7fd59977 |
28 | //======================================================================= |
29 | //function : AIS_Selection |
02974a19 |
30 | //purpose : |
7fd59977 |
31 | //======================================================================= |
02974a19 |
32 | AIS_Selection::AIS_Selection() |
7fd59977 |
33 | { |
02974a19 |
34 | // for maximum performance on medium selections (< 100000 objects) |
35 | myResultMap.ReSize (THE_MaxSizeOfResult); |
7fd59977 |
36 | } |
37 | |
7fd59977 |
38 | //======================================================================= |
02974a19 |
39 | //function : Clear |
40 | //purpose : |
7fd59977 |
41 | //======================================================================= |
02974a19 |
42 | void AIS_Selection::Clear() |
7fd59977 |
43 | { |
8d2c79f4 |
44 | for (AIS_NListOfEntityOwner::Iterator aSelIter (Objects()); aSelIter.More(); aSelIter.Next()) |
45 | { |
46 | const Handle(SelectMgr_EntityOwner) anObject = aSelIter.Value(); |
47 | anObject->SetSelected (Standard_False); |
48 | } |
016e5959 |
49 | myresult.Clear(); |
50 | myResultMap.Clear(); |
b5cce1ab |
51 | myIterator = AIS_NListOfEntityOwner::Iterator(); |
7fd59977 |
52 | } |
53 | |
7fd59977 |
54 | //======================================================================= |
55 | //function : Select |
02974a19 |
56 | //purpose : |
7fd59977 |
57 | //======================================================================= |
f9998f03 |
58 | AIS_SelectStatus AIS_Selection::Select (const Handle(SelectMgr_EntityOwner)& theOwner, |
59 | const Handle(SelectMgr_Filter)& theFilter, |
60 | const AIS_SelectionScheme theSelScheme, |
61 | const Standard_Boolean theIsDetected) |
7fd59977 |
62 | { |
f9998f03 |
63 | if (theOwner.IsNull() |
64 | || !theOwner->HasSelectable()) |
02974a19 |
65 | { |
66 | return AIS_SS_NotDone; |
67 | } |
68 | |
f9998f03 |
69 | const Standard_Boolean isDetected = theIsDetected |
70 | && (theFilter.IsNull() || theFilter->IsOk (theOwner)); |
71 | |
72 | const Standard_Boolean wasSelected = theOwner->IsSelected(); |
73 | const Standard_Boolean toSelect = theOwner->Select (theSelScheme, isDetected); |
74 | |
75 | if (toSelect && !wasSelected) |
02974a19 |
76 | { |
77 | AIS_NListOfEntityOwner::Iterator aListIter; |
f9998f03 |
78 | myresult.Append (theOwner, aListIter); |
79 | myResultMap.Bind (theOwner, aListIter); |
80 | theOwner->SetSelected (Standard_True); |
02974a19 |
81 | return AIS_SS_Added; |
82 | } |
83 | |
f9998f03 |
84 | if (!toSelect && !wasSelected) |
85 | { |
86 | return AIS_SS_NotDone; |
87 | } |
88 | |
89 | AIS_NListOfEntityOwner::Iterator aListIter = myResultMap.Find (theOwner); |
02974a19 |
90 | if (myIterator == aListIter) |
91 | { |
92 | if (myIterator.More()) |
7fd59977 |
93 | { |
02974a19 |
94 | myIterator.Next(); |
7fd59977 |
95 | } |
96 | else |
02974a19 |
97 | { |
98 | myIterator = AIS_NListOfEntityOwner::Iterator(); |
99 | } |
7fd59977 |
100 | } |
7fd59977 |
101 | |
02974a19 |
102 | // In the mode of advanced mesh selection only one owner is created for all selection modes. |
103 | // It is necessary to check the current detected entity |
104 | // and remove the owner from map only if the detected entity is the same as previous selected (IsForcedHilight call) |
f9998f03 |
105 | if (theOwner->IsForcedHilight()) |
02974a19 |
106 | { |
107 | return AIS_SS_Added; |
108 | } |
7fd59977 |
109 | |
02974a19 |
110 | myresult.Remove (aListIter); |
f9998f03 |
111 | myResultMap.UnBind (theOwner); |
112 | theOwner->SetSelected (Standard_False); |
7fd59977 |
113 | |
02974a19 |
114 | // update list iterator for next object in <myresult> list if any |
115 | if (aListIter.More()) |
116 | { |
117 | const Handle(SelectMgr_EntityOwner)& aNextObject = aListIter.Value(); |
118 | if (myResultMap.IsBound (aNextObject)) |
119 | { |
120 | myResultMap (aNextObject) = aListIter; |
121 | } |
122 | else |
123 | { |
124 | myResultMap.Bind (aNextObject, aListIter); |
125 | } |
126 | } |
127 | return AIS_SS_Removed; |
7fd59977 |
128 | } |
129 | |
7fd59977 |
130 | //======================================================================= |
02974a19 |
131 | //function : AddSelect |
132 | //purpose : |
7fd59977 |
133 | //======================================================================= |
02974a19 |
134 | AIS_SelectStatus AIS_Selection::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject) |
016e5959 |
135 | { |
02974a19 |
136 | if (theObject.IsNull() |
137 | || !theObject->HasSelectable() |
138 | || myResultMap.IsBound (theObject)) |
139 | { |
140 | return AIS_SS_NotDone; |
141 | } |
7fd59977 |
142 | |
02974a19 |
143 | AIS_NListOfEntityOwner::Iterator aListIter; |
144 | myresult.Append (theObject, aListIter); |
145 | myResultMap.Bind (theObject, aListIter); |
8d2c79f4 |
146 | theObject->SetSelected (Standard_True); |
02974a19 |
147 | return AIS_SS_Added; |
7fd59977 |
148 | } |
75cf8250 |
149 | |
150 | //======================================================================= |
151 | //function : SelectOwners |
152 | //purpose : |
153 | //======================================================================= |
154 | void AIS_Selection::SelectOwners (const AIS_NArray1OfEntityOwner& thePickedOwners, |
155 | const AIS_SelectionScheme theSelScheme, |
156 | const Standard_Boolean theToAllowSelOverlap, |
157 | const Handle(SelectMgr_Filter)& theFilter) |
158 | { |
f9998f03 |
159 | (void)theToAllowSelOverlap; |
75cf8250 |
160 | |
f9998f03 |
161 | if (theSelScheme == AIS_SelectionScheme_ReplaceExtra |
162 | && thePickedOwners.Size() == myresult.Size()) |
163 | { |
164 | // If picked owners is equivalent to the selected then just clear selected. |
165 | Standard_Boolean isTheSame = Standard_True; |
166 | for (AIS_NArray1OfEntityOwner::Iterator aPickedIter (thePickedOwners); aPickedIter.More(); aPickedIter.Next()) |
75cf8250 |
167 | { |
f9998f03 |
168 | if (!myResultMap.IsBound (aPickedIter.Value())) |
75cf8250 |
169 | { |
f9998f03 |
170 | isTheSame = Standard_False; |
171 | break; |
75cf8250 |
172 | } |
75cf8250 |
173 | } |
f9998f03 |
174 | if (isTheSame) |
75cf8250 |
175 | { |
f9998f03 |
176 | Clear(); |
177 | return; |
75cf8250 |
178 | } |
179 | } |
f9998f03 |
180 | |
181 | if (theSelScheme == AIS_SelectionScheme_Replace |
182 | || theSelScheme == AIS_SelectionScheme_ReplaceExtra |
183 | || theSelScheme == AIS_SelectionScheme_Clear) |
184 | { |
185 | Clear(); |
186 | } |
187 | |
188 | for (AIS_NArray1OfEntityOwner::Iterator aPickedIter (thePickedOwners); aPickedIter.More(); aPickedIter.Next()) |
189 | { |
190 | const Handle(SelectMgr_EntityOwner)& anOwner = aPickedIter.Value(); |
191 | Select (anOwner, theFilter, theSelScheme, true); |
192 | } |
75cf8250 |
193 | } |
194 | |
195 | //======================================================================= |
196 | //function : appendOwner |
197 | //purpose : |
198 | //======================================================================= |
199 | AIS_SelectStatus AIS_Selection::appendOwner (const Handle(SelectMgr_EntityOwner)& theOwner, |
200 | const Handle(SelectMgr_Filter)& theFilter) |
201 | { |
202 | if (theOwner.IsNull() |
203 | || !theOwner->HasSelectable() |
204 | || !theFilter->IsOk (theOwner)) |
205 | { |
206 | return AIS_SS_NotDone; |
207 | } |
208 | |
209 | return AddSelect (theOwner); |
210 | } |