0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
index 901448c..70ba165 100644 (file)
@@ -19,7 +19,6 @@
 #include <AIS_GlobalStatus.hxx>
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_InteractiveObject.hxx>
-#include <AIS_LocalContext.hxx>
 #include <AIS_MapIteratorOfMapOfInteractive.hxx>
 #include <AIS_MapOfInteractive.hxx>
 #include <AIS_Selection.hxx>
@@ -31,7 +30,7 @@
 #include <Prs3d_LineAspect.hxx>
 #include <Prs3d_Presentation.hxx>
 #include <Quantity_Color.hxx>
-#include <SelectBasics_SensitiveEntity.hxx>
+#include <Select3D_SensitiveEntity.hxx>
 #include <SelectMgr_EntityOwner.hxx>
 #include <SelectMgr_Filter.hxx>
 #include <SelectMgr_OrFilter.hxx>
@@ -44,7 +43,6 @@
 #include <TCollection_ExtendedString.hxx>
 #include <TColStd_ListIteratorOfListOfInteger.hxx>
 #include <TopLoc_Location.hxx>
-#include <TopoDS_Shape.hxx>
 #include <V3d_AmbientLight.hxx>
 #include <V3d_DirectionalLight.hxx>
 #include <V3d_Light.hxx>
 
 typedef NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > AIS_MapOfObjSelectedOwners;
 
+namespace
+{
+  TopoDS_Shape AIS_InteractiveContext_myDummyShape;
+}
+
 //=======================================================================
 //function : highlightWithColor
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::highlightWithColor (const Handle(SelectMgr_EntityOwner)& theOwner,
-                                                 const Quantity_NameOfColor theColor,
                                                  const Handle(V3d_Viewer)& theViewer)
 {
-  const Handle(AIS_InteractiveObject) anObj =
-    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
   if (anObj.IsNull())
+  {
     return;
-  const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
+  }
+
+  const Handle(Prs3d_Drawer)& aStyle = getHiStyle (anObj, theOwner);
+  const Standard_Integer aHiMode = getHilightMode (anObj, aStyle, -1);
 
   myMainPM->BeginImmediateDraw();
-  theOwner->HilightWithColor (myMainPM, theColor, aHiMode);
+  theOwner->HilightWithColor (myMainPM, aStyle, aHiMode);
   myMainPM->EndImmediateDraw (theViewer.IsNull() ? myMainVwr : theViewer);
 }
 
@@ -78,34 +83,72 @@ void AIS_InteractiveContext::highlightWithColor (const Handle(SelectMgr_EntityOw
 //function : highlightSelected
 //purpose  :
 //=======================================================================
-void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
-                                                const Quantity_NameOfColor theSelColor)
+void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner)
 {
-  const Handle(AIS_InteractiveObject) anObj =
-    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  AIS_NListOfEntityOwner anOwners;
+  const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
   if (anObj.IsNull())
+  {
     return;
-  const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
+  }
 
   if (!theOwner->IsAutoHilight())
   {
-    AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-    const Handle(AIS_Selection)& aCurSel = AIS_Selection::CurrentSelection();
     SelectMgr_SequenceOfOwner aSeq;
-    for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
+    for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
     {
-      const Handle(SelectMgr_EntityOwner) aSelOwnr =
-        Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
-      if (aSelOwnr->Selectable() != anObj)
-        continue;
-      aSeq.Append (aSelOwnr);
+      if (aSelIter.Value()->IsSameSelectable (anObj))
+      {
+        anOwners.Append (aSelIter.Value());
+      }
+    }
+  }
+  else
+  {
+    anOwners.Append (theOwner);
+  }
+  highlightOwners (anOwners);
+}
+
+//=======================================================================
+//function : highlightGlobal
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightGlobal (const Handle(AIS_InteractiveObject)& theObj,
+                                              const Handle(Prs3d_Drawer)& theStyle,
+                                              const Standard_Integer theDispMode)
+{
+  if (theObj.IsNull())
+  {
+    return;
+  }
+
+  const Standard_Integer aHiMode = getHilightMode (theObj, theStyle, theDispMode);
+  const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
+
+  if (aGlobOwner.IsNull())
+  {
+    myMainPM->Color (theObj, theStyle, aHiMode);
+    return;
+  }
+
+  AIS_NListOfEntityOwner anOwners;
+  if (!aGlobOwner->IsAutoHilight())
+  {
+    SelectMgr_SequenceOfOwner aSeq;
+    for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+    {
+      if (aSelIter.Value()->IsSameSelectable (theObj))
+      {
+        anOwners.Append (aSelIter.Value());
+      }
     }
-    anObj->HilightSelected (myMainPM, aSeq);
   }
   else
   {
-    theOwner->HilightWithColor (myMainPM, theSelColor, aHiMode);
+    anOwners.Append (aGlobOwner);
   }
+  highlightOwners (anOwners);
 }
 
 //=======================================================================
@@ -114,50 +157,161 @@ void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwn
 //=======================================================================
 void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
 {
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
+  unhighlightOwners (mySelection->Objects(), theIsToHilightSubIntensity);
+}
+
+//=======================================================================
+//function : unhighlightOwners
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightOwners (const AIS_NListOfEntityOwner& theOwners,
+                                                const Standard_Boolean theIsToHilightSubIntensity)
+{
   NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
-  for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (theOwners); aSelIter.More(); aSelIter.Next())
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
-    if (anOwner.IsNull() || !anOwner->HasSelectable())
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    const Handle(AIS_InteractiveObject) anInteractive = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    Handle(AIS_GlobalStatus) aStatus;
+    if (!myObjects.Find (anInteractive, aStatus))
+    {
       continue;
+    }
 
-    const Handle(AIS_InteractiveObject) anInteractive =
-      Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
     if (anOwner->IsAutoHilight())
     {
-      const Standard_Integer aHiMode = anInteractive->HasHilightMode() ? anInteractive->HilightMode() : 0;
-      anOwner->Unhilight (myMainPM, aHiMode);
+      anOwner->Unhilight (myMainPM);
       if (theIsToHilightSubIntensity)
       {
-        if (myObjects.IsBound (anInteractive) && myObjects (anInteractive)->IsSubIntensityOn())
+        if (aStatus->IsSubIntensityOn())
         {
-          anOwner->HilightWithColor (myMainPM, mySubIntensity, aHiMode);
+          const Standard_Integer aHiMode = getHilightMode (anInteractive, aStatus->HilightStyle(), aStatus->DisplayMode());
+          highlightWithSubintensity (anOwner, aHiMode);
         }
       }
     }
     else
     {
-      if (!anObjToClear.Contains (anInteractive))
-        anObjToClear.Add (anInteractive);
+      anObjToClear.Add (anInteractive);
     }
-    anOwner->State (0);
     if (anOwner == anInteractive->GlobalSelOwner())
     {
-      myObjects.ChangeFind (anInteractive)->SetHilightStatus (Standard_False);
+      aStatus->SetHilightStatus (Standard_False);
     }
   }
   for (NCollection_IndexedMap<Handle(AIS_InteractiveObject)>::Iterator anIter (anObjToClear); anIter.More(); anIter.Next())
   {
     const Handle(AIS_InteractiveObject)& anObj = anIter.Value();
-    const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
-    myMainPM->Unhighlight (anObj, aHiMode);
+    myMainPM->Unhighlight (anObj);
     anObj->ClearSelected();
   }
 }
 
+//=======================================================================
+//function : unhighlightGlobal
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightGlobal (const Handle(AIS_InteractiveObject)& theObj)
+{
+  if (theObj.IsNull())
+  {
+    return;
+  }
+
+  const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
+  if (aGlobOwner.IsNull())
+  {
+    myMainPM->Unhighlight (theObj);
+    return;
+  }
+
+  AIS_NListOfEntityOwner anOwners;
+  anOwners.Append (aGlobOwner);
+  unhighlightOwners (anOwners);
+}
+
+//=======================================================================
+//function : turnOnSubintensity
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::turnOnSubintensity (const Handle(AIS_InteractiveObject)& theObject,
+                                                 const Standard_Integer theDispMode,
+                                                 const Standard_Boolean theIsDisplayedOnly) const
+{
+  // the only differ with selection highlight is color, so sync transparency values
+  const Handle(Prs3d_Drawer)& aSubStyle = myStyles[Prs3d_TypeOfHighlight_SubIntensity];
+  aSubStyle->SetTransparency (myStyles[Prs3d_TypeOfHighlight_Selected]->Transparency());
+
+  if (theObject.IsNull())
+  {
+    for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjsIter (myObjects); anObjsIter.More(); anObjsIter.Next())
+    {
+      const Handle(AIS_GlobalStatus)& aStatus = anObjsIter.Value();
+      if (aStatus->GraphicStatus() != AIS_DS_Displayed && theIsDisplayedOnly)
+        continue;
+
+      aStatus->SubIntensityOn();
+      myMainPM->Color (anObjsIter.Key(), aSubStyle, theDispMode != -1 ? theDispMode : aStatus->DisplayMode());
+    }
+  }
+  else
+  {
+    Handle(AIS_GlobalStatus) aStatus;
+    if (!myObjects.Find (theObject, aStatus))
+      return;
+
+    if (aStatus->GraphicStatus() != AIS_DS_Displayed && theIsDisplayedOnly)
+      return;
+
+    aStatus->SubIntensityOn();
+    myMainPM->Color (theObject, aSubStyle, theDispMode != -1 ? theDispMode : aStatus->DisplayMode());
+  }
+}
+
+//=======================================================================
+//function : highlightWithSubintensity
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightWithSubintensity (const Handle(AIS_InteractiveObject)& theObject,
+                                                        const Standard_Integer theMode) const
+{
+  // the only differ with selection highlight is color, so
+  // sync transparency values
+  myStyles[Prs3d_TypeOfHighlight_SubIntensity]->SetTransparency (myStyles[Prs3d_TypeOfHighlight_Selected]->Transparency());
+
+  myMainPM->Color (theObject, myStyles[Prs3d_TypeOfHighlight_SubIntensity], theMode);
+}
+
+//=======================================================================
+//function : highlightWithSubintensity
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightWithSubintensity (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                        const Standard_Integer theMode) const
+{
+  // the only differ with selection highlight is color, so
+  // sync transparency values
+  myStyles[Prs3d_TypeOfHighlight_SubIntensity]->SetTransparency (myStyles[Prs3d_TypeOfHighlight_Selected]->Transparency());
+
+  theOwner->HilightWithColor (myMainPM, myStyles[Prs3d_TypeOfHighlight_SubIntensity], theMode);
+}
+
+//=======================================================================
+//function : isSlowHiStyle
+//purpose  :
+//=======================================================================
+Standard_Boolean AIS_InteractiveContext::isSlowHiStyle (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                        const Handle(V3d_Viewer)& theViewer) const
+{
+  if (const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()))
+  {
+    const Handle(Prs3d_Drawer)& aHiStyle = getHiStyle (anObj, myLastPicked);
+    return aHiStyle->ZLayer() == Graphic3d_ZLayerId_UNKNOWN
+       || !theViewer->ZLayerSettings (aHiStyle->ZLayer()).IsImmediate();
+  }
+  return Standard_False;
+}
+
 //=======================================================================
 //function : MoveTo
 //purpose  :
@@ -167,23 +321,17 @@ AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  th
                                                       const Handle(V3d_View)& theView,
                                                       const Standard_Boolean  theToRedrawOnUpdate)
 {
-  if (HasOpenedContext())
-  {
-    myWasLastMain = Standard_True;
-    return myLocalContexts (myCurLocalIndex)->MoveTo (theXPix, theYPix, theView, theToRedrawOnUpdate);
-  }
-
-  myAISCurDetected = 0;
-  myAISDetectedSeq.Clear();
-
   if (theView->Viewer() != myMainVwr)
   {
-    return AIS_SOD_Error;
+    throw Standard_ProgramError ("AIS_InteractiveContext::MoveTo() - invalid argument");
   }
 
+  myCurDetected = 0;
+  myCurHighlighted = 0;
+  myDetectedSeq.Clear();
+  myLastActiveView = theView.get();
+
   // preliminaires
-  myLastPicked  = myLastinMain;
-  myWasLastMain = Standard_True;
   AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
   Standard_Boolean      toUpdateViewer = Standard_False;
 
@@ -194,31 +342,41 @@ AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  th
   // (the objects must be AIS_Shapes)
   const Standard_Integer aDetectedNb = myMainSel->NbPicked();
   Standard_Integer aNewDetected = 0;
+  Standard_Boolean toIgnoreDetTop = Standard_False;
   for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
   {
     Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
     if (anOwner.IsNull()
      || !myFilters->IsOk (anOwner))
     {
+      if (myPickingStrategy == SelectMgr_PickingStrategy_OnlyTopmost)
+      {
+        toIgnoreDetTop = Standard_True;
+      }
       continue;
     }
 
-    if (aNewDetected < 1)
+    if (aNewDetected < 1
+    && !toIgnoreDetTop)
     {
       aNewDetected = aDetIter;
     }
-    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-    if (!anObj.IsNull())
-    {
-      myAISDetectedSeq.Append (anObj);
-    }
+
+    myDetectedSeq.Append (aDetIter);
   }
 
   if (aNewDetected >= 1)
   {
-    // does nothing if previously detected object is equal to the current one
+    myCurHighlighted = myDetectedSeq.Lower();
+
+    // Does nothing if previously detected object is equal to the current one.
+    // However in advanced selection modes the owners comparison
+    // is not effective because in that case only one owner manage the
+    // selection in current selection mode. It is necessary to check the current detected
+    // entity and hilight it only if the detected entity is not the same as
+    // previous detected (IsForcedHilight call)
     Handle(SelectMgr_EntityOwner) aNewPickedOwner = myMainSel->Picked (aNewDetected);
-    if (aNewPickedOwner == myLastPicked)
+    if (aNewPickedOwner == myLastPicked && !aNewPickedOwner->IsForcedHilight())
     {
       return myLastPicked->IsSelected()
            ? AIS_SOD_Selected
@@ -232,31 +390,31 @@ AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  th
     // method call. As result it is necessary to rehighligt it with mySelectionColor.
     if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
     {
-      myMainPM->ClearImmediateDraw();
-      const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
-      const Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
-      if (!myLastPicked->IsSelected())
+      if (isSlowHiStyle (myLastPicked, theView->Viewer()))
       {
-        myLastPicked->Unhilight (myMainPM, aHiMod);
-        toUpdateViewer = Standard_True;
-      }
-      else if (myToHilightSelected)
-      {
-        highlightWithColor (aNewPickedOwner, mySelectionColor, theView->Viewer());
-        toUpdateViewer = Standard_True;
+        theView->Viewer()->Invalidate();
       }
+
+      clearDynamicHighlight();
+      toUpdateViewer = Standard_True;
     }
 
     // initialize myLastPicked field with currently detected object
     myLastPicked = aNewPickedOwner;
-    myLastinMain = myLastPicked;
 
     // highlight detected object if it is not selected or myToHilightSelected flag is true
     if (myLastPicked->HasSelectable())
     {
-      if (!myLastPicked->IsSelected() || myToHilightSelected)
+      if (myAutoHilight
+       && (!myLastPicked->IsSelected()
+         || myToHilightSelected))
       {
-        highlightWithColor (myLastPicked, myHilightColor, theView->Viewer());
+        if (isSlowHiStyle (myLastPicked, theView->Viewer()))
+        {
+          theView->Viewer()->Invalidate();
+        }
+
+        highlightWithColor (myLastPicked, theView->Viewer());
         toUpdateViewer = Standard_True;
       }
 
@@ -270,40 +428,59 @@ AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  th
     // previously detected object is unhilighted if it is not selected or hilighted
     // with selection color if it is selected
     aStatus = AIS_SOD_Nothing;
-    if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
+    if (myAutoHilight
+    && !myLastPicked.IsNull()
+     && myLastPicked->HasSelectable())
     {
-      myMainPM->ClearImmediateDraw();
-      const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
-      Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
-      if (!myLastPicked->IsSelected())
-      {
-        if (myLastPicked->IsAutoHilight())
-        {
-          myLastPicked->Unhilight (myMainPM, aHiMod);
-        }
-        toUpdateViewer = Standard_True;
-      }
-      else if (myToHilightSelected)
+      if (isSlowHiStyle (myLastPicked, theView->Viewer()))
       {
-        highlightSelected (myLastPicked, mySelectionColor);
-        toUpdateViewer = Standard_True;
+        theView->Viewer()->Invalidate();
       }
+
+      clearDynamicHighlight();
+      toUpdateViewer = Standard_True;
     }
 
-    myLastinMain.Nullify();
     myLastPicked.Nullify();
   }
 
   if (toUpdateViewer
    && theToRedrawOnUpdate)
   {
-    theView->Viewer()->Update();
+    if (theView->ComputedMode())
+    {
+      theView->Viewer()->Update();
+    }
+    else
+    {
+      if (theView->IsInvalidated())
+      {
+        theView->Viewer()->Redraw();
+      }
+      else
+      {
+        theView->Viewer()->RedrawImmediate();
+      }
+    }
   }
 
-  mylastmoveview = theView;
   return aStatus;
 }
 
+//=======================================================================
+//function : AddSelect
+//purpose  : 
+//=======================================================================
+AIS_StatusOfPick AIS_InteractiveContext::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
+{
+  mySelection->AddSelect (theObject);
+
+  Standard_Integer aSelNum = NbSelected();
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
+}
+
 //=======================================================================
 //function : Select
 //purpose  : 
@@ -315,40 +492,29 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMi
                                                  const Handle(V3d_View)& theView,
                                                  const Standard_Boolean  toUpdateViewer)
 {
-  // all objects detected by the selector are taken, previous current objects are emptied,
-  // new objects are put...
-
-  if (HasOpenedContext())
+  if (theView->Viewer() != myMainVwr)
   {
-    return myLocalContexts(myCurLocalIndex)->Select (theXPMin, theYPMin,
-                                                     theXPMax, theYPMax,
-                                                     theView, toUpdateViewer);
+    throw Standard_ProgramError ("AIS_InteractiveContext::Select() - invalid argument");
   }
 
+  // all objects detected by the selector are taken, previous current objects are emptied,
+  // new objects are put...
   ClearSelected (Standard_False);
-
-  Handle(StdSelect_ViewerSelector3d) aSelector;
-
-  if (theView->Viewer() == myMainVwr)
-  {
-    aSelector = myMainSel;
-    myWasLastMain = Standard_True;
-  }
-
-  aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-
-  for (aSelector->Init(); aSelector->More(); aSelector->Next())
+  myLastActiveView = theView.get();
+  myMainSel->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
   {
-    const Handle(SelectMgr_EntityOwner)& aCurOwner = aSelector->Picked();
+    const Handle(SelectMgr_EntityOwner)& aCurOwner = myMainSel->Picked (aPickIter);
     if (aCurOwner.IsNull() || !aCurOwner->HasSelectable() || !myFilters->IsOk (aCurOwner))
       continue;
 
-    AIS_Selection::Select (aCurOwner);
-    aCurOwner->State (1);
+    mySelection->Select (aCurOwner);
   }
 
-  HilightSelected (toUpdateViewer);
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
+  }
 
   Standard_Integer aSelNum = NbSelected();
 
@@ -366,39 +532,29 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& the
                                                  const Handle(V3d_View)&     theView,
                                                  const Standard_Boolean      toUpdateViewer)
 {
-  // all objects detected by the selector are taken, previous current objects are emptied,
-  // new objects are put...
-
-  if (HasOpenedContext())
+  if (theView->Viewer() != myMainVwr)
   {
-    return myLocalContexts(myCurLocalIndex)->Select (thePolyline, theView, toUpdateViewer);
+    throw Standard_ProgramError ("AIS_InteractiveContext::Select() - invalid argument");
   }
 
+  // all objects detected by the selector are taken, previous current objects are emptied,
+  // new objects are put...
   ClearSelected (Standard_False);
-
-  Handle(StdSelect_ViewerSelector3d) aSelector;
-
-  if (theView->Viewer() == myMainVwr)
-  {
-    aSelector = myMainSel;
-    myWasLastMain = Standard_True;
-  }
-
-  aSelector->Pick (thePolyline, theView);
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-
-  for (aSelector->Init(); aSelector->More(); aSelector->Next())
+  myLastActiveView = theView.get();
+  myMainSel->Pick (thePolyline, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
+    const Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aPickIter);
     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
-    AIS_Selection::Select (anOwner);
-    anOwner->State (1);
+    mySelection->Select (anOwner);
   }
 
-  HilightSelected (toUpdateViewer);
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
+  }
 
   Standard_Integer aSelNum = NbSelected();
 
@@ -414,25 +570,28 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& the
 //=======================================================================
 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdateViewer)
 {
-  if (HasOpenedContext())
+  if (!myLastPicked.IsNull())
   {
-    if(myWasLastMain)
+    Graphic3d_Vec2i aMousePos (-1, -1);
+    if (myMainSel->GetManager().GetActiveSelectionType() == SelectBasics_SelectingVolumeManager::Point)
     {
-      return myLocalContexts(myCurLocalIndex)->Select (toUpdateViewer);
+      aMousePos.SetValues ((Standard_Integer )myMainSel->GetManager().GetMousePosition().X(),
+                           (Standard_Integer )myMainSel->GetManager().GetMousePosition().Y());
     }
-    else
+    if (myLastPicked->HandleMouseClick (aMousePos, Aspect_VKeyMouse_LeftButton, Aspect_VKeyFlags_NONE, false))
     {
-      myLocalContexts(myCurLocalIndex)->SetSelected (Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable()), toUpdateViewer);
-      return AIS_SOP_OneSelected;
+      return AIS_SOP_NothingSelected;
     }
-  }
 
-  myMainPM->ClearImmediateDraw();
-  if (myWasLastMain && !myLastinMain.IsNull())
-  {
-    if(!myLastinMain->IsSelected())
+    if (myAutoHilight)
+    {
+      clearDynamicHighlight();
+    }
+    if (!myLastPicked->IsSelected()
+      || myLastPicked->IsForcedHilight()
+      || NbSelected() > 1)
     {
-      SetSelected (myLastinMain, Standard_False);
+      SetSelected (myLastPicked, Standard_False);
       if(toUpdateViewer)
       {
         UpdateCurrentViewer();
@@ -441,13 +600,7 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdate
   }
   else
   {
-    unhighlightSelected (Standard_True);
-
-    AIS_Selection::Select();
-    if (toUpdateViewer && myWasLastMain)
-    {
-        UpdateCurrentViewer();
-    }
+    ClearSelected (toUpdateViewer);
   }
 
   Standard_Integer aSelNum = NbSelected();
@@ -463,27 +616,13 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdate
 //=======================================================================
 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
 {
-  if (HasOpenedContext())
+  if (myAutoHilight)
   {
-    if(myWasLastMain)
-    {
-      return myLocalContexts (myCurLocalIndex)->ShiftSelect (toUpdateViewer);
-    }
-    else
-    {
-      myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (myLastPicked, toUpdateViewer);
-
-      Standard_Integer aSelNum = NbSelected();
-      return (aSelNum == 0) ? AIS_SOP_NothingSelected
-                            : (aSelNum == 1) ? AIS_SOP_OneSelected
-                                             : AIS_SOP_SeveralSelected;
-    }
+    clearDynamicHighlight();
   }
-
-  myMainPM->ClearImmediateDraw();
-  if (myWasLastMain && !myLastinMain.IsNull())
+  if (!myLastPicked.IsNull())
   {
-    AddOrRemoveSelected (myLastinMain, toUpdateViewer);
+    AddOrRemoveSelected (myLastPicked, toUpdateViewer);
   }
 
   Standard_Integer aSelNum = NbSelected();
@@ -504,39 +643,30 @@ AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer the
                                                       const Handle(V3d_View)& theView,
                                                       const Standard_Boolean toUpdateViewer)
 {
-  if (HasOpenedContext())
+  if (theView->Viewer() != myMainVwr)
   {
-    return myLocalContexts(myCurLocalIndex)->ShiftSelect (theXPMin, theYPMin, theXPMax, theYPMax,
-                                                          theView, toUpdateViewer);
+    throw Standard_ProgramError ("AIS_InteractiveContext::ShiftSelect() - invalid argument");
   }
 
-  UnhilightSelected (Standard_False);
-
-  Handle(StdSelect_ViewerSelector3d) aSelector;
-  if (theView->Viewer() == myMainVwr)
-  {
-    aSelector = myMainSel;
-    myWasLastMain = Standard_True;
-  }
-  else
+  myLastActiveView = theView.get();
+  if (myAutoHilight)
   {
-    return AIS_SOP_NothingSelected;
+    UnhilightSelected (Standard_False);
   }
-
-  aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  for (aSelector->Init(); aSelector->More(); aSelector->Next())
+  myMainSel->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =  Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
+    const Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aPickIter);
     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
-    AIS_SelectStatus aSelStatus = AIS_Selection::Select (anOwner);
-    Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
-    anOwner->State (aState);
+    mySelection->Select (anOwner);
   }
 
-  HilightSelected (toUpdateViewer);
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
+  }
 
   Standard_Integer aSelNum = NbSelected();
 
@@ -554,40 +684,30 @@ AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d
                                                       const Handle(V3d_View)& theView,
                                                       const Standard_Boolean toUpdateViewer)
 {
-  if (HasOpenedContext())
+  if (theView->Viewer() != myMainVwr)
   {
-    return myLocalContexts(myCurLocalIndex)->ShiftSelect (thePolyline, theView, toUpdateViewer);
+    throw Standard_ProgramError ("AIS_InteractiveContext::ShiftSelect() - invalid argument");
   }
 
-  UnhilightSelected (Standard_False);
-
-  Handle(StdSelect_ViewerSelector3d) aSelector;
-
-  if (theView->Viewer() == myMainVwr)
-  {
-    aSelector= myMainSel;
-    myWasLastMain = Standard_True;
-  }
-  else
+  myLastActiveView = theView.get();
+  if (myAutoHilight)
   {
-    return AIS_SOP_NothingSelected;
+    UnhilightSelected (Standard_False);
   }
-
-  aSelector->Pick (thePolyline, theView);
-
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  for (aSelector->Init(); aSelector->More(); aSelector->Next())
+  myMainSel->Pick (thePolyline, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =  Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
+    const Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aPickIter);
     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
-    AIS_SelectStatus aSelStatus = AIS_Selection::Select (anOwner);
-    Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
-    anOwner->State (aState);
+    mySelection->Select (anOwner);
   }
 
-  HilightSelected (toUpdateViewer);
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
+  }
 
   Standard_Integer aSelNum = NbSelected();
 
@@ -597,247 +717,82 @@ AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d
 }
 
 //=======================================================================
-//function : SetCurrentObject
-//purpose  : OBSOLETE, please use SetSelected() instead
-//TODO     : Remove in process of local context deletion
+//function : HilightSelected
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetCurrentObject (const Handle(AIS_InteractiveObject)& theObject,
-                                               const Standard_Boolean theToUpdateViewer)
+void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdateViewer)
 {
-  if (HasOpenedContext())
-    return;
+  // In case of selection without using local context
+  clearDynamicHighlight();
 
-  SetSelected (theObject, theToUpdateViewer);
+  highlightOwners (mySelection->Objects());
+
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
 //=======================================================================
-//function : AddOrRemoveCurrentObject
-//purpose  : OBSOLETE, please use AddOrRemoveSelected() instead
-//TODO     : Remove in process of local context deletion
+//function : highlightOwners
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::AddOrRemoveCurrentObject (const Handle(AIS_InteractiveObject)& theObj,
-                                                       const Standard_Boolean theIsToUpdateViewer)
+void AIS_InteractiveContext::highlightOwners (const AIS_NListOfEntityOwner& theOwners)
 {
-  if (HasOpenedContext())
-    return;
+  AIS_MapOfObjSelectedOwners anObjOwnerMap;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (theOwners); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    if (anObj.IsNull())
+      continue;
+
+    const Handle(Prs3d_Drawer)& anObjSelStyle = getSelStyle (anObj, anOwner);
+    Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind(anObj);
+    if (anOwner == anObj->GlobalSelOwner())
+    {
+      aState->SetHilightStatus (Standard_True);
+      aState->SetHilightStyle (anObjSelStyle);
+    }
+    if (!anOwner->IsAutoHilight())
+    {
+      NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq;
+      if (anObjOwnerMap.Find (anObj, aSeq))
+      {
+        aSeq->Append (anOwner);
+      }
+      else
+      {
+        aSeq = new SelectMgr_SequenceOfOwner();
+        aSeq->Append (anOwner);
+        anObjOwnerMap.Bind (anObj, aSeq);
+      }
+    }
+    else
+    {
+      const Standard_Integer aHiMode = getHilightMode (anObj, anObjSelStyle, aState->DisplayMode());
+      anOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
+    }
+  }
 
-  AddOrRemoveSelected (theObj, theIsToUpdateViewer);
+  if (!anObjOwnerMap.IsEmpty())
+  {
+    for (AIS_MapOfObjSelectedOwners::Iterator anIter (anObjOwnerMap); anIter.More(); anIter.Next())
+    {
+      anIter.Key()->HilightSelected (myMainPM, *anIter.Value());
+    }
+    anObjOwnerMap.Clear();
+  }
 }
+
 //=======================================================================
-//function : UpdateCurrent
-//purpose  : OBSOLETE, please use UpdateSelected() instead
-//TODO     : Remove in process of local context deletion
+//function : UnhilightSelected
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::UpdateCurrent()
+void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpdateViewer)
 {
-  UpdateSelected();
-}
-
-//=======================================================================
-//function : IsCurrent
-//purpose  : OBSOLETE, please use IsSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-Standard_Boolean AIS_InteractiveContext::IsCurrent (const Handle(AIS_InteractiveObject)& theObject) const
-{
-  return IsSelected (theObject);
-}
-
-//=======================================================================
-//function : InitCurrent
-//purpose  : OBSOLETE, please use InitSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-void AIS_InteractiveContext::InitCurrent()
-{
-  if (HasOpenedContext())
-    return;
-
-  InitSelected();
-}
-
-//=======================================================================
-//function : MoreCurrent
-//purpose  : OBSOLETE, please use MoreSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-Standard_Boolean AIS_InteractiveContext::MoreCurrent() const 
-{
-  return !HasOpenedContext() && MoreSelected();
-}
-
-//=======================================================================
-//function : NextCurrent
-//purpose  : OBSOLETE, please use NextSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-void AIS_InteractiveContext::NextCurrent()
-{
-  if (HasOpenedContext())
-    return;
-
-  NextSelected();
-}
-
-//=======================================================================
-//function : Current
-//purpose  : OBSOLETE, please use SelectedInteractive() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::Current() const 
-{
-  return HasOpenedContext() ? NULL : SelectedInteractive();
-}
-
-//=======================================================================
-//function : NbCurrents
-//purpose  : OBSOLETE, please use NbSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-Standard_Integer AIS_InteractiveContext::NbCurrents()
-{
-  return HasOpenedContext() ? -1 : NbSelected();
-}
-
-//=======================================================================
-//function : HilightCurrents
-//purpose  : OBSOLETE, please use HilightSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-void AIS_InteractiveContext::HilightCurrents (const Standard_Boolean theToUpdateViewer)
-{
-  if (HasOpenedContext())
-    return;
-
-  HilightSelected (theToUpdateViewer);
-}
-
-//=======================================================================
-//function : UnhilightCurrents
-//purpose  : OBSOLETE, please use UnhilightSelected() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-void AIS_InteractiveContext::UnhilightCurrents (const Standard_Boolean theToUpdateViewer)
-{
-  if (HasOpenedContext())
-    return;
-
-  UnhilightSelected (theToUpdateViewer);
-}
-
-//=======================================================================
-//function : ClearCurrents
-//purpose  : OBSOLETE, please use ClearCurrents() instead
-//TODO     : Remove in process of local context deletion
-//=======================================================================
-void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean theToUpdateViewer)
-{
-  if (HasOpenedContext())
-    return;
-
-  ClearSelected (theToUpdateViewer);
-}
-
-
-//=======================================================================
-//function : HilightSelected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdateViewer)
-{
-  if (HasOpenedContext())
-  {
-    return myLocalContexts (myCurLocalIndex)->HilightPicked (theToUpdateViewer);
-  }
-
-  // In case of selection without using local context
-  myMainPM->ClearImmediateDraw();
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aSel = AIS_Selection::Selection (myCurrentName.ToCString());
-  AIS_MapOfObjSelectedOwners anObjOwnerMap;
-  for (aSel->Init(); aSel->More(); aSel->Next())
-  {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
-    if (!anOwner.IsNull() && anOwner->HasSelectable())
-    {
-      const Handle(AIS_InteractiveObject) anObj =
-        Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-      if (anOwner == anObj->GlobalSelOwner())
-      {
-        Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObj);
-        aState->SetHilightStatus (Standard_True);
-        aState->SetHilightColor (mySelectionColor);
-      }
-      anOwner->State (1);
-      if (!anOwner->IsAutoHilight())
-      {
-        NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq;
-        if (anObjOwnerMap.Find (anObj, aSeq))
-        {
-          aSeq->Append (anOwner);
-        }
-        else
-        {
-          aSeq = new SelectMgr_SequenceOfOwner();
-          aSeq->Append (anOwner);
-          anObjOwnerMap.Bind (anObj, aSeq);
-        }
-      }
-      else
-      {
-        const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
-        anOwner->HilightWithColor (myMainPM, mySelectionColor, aHiMode);
-      }
-    }
-  }
-
-  if (!anObjOwnerMap.IsEmpty())
-  {
-    for (AIS_MapOfObjSelectedOwners::Iterator anIter (anObjOwnerMap); anIter.More(); anIter.Next())
-    {
-      anIter.Key()->HilightSelected (myMainPM, *anIter.Value());
-    }
-    anObjOwnerMap.Clear();
-  }
-
-  if (theToUpdateViewer)
-    UpdateCurrentViewer();
-}
-
-//=======================================================================
-//function : UnhilightSelected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpdateViewer)
-{
-  if (HasOpenedContext())
-  {
-    return myLocalContexts (myCurLocalIndex)->UnhilightPicked (theToUpdateViewer);
-  }
-
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aSel = AIS_Selection::Selection (myCurrentName.ToCString());
-  for (aSel->Init(); aSel->More(); aSel->Next())
-  {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
-    if (!anOwner.IsNull() && anOwner->HasSelectable())
-    {
-      const Handle(AIS_InteractiveObject) anObj =
-        Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-      if (anOwner == anObj->GlobalSelOwner())
-      {
-        myObjects.ChangeFind (anObj)->SetHilightStatus (Standard_False);
-      }
-      anOwner->State (0);
-      const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HasHilightMode() : 0;
-      anOwner->Unhilight (myMainPM, aHiMode);
-    }
-  }
-
-  if (theToUpdateViewer)
-    UpdateCurrentViewer();
+  unhighlightSelected();
+
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
 
@@ -847,33 +802,22 @@ void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpda
 //=======================================================================
 void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateViewer)
 {
-  if (HasOpenedContext())
-    return myLocalContexts (myCurLocalIndex)->ClearSelected (theToUpdateViewer);
-
   if (NbSelected() == 0)
     return;
 
-  unhighlightSelected();
-
-  AIS_Selection::Select();
-  myMainPM->ClearImmediateDraw();
-
-  if (theToUpdateViewer)
-    UpdateCurrentViewer();
-}
+  if (myAutoHilight)
+  {
+    unhighlightSelected();
+  }
 
-//=======================================================================
-//function : UpdateSelected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::UpdateSelected (const Standard_Boolean theToUpdateViewer)
-{
-  if (HasOpenedContext())
+  mySelection->Clear();
+  if (myAutoHilight)
   {
-    return myLocalContexts(myCurLocalIndex)->UpdateSelected (theToUpdateViewer);
+    clearDynamicHighlight();
   }
 
-  HilightSelected (theToUpdateViewer);
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
 //=======================================================================
@@ -883,78 +827,72 @@ void AIS_InteractiveContext::UpdateSelected (const Standard_Boolean theToUpdateV
 void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& theObject,
                                           const Standard_Boolean theToUpdateViewer)
 {
-  if(HasOpenedContext())
+  if (theObject.IsNull())
   {
-    return myLocalContexts (myCurLocalIndex)->SetSelected (theObject, theToUpdateViewer);
+    return;
   }
 
-  if (theObject.IsNull())
+  if (!myObjects.IsBound (theObject))
+  {
     return;
-  if(!myObjects.IsBound (theObject))
-    Display (theObject, Standard_False);
-  if (theObject->HasSelection (0))
+  }
+
+  Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
+  if (anOwner.IsNull())
+  {
     return;
+  }
 
-  if (NbSelected() == 1 && myObjects (theObject)->IsHilighted())
+  const Handle(Prs3d_Drawer)& anObjSelStyle = getSelStyle (theObject, anOwner);
+  if (NbSelected() == 1 && myObjects (theObject)->IsHilighted() && myAutoHilight)
   {
-    Quantity_NameOfColor aHiCol;
-    Standard_Boolean hasHiCol = Standard_False;
-    if (IsHilighted (theObject, hasHiCol, aHiCol))
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (HighlightStyle (theObject, aCustomStyle))
     {
-      if (hasHiCol && aHiCol!= mySelectionColor)
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
       {
-        HilightWithColor (theObject, mySelectionColor, theToUpdateViewer);
+        HilightWithColor (theObject, anObjSelStyle, theToUpdateViewer);
       }
     }
     return;
   }
 
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
-  for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
-    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+    const Handle(SelectMgr_EntityOwner)& aSelOwner = aSelIter.Value();
+    if (!myFilters->IsOk (aSelOwner))
+    {
       continue;
+    }
 
-    Handle(AIS_InteractiveObject) aSelectable =
-      Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-    Unhilight (aSelectable, Standard_False);
-    anOwner->State (0);
-    if (anOwner == aSelectable->GlobalSelOwner())
+    Handle(AIS_InteractiveObject) aSelectable = Handle(AIS_InteractiveObject)::DownCast (aSelOwner->Selectable());
+    if (myAutoHilight)
+    {
+      Unhilight (aSelectable, Standard_False);
+    }
+    if (aSelOwner == aSelectable->GlobalSelOwner())
     {
       myObjects.ChangeFind (aSelectable)->SetHilightStatus (Standard_False);
     }
   }
 
   // added to avoid untimely viewer update...
-  const Handle(SelectMgr_Selection)& aSel = theObject->Selection (0);
-  if (aSel->IsEmpty())
-    return;
-  aSel->Init();
-  Handle(SelectMgr_EntityOwner) anOwner =
-    Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
-  AIS_Selection::ClearAndSelect (anOwner);
-  anOwner->State (1);
-  if (anOwner == theObject->GlobalSelOwner())
-  {
-    Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (theObject);
-    aState->SetHilightStatus (Standard_True);
-    aState->SetHilightColor (mySelectionColor);
-  }
-  Quantity_NameOfColor aHiCol;
-  Standard_Boolean hasHiCol = Standard_False;
-  if (IsHilighted (theObject, hasHiCol, aHiCol))
-  {
-    if (hasHiCol && aHiCol!= mySelectionColor)
+  mySelection->ClearAndSelect (anOwner);
+
+  if (myAutoHilight)
+  {
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (HighlightStyle (theObject, aCustomStyle))
     {
-      HilightWithColor (theObject, mySelectionColor, Standard_False);
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+      {
+        HilightWithColor (theObject, anObjSelStyle, Standard_False);
+      }
+    }
+    else
+    {
+      HilightWithColor (theObject, anObjSelStyle, Standard_False);
     }
-  }
-  else
-  {
-    HilightWithColor (theObject, mySelectionColor, Standard_False);
   }
 
   if (theToUpdateViewer)
@@ -971,43 +909,39 @@ void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& t
   if (theOwner.IsNull() || !theOwner->HasSelectable() || !myFilters->IsOk (theOwner))
     return;
 
-  const Handle(AIS_InteractiveObject) anObject =
-    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
-
-  if (NbSelected() == 1 && theOwner->IsSelected())
+  const Handle(AIS_InteractiveObject) anObject = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  const Handle(Prs3d_Drawer)& anObjSelStyle = getSelStyle (anObject, theOwner);
+  if (NbSelected() == 1 && theOwner->IsSelected() && !theOwner->IsForcedHilight())
   {
-    Quantity_NameOfColor aCustomColor;
-    Standard_Boolean isCustomColorSet;
-    if (IsHilighted (theOwner, isCustomColorSet, aCustomColor))
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (myAutoHilight && HighlightStyle (theOwner, aCustomStyle))
     {
-      if (isCustomColorSet && aCustomColor != mySelectionColor)
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
       {
         const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
-        theOwner->HilightWithColor (myMainPM, mySelectionColor, aHiMode);
+        theOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
       }
     }
     return;
   }
 
   if (!myObjects.IsBound (anObject))
-    Display (anObject, Standard_False);
-
-  unhighlightSelected();
+    return;
 
-  AIS_Selection::ClearAndSelect (theOwner);
-  theOwner->State (1);
-  Quantity_NameOfColor aCustomColor;
-  Standard_Boolean isCustomColorSet;
-  if (!IsHilighted (theOwner, isCustomColorSet, aCustomColor) || (isCustomColorSet && aCustomColor!= mySelectionColor))
+  if (myAutoHilight)
   {
-    highlightSelected (theOwner, mySelectionColor);
+    unhighlightSelected();
   }
 
-  if (theOwner == anObject->GlobalSelOwner())
+  mySelection->ClearAndSelect (theOwner);
+  if (myAutoHilight)
   {
-    Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObject);
-    aState->SetHilightStatus (Standard_True);
-    aState->SetHilightColor (mySelectionColor);
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (!HighlightStyle (theOwner, aCustomStyle) ||
+      (!aCustomStyle.IsNull() && aCustomStyle != anObjSelStyle))
+    {
+      highlightSelected (theOwner);
+    }
   }
 
   if (theToUpdateViewer)
@@ -1023,47 +957,18 @@ void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& t
 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
                                                   const Standard_Boolean theToUpdateViewer)
 {
-  if (!theObject->HasInteractiveContext())
-    theObject->SetContext (this);
-
-  if (HasOpenedContext())
-    return myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (theObject, theToUpdateViewer);
-
-  if (theObject.IsNull() || !myObjects.IsBound (theObject) || !theObject->HasSelection (0))
-    return;
-
-  const Handle(SelectMgr_Selection)& aSel = theObject->Selection (0);
-
-  if (aSel->IsEmpty())
-    return;
-
-  aSel->Init();
-  const Handle(SelectMgr_EntityOwner) anOwner =
-    Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
-
-  if (anOwner.IsNull() || !anOwner->HasSelectable())
+  if (theObject.IsNull()
+  || !myObjects.IsBound (theObject))
+  {
     return;
+  }
 
-  AddOrRemoveSelected (anOwner, theToUpdateViewer);
-}
-//=======================================================================
-//function : AddOrRemoveSelected
-//purpose  : 
-//=======================================================================
-
-void AIS_InteractiveContext::AddOrRemoveSelected (const TopoDS_Shape& aShap,
-                                           const Standard_Boolean updateviewer)
-{ 
-  if(!HasOpenedContext()) {
-#ifdef OCCT_DEBUG
-    cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
-#endif
-    return;
+  const Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
+  if (!anOwner.IsNull()
+    && anOwner->HasSelectable())
+  {
+    AddOrRemoveSelected (anOwner, theToUpdateViewer);
   }
-  
-  myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(aShap,updateviewer);
-  if(updateviewer) UpdateCurrentViewer();
-  
 }
 
 //=======================================================================
@@ -1074,132 +979,116 @@ void AIS_InteractiveContext::AddOrRemoveSelected (const TopoDS_Shape& aShap,
 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
                                                   const Standard_Boolean theToUpdateViewer)
 {
-  if (HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected (theOwner, theToUpdateViewer);
-
   if (theOwner.IsNull() || !theOwner->HasSelectable())
     return;
 
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
+  if (!myFilters->IsOk(theOwner) && !theOwner->IsSelected())
+    return;
 
-  AIS_SelectStatus aSelStat = AIS_Selection::Select (theOwner);
+  mySelection->Select (theOwner);
 
-  Standard_Integer aState = aSelStat == AIS_SS_Added ?  1 : 0;
-  theOwner->State (aState);
-  const Handle(AIS_InteractiveObject) anObj =
-    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
-  const Standard_Boolean isGlobal = anObj->GlobalSelOwner() == theOwner;
-  Handle(AIS_GlobalStatus)& aStatus = myObjects.ChangeFind (anObj);
-  const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
-  if (aState == 1)
+  if (myAutoHilight)
   {
-    highlightSelected (theOwner, mySelectionColor);
-    if (isGlobal)
+    const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+    Handle(AIS_GlobalStatus)& aStatus = myObjects.ChangeFind (anObj);
+    if (theOwner->IsSelected())
     {
-      aStatus->SetHilightStatus (Standard_True);
-      aStatus->SetHilightColor (mySelectionColor);
+      highlightSelected (theOwner);
     }
-  }
-  else
-  {
-    if (theOwner->IsAutoHilight())
-      theOwner->Unhilight (myMainPM, aHiMode);
     else
-      anObj->ClearSelected();
-    aStatus->SetHilightStatus (Standard_False);
-    aStatus->SetHilightColor (Quantity_NOC_WHITE);
+    {
+      AIS_NListOfEntityOwner anOwners;
+      anOwners.Append (theOwner);
+      unhighlightOwners (anOwners);
+
+      aStatus->SetHilightStyle (Handle(Prs3d_Drawer)());
+    }
   }
 
   if (theToUpdateViewer)
     UpdateCurrentViewer();
 }
 
-
-//=======================================================================
-//function : IsSelected
-//purpose  :
-//=======================================================================
-Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
+// =======================================================================
+// function : SetSelectedState
+// purpose  :
+// =======================================================================
+Standard_Boolean AIS_InteractiveContext::SetSelectedState (const Handle(SelectMgr_EntityOwner)& theEntity,
+                                                           const Standard_Boolean theIsSelected)
 {
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->IsSelected (theObj);
+  if (theEntity.IsNull())
+  {
+    throw Standard_ProgramError ("Internal error: AIS_InteractiveContext::SetSelectedState() called with NO object");
+  }
 
-  if (theObj.IsNull() || !myObjects.IsBound (theObj))
-    return Standard_False;
+  if (!theEntity->HasSelectable()
+    || mySelection->IsSelected (theEntity) == theIsSelected)
+  {
+    return false;
+  }
 
-  const Standard_Integer aGlobalSelMode = theObj->GlobalSelectionMode();
-  const TColStd_ListOfInteger& anActivatedModes = myObjects (theObj)->SelectionModes();
-  Standard_Boolean isGlobalModeActivated = Standard_False;
-  for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
+  if (theEntity->IsAutoHilight())
   {
-    if (aModeIter.Value() == aGlobalSelMode)
-    {
-      isGlobalModeActivated = Standard_True;
-      break;
-    }
+    AddOrRemoveSelected (theEntity, false);
+    return true;
   }
-  if (!theObj->HasSelection (aGlobalSelMode) || !isGlobalModeActivated)
-    return Standard_False;
 
-  return myObjects (theObj)->IsHilighted();
+  if (theIsSelected)
+  {
+    const AIS_SelectStatus aSelStatus = mySelection->AddSelect (theEntity);
+    theEntity->SetSelected (true);
+    return aSelStatus == AIS_SS_Added;
+  }
+  else
+  {
+    const AIS_SelectStatus aSelStatus = mySelection->Select (theEntity);
+    theEntity->SetSelected (false);
+    return aSelStatus == AIS_SS_Removed;
+  }
 }
 
 //=======================================================================
 //function : IsSelected
-//purpose  : Returns true is the owner given is selected
+//purpose  :
 //=======================================================================
-Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(SelectMgr_EntityOwner)& theOwner) const
+Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
 {
-  if (HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->IsSelected (theOwner);
-
-  if (theOwner.IsNull())
+  if (theObj.IsNull())
+  {
     return Standard_False;
+  }
 
-  return theOwner->IsSelected();
-}
-
-//=======================================================================
-//function : InitSelected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::InitSelected()
-{
-  if (HasOpenedContext())
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
+  if (aStatus == NULL)
   {
-    myLocalContexts (myCurLocalIndex)->InitSelected();
-    return;
+    return Standard_False;
   }
 
-  AIS_Selection::Selection (myCurrentName.ToCString())->Init();
+  const Standard_Integer aGlobalSelMode = theObj->GlobalSelectionMode();
+  const TColStd_ListOfInteger& anActivatedModes = (*aStatus)->SelectionModes();
+  for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
+  {
+    if (aModeIter.Value() == aGlobalSelMode)
+    {
+      if (Handle(SelectMgr_EntityOwner) aGlobOwner = theObj->GlobalSelOwner())
+      {
+        return aGlobOwner->IsSelected();
+      }
+      return Standard_False;
+    }
+  }
+  return Standard_False;
 }
 
 //=======================================================================
-//function : MoreSelected
+//function : FirstSelectedObject
 //purpose  :
 //=======================================================================
-Standard_Boolean AIS_InteractiveContext::MoreSelected() const
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject() const
 {
-  if (HasOpenedContext())
-    return myLocalContexts (myCurLocalIndex)->MoreSelected();
-
-  return AIS_Selection::Selection (myCurrentName.ToCString())->More();
-}
-
-//=======================================================================
-//function : NextSelected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::NextSelected()
-{
-  if(HasOpenedContext())
-  {
-    return myLocalContexts (myCurLocalIndex)->NextSelected();
-    return;
-  }
-
-  AIS_Selection::Selection (myCurrentName.ToCString())->Next();
+  return !mySelection->Objects().IsEmpty()
+        ? Handle(AIS_InteractiveObject)::DownCast (mySelection->Objects().First()->Selectable())
+        : Handle(AIS_InteractiveObject)();
 }
 
 //=======================================================================
@@ -1208,13 +1097,11 @@ void AIS_InteractiveContext::NextSelected()
 //=======================================================================
 Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
 {
-  if(HasOpenedContext())
-  {
-    return myLocalContexts(myCurLocalIndex)->HasSelectedShape();
-  }
+  if (!mySelection->More())
+    return Standard_False;
 
-  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (SelectedInteractive());
-  return !aShape.IsNull();
+  const Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+  return !anOwner.IsNull() && anOwner->HasShape();
 }
 
 //=======================================================================
@@ -1223,57 +1110,16 @@ Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
 //=======================================================================
 TopoDS_Shape AIS_InteractiveContext::SelectedShape() const
 {
-  if (HasOpenedContext())
-  {
-    return myLocalContexts (myCurLocalIndex)->SelectedShape();
-  }
-
-  if (AIS_Selection::Selection (myCurrentName.ToCString())->Extent() == 0)
+  if (!mySelection->More())
     return TopoDS_Shape();
 
-  const Handle(StdSelect_BRepOwner) anOwner =
-    Handle(StdSelect_BRepOwner)::DownCast (AIS_Selection::Selection (myCurrentName.ToCString())->Value());
-  if (!anOwner->HasSelectable())
+  const Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+  if (anOwner.IsNull() || !anOwner->HasSelectable())
     return TopoDS_Shape();
 
   return anOwner->Shape().Located (anOwner->Location() * anOwner->Shape().Location());
 }
 
-//=======================================================================
-//function : SelectedInteractive
-//purpose  :
-//=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::SelectedInteractive() const 
-{
-  if (HasOpenedContext())
-  {
-    return myLocalContexts(myCurLocalIndex)->SelectedInteractive();
-  }
-
-  const Handle(SelectMgr_EntityOwner) anOwner =
-    Handle(SelectMgr_EntityOwner)::DownCast (AIS_Selection::Selection (myCurrentName.ToCString())->Value());
-  if (anOwner.IsNull() || !anOwner->HasSelectable())
-    return NULL;
-
-  return Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-}
-//=======================================================================
-//function : SelectedOwner
-//purpose  :
-//=======================================================================
-Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const
-{
-  if(HasOpenedContext())
-  {
-    return myLocalContexts(myCurLocalIndex)->SelectedOwner();
-  }
-
-  Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
-
-  return aCurSel->Extent() > 0 ?
-    Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value()) : NULL;
-}
-
 //=======================================================================
 //function : EntityOwners
 //purpose  : 
@@ -1282,142 +1128,67 @@ void AIS_InteractiveContext::EntityOwners(Handle(SelectMgr_IndexedMapOfOwner)& t
                                          const Handle(AIS_InteractiveObject)& theIObj,
                                          const Standard_Integer theMode) const 
 {
-  if ( theIObj.IsNull() )
-      return;
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
   TColStd_ListOfInteger aModes;
-  if ( theMode == -1 )
-    ActivatedModes( theIObj, aModes );
+  if (theMode == -1)
+  {
+    ActivatedModes (theIObj, aModes);
+  }
   else
-    aModes.Append( theMode );
+  {
+    aModes.Append (theMode);
+  }
 
   if (theOwners.IsNull())
+  {
     theOwners = new SelectMgr_IndexedMapOfOwner();
+  }
 
-  TColStd_ListIteratorOfListOfInteger anItr( aModes );
-  for (; anItr.More(); anItr.Next() )
+  for (TColStd_ListIteratorOfListOfInteger anItr (aModes); anItr.More(); anItr.Next())
   {
-    int aMode = anItr.Value();
-    if ( !theIObj->HasSelection( aMode ) )
+    const int aMode = anItr.Value();
+    const Handle(SelectMgr_Selection)& aSel = theIObj->Selection (aMode);
+    if (aSel.IsNull())
+    {
       continue;
+    }
 
-    Handle(SelectMgr_Selection) aSel = theIObj->Selection(aMode);
-
-    for ( aSel->Init(); aSel->More(); aSel->Next() )
+    for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
     {
-      Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive()->BaseSensitive();
-      if ( aEntity.IsNull() )
-       continue;
-
-      Handle(SelectMgr_EntityOwner) aOwner =
-       Handle(SelectMgr_EntityOwner)::DownCast(aEntity->OwnerId());
-      if ( !aOwner.IsNull() )
-       theOwners->Add( aOwner );
+      if (Handle(Select3D_SensitiveEntity) aEntity = aSelEntIter.Value()->BaseSensitive())
+      {
+        if (const Handle(SelectMgr_EntityOwner)& aOwner = aEntity->OwnerId())
+        {
+          theOwners->Add (aOwner);
+        }
+      }
     }
   }
 }
 
-//=======================================================================
-//function : NbSelected
-//purpose  :
-//=======================================================================
-Standard_Integer AIS_InteractiveContext::NbSelected()
-{
-  Standard_Integer aNbSelected = 0;
-  for (InitSelected(); MoreSelected(); NextSelected())
-  {
-    aNbSelected++;
-  }
-
-  return aNbSelected;
-}
-
-//=======================================================================
-//function : HasApplicative
-//purpose  :
-//=======================================================================
-  Standard_Boolean AIS_InteractiveContext::HasApplicative() const 
-{
-  return SelectedInteractive()->HasOwner();
-}
-
-//=======================================================================
-//function : Applicative
-//purpose  :
-//=======================================================================
-Handle(Standard_Transient) AIS_InteractiveContext::Applicative() const 
-{
-  return SelectedInteractive()->GetOwner();
-}
-
-//==================================================
-// Function: HasDetected
-// Purpose :
-//==================================================
-Standard_Boolean AIS_InteractiveContext::HasDetected() const
-{
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->HasDetected();
-
-  return !myLastPicked.IsNull();
-}
-
 //=======================================================================
 //function : HasDetectedShape
 //purpose  : 
 //=======================================================================
-
 Standard_Boolean AIS_InteractiveContext::HasDetectedShape() const 
 {
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->HasDetectedShape();
-  return Standard_False;
+  Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (myLastPicked);
+  return !anOwner.IsNull()
+       && anOwner->HasShape();
 }
 
 //=======================================================================
 //function : DetectedShape
 //purpose  : 
 //=======================================================================
-
-const TopoDS_Shape&
-AIS_InteractiveContext::DetectedShape() const
-{
-  return myLocalContexts(myCurLocalIndex)->DetectedShape();
-}                                          
-
-//=======================================================================
-//function : DetectedInteractive
-//purpose  :
-//=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedInteractive() const
-{
-  if (HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->DetectedInteractive();
-
-  return Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
-}
-
-
-Standard_Boolean AIS_InteractiveContext::HasNextDetected() const 
-{
-  if(!HasOpenedContext())
-    return Standard_False; // temporaire
-  else
-    return myLocalContexts(myCurLocalIndex)->HasNextDetected();
-  
-}
-
-
-//=======================================================================
-//function : DetectedOwner
-//purpose  : 
-//=======================================================================
-Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
+const TopoDS_Shape& AIS_InteractiveContext::DetectedShape() const
 {
-  if (HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->DetectedOwner();
-
-  return myLastPicked;
+  Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (myLastPicked);
+  return anOwner->Shape();
 }
 
 //=======================================================================
@@ -1427,134 +1198,101 @@ Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
 Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
                                                               const Standard_Boolean  theToRedrawImmediate)
 {
-  return HasOpenedContext()
-       ? myLocalContexts (myCurLocalIndex)->HilightNextDetected (theView, theToRedrawImmediate)
-       : 0;
-    
-}
-
-//=======================================================================
-//function : HilightNextDetected
-//purpose  :
-//=======================================================================
-Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
-                                                                  const Standard_Boolean  theToRedrawImmediate)
-{
-  return HasOpenedContext()
-       ? myLocalContexts (myCurLocalIndex)->HilightPreviousDetected (theView, theToRedrawImmediate)
-       : 0;
-    
-}
-
-//=======================================================================
-//function : InitDetected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::InitDetected()
-{
-  if (HasOpenedContext())
+  myMainPM->ClearImmediateDraw();
+  if (myDetectedSeq.IsEmpty())
   {
-    myLocalContexts(myCurLocalIndex)->InitDetected();
-    return;
+    return 0;
   }
 
-  if(myAISDetectedSeq.Length() != 0)
+  if (++myCurHighlighted > myDetectedSeq.Upper())
   {
-    myAISCurDetected = 1;
+    myCurHighlighted = myDetectedSeq.Lower();
   }
-}
-
-//=======================================================================
-//function : MoreDetected
-//purpose  :
-//=======================================================================
-Standard_Boolean AIS_InteractiveContext::MoreDetected() const
-{
-  if (HasOpenedContext())
+  const Handle(SelectMgr_EntityOwner)& anOwner = myMainSel->Picked (myDetectedSeq (myCurHighlighted));
+  if (anOwner.IsNull())
   {
-    return myLocalContexts(myCurLocalIndex)->MoreDetected();
+    return 0;
   }
 
-  return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length()) ?
-          Standard_True : Standard_False;
-}
+  highlightWithColor (anOwner, theView->Viewer());
+  myLastPicked = anOwner;
 
-//=======================================================================
-//function : NextDetected
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::NextDetected()
-{
-  if(HasOpenedContext())
+  if (theToRedrawImmediate)
   {
-    myLocalContexts(myCurLocalIndex)->NextDetected();
-    return;
+    myMainPM->RedrawImmediate (theView->Viewer());
+    myMainVwr->RedrawImmediate();
   }
 
-  myAISCurDetected++;
+  return myCurHighlighted;
 }
 
 //=======================================================================
-//function : DetectedCurrentShape
+//function : HilightPreviousDetected
 //purpose  :
 //=======================================================================
-const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
+Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
+                                                                  const Standard_Boolean  theToRedrawImmediate)
 {
-  if (HasOpenedContext())
+  myMainPM->ClearImmediateDraw();
+  if (myDetectedSeq.IsEmpty())
   {
-    return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
+    return 0;
   }
 
-  static TopoDS_Shape aDummyShape;
+  if (--myCurHighlighted < myDetectedSeq.Lower())
+  {
+    myCurHighlighted = myDetectedSeq.Upper();
+  }
+  const Handle(SelectMgr_EntityOwner)& anOwner = myMainSel->Picked (myDetectedSeq (myCurHighlighted));
+  if (anOwner.IsNull())
+  {
+    return 0;
+  }
 
-  Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
+  highlightWithColor (anOwner, theView->Viewer());
+  myLastPicked = anOwner;
 
-  if (aCurrentShape.IsNull())
+  if (theToRedrawImmediate)
   {
-    return aDummyShape;
+    myMainPM->RedrawImmediate (theView->Viewer());
+    myMainVwr->RedrawImmediate();
   }
 
-  return aCurrentShape->Shape();
+  return myCurHighlighted;
 }
 
 //=======================================================================
-//function : DetectedCurrentObject
+//function : DetectedCurrentOwner
 //purpose  :
 //=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
+Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedCurrentOwner() const
 {
-  if (HasOpenedContext())
-  {
-    return myLocalContexts(myCurLocalIndex)->DetectedCurrentObject();
-  }
-
-  return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
+  return MoreDetected()
+       ? myMainSel->Picked (myDetectedSeq (myCurDetected))
+       : Handle(SelectMgr_EntityOwner)();
 }
 
 //=======================================================================
-//function : FirstSelectedObject
+//function : DetectedCurrentShape
 //purpose  :
 //=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject()
+const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
 {
-  Handle(AIS_InteractiveObject) anObject;
-
-  if (HasOpenedContext())
-    return anObject;
-
-  InitSelected();
-  if (MoreSelected())
-  {
-    return SelectedInteractive();
-  }
-  return anObject;
+  Standard_DISABLE_DEPRECATION_WARNINGS
+  Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
+  Standard_ENABLE_DEPRECATION_WARNINGS
+  return !aCurrentShape.IsNull()
+        ? aCurrentShape->Shape()
+        : AIS_InteractiveContext_myDummyShape;
 }
 
 //=======================================================================
-//function : RedrawImmediate
-//purpose  : Redisplays immediate strucures of the viewer given according to their visibility
+//function : DetectedCurrentObject
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
 {
-  myMainPM->RedrawImmediate (theViewer);
+  return MoreDetected()
+       ? Handle(AIS_InteractiveObject)::DownCast (myMainSel->Picked (myDetectedSeq (myCurDetected))->Selectable())
+       : Handle(AIS_InteractiveObject)();
 }