1 // Created on: 1995-02-13
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <SelectMgr_SelectionManager.hxx>
19 #include <SelectMgr_SelectableObject.hxx>
20 #include <SelectMgr_Selection.hxx>
21 #include <TCollection_AsciiString.hxx>
23 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectionManager,Standard_Transient)
25 //==================================================
28 //==================================================
29 SelectMgr_SelectionManager::SelectMgr_SelectionManager (const Handle(SelectMgr_ViewerSelector)& theSelector)
30 : mySelector (theSelector)
35 //==================================================
38 //==================================================
39 Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
41 return myGlobal.Contains (theObject);
44 //==================================================
47 //==================================================
48 void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
49 const Standard_Integer theMode)
51 if (myGlobal.Contains(theObject))
54 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
56 Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
59 if (!theObject->HasOwnPresentations())
62 myGlobal.Add(theObject);
63 if (!mySelector->Contains (theObject) && theObject->HasOwnPresentations())
65 mySelector->AddSelectableObject (theObject);
68 loadMode (theObject, theMode);
71 //==================================================
74 //==================================================
75 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
77 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
79 Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
82 if (!theObject->HasOwnPresentations())
85 if (myGlobal.Contains (theObject))
87 if (mySelector->Contains (theObject))
89 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
91 mySelector->RemoveSelectionOfObject (theObject, aSelIter.Value());
92 aSelIter.Value()->UpdateBVHStatus (SelectMgr_TBU_Remove);
93 mySelector->Deactivate (aSelIter.Value());
95 mySelector->RemoveSelectableObject (theObject);
97 myGlobal.Remove (theObject);
100 theObject->ClearSelections();
103 //==================================================
104 // Function: Activate
106 //==================================================
107 void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
108 const Standard_Integer theMode)
113 for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
115 Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode);
117 if (!theObject->HasOwnPresentations())
120 Standard_Boolean isComputed = Standard_False;
121 if (const Handle(SelectMgr_Selection)& aSelOld = theObject->Selection (theMode))
123 isComputed = !aSelOld->IsEmpty();
127 loadMode (theObject, theMode);
130 const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
131 switch (aSelection->UpdateStatus())
133 case SelectMgr_TOU_Full:
135 if (theObject->HasSelection (theMode))
137 mySelector->RemoveSelectionOfObject (theObject, aSelection);
139 theObject->RecomputePrimitives (theMode);
140 // pass through SelectMgr_TOU_Partial
143 case SelectMgr_TOU_Partial:
145 theObject->UpdateTransformations (aSelection);
146 mySelector->RebuildObjectsTree();
152 aSelection->UpdateStatus(SelectMgr_TOU_None);
154 switch (aSelection->BVHUpdateStatus())
156 case SelectMgr_TBU_Add:
157 case SelectMgr_TBU_Renew:
159 mySelector->AddSelectionToObject (theObject, aSelection);
162 case SelectMgr_TBU_Remove:
164 if (aSelection->GetSelectionState() == SelectMgr_SOS_Deactivated)
166 mySelector->AddSelectionToObject (theObject, aSelection);
173 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
175 if (myGlobal.Contains (theObject))
177 mySelector->Activate (theObject->Selection (theMode));
181 //==================================================
182 // Function: Deactivate
184 //==================================================
185 void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableObject)& theObject,
186 const Standard_Integer theMode)
188 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
190 Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
192 if (!theObject->HasOwnPresentations())
196 if (!myGlobal.Contains(theObject))
201 const Handle(SelectMgr_Selection)& aSel = theObject->Selection (theMode);
204 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
206 mySelector->Deactivate (aSelIter.Value());
209 else if (!aSel.IsNull())
211 mySelector->Deactivate (aSel);
215 //=======================================================================
216 //function : IsActivated
218 //=======================================================================
219 Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr_SelectableObject)& theObject,
220 const Standard_Integer theMode) const
222 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
224 if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode))
225 return Standard_True;
227 if (!theObject->HasOwnPresentations())
229 return Standard_False;
231 if (!myGlobal.Contains(theObject))
233 return Standard_False;
238 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
240 if (mySelector->Status (aSelIter.Value()) == SelectMgr_SOS_Activated)
242 return Standard_True;
245 return Standard_False;
248 const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
249 if (aSelection.IsNull())
251 return Standard_False;
253 return !aSelection.IsNull()
254 && mySelector->Status (aSelection) == SelectMgr_SOS_Activated;
257 //=======================================================================
258 //function : ClearSelectionStructures
259 //purpose : Removes sensitive entities from all viewer selectors
260 // after method Clear() was called to the selection they belonged to
261 // or it was recomputed somehow
262 //=======================================================================
263 void SelectMgr_SelectionManager::ClearSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
264 const Standard_Integer theMode)
266 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
268 ClearSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
271 if (!theObj->HasOwnPresentations())
275 if (!myGlobal.Contains(theObj))
282 if (const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode))
284 mySelector->RemoveSelectionOfObject (theObj, aSelection);
285 aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
290 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObj->Selections()); aSelIter.More(); aSelIter.Next())
292 const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
293 mySelector->RemoveSelectionOfObject (theObj, aSelection);
294 aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
297 mySelector->RebuildObjectsTree();
300 //=======================================================================
301 //function : RestoreSelectionStructuress
302 //purpose : Re-adds newely calculated sensitive entities of recomputed selection
303 // defined by mode theMode to all viewer selectors contained that selection.
304 //=======================================================================
305 void SelectMgr_SelectionManager::RestoreSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
306 const Standard_Integer theMode)
308 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
310 RestoreSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
312 if (!theObj->HasOwnPresentations())
316 if (!myGlobal.Contains(theObj))
323 if (const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode))
325 mySelector->AddSelectionToObject (theObj, aSelection);
326 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
331 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObj->Selections()); aSelIter.More(); aSelIter.Next())
333 const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
334 mySelector->AddSelectionToObject (theObj, aSelection);
335 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
338 mySelector->RebuildObjectsTree();
341 //==================================================
342 // Function: recomputeSelectionMode
344 //==================================================
345 void SelectMgr_SelectionManager::recomputeSelectionMode (const Handle(SelectMgr_SelectableObject)& theObject,
346 const Handle(SelectMgr_Selection)& theSelection,
347 const Standard_Integer theMode)
349 theSelection->UpdateStatus (SelectMgr_TOU_Full);
351 ClearSelectionStructures (theObject, theMode);
352 theObject->RecomputePrimitives (theMode);
353 RestoreSelectionStructures (theObject, theMode);
354 theSelection->UpdateStatus (SelectMgr_TOU_None);
355 theSelection->UpdateBVHStatus (SelectMgr_TBU_None);
358 //==================================================
361 //==================================================
362 void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
363 const Standard_Boolean theIsForce,
364 const Standard_Integer theMode)
370 ClearSelectionStructures (theObject);
371 theObject->RecomputePrimitives();
372 theObject->UpdateTransformation();
373 RestoreSelectionStructures (theObject);
375 else if (theObject->HasSelection (theMode))
377 ClearSelectionStructures (theObject, theMode);
378 theObject->RecomputePrimitives (theMode);
379 theObject->UpdateTransformation();
380 RestoreSelectionStructures (theObject, theMode);
385 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
387 RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
389 if (!theObject->HasOwnPresentations())
393 if (!myGlobal.Contains (theObject))
400 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
402 const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
403 const Standard_Integer aSelMode = aSelection->Mode();
404 recomputeSelectionMode (theObject, aSelection, aSelMode);
409 if (const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode))
411 recomputeSelectionMode (theObject, aSelection, theMode);
416 //=======================================================================
418 //purpose : Selections are recalculated if they are flagged
419 // "TO RECALCULATE" and activated in one of selectors.
420 // If ForceUpdate = True, and they are "TO RECALCULATE"
421 // This is done without caring for the state of activation.
422 //=======================================================================
423 void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
424 const Standard_Boolean theIsForce)
426 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
428 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
430 if (!theObject->HasOwnPresentations())
435 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
437 const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
438 if (theIsForce || mySelector->Status (aSelection) == SelectMgr_SOS_Activated)
440 switch (aSelection->UpdateStatus())
442 case SelectMgr_TOU_Full:
444 ClearSelectionStructures (theObject, aSelection->Mode());
445 theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
446 RestoreSelectionStructures (theObject, aSelection->Mode());
447 // pass through SelectMgr_TOU_Partial
450 case SelectMgr_TOU_Partial:
452 theObject->UpdateTransformations (aSelection);
453 mySelector->RebuildObjectsTree();
459 aSelection->UpdateStatus (SelectMgr_TOU_None);
460 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
465 //==================================================
466 // Function: loadMode
467 // Purpose : Private Method
468 //==================================================
469 void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
470 const Standard_Integer theMode)
477 if (const Handle(SelectMgr_Selection)& aSelOld = theObject->Selection (theMode))
479 if (aSelOld->IsEmpty())
481 if (aSelOld->BVHUpdateStatus() == SelectMgr_TBU_Remove)
483 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
484 theObject->AddSelection (aNewSel, theMode);
485 aNewSel->UpdateBVHStatus (SelectMgr_TBU_Remove);
486 aNewSel->SetSelectionState (SelectMgr_SOS_Deactivated);
492 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
493 theObject->AddSelection (aNewSel, theMode);
494 if (myGlobal.Contains (theObject))
496 mySelector->AddSelectionToObject (theObject, aNewSel);
497 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
501 //=======================================================================
502 //function : SetUpdateMode
504 //=======================================================================
505 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
506 const SelectMgr_TypeOfUpdate theType)
508 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
510 aSelIter.Value()->UpdateStatus (theType);
514 //=======================================================================
515 //function : SetUpdateMode
517 //=======================================================================
518 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
519 const Standard_Integer theMode,
520 const SelectMgr_TypeOfUpdate theType)
522 if (const Handle(SelectMgr_Selection)& aSel = theObject->Selection (theMode))
524 aSel->UpdateStatus (theType);
528 //=======================================================================
529 //function : SetSelectionSensitivity
530 //purpose : Allows to manage sensitivity of a particular selection of interactive object theObject and
531 // changes previous sensitivity value of all sensitive entities in selection with theMode
532 // to the given theNewSensitivity.
533 //=======================================================================
534 void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
535 const Standard_Integer theMode,
536 const Standard_Integer theNewSens)
538 Standard_ASSERT_RAISE (theNewSens > 0, "Error! Selection sensitivity have positive value.");
539 if (theObject.IsNull())
544 const Handle(SelectMgr_Selection)& aSel = theObject->Selection (theMode);
550 const Standard_Integer aPrevSens = aSel->Sensitivity();
551 aSel->SetSensitivity (theNewSens);
552 if (myGlobal.Contains (theObject)
553 && mySelector->Contains (theObject))
555 mySelector->myTolerances.Decrement (aPrevSens);
556 mySelector->myTolerances.Add (theNewSens);
557 mySelector->myToUpdateTolerance = Standard_True;
561 //=======================================================================
562 //function : UpdateSelection
564 //=======================================================================
565 void SelectMgr_SelectionManager::UpdateSelection (const Handle(SelectMgr_SelectableObject)& theObject)
567 if (myGlobal.Contains (theObject)
568 && mySelector->Contains (theObject))
570 mySelector->MoveSelectableObject (theObject);