1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <AIS_Selection.hxx>
17 #include <AIS_InteractiveObject.hxx>
18 #include <AIS_SelectionScheme.hxx>
19 #include <SelectMgr_Filter.hxx>
21 IMPLEMENT_STANDARD_RTTIEXT(AIS_Selection, Standard_Transient)
25 static const Standard_Integer THE_MaxSizeOfResult = 100000;
28 //=======================================================================
29 //function : AIS_Selection
31 //=======================================================================
32 AIS_Selection::AIS_Selection()
34 // for maximum performance on medium selections (< 100000 objects)
35 myResultMap.ReSize (THE_MaxSizeOfResult);
38 //=======================================================================
41 //=======================================================================
42 void AIS_Selection::Clear()
44 for (AIS_NListOfEntityOwner::Iterator aSelIter (Objects()); aSelIter.More(); aSelIter.Next())
46 const Handle(SelectMgr_EntityOwner) anObject = aSelIter.Value();
47 anObject->SetSelected (Standard_False);
51 myIterator = AIS_NListOfEntityOwner::Iterator();
54 //=======================================================================
57 //=======================================================================
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)
64 || !theOwner->HasSelectable())
66 return AIS_SS_NotDone;
69 const Standard_Boolean isDetected = theIsDetected
70 && (theFilter.IsNull() || theFilter->IsOk (theOwner));
72 const Standard_Boolean wasSelected = theOwner->IsSelected();
73 const Standard_Boolean toSelect = theOwner->Select (theSelScheme, isDetected);
75 if (toSelect && !wasSelected)
77 AIS_NListOfEntityOwner::Iterator aListIter;
78 myresult.Append (theOwner, aListIter);
79 myResultMap.Bind (theOwner, aListIter);
80 theOwner->SetSelected (Standard_True);
84 if (!toSelect && !wasSelected)
86 return AIS_SS_NotDone;
89 AIS_NListOfEntityOwner::Iterator aListIter = myResultMap.Find (theOwner);
90 if (myIterator == aListIter)
92 if (myIterator.More())
98 myIterator = AIS_NListOfEntityOwner::Iterator();
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)
105 if (theOwner->IsForcedHilight())
110 myresult.Remove (aListIter);
111 myResultMap.UnBind (theOwner);
112 theOwner->SetSelected (Standard_False);
114 // update list iterator for next object in <myresult> list if any
115 if (aListIter.More())
117 const Handle(SelectMgr_EntityOwner)& aNextObject = aListIter.Value();
118 if (myResultMap.IsBound (aNextObject))
120 myResultMap (aNextObject) = aListIter;
124 myResultMap.Bind (aNextObject, aListIter);
127 return AIS_SS_Removed;
130 //=======================================================================
131 //function : AddSelect
133 //=======================================================================
134 AIS_SelectStatus AIS_Selection::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
136 if (theObject.IsNull()
137 || !theObject->HasSelectable()
138 || myResultMap.IsBound (theObject))
140 return AIS_SS_NotDone;
143 AIS_NListOfEntityOwner::Iterator aListIter;
144 myresult.Append (theObject, aListIter);
145 myResultMap.Bind (theObject, aListIter);
146 theObject->SetSelected (Standard_True);
150 //=======================================================================
151 //function : SelectOwners
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)
159 (void)theToAllowSelOverlap;
161 if (theSelScheme == AIS_SelectionScheme_ReplaceExtra
162 && thePickedOwners.Size() == myresult.Size())
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())
168 if (!myResultMap.IsBound (aPickedIter.Value()))
170 isTheSame = Standard_False;
181 if (theSelScheme == AIS_SelectionScheme_Replace
182 || theSelScheme == AIS_SelectionScheme_ReplaceExtra
183 || theSelScheme == AIS_SelectionScheme_Clear)
188 for (AIS_NArray1OfEntityOwner::Iterator aPickedIter (thePickedOwners); aPickedIter.More(); aPickedIter.Next())
190 const Handle(SelectMgr_EntityOwner)& anOwner = aPickedIter.Value();
191 Select (anOwner, theFilter, theSelScheme, true);
195 //=======================================================================
196 //function : appendOwner
198 //=======================================================================
199 AIS_SelectStatus AIS_Selection::appendOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
200 const Handle(SelectMgr_Filter)& theFilter)
202 if (theOwner.IsNull()
203 || !theOwner->HasSelectable()
204 || !theFilter->IsOk (theOwner))
206 return AIS_SS_NotDone;
209 return AddSelect (theOwner);