0027893: Visualization - AIS_InteractiveContext::SetSelected does not work
[occt.git] / src / SelectMgr / SelectMgr_SelectionManager.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 18b648e..3e6de7b
-// Copyright:  Matra-Datavision 1995
-// File:  SelectMgr_SelectionManager.cxx
-// Created:  Mon Feb 13 14:40:56 1995
-// Author:  Mister rmi
-//  <rmi>
+// Created on: 1995-02-13
+// Created by: Mister rmi
+// Copyright (c) 1995-1999 Matra Datavision
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 
-// Modified     jmi/rob 29/8/96
-//              appel de Loadmode dans Load d'un objet fait une seule fois.
-//              
-
-#include <SelectMgr_SelectionManager.ixx>
-#include <SelectMgr_ViewerSelector.hxx>
+#include <OSD_Environment.hxx>
+#include <SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors.hxx>
+#include <SelectMgr_SelectableObject.hxx>
 #include <SelectMgr_Selection.hxx>
+#include <SelectMgr_SelectionManager.hxx>
 #include <SelectMgr_SequenceOfSelector.hxx>
-#include <TColStd_MapIteratorOfMapOfTransient.hxx>
-#include <TColStd_MapIteratorOfMapOfTransient.hxx>
+#include <SelectMgr_ViewerSelector.hxx>
+#include <Standard_Type.hxx>
 #include <TCollection_AsciiString.hxx>
-#include <TColStd_ListOfInteger.hxx>
 #include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors.hxx>
-#include <OSD_Environment.hxx>
-
+#include <TColStd_ListOfInteger.hxx>
+#include <TColStd_MapIteratorOfMapOfTransient.hxx>
 
-static Standard_Boolean SelectDebugModeOnSM()
-{
-  static Standard_Integer isDebugMode( -1 );
-  if ( isDebugMode < 0 ) {
-    isDebugMode = 1;
-    OSD_Environment selectdb("SELECTIONDEBUG");
-    if ( selectdb.Value().IsEmpty() )
-      isDebugMode = 0;
-  }                       
-  return ( isDebugMode != 0 );
-}
+IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectionManager,MMgt_TShared)
 
-static Standard_Integer SMSearch(const SelectMgr_SequenceOfSelector& seq,
-                                 const Handle(SelectMgr_ViewerSelector)& theSel)
+static Standard_Integer FindIndex (const SelectMgr_SequenceOfSelector& theSelectorsSeq,
+                                   const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  Standard_Integer ifound=0;
-  for (Standard_Integer i=1;i<=seq.Length()&& ifound==0;i++)
-  {if(theSel==seq.Value(i)) ifound=i;}
-  return ifound;
+  Standard_Integer aFoundIdx = 0;
 
-} 
-
-// Unused :
-#ifdef DEB
-static Standard_Boolean SMListContains(const TColStd_ListOfInteger& LL,
-                                       const Standard_Integer aMode) 
-{
-  TColStd_ListIteratorOfListOfInteger LIt;
-  for (LIt.Initialize(LL);LIt.More();LIt.Next())
-  {if(LIt.Value()==aMode) return Standard_True;}
-  return Standard_False;
-} 
+  for (Standard_Integer anIdx = 1; anIdx <= theSelectorsSeq.Length() && aFoundIdx==0; anIdx++)
+  {
+    if (theSelector == theSelectorsSeq.Value (anIdx))
+      aFoundIdx = anIdx;
+  }
 
-#endif
+  return aFoundIdx;
+}
 
 //==================================================
 // Function: Create
 // Purpose :
 //==================================================
-
-SelectMgr_SelectionManager::SelectMgr_SelectionManager()
-{}
-
+SelectMgr_SelectionManager::SelectMgr_SelectionManager() {}
 
 //==================================================
 // Function: Add
 // Purpose :
 //==================================================
-void SelectMgr_SelectionManager::
-Add (const Handle(SelectMgr_ViewerSelector)& aViewSel)
+void SelectMgr_SelectionManager::Add (const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  myselectors.Add(aViewSel);
+  mySelectors.Add (theSelector);
 }
 
-
-
 //==================================================
 // Function: Remove
 // Purpose :
 //==================================================
-void SelectMgr_SelectionManager::
-Remove (const Handle(SelectMgr_ViewerSelector)& aViewSel)
+void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors It(mylocal);
-  for(;It.More();It.Next())
+  for (SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors aSelIter (myLocal); aSelIter.More(); aSelIter.Next())
   {
-    SelectMgr_SequenceOfSelector& theviews =mylocal.ChangeFind(It.Key());
-    Standard_Integer rank = SMSearch(theviews,aViewSel);
-    if(rank!=0 && rank<=theviews.Length()) theviews.Remove(rank);
+    SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (aSelIter.Key());
+    Standard_Integer aRank = FindIndex (theSelectors, theSelector);
+    if (aRank != 0 && aRank <= theSelectors.Length())
+      theSelectors.Remove (aRank);
   }
-  if(myselectors.Contains(aViewSel)) myselectors.Remove(aViewSel);
+
+  if (mySelectors.Contains (theSelector))
+    mySelectors.Remove (theSelector);
 }
 
 //==================================================
 // Function: Contains
 // Purpose :
 //==================================================
-Standard_Boolean SelectMgr_SelectionManager::
-Contains (const Handle(SelectMgr_ViewerSelector)& aViewSel) const
-{return myselectors.Contains(aViewSel);}
+Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_ViewerSelector)& theSelector) const
+{
+  return mySelectors.Contains (theSelector);
+}
 
 //==================================================
 // Function: Contains
 // Purpose :
 //==================================================
-Standard_Boolean SelectMgr_SelectionManager::
-Contains (const Handle(SelectMgr_SelectableObject)& aSelObj) const
-{if (myglobal.Contains(aSelObj)) return Standard_True;
-if (mylocal.IsBound(aSelObj)) return Standard_True;
-return Standard_False;
-}
-
+Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
+{
+  if (myGlobal.Contains (theObject))
+    return Standard_True;
 
+  if (myLocal.IsBound (theObject))
+    return Standard_True;
 
-//==================================================
-// Function: Load
-// Purpose :
-//==================================================
-
-void SelectMgr_SelectionManager::
-Load (const Handle(SelectMgr_SelectableObject)& anObject,
-      const Standard_Integer amode)
-{
-  if(!myglobal.Contains(anObject))
-    myglobal.Add(anObject);
-  if(amode!=-1) 
-    LoadMode (anObject,amode);
+  return Standard_False;
 }
 
-
 //==================================================
 // Function: Load
 // Purpose :
 //==================================================
-void SelectMgr_SelectionManager::
-Load (const Handle(SelectMgr_SelectableObject)& anObject,
-      const Handle(SelectMgr_ViewerSelector)& aview,      
-      const Standard_Integer amode)
+void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
+                                       const Standard_Integer theMode)
 {
-  if(!myselectors.Contains(aview)) myselectors.Add(aview);
-  if(amode!=-1)
-    LoadMode (anObject,amode);
-
+  if (myGlobal.Contains(theObject))
+    return;
 
-  if (mylocal.IsBound(anObject)){
-    SelectMgr_SequenceOfSelector& theviews = mylocal.ChangeFind(anObject);
-    if (SMSearch(theviews,aview)==0) theviews.Append(aview);
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
   }
-  else {
-    if(!myglobal.Contains(anObject)){
-      SelectMgr_SequenceOfSelector newviews;
-      newviews.Append(aview);
-      mylocal.Bind(anObject,newviews);
+
+  if (!theObject->HasOwnPresentations())
+    return;
+
+  myGlobal.Add(theObject);
+  for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+  {
+    Handle(SelectMgr_ViewerSelector) aSelector =
+      Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+    if (!aSelector->Contains (theObject) && theObject->HasOwnPresentations())
+    {
+      aSelector->AddSelectableObject (theObject);
     }
   }
+  if (theMode != -1)
+    loadMode (theObject, theMode);
 }
 
 
 //==================================================
-// Function: Remove
+// Function: Load
 // Purpose :
 //==================================================
-
-void SelectMgr_SelectionManager::
-Remove(const Handle(SelectMgr_SelectableObject)& anObject)
+void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
+                                       const Handle(SelectMgr_ViewerSelector)& theSelector,
+                                       const Standard_Integer theMode)
 {
+  if (!mySelectors.Contains (theSelector))
+  {
+    mySelectors.Add (theSelector);
+  }
 
-  if(myglobal.Contains(anObject)) {
-    TColStd_MapIteratorOfMapOfTransient It(myselectors);
-    for(;It.More();It.Next())
-    {
-      Handle(SelectMgr_ViewerSelector) curview = 
-        Handle(SelectMgr_ViewerSelector)::DownCast(It.Key());
-      if(curview->Contains(anObject)){
-        for(anObject->Init();anObject->More();anObject->Next())
-        {
-          curview->Remove(anObject->CurrentSelection());
-        }
+  if (theMode != -1)
+    loadMode (theObject, theMode, theSelector);
 
-      }
+  if (theObject->HasOwnPresentations())
+    theSelector->AddSelectableObject (theObject);
+
+  if (myLocal.IsBound (theObject))
+  {
+    SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
+    if (FindIndex (aSelectors, theSelector) == 0)
+    {
+        aSelectors.Append (theSelector);
     }
-    myglobal.Remove(anObject);
   }
-
-  else if(mylocal.IsBound(anObject)) {
-    SelectMgr_SequenceOfSelector& seq = mylocal.ChangeFind (anObject);
-    for (Standard_Integer i=1;i<=seq.Length();i++) {
-      Handle(SelectMgr_ViewerSelector) curview =
-        Handle(SelectMgr_ViewerSelector)::DownCast(seq(i));
-      if(curview->Contains(anObject)){
-        for(anObject->Init();anObject->More();anObject->Next())
-        {
-          curview->Remove(anObject->CurrentSelection());
-        }
+  else
+  {
+    if (!myGlobal.Contains (theObject))
+    {
+      for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+      {
+        Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
       }
+      if (!theObject->HasOwnPresentations())
+        return;
 
+      SelectMgr_SequenceOfSelector aSelectors;
+      aSelectors.Append (theSelector);
+      myLocal.Bind (theObject, aSelectors);
     }
-    mylocal.UnBind(anObject);
   }
 }
 
+
 //==================================================
 // Function: Remove
 // Purpose :
 //==================================================
-
-void SelectMgr_SelectionManager::
-Remove(const Handle(SelectMgr_SelectableObject)& anObject,
-       const Handle(SelectMgr_ViewerSelector)& aVS)
+void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
 {
-  if(aVS->Contains(anObject)) {
-    for(anObject->Init();anObject->More();anObject->Next()){
-      aVS->Remove(anObject->CurrentSelection());
-    }
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
+  }
 
+  if (!theObject->HasOwnPresentations())
+    return;
 
-    if(mylocal.IsBound(anObject)) {
-      SelectMgr_SequenceOfSelector& seq = mylocal.ChangeFind (anObject);
-      Standard_Boolean NotFound (Standard_True);
-      for (Standard_Integer i=1;i<=seq.Length()&&NotFound;i++) {
-        if(seq(i)== aVS){
-          seq.Remove(i);
-          NotFound =Standard_False;
-        }
+  if (myGlobal.Contains (theObject))
+  {
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector) aCurSelector =
+        Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+
+      if (!aCurSelector->Contains (theObject))
+        continue;
+
+      for (theObject->Init(); theObject->More(); theObject->Next())
+      {
+        aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
+        theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
       }
-      if(seq.IsEmpty())
-        mylocal.UnBind(anObject);
+      aCurSelector->RemoveSelectableObject (theObject);
     }
+
+    myGlobal.Remove (theObject);
   }
+  else if (myLocal.IsBound (theObject))
+  {
+    SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
+    for (Standard_Integer aSelectorsIdx = 1; aSelectorsIdx <= aSelectors.Length(); aSelectorsIdx++)
+    {
+      Handle(SelectMgr_ViewerSelector) aCurSelector = aSelectors (aSelectorsIdx);
+      if (!aCurSelector->Contains (theObject))
+        continue;
+
+      for (theObject->Init(); theObject->More(); theObject->Next())
+      {
+        aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
+        theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
+      }
+      aCurSelector->RemoveSelectableObject (theObject);
+    }
+
+    myLocal.UnBind (theObject);
+  }
+
+  theObject->ClearSelections();
 }
 
 //==================================================
-// Function: Activate
+// Function: Remove
 // Purpose :
 //==================================================
-
-void SelectMgr_SelectionManager::
-Activate(const Handle(SelectMgr_SelectableObject)& anObject,
-         const Standard_Integer aMode,
-         const Standard_Boolean AutomaticProj)
+void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject,
+                                         const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  if(aMode==-1) return;
-  //  Standard_Boolean global = Standard_False;
-  if(!anObject->HasSelection(aMode)) LoadMode(anObject,aMode);
+  if (!theSelector->Contains (theObject))
+    return;
 
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector);
+  }
 
-  if(myglobal.Contains(anObject)) {
-    TColStd_MapIteratorOfMapOfTransient It(myselectors);
+  if (!theObject->HasOwnPresentations())
+    return;
 
-    for(;It.More();It.Next()){
-      Handle(SelectMgr_ViewerSelector) curview = 
-        Handle(SelectMgr_ViewerSelector)::DownCast(It.Key());
-      Activate(anObject,aMode,curview,AutomaticProj);
-    }
+  for (theObject->Init(); theObject->More(); theObject->Next())
+  {
+    theSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
+    theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
   }
 
-  else if(mylocal.IsBound(anObject)) {
-    SelectMgr_SequenceOfSelector& seq = mylocal.ChangeFind (anObject);
-    for (Standard_Integer i=1;i<=seq.Length();i++) {
-      Handle(SelectMgr_ViewerSelector) curview =
-        Handle(SelectMgr_ViewerSelector)::DownCast(seq(i));
-      // ATTENTION : si la selection est a remettre a jour, on le fait la ....      
-      const Handle(SelectMgr_Selection)& Sel = anObject->Selection(aMode);
+  theSelector->RemoveSelectableObject (theObject);
 
-      switch(Sel->UpdateStatus()){
-      case SelectMgr_TOU_Full:
-        anObject->UpdateSelection(aMode); // pas de break expres...
-      case SelectMgr_TOU_Partial:
-        {
-          if(anObject->HasLocation())
-            anObject->UpdateLocation(Sel);
-          Sel->UpdateStatus(SelectMgr_TOU_None);
-          break;
-        }
-#ifndef DEB
-      default:
+  if (myLocal.IsBound (theObject))
+  {
+    SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
+    for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); aSelectorIdx++)
+    {
+      if (aSelectors (aSelectorIdx) == theSelector)
+      {
+        aSelectors.Remove (aSelectorIdx);
         break;
-#endif
       }
+    }
 
-      curview->Activate(Sel,AutomaticProj);
+    if (aSelectors.IsEmpty())
+    {
+      myLocal.UnBind (theObject);
     }
   }
 }
 
-
 //==================================================
 // Function: Activate
 // Purpose :
 //==================================================
-
-void SelectMgr_SelectionManager::
-Activate(const Handle(SelectMgr_SelectableObject)& anObject,
-         const Standard_Integer aMode,
-         const Handle(SelectMgr_ViewerSelector)& aViewSel,
-         const Standard_Boolean AutomaticProj)
+void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
+                                           const Standard_Integer theMode,
+                                           const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  if(aMode==-1) return;
+  if (theMode == -1)
+    return;
 
-  if(!myselectors.Contains(aViewSel)) return;
+  if (!theSelector.IsNull() && !mySelectors.Contains (theSelector))
+    return;
 
-  if (!anObject->HasSelection(aMode)) LoadMode(anObject,aMode);
+  for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
+  {
+    Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
+  }
 
-  // ATTENTION : si la selection est a remettre a jour, on le fait la ....      
-  const Handle(SelectMgr_Selection)& Sel = anObject->Selection(aMode);
+  if (!theObject->HasOwnPresentations())
+    return;
 
-  switch(Sel->UpdateStatus()){
-  case SelectMgr_TOU_Full:
-    anObject->UpdateSelection(aMode); 
-  case SelectMgr_TOU_Partial:
+  Standard_Boolean isComputed = Standard_False;
+  if (theObject->HasSelection (theMode))
+  {
+    isComputed = theObject->Selection (theMode)->IsEmpty() ? 0 : 1;
+  }
+
+  if (!isComputed)
+    loadMode (theObject, theMode);
+
+  if (theSelector.IsNull())
+  {
+    if (myGlobal.Contains (theObject))
+    {
+      for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+      {
+        Handle(SelectMgr_ViewerSelector) aCurSelector =
+          Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+        Activate (theObject, theMode, aCurSelector);
+      }
+    }
+    else if (myLocal.IsBound (theObject))
     {
-      if(anObject->HasLocation())
-        anObject->UpdateLocation(Sel);
-      break;
+      SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (theObject);
+      for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= theSelectors.Length(); aSelectorIdx++)
+      {
+        Handle(SelectMgr_ViewerSelector) aCurSelector = theSelectors (aSelectorIdx);
+        Activate (theObject, theMode, aCurSelector);
+      }
     }
-#ifndef DEB
+  }
+
+  const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
+
+  switch (aSelection->UpdateStatus())
+  {
+  case SelectMgr_TOU_Full:
+    if (theObject->HasSelection (theMode))
+      theSelector->RemoveSelectionOfObject (theObject, aSelection);
+    theObject->RecomputePrimitives (theMode);
+  case SelectMgr_TOU_Partial:
+    if(theObject->HasTransformation())
+      theObject->UpdateTransformations (aSelection);
+    theSelector->RebuildObjectsTree();
+    break;
   default:
     break;
-#endif
   }
-  Sel->UpdateStatus(SelectMgr_TOU_None);
+  aSelection->UpdateStatus(SelectMgr_TOU_None);
 
-  if  (myglobal.Contains(anObject)) 
-    aViewSel->Activate (anObject->Selection(aMode));
+  switch (aSelection->BVHUpdateStatus())
+  {
+  case SelectMgr_TBU_Add:
+  case SelectMgr_TBU_Renew:
+    theSelector->AddSelectionToObject (theObject, aSelection);
+    break;
+  case SelectMgr_TBU_Remove:
+    if (aSelection->GetSelectionState() == SelectMgr_SOS_Deactivated)
+      theSelector->AddSelectionToObject (theObject, aSelection);
+    break;
+  default:
+    break;
+  }
+  aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
 
-  else {
-    if (mylocal.IsBound(anObject)) {
-      if (SMSearch(mylocal.Find(anObject),aViewSel)==0)
-        (mylocal.ChangeFind (anObject)).Append(aViewSel);
-      aViewSel->Activate (anObject->Selection(aMode),AutomaticProj);
+  if (myGlobal.Contains (theObject))
+  {
+    const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
+    if (theMode != aGlobalSelMode && theSelector->IsActive (theObject, aGlobalSelMode))
+    {
+      theSelector->Deactivate (theObject->Selection (aGlobalSelMode));
     }
+    theSelector->Activate (theObject->Selection (theMode));
   }
-}
-
-//==================================================
-// Function: Deactivate
-// Purpose :
-//==================================================
-
-void SelectMgr_SelectionManager::
-Deactivate(const Handle(SelectMgr_SelectableObject)& anObject)
-{
-  Standard_Boolean global = Standard_False;
-  if(myglobal.Contains(anObject)) global = Standard_True;
-  TColStd_MapIteratorOfMapOfTransient It(myselectors);
-  Handle(SelectMgr_ViewerSelector) curview; 
-  for(;It.More();It.Next()){
-    curview = Handle(SelectMgr_ViewerSelector)::DownCast(It.Key());
-    if (global || mylocal.IsBound (anObject)) {
-      for (anObject->Init();anObject->More();anObject->Next())
-      {curview->Deactivate(anObject->CurrentSelection());}  
-
+  else
+  {
+    if (myLocal.IsBound (theObject))
+    {
+      if (FindIndex (myLocal.Find (theObject), theSelector) == 0)
+        (myLocal.ChangeFind (theObject)).Append (theSelector);
+      theSelector->Activate (theObject->Selection (theMode));
     }
-
   }
 }
 
@@ -363,505 +392,609 @@ Deactivate(const Handle(SelectMgr_SelectableObject)& anObject)
 // Function: Deactivate
 // Purpose :
 //==================================================
+void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableObject)& theObject,
+                                             const Standard_Integer theMode,
+                                             const Handle(SelectMgr_ViewerSelector)& theSelector)
+{
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
+  }
 
-void SelectMgr_SelectionManager::
-Deactivate(const Handle(SelectMgr_SelectableObject)& anObject,
-           const Standard_Integer amode)
+  if (!theObject->HasOwnPresentations())
+    return;
 
-{
-  Standard_Boolean global = Standard_False;
-  if(myglobal.Contains(anObject)) global = Standard_True;
-  TColStd_MapIteratorOfMapOfTransient It(myselectors);
-  Handle(SelectMgr_ViewerSelector) curview;
-  for(;It.More();It.Next()){
-    curview = Handle(SelectMgr_ViewerSelector)::DownCast(It.Key());
-    if (global || mylocal.IsBound(anObject)) {
-      if(anObject->HasSelection(amode))
-        curview->Deactivate(anObject->Selection(amode));
+  Standard_Boolean isInGlobal = myGlobal.Contains (theObject);
+  Standard_Boolean hasSelection = theMode == -1 ? Standard_True : theObject->HasSelection (theMode);
 
+  if (theSelector.IsNull())
+  {
+    Handle(SelectMgr_ViewerSelector) aSelector;
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
+    {
+      aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
+      if (isInGlobal || myLocal.IsBound (theObject))
+      {
+        if (theMode == -1)
+        {
+          for (theObject->Init(); theObject->More(); theObject->Next())
+          {
+            aSelector->Deactivate (theObject->CurrentSelection());
+          }
+        }
+        else
+        {
+          if (hasSelection)
+            aSelector->Deactivate (theObject->Selection (theMode));
+        }
+      }
     }
   }
-}
-
-//==================================================
-// Function: Deactivate
-// Purpose :
-//==================================================
-
-void SelectMgr_SelectionManager::
-Deactivate(const Handle(SelectMgr_SelectableObject)& anObject,
-           const Standard_Integer aMode,
-           const Handle(SelectMgr_ViewerSelector)& aViewSel)
-{
-  if(myselectors.Contains(aViewSel))
+  else
   {
-    if(myglobal.Contains(anObject)|| mylocal.IsBound(anObject)) 
-      if(anObject->HasSelection(aMode))
-        aViewSel->Deactivate (anObject->Selection(aMode));
-  }  
-
+    if (theMode == -1)
+    {
+      for (theObject->Init(); theObject->More(); theObject->Next())
+      {
+        theSelector->Deactivate (theObject->CurrentSelection());
+      }
+    }
+    else
+      if (hasSelection)
+        theSelector->Deactivate (theObject->Selection (theMode));
+  }
 }
-//==================================================
-// Function: Deactivate
-// Purpose :
-//==================================================
-
-void SelectMgr_SelectionManager::
-Deactivate(const Handle(SelectMgr_SelectableObject)& anObject,
-           const Handle(SelectMgr_ViewerSelector)& aViewSel)
 
+//=======================================================================
+//function : IsActivated
+//purpose  :
+//=======================================================================
+Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                          const Standard_Integer theMode,
+                                                          const Handle(SelectMgr_ViewerSelector)& theSelector) const
 {
-  if(myselectors.Contains(aViewSel))
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
   {
-    if(myglobal.Contains(anObject)|| mylocal.IsBound(anObject)) {
-      for (anObject->Init();anObject->More();anObject->Next())
-      {aViewSel->Deactivate(anObject->CurrentSelection());}}  
+    if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector))
+      return Standard_True;
+  }
 
-  }  
+  if (!theObject->HasOwnPresentations())
+    return Standard_False;
 
-}
+  if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
+    return Standard_False;
 
+  if (theMode == -1 && theSelector.IsNull())
+  {
+    for (theObject->Init(); theObject->More(); theObject->Next())
+    {
+      if (IsActivated (theObject, theObject->CurrentSelection()->Mode()))
+        return Standard_True;
+    }
 
-//==================================================
-// Function: Sleep
-// Purpose :
-//==================================================
-void SelectMgr_SelectionManager::
-Sleep (const Handle(SelectMgr_ViewerSelector)& aViewSel)
-{
-  if (myselectors.Contains(aViewSel))
-    aViewSel->Sleep();
-}
+    return Standard_False;
+  }
 
-void SelectMgr_SelectionManager::
-Sleep (const Handle(SelectMgr_SelectableObject)& anObject)
-{
+  if (!theObject->HasSelection (theMode))
+    return Standard_False;
 
-  if(myglobal.Contains(anObject)){
-    for( TColStd_MapIteratorOfMapOfTransient It(myselectors);
-      It.More();It.Next())
-      Handle(SelectMgr_ViewerSelector)::DownCast(It.Key())->Sleep(anObject);
+  const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
+  if (theSelector.IsNull())
+  {
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
+      if (aSelector->Status (aSelection) == SelectMgr_SOS_Activated)
+        return Standard_True;
+    }
   }
-  else if(mylocal.IsBound(anObject)){
-    const SelectMgr_SequenceOfSelector & VSeq = mylocal(anObject);
-    for (Standard_Integer I=1;I<=VSeq.Length();I++)
-      VSeq(I)->Sleep(anObject);
+  else
+  {
+    return theSelector->Status (aSelection) == SelectMgr_SOS_Activated;
   }
 
-
+  return Standard_False;
 }
 
 //=======================================================================
-//function : Sleep
-//purpose  : 
+//function : ClearSelectionStructures
+//purpose  : Removes sensitive entities from all viewer selectors
+//           after method Clear() was called to the selection they belonged to
+//           or it was recomputed somehow
 //=======================================================================
-void SelectMgr_SelectionManager::
-Sleep(const Handle(SelectMgr_SelectableObject)& anObject,
-      const Handle(SelectMgr_ViewerSelector)& aViewSel)
+void SelectMgr_SelectionManager::ClearSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
+                                                           const Standard_Integer theMode,
+                                                           const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  if(!myselectors.Contains(aViewSel)) return;
-
-  if(!myglobal.Contains(anObject)){
-    if(!mylocal.IsBound(anObject))
-      return;
-    if(SMSearch(mylocal(anObject),aViewSel)==0)
-      return;
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    ClearSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
   }
-  aViewSel->Sleep(anObject);
-}
-
 
+  if (!theObj->HasOwnPresentations())
+    return;
 
-//==================================================
-// Function: Awake
-// Purpose :
-//==================================================
-void SelectMgr_SelectionManager::
-Awake (const Handle(SelectMgr_ViewerSelector)& aViewSel,
-       const Standard_Boolean AutomaticProj)
-{
-  if (myselectors.Contains(aViewSel))
-    aViewSel->Awake(AutomaticProj);
-}
-
+  if (theSelector.IsNull())
+  {
+    if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
+      return;
 
-//=======================================================================
-//function : Awake
-//purpose  : 
-//=======================================================================
-void SelectMgr_SelectionManager::Awake (const Handle(SelectMgr_SelectableObject)& anObject,
-                                        const Standard_Boolean AutomaticProj)
-{
-  if(myglobal.Contains(anObject)){
-    for( TColStd_MapIteratorOfMapOfTransient It(myselectors);
-      It.More();It.Next())
-      Handle(SelectMgr_ViewerSelector)::DownCast( It.Key())->Awake(anObject,AutomaticProj);
+    TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
+    Handle(SelectMgr_ViewerSelector) aSelector;
+    for( ; aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+      ClearSelectionStructures (theObj, theMode, aSelector);
+    }
   }
-  else if(mylocal.IsBound(anObject)){
-    const SelectMgr_SequenceOfSelector & VSeq = mylocal(anObject);
-    for (Standard_Integer I=1;I<=VSeq.Length();I++)
-      VSeq(I)->Awake(anObject,AutomaticProj);
+  else
+  {
+    if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
+      return;
+
+    if (theMode != -1)
+    {
+      if (theObj->HasSelection (theMode))
+      {
+        const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
+        if (theObj->HasSelection (theMode))
+        {
+          theSelector->RemoveSelectionOfObject (theObj, aSelection);
+          aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
+        }
+      }
+    }
+    else
+    {
+      for (theObj->Init(); theObj->More(); theObj->Next())
+      {
+        const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
+        theSelector->RemoveSelectionOfObject (theObj, aSelection);
+        aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
+      }
+    }
+    theSelector->RebuildObjectsTree();
   }
 }
 
 //=======================================================================
-//function : Awake
-//purpose  : 
+//function : RestoreSelectionStructuress
+//purpose  : Re-adds newely calculated sensitive  entities of recomputed selection
+//           defined by mode theMode to all viewer selectors contained that selection.
 //=======================================================================
-void SelectMgr_SelectionManager::Awake (const Handle(SelectMgr_SelectableObject)& anObject,
-                                        const Handle(SelectMgr_ViewerSelector)& aViewSel,
-                                        const Standard_Boolean AutomaticProj)
+void SelectMgr_SelectionManager::RestoreSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
+                                                             const Standard_Integer theMode,
+                                                             const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  if(!myselectors.Contains(aViewSel)) return;
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    RestoreSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
+  }
 
-  if(!myglobal.Contains(anObject)){
-    if(!mylocal.IsBound(anObject))
-      return;
-    if(SMSearch(mylocal(anObject),aViewSel)==0)
+  if (!theObj->HasOwnPresentations())
+    return;
+
+  if (theSelector.IsNull())
+  {
+    if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
       return;
+
+    TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
+    Handle(SelectMgr_ViewerSelector) aSelector;
+    for( ; aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+      RestoreSelectionStructures (theObj, theMode, aSelector);
+    }
   }
-  aViewSel->Awake(anObject,AutomaticProj);
+  else
+  {
+    if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
+      return;
 
+    if (theMode != -1)
+    {
+      if (theObj->HasSelection (theMode))
+      {
+        const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
+        if (theObj->HasSelection (theMode))
+        {
+          theSelector->AddSelectionToObject (theObj, aSelection);
+          aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
+        }
+      }
+    }
+    else
+    {
+      for (theObj->Init(); theObj->More(); theObj->Next())
+      {
+        const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
+        theSelector->AddSelectionToObject (theObj, aSelection);
+        aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
+      }
+    }
+    theSelector->RebuildObjectsTree();
+  }
 }
 
-
 //=======================================================================
-//function : IsActivated
-//purpose  : 
+//function : rebuildSelectionStructures
+//purpose  : Internal function that marks 1st level BVH of object theObj
+//           as outdated
 //=======================================================================
-Standard_Boolean SelectMgr_SelectionManager::IsActivated(const Handle(SelectMgr_SelectableObject)& anObject) const
+void SelectMgr_SelectionManager::rebuildSelectionStructures (const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-  for(anObject->Init();anObject->More();anObject->Next()){
-    if(IsActivated(anObject,anObject->CurrentSelection()->Mode()))
-      return Standard_True;
+  if (theSelector.IsNull())
+  {
+    Handle(SelectMgr_ViewerSelector) aSelector;
+    for(TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+      rebuildSelectionStructures (aSelector);
+    }
   }
-  return Standard_False;
-}
-//=======================================================================
-//function : IsActivated
-//purpose  : 
-//=======================================================================
-Standard_Boolean SelectMgr_SelectionManager::IsActivated(const Handle(SelectMgr_SelectableObject)& anObject,
-                                                         const Standard_Integer aMode) const
-{
-  if(!anObject->HasSelection(aMode)) return Standard_False;
-  if (!(myglobal.Contains(anObject) || mylocal.IsBound(anObject))) 
-    return Standard_False;
-
-  Handle(Standard_Transient) Tr;
-  const Handle(SelectMgr_Selection)& Sel = anObject->Selection(aMode);
-  for(TColStd_MapIteratorOfMapOfTransient It(myselectors);It.More();It.Next()){
-    Tr = It.Key();
-    Handle(SelectMgr_ViewerSelector) VS = *((Handle(SelectMgr_ViewerSelector)*)&Tr);
-    if(VS->Status(Sel)==SelectMgr_SOS_Activated)
-      return Standard_True;
+  else
+  {
+    theSelector->RebuildObjectsTree();
   }
-  return Standard_False;
-
 }
 
-//=======================================================================
-//function : IsActivated
-//purpose  : 
-//=======================================================================
-Standard_Boolean SelectMgr_SelectionManager::IsActivated(const Handle(SelectMgr_SelectableObject)& anObject,
-                                                         const Handle(SelectMgr_ViewerSelector)& VS,
-                                                         const Standard_Integer aMode) const
+//==================================================
+// Function: recomputeSelectionMode
+// Purpose :
+//==================================================
+void SelectMgr_SelectionManager::recomputeSelectionMode (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                         const Handle(SelectMgr_Selection)& theSelection,
+                                                         const Standard_Integer theMode)
 {
-  if(!anObject->HasSelection(aMode))                               
-    return Standard_False;
-  if(!myselectors.Contains(VS))                                   
-    return Standard_False;
-  if (!(myglobal.Contains(anObject) || mylocal.IsBound(anObject))) 
-    return Standard_False;
-  const Handle(SelectMgr_Selection)& Sel = anObject->Selection(aMode);
-  return (VS->Status(Sel)==SelectMgr_SOS_Activated);
+  theSelection->UpdateStatus (SelectMgr_TOU_Full);
+
+  for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
+  {
+    Handle(SelectMgr_ViewerSelector) aCurSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
+
+    ClearSelectionStructures (theObject, theMode, aCurSelector);
+    theObject->RecomputePrimitives (theMode);
+    RestoreSelectionStructures (theObject, theMode, aCurSelector);
+    theSelection->UpdateStatus (SelectMgr_TOU_None);
+    theSelection->UpdateBVHStatus (SelectMgr_TBU_None);
+  }
 }
 
 //==================================================
 // Function: Update
 // Purpose :
 //==================================================
-void SelectMgr_SelectionManager::
-RecomputeSelection (const Handle(SelectMgr_SelectableObject)& anObject,
-                    const Standard_Boolean ForceUpdate,
-                    const Standard_Integer aMode)
+void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                     const Standard_Boolean theIsForce,
+                                                     const Standard_Integer theMode)
 {
-  if( SelectDebugModeOnSM() ) cout<<"===>SelectMgr_SelectionManager::Update"<<endl;
-
-  if(ForceUpdate){
-    if( SelectDebugModeOnSM() ) cout<<"\tRecalcul Complet des selections"<<endl;
-    if(aMode==-1){
-      anObject->UpdateSelection();
-      anObject->UpdateLocation();
+  if (theIsForce)
+  {
+    if (theMode == -1)
+    {
+      ClearSelectionStructures (theObject);
+      theObject->RecomputePrimitives();
+      theObject->UpdateTransformation();
+      RestoreSelectionStructures (theObject);
     }
-    else if(anObject->HasSelection(aMode)){
-      anObject->UpdateSelection(aMode);
-      anObject->UpdateLocation();
+    else if (theObject->HasSelection (theMode))
+    {
+      ClearSelectionStructures (theObject, theMode);
+      theObject->RecomputePrimitives (theMode);
+      theObject->UpdateTransformation();
+      RestoreSelectionStructures (theObject, theMode);
     }
     return;
   }
-  // l'objet n'est pas connu du SMgr.
-  if (!(myglobal.Contains(anObject) || mylocal.IsBound(anObject))){
-    if( SelectDebugModeOnSM() ) {cout<<"\tObjet non charge dans le SelectionManager"<<endl;
-    cout<<"\t on flagge ses selections eventuelles"<<endl;}
-    if( aMode == -1 ){
-      for(anObject->Init();anObject->More();anObject->Next()){
-        if( SelectDebugModeOnSM() ) cout<<"\t\t Mode "<<anObject->CurrentSelection()->Mode()<<"  ";
-        anObject->CurrentSelection()->UpdateStatus(SelectMgr_TOU_Full);
-      }
-      if( SelectDebugModeOnSM() )  
-        cout << endl;
-    }
-    else if (anObject->HasSelection(aMode))
-      anObject->Selection(aMode)->UpdateStatus(SelectMgr_TOU_Full);
-  }
-
-  // la il l'est, il s'agit de recalculer ce qui doit l'etre
-  // et de flagger ce qui est en sommeil...
-  else{
-    TColStd_MapIteratorOfMapOfTransient It;
-    Handle(Standard_Transient) Tr;
-    Standard_Boolean Found;
-    // on balaye les selections de l'objet
-
-    for(anObject->Init();anObject->More();anObject->Next()){
-      const Handle(SelectMgr_Selection)& Sel = anObject->CurrentSelection();
-      Sel->UpdateStatus(SelectMgr_TOU_Full);
-      Standard_Integer curmode = Sel->Mode();
-      Found = Standard_False;
-
-      // balayage des selecteurs ...
-      for(It.Initialize(myselectors);It.More();It.Next()){
-        Tr = It.Key();
-        Handle(SelectMgr_ViewerSelector) VS = *((Handle(SelectMgr_ViewerSelector)*)&Tr);
-        if(VS->Status(Sel)==SelectMgr_SOS_Activated){
-          Found  = Standard_True;
-          switch(Sel->UpdateStatus()){
-    case SelectMgr_TOU_Full:
-      anObject->UpdateSelection(curmode); // pas de break expres...
-    case SelectMgr_TOU_Partial:
-      anObject->UpdateLocation(Sel);
-      break;
-#ifndef DEB
-    default:
-      break;
-#endif
-          }
-          if(Found){
-            VS->Convert(Sel);
-            Sel->UpdateStatus(SelectMgr_TOU_None);
-          }
-        }
-      }
+
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
+  }
+
+  if (!theObject->HasOwnPresentations())
+    return;
+
+  if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
+    return;
+
+  if (theMode == -1)
+  {
+    for (theObject->Init(); theObject->More(); theObject->Next())
+    {
+      const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
+      Standard_Integer aSelMode = aSelection->Mode();
+      recomputeSelectionMode (theObject, aSelection, aSelMode);
     }
   }
+  else
+  {
+    if (!theObject->HasSelection (theMode))
+      return;
+
+    const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
+    recomputeSelectionMode (theObject, aSelection, theMode);
+  }
 }
 
 //=======================================================================
 //function : Update
-//purpose  : On recalcule les Selections si elles sont flaggees
-//           "A RECALCULER" et qu'elles sont activees dans un des selecteurs.
-//           Si ForceUpdate = True, et qu'elles sont "A RECALCULER"
-//           On le fait sans se preoccuper de l'etat d'activation.
+//purpose  : Selections are recalculated if they are flagged
+//           "TO RECALCULATE" and activated in one of selectors.
+//           If ForceUpdate = True, and they are "TO RECALCULATE"
+//           This is done without caring for the state of activation.
 //=======================================================================
-void SelectMgr_SelectionManager::Update(const Handle(SelectMgr_SelectableObject)& anObject,
-                                        const Standard_Boolean ForceUpdate)
+void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
+                                         const Standard_Boolean theIsForce)
 {
-  Standard_Boolean wasrecomputed;
+  for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
+  {
+    Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
+  }
 
-  for(anObject->Init();anObject->More();anObject->Next()){
-    const Handle(SelectMgr_Selection)& Sel = anObject->CurrentSelection();
-    wasrecomputed = Standard_False;
-    if(ForceUpdate){
-      switch(Sel->UpdateStatus()){
+  if (!theObject->HasOwnPresentations())
+    return;
+
+  for (theObject->Init(); theObject->More(); theObject->Next())
+  {
+    const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
+    if (theIsForce)
+    {
+      switch (aSelection->UpdateStatus())
+      {
       case SelectMgr_TOU_Full:
-        anObject->UpdateSelection(Sel->Mode()); // pas de break expres...
+        ClearSelectionStructures (theObject, aSelection->Mode());
+        theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
+        RestoreSelectionStructures (theObject, aSelection->Mode());
       case SelectMgr_TOU_Partial:
-        anObject->UpdateLocation(Sel);
-        wasrecomputed = Standard_True;
+        theObject->UpdateTransformations (aSelection);
+        rebuildSelectionStructures();
         break;
-#ifndef DEB
       default:
         break;
-#endif
       }
-      Sel->UpdateStatus(SelectMgr_TOU_None);
+      aSelection->UpdateStatus (SelectMgr_TOU_None);
+      aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
     }
 
-    // on regarde quels selecteurs sont concernes par la selection
-    // pour refaire les projections si besoin est.
-    Handle(Standard_Transient) Tr;
-    for(TColStd_MapIteratorOfMapOfTransient It(myselectors);It.More();It.Next()){
-      Tr = It.Key();
-      Handle(SelectMgr_ViewerSelector) VS = *((Handle(SelectMgr_ViewerSelector)*)&Tr);
-      if(VS->Status(Sel)==SelectMgr_SOS_Activated)
-        switch(Sel->UpdateStatus()){
-  case SelectMgr_TOU_Full:
-    anObject->UpdateSelection(Sel->Mode()); // pas de break expres...
-  case SelectMgr_TOU_Partial:
-    anObject->UpdateLocation(Sel);
-    wasrecomputed = Standard_True;
-    break;
-#ifndef DEB
-  default:
-    break;
-#endif
-      }
-      if(wasrecomputed)
-        VS->Convert(Sel);
-      Sel->UpdateStatus(SelectMgr_TOU_None);
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
+      Update (theObject, aSelector, Standard_False);
     }
   }
 }
 
-
 //==================================================
 // Function: Update
-// Purpose : Attention, il faut savoir ce que l'on fait....
+// Purpose : Attention, it is required to know what is done...
 //==================================================
-void SelectMgr_SelectionManager::
-Update(const Handle(SelectMgr_SelectableObject)& anObject,
-       const Handle(SelectMgr_ViewerSelector)& aViewSel,
-       const Standard_Boolean ForceUpdate)
-{ 
-  if( SelectDebugModeOnSM() ) cout<<"==>SelectMgr_SelectionManager::Update(obj,VS)"<<endl;
-  if(!myselectors.Contains(aViewSel)) return;
-
-  Standard_Boolean okay = myglobal.Contains(anObject);
-  if(!okay)
-    okay = (mylocal.IsBound(anObject) && (SMSearch(mylocal.Find(anObject),aViewSel)!=0)) ;
-  if(!okay) return;
-
-
-  // 
-  Standard_Boolean wasrecomputed;
-  for(anObject->Init();anObject->More();anObject->Next()){
-    const Handle(SelectMgr_Selection)& Sel = anObject->CurrentSelection();
-    wasrecomputed = Standard_False;
-    if(ForceUpdate){
-      switch(Sel->UpdateStatus()){
+void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
+                                         const Handle(SelectMgr_ViewerSelector)& theSelector,
+                                         const Standard_Boolean theIsForce)
+{
+  if (!mySelectors.Contains (theSelector))
+    return;
+
+  Standard_Boolean isKnown = myGlobal.Contains (theObject);
+  if (!isKnown)
+    isKnown = (myLocal.IsBound (theObject) && (FindIndex (myLocal.Find (theObject), theSelector) != 0));
+  if (!isKnown)
+    return;
+
+  for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
+  {
+    Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theSelector, theIsForce);
+  }
+
+  if (!theObject->HasOwnPresentations())
+    return;
+
+  for (theObject->Init(); theObject->More(); theObject->Next())
+  {
+    const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
+    if (theIsForce)
+    {
+      switch (aSelection->UpdateStatus())
+      {
       case SelectMgr_TOU_Full:
-        anObject->UpdateSelection(Sel->Mode()); // pas de break expres...
+        ClearSelectionStructures (theObject, aSelection->Mode());
+        theObject->RecomputePrimitives (aSelection->Mode());
+        RestoreSelectionStructures (theObject, aSelection->Mode());
       case SelectMgr_TOU_Partial:
-        anObject->UpdateLocation(Sel);
-        wasrecomputed = Standard_True;
+        theObject->UpdateTransformations (aSelection);
+        rebuildSelectionStructures();
         break;
-#ifndef DEB
       default:
         break;
-#endif
       }
-      Sel->UpdateStatus(SelectMgr_TOU_None);
+      aSelection->UpdateStatus (SelectMgr_TOU_None);
+      aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
     }
 
-    if(aViewSel->Status(Sel) == SelectMgr_SOS_Activated){
-      switch(Sel->UpdateStatus()){
+    if (theSelector->Status (aSelection) == SelectMgr_SOS_Activated)
+    {
+      switch (aSelection->UpdateStatus())
+      {
       case SelectMgr_TOU_Full:
-        anObject->UpdateSelection(Sel->Mode());
+        ClearSelectionStructures (theObject, aSelection->Mode(), theSelector);
+        theObject->RecomputePrimitives (aSelection->Mode());
+        RestoreSelectionStructures (theObject, aSelection->Mode(), theSelector);
       case SelectMgr_TOU_Partial:
-        if(anObject->HasLocation())
-          anObject->UpdateLocation(Sel);
-        wasrecomputed = Standard_True;
+        if (theObject->HasTransformation())
+        {
+          theObject->UpdateTransformations (aSelection);
+          theSelector->RebuildObjectsTree();
+        }
         break;
-#ifndef DEB
       default:
         break;
-#endif
       }
-      if(wasrecomputed)
-        aViewSel->Convert(Sel);
-      Sel->UpdateStatus(SelectMgr_TOU_None);
+
+      aSelection->UpdateStatus(SelectMgr_TOU_None);
+      aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
     }
   }
 }
 
 //==================================================
-// Function: Status
-// Purpose :
+// Function: loadMode
+// Purpose : Private Method
 //==================================================
-TCollection_AsciiString SelectMgr_SelectionManager::
-Status() const{
-  TCollection_AsciiString theMgrStatus("\t\t\tStatus of the SelectManager :;\n\t\t\t============================\n");
-
-  TCollection_AsciiString nbview (myselectors.Extent()),nbglobal(myglobal.Extent());
-
-  theMgrStatus +=             "\t Number of ViewerSelectors: ";
-  theMgrStatus += nbview +  "\n\t Number of global objects : " + nbglobal+"\n";
-  theMgrStatus = theMgrStatus+"\t Number of local objects  : " + TCollection_AsciiString (mylocal.Extent())+"  \n";
+void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
+                                           const Standard_Integer theMode,
+                                           const Handle(SelectMgr_ViewerSelector)& theSelector)
+{
+  if (theMode == -1)
+    return;
 
-  return theMgrStatus;
+  if (!theObject->HasSelection (theMode))
+  {
+    Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
+    theObject->AddSelection (aNewSel, theMode);
+    if (theSelector.IsNull())
+    {
+      if (myGlobal.Contains (theObject))
+      {
+        TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors);
+        for ( ; aSelectorIter.More(); aSelectorIter.Next())
+        {
+          Handle(SelectMgr_ViewerSelector) aSelector =
+            Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
+          aSelector->AddSelectionToObject (theObject, aNewSel);
+          aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
+        }
+      }
+      else if (myLocal.IsBound (theObject))
+      {
+        const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
+        for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); ++aSelectorIdx)
+        {
+          aSelectors (aSelectorIdx)->AddSelectionToObject (theObject, aNewSel);
+          aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
+        }
+      }
+    }
+    else
+    {
+      theSelector->AddSelectionToObject (theObject, aNewSel);
+      aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
+    }
+  }
+  else if (theObject->Selection (theMode)->IsEmpty())
+  {
+    if (theObject->Selection (theMode)->BVHUpdateStatus() == SelectMgr_TBU_Remove)
+    {
+      Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
+      theObject->AddSelection (aNewSel, theMode);
+      theObject->Selection (theMode)->UpdateBVHStatus (SelectMgr_TBU_Remove);
+      theObject->Selection (theMode)->SetSelectionState (SelectMgr_SOS_Deactivated);
+    }
+  }
 }
 
-//==================================================
-// Function: Status
-// Purpose :
-//==================================================
-
-
-TCollection_AsciiString SelectMgr_SelectionManager::
-Status(const Handle(SelectMgr_SelectableObject)& anObject) const
+//=======================================================================
+//function : SetUpdateMode
+//purpose  :
+//=======================================================================
+void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                const SelectMgr_TypeOfUpdate theType)
 {
-  TCollection_AsciiString TheStatus("\t\tStatus of object:");
+  for (theObject->Init(); theObject->More(); theObject->Next())
+    theObject->CurrentSelection()->UpdateStatus (theType);
+}
 
-  if(myglobal.Contains(anObject))
-  {TheStatus += "GLOBAL (available for all viewers in the SelectionManager)\n";}
-  else if (mylocal.IsBound(anObject))TheStatus +="LOCAL:\n\t\t"; 
-  TColStd_MapIteratorOfMapOfTransient It(myselectors);
-  Standard_Integer iv = 0;
-  //  Standard_Boolean FirstTime=Standard_True;
-  for(;It.More();It.Next()){
-    const Handle(SelectMgr_ViewerSelector)& curview = 
-      Handle(SelectMgr_ViewerSelector)::DownCast(It.Key());  
-    iv++;
-    TheStatus = TheStatus + "status in the ViewerSelector :"+TCollection_AsciiString(iv)+"\n\t\t";
-    TheStatus+=curview->Status(anObject);
-    TheStatus+="\n\t\t----------------------\n\t\t";
-  }
+//=======================================================================
+//function : SetUpdateMode
+//purpose  :
+//=======================================================================
+void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                const Standard_Integer theMode,
+                                                const SelectMgr_TypeOfUpdate theType)
+{
+  if (theObject->HasSelection (theMode))
+    theObject->Selection (theMode)->UpdateStatus (theType);
+}
 
-  return TheStatus;
+//=======================================================================
+//function : SetSelectionSensitivity
+//purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject and
+//           changes previous sensitivity value of all sensitive entities in selection with theMode
+//           to the given theNewSensitivity.
+//=======================================================================
+void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                          const Standard_Integer theMode,
+                                                          const Standard_Integer theNewSens)
+{
+  Standard_ASSERT_RAISE (theNewSens > 0,
+    "Error! Selection sensitivity have positive value.");
 
-}
+  if (theObject.IsNull() || !theObject->HasSelection (theMode))
+    return;
 
-//==================================================
-// Function: LoadMode
-// Purpose : Private Method
-//==================================================
+  Handle(SelectMgr_Selection) aSel = theObject->Selection (theMode);
+  const Standard_Integer aPrevSens = aSel->Sensitivity();
+  aSel->SetSensitivity (theNewSens);
 
+  if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
+    return;
 
-void SelectMgr_SelectionManager
-::LoadMode (const Handle(SelectMgr_SelectableObject)& anObject,
-            const Standard_Integer amode)
-{
-  if(amode==-1) return;
-  if(!anObject->HasSelection(amode))
+  if (myGlobal.Contains (theObject))
   {
-    Handle(SelectMgr_Selection) NewSel = new SelectMgr_Selection(amode); 
-    anObject->AddSelection (NewSel,amode);
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+      if (aSelector->Contains (theObject))
+      {
+        aSelector->myTolerances.Decrement (aPrevSens);
+        aSelector->myTolerances.Add (theNewSens);
+        aSelector->myToUpdateTolerance = Standard_True;
+      }
+    }
+  }
+  if (myLocal.IsBound (theObject))
+  {
+    const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
+    for (SelectMgr_SequenceOfSelector::Iterator aLocalIter (aSelectors); aLocalIter.More(); aLocalIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector)& aCurSel = aLocalIter.ChangeValue();
+      aCurSel->myTolerances.Decrement (aPrevSens);
+      aCurSel->myTolerances.Add (theNewSens);
+      aCurSel->myToUpdateTolerance = Standard_True;
+    }
   }
 }
 
-
 //=======================================================================
-//function : SetUpdateMode
-//purpose  : 
+//function : UpdateSelection
+//purpose  :
 //=======================================================================
-
-void SelectMgr_SelectionManager::
-SetUpdateMode(const Handle(SelectMgr_SelectableObject)& anObject,
-              const SelectMgr_TypeOfUpdate aType)
+void SelectMgr_SelectionManager::UpdateSelection (const Handle(SelectMgr_SelectableObject)& theObject)
 {
-  for(anObject->Init();anObject->More();anObject->Next())
-    anObject->CurrentSelection()->UpdateStatus(aType);
-
-}
+  if (myGlobal.Contains (theObject))
+  {
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+      if (aSelector->Contains (theObject))
+      {
+        aSelector->MoveSelectableObject (theObject);
+      }
+    }
+  }
 
-void SelectMgr_SelectionManager::
-SetUpdateMode(const Handle(SelectMgr_SelectableObject)& anObject,
-              const Standard_Integer aMode,
-              const SelectMgr_TypeOfUpdate aType)
-{
-  if(anObject->HasSelection(aMode))
-    anObject->Selection(aMode)->UpdateStatus(aType);
+  if (myLocal.IsBound (theObject))
+  {
+    const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
+    for (SelectMgr_SequenceOfSelector::Iterator aSelectorsIter (aSelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector)& aSelector = aSelectorsIter.ChangeValue();
+      if (aSelector->Contains (theObject))
+      {
+        aSelector->MoveSelectableObject (theObject);
+      }
+    }
+  }
 }
-