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)& theObject)
60 if (theObject.IsNull()
61 || !theObject->HasSelectable())
63 return AIS_SS_NotDone;
66 if (!myResultMap.IsBound (theObject))
68 AIS_NListOfEntityOwner::Iterator aListIter;
69 myresult.Append (theObject, aListIter);
70 myResultMap.Bind (theObject, aListIter);
71 theObject->SetSelected (Standard_True);
75 AIS_NListOfEntityOwner::Iterator aListIter = myResultMap.Find (theObject);
76 if (myIterator == aListIter)
78 if (myIterator.More())
84 myIterator = AIS_NListOfEntityOwner::Iterator();
88 // In the mode of advanced mesh selection only one owner is created for all selection modes.
89 // It is necessary to check the current detected entity
90 // and remove the owner from map only if the detected entity is the same as previous selected (IsForcedHilight call)
91 if (theObject->IsForcedHilight())
96 myresult.Remove (aListIter);
97 myResultMap.UnBind (theObject);
98 theObject->SetSelected (Standard_False);
100 // update list iterator for next object in <myresult> list if any
101 if (aListIter.More())
103 const Handle(SelectMgr_EntityOwner)& aNextObject = aListIter.Value();
104 if (myResultMap.IsBound (aNextObject))
106 myResultMap (aNextObject) = aListIter;
110 myResultMap.Bind (aNextObject, aListIter);
113 return AIS_SS_Removed;
116 //=======================================================================
117 //function : AddSelect
119 //=======================================================================
120 AIS_SelectStatus AIS_Selection::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
122 if (theObject.IsNull()
123 || !theObject->HasSelectable()
124 || myResultMap.IsBound (theObject))
126 return AIS_SS_NotDone;
129 AIS_NListOfEntityOwner::Iterator aListIter;
130 myresult.Append (theObject, aListIter);
131 myResultMap.Bind (theObject, aListIter);
132 theObject->SetSelected (Standard_True);
136 //=======================================================================
137 //function : SelectOwners
139 //=======================================================================
140 void AIS_Selection::SelectOwners (const AIS_NArray1OfEntityOwner& thePickedOwners,
141 const AIS_SelectionScheme theSelScheme,
142 const Standard_Boolean theToAllowSelOverlap,
143 const Handle(SelectMgr_Filter)& theFilter)
145 (void )theToAllowSelOverlap;
146 switch (theSelScheme)
148 case AIS_SelectionScheme_UNKNOWN:
152 case AIS_SelectionScheme_ReplaceExtra:
154 // If picked owners is equivalent to the selected then just clear selected
155 // Else go to AIS_SelectionScheme_Replace
156 if (thePickedOwners.Size() == myresult.Size())
158 Standard_Boolean isTheSame = Standard_True;
159 for (AIS_NArray1OfEntityOwner::Iterator aSelIter (thePickedOwners); aSelIter.More(); aSelIter.Next())
161 if (!myResultMap.IsBound (aSelIter.Value()))
163 isTheSame = Standard_False;
175 case AIS_SelectionScheme_Replace:
178 for (AIS_NArray1OfEntityOwner::Iterator aSelIter (thePickedOwners); aSelIter.More(); aSelIter.Next())
180 appendOwner (aSelIter.Value(), theFilter);
185 case AIS_SelectionScheme_Add:
187 for (AIS_NArray1OfEntityOwner::Iterator aSelIter (thePickedOwners); aSelIter.More(); aSelIter.Next())
189 appendOwner (aSelIter.Value(), theFilter);
193 case AIS_SelectionScheme_Remove:
195 for (AIS_NArray1OfEntityOwner::Iterator aSelIter (thePickedOwners); aSelIter.More(); aSelIter.Next())
197 if (myResultMap.IsBound (aSelIter.Value()))
199 Select (aSelIter.Value());
204 case AIS_SelectionScheme_XOR:
206 for (AIS_NArray1OfEntityOwner::Iterator aSelIter (thePickedOwners); aSelIter.More(); aSelIter.Next())
208 const Handle(SelectMgr_EntityOwner)& anOwner = aSelIter.Value();
210 || !anOwner->HasSelectable()
211 || !theFilter->IsOk (anOwner))
220 case AIS_SelectionScheme_Clear:
228 //=======================================================================
229 //function : appendOwner
231 //=======================================================================
232 AIS_SelectStatus AIS_Selection::appendOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
233 const Handle(SelectMgr_Filter)& theFilter)
235 if (theOwner.IsNull()
236 || !theOwner->HasSelectable()
237 || !theFilter->IsOk (theOwner))
239 return AIS_SS_NotDone;
242 return AddSelect (theOwner);