0030784: Visualization - check if selectable of owner in internal container in AIS_In...
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 6f4323a..7c9b44c
 // Created on: 1997-01-29
 // Created by: Robert COUBLANC
 // Copyright (c) 1997-1999 Matra Datavision
-// Copyright (c) 1999-2012 OPEN CASCADE SAS
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// 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.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 
-#define UKI60826       //GG_161199     Use the requested selection color instead of default
-
-#define        IMP280200       //GG Don't returns a NULL shape in SelectedShape()
-//                     method when the current selected is a shape !
+#include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
+#include <AIS_GlobalStatus.hxx>
+#include <AIS_InteractiveContext.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_MapIteratorOfMapOfInteractive.hxx>
+#include <AIS_MapOfInteractive.hxx>
+#include <AIS_Selection.hxx>
+#include <AIS_Shape.hxx>
+#include <AIS_StatusOfDetection.hxx>
+#include <AIS_StatusOfPick.hxx>
+#include <Aspect_Grid.hxx>
+#include <Prs3d_BasicAspect.hxx>
+#include <Prs3d_LineAspect.hxx>
+#include <Prs3d_Presentation.hxx>
+#include <Quantity_Color.hxx>
+#include <Select3D_SensitiveEntity.hxx>
+#include <SelectMgr_EntityOwner.hxx>
+#include <SelectMgr_Filter.hxx>
+#include <SelectMgr_OrFilter.hxx>
+#include <SelectMgr_Selection.hxx>
+#include <SelectMgr_SelectionManager.hxx>
+#include <Standard_Transient.hxx>
+#include <StdSelect_BRepOwner.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TCollection_ExtendedString.hxx>
+#include <TColStd_ListIteratorOfListOfInteger.hxx>
+#include <TopLoc_Location.hxx>
+#include <V3d_AmbientLight.hxx>
+#include <V3d_DirectionalLight.hxx>
+#include <V3d_Light.hxx>
+#include <V3d_PositionalLight.hxx>
+#include <V3d_SpotLight.hxx>
+#include <V3d_View.hxx>
+#include <V3d_Viewer.hxx>
 
-#define BUC60814       //GG_300101     Idem UKI60826
+typedef NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > AIS_MapOfObjSelectedOwners;
 
-#define IMP150501       //GG_150501     CADPAK_V2 Add Drag() method
+namespace
+{
+  TopoDS_Shape AIS_InteractiveContext_myDummyShape;
+}
 
-#define IMP160701      //ZSV  Add InitDetected(),MoreDetected(),NextDetected(),
-//                       DetectedCurrentShape(),DetectedCurrentObject()
-//                       methods
+//=======================================================================
+//function : highlightWithColor
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightWithColor (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                 const Handle(V3d_Viewer)& theViewer)
+{
+  const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  if (anObj.IsNull())
+  {
+    return;
+  }
 
-#define IMP191001      //GG Avoid to raise when switching with the 
-//                     SetAutomaticHilight() method.
-//                     Thanks to IFO of SAMTECH company for this improvment.
+  const Handle(Prs3d_Drawer)& aStyle = getHiStyle (anObj, theOwner);
+  const Standard_Integer aHiMode = getHilightMode (anObj, aStyle, -1);
 
-#define OCC138          //VTN Avoding infinit loop in AddOrRemoveCurrentObject method.
+  myMainPM->BeginImmediateDraw();
+  theOwner->HilightWithColor (myMainPM, aStyle, aHiMode);
+  myMainPM->EndImmediateDraw (theViewer.IsNull() ? myMainVwr : theViewer);
+}
 
-#define OCC9657
+//=======================================================================
+//function : highlightSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner)
+{
+  AIS_NListOfEntityOwner anOwners;
+  const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  if (anObj.IsNull())
+  {
+    return;
+  }
 
-#include <AIS_InteractiveContext.jxx>
-#include <SelectMgr_EntityOwner.hxx>
-#include <AIS_Selection.hxx>
-#include <AIS_StatusOfDetection.hxx>
-#include <AIS_StatusOfPick.hxx>
+  if (!theOwner->IsAutoHilight())
+  {
+    SelectMgr_SequenceOfOwner aSeq;
+    for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+    {
+      if (aSelIter.Value()->IsSameSelectable (anObj))
+      {
+        anOwners.Append (aSelIter.Value());
+      }
+    }
+  }
+  else
+  {
+    anOwners.Append (theOwner);
+  }
+  highlightOwners (anOwners);
+}
 
-#include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
-#include <AIS_LocalContext.hxx>
-#include <AIS_GlobalStatus.hxx>
-#include <AIS_Shape.hxx>
+//=======================================================================
+//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;
+  }
 
-#include <V3d_Light.hxx>
-#include <V3d_PositionalLight.hxx>
-#include <V3d_SpotLight.hxx>
-#include <V3d_DirectionalLight.hxx>
-#include <V3d_AmbientLight.hxx>
+  const Standard_Integer aHiMode = getHilightMode (theObj, theStyle, theDispMode);
+  const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
 
-#include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <SelectMgr_Selection.hxx>
-#include <SelectBasics_SensitiveEntity.hxx>
+  if (aGlobOwner.IsNull())
+  {
+    myMainPM->Color (theObj, theStyle, aHiMode);
+    return;
+  }
 
-#ifdef IMP150501
-#include <Visual3d_TransientManager.hxx>
-#include <Prs3d_Presentation.hxx>
-#endif
+  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());
+      }
+    }
+  }
+  else
+  {
+    anOwners.Append (aGlobOwner);
+  }
+  highlightOwners (anOwners);
+}
 
-#ifdef OCC9657
-#include <AIS_MapOfInteractive.hxx>
-#include <AIS_MapIteratorOfMapOfInteractive.hxx>
-#endif
+//=======================================================================
+//function : unhighlightSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
+{
+  unhighlightOwners (mySelection->Objects(), theIsToHilightSubIntensity);
+}
 
-//unused
-/*#ifdef DEB
-static void InfoOnLight(const Handle(V3d_View) aView)
+//=======================================================================
+//function : unhighlightOwners
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightOwners (const AIS_NListOfEntityOwner& theOwners,
+                                                const Standard_Boolean theIsToHilightSubIntensity)
 {
-  Standard_Real V1=0.,V2=0.,V3=0.;
-  Standard_Integer IndexOfLit(0);
-  
-  static Standard_Boolean FirstTime (Standard_True);
-  if(FirstTime)
+  NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (theOwners); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    const Handle(AIS_InteractiveObject) anInteractive = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (anInteractive);
+    if (!aStatusPtr)
     {
-      FirstTime = Standard_False;
-      for (aView->InitActiveLights();aView->MoreActiveLights();aView->NextActiveLights()){
-       IndexOfLit++;
-       cout<<"lumiere no "<<IndexOfLit<<"\n************\n\t";
-       Handle(V3d_Light) Li = aView->ActiveLight();
-       Quantity_NameOfColor Col;
-       Li->Color(Col);
-       cout<<"Color :"<<Standard_Integer(Col)<<"\n\t"<<endl;
-       cout<<"type :";
-       switch(Li->Type()){
-       case V3d_AMBIENT:
-         {
-           cout<<" AMBIENT\n\t";
-//POP K4L
-//         Handle(V3d_AmbientLight)::DownCast(Li)->DisplayPosition(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           break;
-         }
-       case V3d_DIRECTIONAL:
-         {
-           cout<<" DIRECTIONAL\n\t";
-           Handle(V3d_DirectionalLight)::DownCast(Li)->DisplayPosition(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           Handle(V3d_DirectionalLight)::DownCast(Li)->Direction(V1,V2,V3);
-           cout<<"Direction : V1 = "<<V1<<"  V2 = "<<V2<<"  V3 = "<<V3<<endl; 
-
-           break;
-         }
-       case V3d_POSITIONAL:
-         {
-           cout<<" POSITIONAL\n\t";
-           Handle(V3d_PositionalLight)::DownCast(Li)->Position(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           Handle(V3d_PositionalLight)::DownCast(Li)->Target(V1,V2,V3);
-           cout<<"Target    : x1 = "<<V1<<"  x2 = "<<V2<<"  x3 = "<<V3<<endl; 
-           break;
-         }
-       case V3d_SPOT:
-         {
-           cout<<" SPOT\n\t";
-           cout<<" DIRECTIONAL\n\t";
-           Handle(V3d_SpotLight)::DownCast(Li)->Position(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           Handle(V3d_SpotLight)::DownCast(Li)->Direction(V1,V2,V3);
-           cout<<"Direction : V1 = "<<V1<<"  V2 = "<<V2<<"  V3 = "<<V3<<endl; 
-           Handle(V3d_PositionalLight)::DownCast(Li)->Target(V1,V2,V3);
-           cout<<"Target    : x1 = "<<V1<<"  x2 = "<<V2<<"  x3 = "<<V3<<endl; 
-           cout<<"\tAngle :"<<Handle(V3d_SpotLight)::DownCast(Li)->Angle()<<endl;
-           break;
-         }
-       }
+      continue;
+    }
+
+    if (anOwner->IsAutoHilight())
+    {
+      anOwner->Unhilight (myMainPM);
+      if (theIsToHilightSubIntensity)
+      {
+        if ((*aStatusPtr)->IsSubIntensityOn())
+        {
+          const Standard_Integer aHiMode = getHilightMode (anInteractive, (*aStatusPtr)->HilightStyle(), (*aStatusPtr)->DisplayMode());
+          highlightWithSubintensity (anOwner, aHiMode);
+        }
       }
     }
+    else
+    {
+      anObjToClear.Add (anInteractive);
+    }
+    if (anOwner == anInteractive->GlobalSelOwner())
+    {
+      (*aStatusPtr)->SetHilightStatus (Standard_False);
+    }
+  }
+  for (NCollection_IndexedMap<Handle(AIS_InteractiveObject)>::Iterator anIter (anObjToClear); anIter.More(); anIter.Next())
+  {
+    const Handle(AIS_InteractiveObject)& anObj = anIter.Value();
+    myMainPM->Unhighlight (anObj);
+    anObj->ClearSelected();
+  }
 }
-#endif
-*/
+
 //=======================================================================
-//function : MoveTo
-//purpose  : 
+//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());
 
-AIS_StatusOfDetection AIS_InteractiveContext::MoveTo(const Standard_Integer XPix, 
-                                  const Standard_Integer YPix, 
-                                  const Handle(V3d_View)& aView)
+  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(HasOpenedContext()){
-    myWasLastMain = Standard_True;
-    return myLocalContexts(myCurLocalIndex)->MoveTo(XPix,YPix,aView);
+  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;
+}
 
-#ifdef IMP160701
-    //Nullify class members storing information about detected AIS objects.
-  myAISCurDetected = 0;
-  myAISDetectedSeq.Clear();
-#endif
+//=======================================================================
+//function : MoveTo
+//purpose  :
+//=======================================================================
+AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  theXPix,
+                                                      const Standard_Integer  theYPix,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean  theToRedrawOnUpdate)
+{
+  if (theView->Viewer() != myMainVwr)
+  {
+    throw Standard_ProgramError ("AIS_InteractiveContext::MoveTo() - invalid argument");
+  }
 
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle (PrsMgr_PresentationManager3d) pmgr ;
-  Handle (StdSelect_ViewerSelector3d) selector;
-  Standard_Boolean ismain = Standard_True,UpdVwr = Standard_False;
-  
-  // Preliminaires
-  if(aView->Viewer()== myMainVwr) {
-    pmgr = myMainPM;
-    selector=myMainSel;
-    myLastPicked = myLastinMain;
-    myWasLastMain = Standard_True;
-  }
-  else 
-    return AIS_SOD_Error;
-  
-  AIS_StatusOfDetection TheStat(AIS_SOD_Nothing);
-  
-  
-  // allonzy
-  selector->Pick(XPix, YPix, aView);
-
-#ifdef IMP160701
-  //filling of myAISDetectedSeq sequence storing information about detected AIS objects
-  // (the objects must be AIS_Shapes).
-  Handle(SelectMgr_EntityOwner) anEntityOwner;
-  const Standard_Integer NbDetected = selector->NbPicked();
-  for(Standard_Integer i_detect = 1;i_detect<=NbDetected;i_detect++)
-  {
-    anEntityOwner = selector->Picked(i_detect);
-    if(!anEntityOwner.IsNull())
-      if(myFilters->IsOk(anEntityOwner))
+  myCurDetected = 0;
+  myCurHighlighted = 0;
+  myDetectedSeq.Clear();
+  myLastActiveView = theView.get();
+
+  // preliminaires
+  AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
+  Standard_Boolean      toUpdateViewer = Standard_False;
+
+  myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
+  myMainSel->Pick (theXPix, theYPix, theView);
+
+  // filling of myAISDetectedSeq sequence storing information about detected AIS objects
+  // (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)
       {
-        Handle(AIS_InteractiveObject) anObj = 
-               Handle(AIS_InteractiveObject)::DownCast(anEntityOwner->Selectable());
-        if(!Handle(AIS_Shape)::DownCast(anObj).IsNull())
-          myAISDetectedSeq.Append(anObj);
+        toIgnoreDetTop = Standard_True;
       }
+      continue;
+    }
+
+    if (aNewDetected < 1
+    && !toIgnoreDetTop)
+    {
+      aNewDetected = aDetIter;
+    }
+
+    myDetectedSeq.Append (aDetIter);
   }
-#endif
 
-  selector->Init();
-  if ( selector->More() )
+  if (aNewDetected >= 1)
   {
-    if ( HasOpenedContext() ) 
+    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 && !aNewPickedOwner->IsForcedHilight())
     {
-      if ( !myFilters->IsOk( selector->OnePicked() ) ) 
-        return AIS_SOD_AllBad;
-      else
-        if ( !myLocalContexts( myCurLocalIndex )->Filter()->IsOk( selector->OnePicked() ) )
-          return AIS_SOD_AllBad;
+      return myLastPicked->IsSelected()
+           ? AIS_SOD_Selected
+           : AIS_SOD_OnlyOneDetected;
     }
  
-    // Does nothing if previously detected object is equal to the current one
-    if ( selector->OnePicked()->Selectable() == myLastPicked )
-      return AIS_SOD_OnlyOneDetected;
     // Previously detected object is unhilighted if it is not selected or hilighted 
     // with selection color if it is selected. Such highlighting with selection color 
     // is needed only if myToHilightSelected flag is true. In this case previously detected
     // object has been already highlighted with myHilightColor during previous MoveTo() 
     // method call. As result it is necessary to rehighligt it with mySelectionColor.
-    if ( !myLastPicked.IsNull() )
+    if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
     {
-      Standard_Integer HiMod =  
-        myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
-      if ( myLastPicked->State() != 1 )
+      if (isSlowHiStyle (myLastPicked, theView->Viewer()))
       {
-       pmgr->Unhighlight( myLastPicked, HiMod );
-        UpdVwr = Standard_True;
+        theView->Viewer()->Invalidate();
       }
-      else if ( myToHilightSelected )
+
+      clearDynamicHighlight();
+      toUpdateViewer = Standard_True;
+    }
+
+    // initialize myLastPicked field with currently detected object
+    myLastPicked = aNewPickedOwner;
+
+    // highlight detected object if it is not selected or myToHilightSelected flag is true
+    if (myLastPicked->HasSelectable())
+    {
+      if (myAutoHilight
+       && (!myLastPicked->IsSelected()
+         || myToHilightSelected))
       {
-        pmgr->Color( myLastPicked, mySelectionColor, HiMod );
-        UpdVwr = Standard_True;
+        if (isSlowHiStyle (myLastPicked, theView->Viewer()))
+        {
+          theView->Viewer()->Invalidate();
+        }
+
+        highlightWithColor (myLastPicked, theView->Viewer());
+        toUpdateViewer = Standard_True;
       }
+
+      aStatus = myLastPicked->IsSelected()
+              ? AIS_SOD_Selected
+              : AIS_SOD_OnlyOneDetected;
     }
-    
-    // Initialize myLastPicked field with currently detected object
-    Handle(SelectMgr_SelectableObject) SO = selector->OnePicked()->Selectable();
-    myLastPicked = *((Handle(AIS_InteractiveObject)*)&SO);
-    
-    if ( ismain )
-      myLastinMain = myLastPicked;
-#ifdef IMP191001
-    // Highlight detected object if it is not selected or myToHilightSelected flag is true
-    if ( !myLastPicked.IsNull() && 
-         ( myLastPicked->State()!= 1 || myToHilightSelected ) )
-#else
-     if ( myLastPicked->State()!= 1 )
-#endif
+  }
+  else
+  {
+    // previously detected object is unhilighted if it is not selected or hilighted
+    // with selection color if it is selected
+    aStatus = AIS_SOD_Nothing;
+    if (myAutoHilight
+    && !myLastPicked.IsNull()
+     && myLastPicked->HasSelectable())
     {
-      Standard_Integer HiMod =  
-        myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
-      pmgr->Color( myLastPicked, myHilightColor, HiMod );
-      UpdVwr = Standard_True;
+      if (isSlowHiStyle (myLastPicked, theView->Viewer()))
+      {
+        theView->Viewer()->Invalidate();
+      }
+
+      clearDynamicHighlight();
+      toUpdateViewer = Standard_True;
     }
-    
-    if ( myLastPicked->State()==1 )
-      TheStat = AIS_SOD_Selected;
+
+    myLastPicked.Nullify();
   }
-  else 
+
+  if (toUpdateViewer
+   && theToRedrawOnUpdate)
   {
-    // Previously detected object is unhilighted if it is not selected or hilighted 
-    // with selection color if it is selected. 
-    TheStat = AIS_SOD_Nothing;
-    if ( !myLastPicked.IsNull() )
+    if (theView->ComputedMode())
+    {
+      theView->Viewer()->Update();
+    }
+    else
     {
-      Standard_Integer HiMod =  
-        myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
-      if ( myLastPicked->State() != 1 )
+      if (theView->IsInvalidated())
       {
-       pmgr->Unhighlight( myLastPicked, HiMod );
-        UpdVwr = Standard_True;
+        theView->Viewer()->Redraw();
       }
-      else if ( myToHilightSelected )
+      else
       {
-        pmgr->Color( myLastPicked, mySelectionColor, HiMod );
-        UpdVwr = Standard_True;
+        theView->Viewer()->RedrawImmediate();
       }
     }
-    
-    if ( ismain )
-      myLastinMain.Nullify();
   }
-  
-  if(UpdVwr) aView->Viewer()->Update();
-  myLastPicked.Nullify();
-  
-  mylastmoveview = aView;
-  return TheStat;
+
+  return aStatus;
 }
 
 //=======================================================================
-//function : Select
+//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;
+}
 
-AIS_StatusOfPick AIS_InteractiveContext::Select(const Standard_Integer XPMin,
-                                               const Standard_Integer  YPMin,
-                                               const Standard_Integer  XPMax,
-                                               const Standard_Integer  YPMax, 
-                                               const Handle(V3d_View)& aView,
-                                               const Standard_Boolean updateviewer)
+//=======================================================================
+//function : Select
+//purpose  : 
+//=======================================================================
+AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMin,
+                                                 const Standard_Integer  theYPMin,
+                                                 const Standard_Integer  theXPMax,
+                                                 const Standard_Integer  theYPMax,
+                                                 const Handle(V3d_View)& theView,
+                                                 const Standard_Boolean  toUpdateViewer)
 {
+  if (theView->Viewer() != myMainVwr)
+  {
+    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);
+  myLastActiveView = theView.get();
+  myMainSel->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner)& aCurOwner = myMainSel->Picked (aPickIter);
+    if (aCurOwner.IsNull() || !aCurOwner->HasSelectable() || !myFilters->IsOk (aCurOwner))
+      continue;
 
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->Select(XPMin,YPMin,XPMax,YPMax,aView,updateviewer);
+    mySelection->Select (aCurOwner);
+  }
 
-  ClearCurrents(Standard_False);
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle(StdSelect_ViewerSelector3d) selector;
-  
-  if(aView->Viewer()== myMainVwr) {
-    selector= myMainSel;
-    myWasLastMain = Standard_True;}
-
-  selector->Pick(XPMin,YPMin,XPMax,YPMax,aView);
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-
-#ifdef OCC9657
-  AIS_MapOfInteractive theSelectedObj;
-  for( selector->Init(); selector->More(); selector->Next() )
-  {
-    Handle( AIS_InteractiveObject ) anObj = 
-      Handle( AIS_InteractiveObject )::DownCast( selector->Picked()->Selectable() );
-    if( !anObj.IsNull() )
-      theSelectedObj.Add( anObj );
-  }
-  AIS_MapIteratorOfMapOfInteractive anIt( theSelectedObj );
-  for( ; anIt.More(); anIt.Next() )
-  {
-    AIS_Selection::Select( anIt.Key() );
-    anIt.Key()->State(1);
-  }
-#else
-  for(selector->Init();selector->More();selector->Next()){
-    const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-    if(!SO.IsNull()){
-#ifdef OCC138
-      AIS_Selection::Select(SO);
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-#else
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-      AIS_Selection::Select(SO);
-#endif //OCC138
-    }
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
   }
 
-#endif //OCC9657
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
+  Standard_Integer aSelNum = NbSelected();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
   
 }
 
@@ -361,64 +528,39 @@ AIS_StatusOfPick AIS_InteractiveContext::Select(const Standard_Integer XPMin,
 //function : Select
 //purpose  : Selection by polyline
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::Select(const TColgp_Array1OfPnt2d& aPolyline,
-                                               const Handle(V3d_View)& aView,
-                                               const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
+                                                 const Handle(V3d_View)&     theView,
+                                                 const Standard_Boolean      toUpdateViewer)
 {
+  if (theView->Viewer() != myMainVwr)
+  {
+    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);
+  myLastActiveView = theView.get();
+  myMainSel->Pick (thePolyline, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aPickIter);
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+      continue;
 
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->Select(aPolyline,aView,updateviewer);
+    mySelection->Select (anOwner);
+  }
 
-  ClearCurrents(Standard_False);
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle(StdSelect_ViewerSelector3d) selector;
-  
-  if(aView->Viewer()== myMainVwr) {
-    selector= myMainSel;
-    myWasLastMain = Standard_True;}
-
-  selector->Pick(aPolyline,aView);
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-
-
-#ifdef OCC9657
-  AIS_MapOfInteractive theSelectedObj;
-  for( selector->Init(); selector->More(); selector->Next() )
-  {
-    Handle( AIS_InteractiveObject ) anObj = 
-      Handle( AIS_InteractiveObject )::DownCast( selector->Picked()->Selectable() );
-    if( !anObj.IsNull() )
-      theSelectedObj.Add( anObj );
-  }
-  AIS_MapIteratorOfMapOfInteractive anIt( theSelectedObj );
-  for( ; anIt.More(); anIt.Next() )
-  {
-    AIS_Selection::Select( anIt.Key() );
-    anIt.Key()->State(1);
-  }
-#else
-  for(selector->Init();selector->More();selector->Next()){
-    const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-    if(!SO.IsNull()){
-#ifdef OCC138
-      AIS_Selection::Select(SO);
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-#else
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-      AIS_Selection::Select(SO);
-#endif //OCC138
-    }
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
   }
-#endif //OCC9657
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
+
+  Standard_Integer aSelNum = NbSelected();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
   
 }
 
@@ -426,1031 +568,743 @@ AIS_StatusOfPick AIS_InteractiveContext::Select(const TColgp_Array1OfPnt2d& aPol
 //function : Select
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::Select(const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdateViewer)
 {
-  if(HasOpenedContext()){
-    if(myWasLastMain)
-      return myLocalContexts(myCurLocalIndex)->Select(updateviewer);
-    else
-      // picking was done in the collector, special processing is required...
-      {
-       myLocalContexts(myCurLocalIndex)->SetSelected(myLastPicked,updateviewer);
-       return AIS_SOP_OneSelected;
-      }
-  }
-//  AIS_StatusOfPick PS(AIS_SOP_NothingSelected);
-  if(myWasLastMain && !myLastinMain.IsNull()){
-    if(myLastinMain->State()!=1){
-      SetCurrentObject(myLastinMain,Standard_False);
-      if(updateviewer)
-       UpdateCurrentViewer();}
-  }
-  else{
-    AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-    Handle(AIS_Selection) S = AIS_Selection::CurrentSelection();
-    Handle(Standard_Transient) Tr;
-    Handle(AIS_InteractiveObject) IO;
-    
-    for(S->Init();S->More();S->Next()){
-      Tr = S->Value();
-      IO = (*((Handle(AIS_InteractiveObject)*)&Tr));
-      IO->State(0);
-      Unhilight(IO,Standard_False);
-      if(myObjects.IsBound(IO)){ // anti-plantage-rob
-       if(myObjects(IO)->IsSubIntensityOn())
-         HilightWithColor(IO,mySubIntensity,Standard_False);
-      }
+  if (!myLastPicked.IsNull())
+  {
+    Graphic3d_Vec2i aMousePos (-1, -1);
+    if (myMainSel->GetManager().GetActiveSelectionType() == SelectBasics_SelectingVolumeManager::Point)
+    {
+      aMousePos.SetValues ((Standard_Integer )myMainSel->GetManager().GetMousePosition().X(),
+                           (Standard_Integer )myMainSel->GetManager().GetMousePosition().Y());
     }
-    
-    AIS_Selection::Select();
-    if(updateviewer){
-      if(myWasLastMain)
-        UpdateCurrentViewer();
+    if (myLastPicked->HandleMouseClick (aMousePos, Aspect_VKeyMouse_LeftButton, Aspect_VKeyFlags_NONE, false))
+    {
+      return AIS_SOP_NothingSelected;
     }
-  }
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-}
-
-//=======================================================================
-//function : ShiftSelect
-//purpose  : 
-//=======================================================================
 
-AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect(const Standard_Boolean updateviewer)
-{
-  if(HasOpenedContext()){
-    if(myWasLastMain)
-      return myLocalContexts(myCurLocalIndex)->ShiftSelect(updateviewer);
-    else{
-      myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(myLastPicked,updateviewer);
-      Standard_Integer NS =NbSelected();
-      if(NS==0) return AIS_SOP_NothingSelected;
-      if(NS==1) return AIS_SOP_OneSelected;
-      return AIS_SOP_SeveralSelected;
+    if (myAutoHilight)
+    {
+      clearDynamicHighlight();
+    }
+    if (!myLastPicked->IsSelected()
+      || myLastPicked->IsForcedHilight()
+      || NbSelected() > 1)
+    {
+      SetSelected (myLastPicked, Standard_False);
+      if(toUpdateViewer)
+      {
+        UpdateCurrentViewer();
+      }
     }
   }
-  if(myWasLastMain && !myLastinMain.IsNull())
-    AddOrRemoveCurrentObject(myLastinMain,updateviewer);
+  else
+  {
+    ClearSelected (toUpdateViewer);
+  }
 
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-}
+  Standard_Integer aSelNum = NbSelected();
 
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
+}
 
 //=======================================================================
 //function : ShiftSelect
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect(const Standard_Integer XPMin, 
-                                                    const Standard_Integer YPMin, 
-                                                    const Standard_Integer XPMax, 
-                                                    const Standard_Integer YPMax, 
-                                                    const Handle(V3d_View)& aView,
-                                                    const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
 {
-  
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->ShiftSelect(XPMin,YPMin,XPMax,YPMax,aView,updateviewer);
-  
-  UnhilightCurrents(Standard_False);
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle(StdSelect_ViewerSelector3d) selector;
-  
-  if(aView->Viewer()== myMainVwr) {
-    selector= myMainSel;
-    myWasLastMain = Standard_True;}
-  else
-    return AIS_SOP_NothingSelected;
-  
-  selector->Pick(XPMin,YPMin,XPMax,YPMax,aView);
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-  for(selector->Init();selector->More();selector->Next()){
-    const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-    if(!SO.IsNull()){
-      AIS_SelectStatus SelStat = AIS_Selection::Select(SO);
-      Standard_Integer mod = (SelStat==AIS_SS_Added)?1:0;
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(mod);
-    }
+  if (myAutoHilight)
+  {
+    clearDynamicHighlight();
+  }
+  if (!myLastPicked.IsNull())
+  {
+    AddOrRemoveSelected (myLastPicked, toUpdateViewer);
   }
-  
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS =NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-  
-}
-
-//=======================================================================
-//function : ShiftSelect
-//purpose  : 
-//=======================================================================
 
-AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect( const TColgp_Array1OfPnt2d& aPolyline,
-                                                      const Handle(V3d_View)& aView,
-                                                      const Standard_Boolean updateviewer )
-{
-    if( HasOpenedContext() )
-        return myLocalContexts( myCurLocalIndex )->ShiftSelect( aPolyline, aView, updateviewer );
+  Standard_Integer aSelNum = NbSelected();
 
-    UnhilightCurrents( Standard_False );
-    // OCC11904 - local variables made non-static - it looks and works better like this
-    Handle(StdSelect_ViewerSelector3d) selector;
-  
-    if( aView->Viewer() == myMainVwr ) {
-        selector= myMainSel;
-        myWasLastMain = Standard_True;
-    }
-    else
-        return AIS_SOP_NothingSelected;
-
-    selector->Pick( aPolyline, aView );
-    
-    AIS_Selection::SetCurrentSelection( myCurrentName.ToCString() );
-    for( selector->Init(); selector->More(); selector->Next() ) {
-        const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-        if( !SO.IsNull() ) {
-            AIS_SelectStatus SelStat = AIS_Selection::Select( SO );
-            Standard_Integer mod = ( SelStat == AIS_SS_Added ) ? 1 : 0;
-            (*((Handle(AIS_InteractiveObject)*)&SO))->State( mod );
-        }
-    }
-    
-    HilightCurrents( updateviewer );
-    Standard_Integer NS = NbCurrents();
-    if( NS == 0 ) return AIS_SOP_NothingSelected;
-    if( NS == 1 ) return AIS_SOP_OneSelected;
-    return AIS_SOP_SeveralSelected;
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                        : AIS_SOP_SeveralSelected;
 }
 
 //=======================================================================
-//function : SetCurrentObject
+//function : ShiftSelect
 //purpose  : 
 //=======================================================================
-
-void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
-                                             const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer theXPMin,
+                                                      const Standard_Integer theYPMin,
+                                                      const Standard_Integer theXPMax,
+                                                      const Standard_Integer theYPMax,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean toUpdateViewer)
 {
-  // single case processing...
-  if(NbCurrents()==1 && anIObj->State()==1){
-    Quantity_NameOfColor HiCol;
-    Standard_Boolean HasHiCol;
-    if(IsHilighted(anIObj,HasHiCol,HiCol)){
-      if(HasHiCol && HiCol!= mySelectionColor) {
-#ifdef UKI60826
-       HilightWithColor(anIObj,mySelectionColor,updateviewer);
-#else
-       Hilight(anIObj,updateviewer);
-#endif
-      }
-    }
-    return;
+  if (theView->Viewer() != myMainVwr)
+  {
+    throw Standard_ProgramError ("AIS_InteractiveContext::ShiftSelect() - invalid argument");
   }
 
+  myLastActiveView = theView.get();
+  if (myAutoHilight)
+  {
+    UnhilightSelected (Standard_False);
+  }
+  myMainSel->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aPickIter);
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+      continue;
 
-  // 
-  if(!HasOpenedContext()) {
-    if(anIObj.IsNull()) return;
-    if(!myObjects.IsBound(anIObj)) 
-      Display(anIObj,Standard_False);
-    
-    AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-    Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-    Handle(Standard_Transient) TR;
-    Handle(AIS_InteractiveObject) IO;
-    sel->Init();
-    while (sel->More()) {
-      TR = sel->Value();
-      IO = *((Handle(AIS_InteractiveObject)*)&TR);
-      Unhilight(IO,Standard_False);
-      IO->State(0);
-      sel->Next();
-    }
-    
-    // added to avoid untimely viewer update...
-    AIS_Selection::ClearAndSelect(anIObj);
-    anIObj->State(1);
-    Quantity_NameOfColor HiCol;
-    Standard_Boolean HasHiCol;
-    if(IsHilighted(anIObj,HasHiCol,HiCol)){
-      if(HasHiCol && HiCol!= mySelectionColor) {
-#ifdef UKI60826
-         HilightWithColor(anIObj,mySelectionColor,Standard_False);
-#else
-         Hilight(anIObj,Standard_False);
-#endif
-      }
-    }
-    else{
-#ifdef UKI60826
-       HilightWithColor(anIObj,mySelectionColor,Standard_False);
-#else
-      Hilight(anIObj,Standard_False);
-#endif
-    }
-    if (updateviewer) 
-      UpdateCurrentViewer();
+    mySelection->Select (anOwner);
   }
-  else{
-#ifdef DEB
-    cout<<"Nothing Done : Opened Local Context"<<endl;
-#endif
+
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
   }
-}
 
-//=======================================================================
-//function : AddOrRemoveCurrentObject
-//purpose  : 
-//=======================================================================
+  Standard_Integer aSelNum = NbSelected();
 
-void AIS_InteractiveContext::
-AddOrRemoveCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
-                        const Standard_Boolean updateviewer)
-{
-  if(anIObj.IsNull()) return;
-//OCC5682 to remove an object from neutral point even if Local Context is opened (Remove method)
-  if(!HasOpenedContext() /*OCC5682*/|| myObjects.IsBound(anIObj)) {
-    
-    AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-    Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-
-    AIS_SelectStatus SelStat = AIS_Selection::Select(anIObj);
-    
-    Standard_Integer mod = (SelStat==AIS_SS_Added) ? 1 : 0;
-    anIObj->State(mod);
-    if(mod==1)
-#ifdef BUC60814
-      HilightWithColor(anIObj,mySelectionColor,Standard_False);
-#else
-      Hilight(anIObj,Standard_False);
-#endif
-    else 
-      Unhilight(anIObj,Standard_False);
-    
-    
-    if(updateviewer) 
-      UpdateCurrentViewer();
-  }
-  else{
-#ifdef DEB
-    cout<<"Nothing Done : Opened Local Context"<<endl;
-#endif
-  }
-}
-//=======================================================================
-//function : UpdateCurrent
-//purpose  : 
-//=======================================================================
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
 
-void AIS_InteractiveContext::UpdateCurrent()
-{
-  HilightCurrents();
 }
 
 //=======================================================================
-//function : IsCurrent
+//function : ShiftSelect
 //purpose  : 
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::IsCurrent(const Handle(AIS_InteractiveObject)& anIObj) const 
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean toUpdateViewer)
 {
+  if (theView->Viewer() != myMainVwr)
+  {
+    throw Standard_ProgramError ("AIS_InteractiveContext::ShiftSelect() - invalid argument");
+  }
 
-  if (anIObj.IsNull()) return Standard_False;
-  return (anIObj->State()==1);
-  
-  //   Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-  //   sel->Init();
-  //   while (sel->More()) {
-  //     if (anIObj == sel->Value()) return Standard_True;
-  //     sel->Next();
-  //   }
-  //   return Standard_False;
-
-}
-
-//=======================================================================
-//function : InitCurrent
-//purpose  : 
-//=======================================================================
+  myLastActiveView = theView.get();
+  if (myAutoHilight)
+  {
+    UnhilightSelected (Standard_False);
+  }
+  myMainSel->Pick (thePolyline, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aPickIter);
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+      continue;
 
-void AIS_InteractiveContext::InitCurrent()
-{
-  AIS_Selection::Selection(myCurrentName.ToCString())->Init();
-}
+    mySelection->Select (anOwner);
+  }
 
-//=======================================================================
-//function : MoreCurrent
-//purpose  : 
-//=======================================================================
+  if (myAutoHilight)
+  {
+    HilightSelected (toUpdateViewer);
+  }
 
-Standard_Boolean AIS_InteractiveContext::MoreCurrent() const 
-{
-  return AIS_Selection::Selection(myCurrentName.ToCString())->More();
+  Standard_Integer aSelNum = NbSelected();
 
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
 }
 
 //=======================================================================
-//function : NextCurrent
-//purpose  : 
+//function : HilightSelected
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::NextCurrent()
+void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdateViewer)
 {
-  AIS_Selection::Selection(myCurrentName.ToCString())->Next();
-}
+  // In case of selection without using local context
+  clearDynamicHighlight();
 
-//=======================================================================
-//function : Current
-//purpose  : 
-//=======================================================================
+  highlightOwners (mySelection->Objects());
 
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::Current() const 
-{
-  Handle(Standard_Transient) TR = AIS_Selection::Selection(myCurrentName.ToCString())->Value();
-  Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&TR);
-  return IO;
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
 //=======================================================================
-//function : FirstCurrentObject
-//purpose  : 
+//function : highlightOwners
+//purpose  :
 //=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstCurrentObject()  
+void AIS_InteractiveContext::highlightOwners (const AIS_NListOfEntityOwner& theOwners)
 {
-  Handle(AIS_InteractiveObject) IO ;
-  InitCurrent();
-  if(MoreCurrent()){
-    return Current();
+  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)* aStatusPtr = myObjects.ChangeSeek (anObj);
+    if (!aStatusPtr)
+    {
+      continue;
+    }
+    if (anOwner == anObj->GlobalSelOwner())
+    {
+      (*aStatusPtr)->SetHilightStatus (Standard_True);
+      (*aStatusPtr)->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, (*aStatusPtr)->DisplayMode());
+      anOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
+    }
   }
-  return IO;
-}
 
-//=======================================================================
-//function : NbCurrents
-//purpose  : 
-//=======================================================================
-Standard_Integer AIS_InteractiveContext::NbCurrents()
-{
-  Standard_Integer i(0);
-  for(InitCurrent();MoreCurrent();NextCurrent())
-    {i++;}
-  return i;
+  if (!anObjOwnerMap.IsEmpty())
+  {
+    for (AIS_MapOfObjSelectedOwners::Iterator anIter (anObjOwnerMap); anIter.More(); anIter.Next())
+    {
+      anIter.Key()->HilightSelected (myMainPM, *anIter.Value());
+    }
+    anObjOwnerMap.Clear();
+  }
 }
 
 //=======================================================================
-//function : HilightCurrents
-//purpose  : 
+//function : UnhilightSelected
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::HilightCurrents(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpdateViewer)
 {
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-  Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-  Handle(Standard_Transient) TR;
-  Handle(AIS_InteractiveObject) IO;
-  sel->Init();
-  while (sel->More()) {
-    TR = sel->Value();
-    IO = *((Handle(AIS_InteractiveObject)*)&TR);
-#ifdef BUC60814
-    HilightWithColor(IO,mySelectionColor,Standard_False);
-#else
-    Hilight(IO,Standard_False);
-#endif
-    sel->Next();
-  }
-  if(updateviewer) 
+  unhighlightSelected();
+
+  if (theToUpdateViewer)
     UpdateCurrentViewer();
 }
 
+
 //=======================================================================
-//function : UnhilightCurrents
-//purpose  : 
+//function : ClearSelected
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::UnhilightCurrents(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateViewer)
 {
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-  Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-  Handle(Standard_Transient) TR;
-  Handle(AIS_InteractiveObject) IO;
-  sel->Init();
-  while (sel->More()) {
-    TR = sel->Value();
-    IO = *((Handle(AIS_InteractiveObject)*)&TR);
-    Unhilight(IO,Standard_False);
-    sel->Next();
-  }
-  if(updateviewer) 
+  if (NbSelected() == 0)
+    return;
+
+  if (myAutoHilight)
+  {
+    unhighlightSelected();
+  }
+
+  mySelection->Clear();
+  if (myAutoHilight)
+  {
+    clearDynamicHighlight();
+  }
+
+  if (theToUpdateViewer)
     UpdateCurrentViewer();
 }
 
 //=======================================================================
-//function : ClearCurrents
-//purpose  : 
+//function : SetSelected
+//purpose  : Sets the whole object as selected and highlights it with selection color
 //=======================================================================
-
-void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& theObject,
+                                          const Standard_Boolean theToUpdateViewer)
 {
-  if(NbCurrents()==0) return;
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-  Handle(AIS_Selection) S = AIS_Selection::CurrentSelection();
-  Handle(Standard_Transient) Tr;
-  Handle(AIS_InteractiveObject) IO;
-  for(S->Init();S->More();S->Next()){
-    Tr = S->Value();
-    IO = (*((Handle(AIS_InteractiveObject)*)&Tr));
-    IO->State(0);
-    Unhilight(IO,Standard_False);
-  }
-  AIS_Selection::Select();
-  if(updateviewer)
-    UpdateCurrentViewer();
-}
+  if (theObject.IsNull())
+  {
+    return;
+  }
 
+  if (!myObjects.IsBound (theObject))
+  {
+    return;
+  }
 
-//=======================================================================
-//function : HilightSelected
-//purpose  : 
-//=======================================================================
+  Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
+  if (anOwner.IsNull())
+  {
+    return;
+  }
 
-void AIS_InteractiveContext::HilightSelected(const Standard_Boolean updateviewer)
-{
-  if(!HasOpenedContext()){
-    AIS_Selection::SetCurrentSelection(mySelectionName.ToCString());
-    Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-    Handle(Standard_Transient) TR;
-    Handle(AIS_InteractiveObject) IO;
-    sel->Init();
-    while (sel->More()) {
-      TR = sel->Value();
-      IO = *((Handle(AIS_InteractiveObject)*)&TR);
-#ifdef BUC60814
-      HilightWithColor(IO,mySelectionColor,Standard_False);
-#else
-      Hilight(IO,Standard_False);
-#endif
-      sel->Next();
+  const Handle(Prs3d_Drawer)& anObjSelStyle = getSelStyle (theObject, anOwner);
+  if (NbSelected() == 1 && myObjects (theObject)->IsHilighted() && myAutoHilight)
+  {
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (HighlightStyle (theObject, aCustomStyle))
+    {
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+      {
+        HilightWithColor (theObject, anObjSelStyle, theToUpdateViewer);
+      }
     }
+    return;
   }
-  else
+
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner)& aSelOwner = aSelIter.Value();
+    if (!myFilters->IsOk (aSelOwner))
     {
-      myLocalContexts(myCurLocalIndex)->HilightPicked(updateviewer);
+      continue;
     }
-  if(updateviewer)
-    UpdateCurrentViewer();
-}
-
-//=======================================================================
-//function : UnhilightSelected
-//purpose  : 
-//=======================================================================
 
-void AIS_InteractiveContext::UnhilightSelected(const Standard_Boolean updateviewer)
-{
-  if(!HasOpenedContext()){
-    AIS_Selection::SetCurrentSelection(mySelectionName.ToCString());
-    Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-    Handle(Standard_Transient) TR;
-    Handle(AIS_InteractiveObject) IO;
-    sel->Init();
-    while (sel->More()) {
-      TR = sel->Value();
-      IO = *((Handle(AIS_InteractiveObject)*)&TR);
-      Unhilight(IO,Standard_False);
-      sel->Next();
+    Handle(AIS_InteractiveObject) aSelectable = Handle(AIS_InteractiveObject)::DownCast (aSelOwner->Selectable());
+    if (myAutoHilight)
+    {
+      Unhilight (aSelectable, Standard_False);
+    }
+    if (aSelOwner == aSelectable->GlobalSelOwner())
+    {
+      if (Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (aSelectable))
+      {
+        (*aStatusPtr)->SetHilightStatus (Standard_False);
+      }
     }
-    if(updateviewer)
-      UpdateCurrentViewer();
   }
-  else
+
+  // added to avoid untimely viewer update...
+  mySelection->ClearAndSelect (anOwner);
+
+  if (myAutoHilight)
+  {
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (HighlightStyle (theObject, aCustomStyle))
+    {
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+      {
+        HilightWithColor (theObject, anObjSelStyle, Standard_False);
+      }
+    }
+    else
     {
-      myLocalContexts(myCurLocalIndex)->UnhilightPicked(updateviewer);
+      HilightWithColor (theObject, anObjSelStyle, Standard_False);
     }
-}
+  }
 
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
+}
 
 //=======================================================================
-//function : ClearSelected
-//purpose  : 
+//function : SetSelected
+//purpose  : Sets the whole object as selected and highlights it with selection color
 //=======================================================================
-
-void AIS_InteractiveContext::ClearSelected(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                          const Standard_Boolean theToUpdateViewer)
 {
-  if(!HasOpenedContext())
-    ClearCurrents(updateviewer);
-  else 
-    myLocalContexts(myCurLocalIndex)->ClearSelected(updateviewer);
-  
-}
+  if (theOwner.IsNull() || !theOwner->HasSelectable() || !myFilters->IsOk (theOwner))
+    return;
 
+  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())
+  {
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (myAutoHilight && HighlightStyle (theOwner, aCustomStyle))
+    {
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+      {
+        const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
+        theOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
+      }
+    }
+    return;
+  }
 
-//=======================================================================
-//function : SetSelectedCurrent
-//purpose  : 
-//=======================================================================
+  if (!myObjects.IsBound (anObject))
+    return;
 
-void AIS_InteractiveContext::SetSelectedCurrent()
-{
+  if (myAutoHilight)
+  {
+    unhighlightSelected();
+  }
 
+  mySelection->ClearAndSelect (theOwner);
+  if (myAutoHilight)
+  {
+    Handle(Prs3d_Drawer) aCustomStyle;
+    if (!HighlightStyle (theOwner, aCustomStyle) ||
+      (!aCustomStyle.IsNull() && aCustomStyle != anObjSelStyle))
+    {
+      highlightSelected (theOwner);
+    }
+  }
 
-#ifdef DEB
-  cout<<"Not Yet Implemented"<<endl;
-#endif
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
 //=======================================================================
-//function : UpdateSelection
-//purpose  : 
+//function : AddOrRemoveSelected
+//purpose  : Adds or removes current object from AIS selection and highlights/unhighlights it.
+//           Since this method makes sence only for neutral point selection of a whole object,
+//           if 0 selection of the object is empty this method simply does nothing.
 //=======================================================================
-
-void AIS_InteractiveContext::UpdateSelected(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
+                                                  const Standard_Boolean theToUpdateViewer)
 {
-  if(!HasOpenedContext()) {
-    HilightSelected();
-#ifndef BUG
+  if (theObject.IsNull()
+  || !myObjects.IsBound (theObject))
+  {
     return;
-#endif
   }
-  myLocalContexts(myCurLocalIndex)->UpdateSelected(updateviewer);
-}
 
-//=======================================================================
-//function : SetCurrentObject
-//purpose  : 
-//=======================================================================
-void AIS_InteractiveContext::SetSelected(const Handle(AIS_InteractiveObject)& anIObj,const Standard_Boolean updateviewer)
-{
-  if(!HasOpenedContext()) 
-    SetCurrentObject(anIObj,updateviewer);
-  else
-    myLocalContexts(myCurLocalIndex)->SetSelected(anIObj,updateviewer);
+  const Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
+  if (!anOwner.IsNull()
+    && anOwner->HasSelectable())
+  {
+    AddOrRemoveSelected (anOwner, theToUpdateViewer);
+  }
 }
 
 //=======================================================================
 //function : AddOrRemoveSelected
-//purpose  : 
+//purpose  : Allows to highlight or unhighlight the owner given depending on
+//           its selection status
 //=======================================================================
-
-void AIS_InteractiveContext::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)& anIObj,
-                                                const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                  const Standard_Boolean theToUpdateViewer)
 {
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
-  if(!HasOpenedContext())
-    AddOrRemoveCurrentObject(anIObj,updateviewer);
-  else
-    myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(anIObj,updateviewer);
-  
-}
-//=======================================================================
-//function : AddOrRemoveSelected
-//purpose  : 
-//=======================================================================
-
-void AIS_InteractiveContext::AddOrRemoveSelected(const TopoDS_Shape& aShap,
-                                           const Standard_Boolean updateviewer)
-{ 
-  if(!HasOpenedContext()) {
-#ifdef DEB
-    cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
-#endif
+  if (theOwner.IsNull() || !theOwner->HasSelectable())
     return;
-  }
-  
-  myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(aShap,updateviewer);
-  if(updateviewer) UpdateCurrentViewer();
-  
-}
-//=======================================================================
-//function : AddOrRemoveSelected
-//purpose  : 
-//=======================================================================
 
-void AIS_InteractiveContext::AddOrRemoveSelected(const Handle(SelectMgr_EntityOwner)& Own,
-                                                const Standard_Boolean updateviewer)
-{ 
-  if(!HasOpenedContext()) {
-#ifdef DEB
-    cout<<" Attempt to remove a selected ownr with no opened local context"<<endl;
-#endif
+  if (!myFilters->IsOk(theOwner) && !theOwner->IsSelected())
     return;
-  }
-  
-  myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(Own,Standard_False);
-  if(updateviewer) UpdateCurrentViewer();
-  
-}
 
+  mySelection->Select (theOwner);
 
+  if (myAutoHilight)
+  {
+    const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+    Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (anObj);
+    if (!aStatusPtr)
+    {
+      return;
+    }
 
-//=======================================================================
-//function : IsSelected
-//purpose  : 
-//=======================================================================
+    if (theOwner->IsSelected())
+    {
+      highlightSelected (theOwner);
+    }
+    else
+    {
+      AIS_NListOfEntityOwner anOwners;
+      anOwners.Append (theOwner);
+      unhighlightOwners (anOwners);
 
-Standard_Boolean AIS_InteractiveContext::
-IsSelected(const Handle(AIS_InteractiveObject)& anIObj) const 
-{
-  if (anIObj.IsNull()) return Standard_False;
-  
-  if(!HasOpenedContext()) 
-    return (anIObj->State()==1);
-  else 
-    return myLocalContexts(myCurLocalIndex)->IsSelected(anIObj);
+      (*aStatusPtr)->SetHilightStyle (Handle(Prs3d_Drawer)());
+    }
+  }
+
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
+// =======================================================================
+// function : SetSelectedState
+// purpose  :
+// =======================================================================
+Standard_Boolean AIS_InteractiveContext::SetSelectedState (const Handle(SelectMgr_EntityOwner)& theEntity,
+                                                           const Standard_Boolean theIsSelected)
+{
+  if (theEntity.IsNull())
+  {
+    throw Standard_ProgramError ("Internal error: AIS_InteractiveContext::SetSelectedState() called with NO object");
+  }
 
-//=======================================================================
-//function : InitSelected
-//purpose  : 
-//=======================================================================
+  if (!theEntity->HasSelectable()
+    || mySelection->IsSelected (theEntity) == theIsSelected)
+  {
+    return false;
+  }
 
-void AIS_InteractiveContext::InitSelected()
-{
-  if(!HasOpenedContext())
-    AIS_Selection::Selection(myCurrentName.ToCString())->Init();
+  if (theEntity->IsAutoHilight())
+  {
+    AddOrRemoveSelected (theEntity, false);
+    return true;
+  }
+
+  if (theIsSelected)
+  {
+    const AIS_SelectStatus aSelStatus = mySelection->AddSelect (theEntity);
+    theEntity->SetSelected (true);
+    return aSelStatus == AIS_SS_Added;
+  }
   else
-    myLocalContexts(myCurLocalIndex)->InitSelected();
+  {
+    const AIS_SelectStatus aSelStatus = mySelection->Select (theEntity);
+    theEntity->SetSelected (false);
+    return aSelStatus == AIS_SS_Removed;
+  }
 }
 
 //=======================================================================
-//function : MoreSelected
-//purpose  : 
+//function : IsSelected
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::MoreSelected() const 
+Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
 {
-  if(!HasOpenedContext())
-    return AIS_Selection::Selection(myCurrentName.ToCString())->More();
-  return myLocalContexts(myCurLocalIndex)->MoreSelected();
-}
+  if (theObj.IsNull())
+  {
+    return Standard_False;
+  }
 
-//=======================================================================
-//function : NextSelected
-//purpose  : 
-//=======================================================================
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
+  if (aStatus == NULL)
+  {
+    return Standard_False;
+  }
 
-void AIS_InteractiveContext::NextSelected()
-{
-  if(!HasOpenedContext())
-    AIS_Selection::Selection(myCurrentName.ToCString())->Next();
-  else
-    myLocalContexts(myCurLocalIndex)->NextSelected();
+  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 : HasSelectedShape
-//purpose  : 
+//function : FirstSelectedObject
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const 
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject() const
 {
-  if(!HasOpenedContext()) {
-#ifdef IMP280200
-    Handle(AIS_Shape) shape = 
-       Handle(AIS_Shape)::DownCast(SelectedInteractive());
-    if( !shape.IsNull() ) return Standard_True;
-#endif
-    return Standard_False;
-  }
-  return myLocalContexts(myCurLocalIndex)->HasShape();
-  
-  
+  return !mySelection->Objects().IsEmpty()
+        ? Handle(AIS_InteractiveObject)::DownCast (mySelection->Objects().First()->Selectable())
+        : Handle(AIS_InteractiveObject)();
 }
 
 //=======================================================================
-//function : SelectedShape
-//purpose  : 
+//function : HasSelectedShape
+//purpose  :
 //=======================================================================
-
-TopoDS_Shape AIS_InteractiveContext::SelectedShape() const 
+Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
 {
+  if (!mySelection->More())
+    return Standard_False;
 
-  if(!HasOpenedContext()){
-    TopoDS_Shape sh;
-#ifdef IMP280200
-    Handle(AIS_Shape) shape = 
-       Handle(AIS_Shape)::DownCast(SelectedInteractive());
-    if( !shape.IsNull() ) sh = shape->Shape();
-#endif
-    return sh;
-  } else
-    return myLocalContexts(myCurLocalIndex)->SelectedShape();
+  const Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+  return !anOwner.IsNull() && anOwner->HasShape();
 }
 
 //=======================================================================
-//function : Interactive
-//purpose  : 
+//function : SelectedShape
+//purpose  :
 //=======================================================================
-
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::Interactive() const 
+TopoDS_Shape AIS_InteractiveContext::SelectedShape() const
 {
-  return SelectedInteractive();
-}
+  if (!mySelection->More())
+    return TopoDS_Shape();
 
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::SelectedInteractive() const 
-{
-  if(!HasOpenedContext()){
-    Handle(Standard_Transient) TR  =AIS_Selection::Selection(myCurrentName.ToCString())->Value();
-    Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&TR);
-    return IO;}
-  
-  return  myLocalContexts(myCurLocalIndex)->SelectedInteractive();
-  
-}
-//=======================================================================
-//function : Interactive
-//purpose  : 
-//=======================================================================
+  const Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+  if (anOwner.IsNull() || !anOwner->HasSelectable())
+    return TopoDS_Shape();
 
-Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const 
-{
-  if(!HasOpenedContext()){
-    Handle(SelectMgr_EntityOwner) Ownr;
-    return Ownr;
-  }
-  return  myLocalContexts(myCurLocalIndex)->SelectedOwner();
-  
+  return anOwner->Shape().Located (anOwner->Location() * anOwner->Shape().Location());
 }
 
 //=======================================================================
 //function : EntityOwners
 //purpose  : 
 //=======================================================================
-void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners,
+void AIS_InteractiveContext::EntityOwners(Handle(SelectMgr_IndexedMapOfOwner)& theOwners,
                                          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);
+  }
 
-  TColStd_ListIteratorOfListOfInteger anItr( aModes );
-  for (; anItr.More(); anItr.Next() )
+  if (theOwners.IsNull())
   {
-    int aMode = anItr.Value();
-    if ( !theIObj->HasSelection( aMode ) )
-      continue;
+    theOwners = new SelectMgr_IndexedMapOfOwner();
+  }
 
-    Handle(SelectMgr_Selection) aSel = theIObj->Selection( aMode );
+  for (TColStd_ListIteratorOfListOfInteger anItr (aModes); anItr.More(); anItr.Next())
+  {
+    const int aMode = anItr.Value();
+    const Handle(SelectMgr_Selection)& aSel = theIObj->Selection (aMode);
+    if (aSel.IsNull())
+    {
+      continue;
+    }
 
-    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();
-      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);
+        }
+      }
     }
   }
 }
 
-Standard_Integer AIS_InteractiveContext::NbSelected() 
-{
-  Standard_Integer i(0);
-  for(InitSelected();MoreSelected();NextSelected())
-    {i++;}
-  return i;
-}
-
-//=======================================================================
-//function : HasApplicative
-//purpose  : 
-//=======================================================================
-
-  Standard_Boolean AIS_InteractiveContext::HasApplicative() const 
-{
-  return Interactive()->HasOwner();
-}
-
-//=======================================================================
-//function : Applicative
-//purpose  : 
-//=======================================================================
-
-Handle(Standard_Transient) AIS_InteractiveContext::Applicative() const 
-{
-  return Interactive()->GetOwner();
-}
-
-
-
-//==================================================
-// Function: 
-// Purpose :
-//==================================================
-Standard_Boolean AIS_InteractiveContext::HasDetected() const 
-{
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->HasDetected();
-  else
-    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 myLastPicked;
-}
-
-
-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();
-  Handle(SelectMgr_EntityOwner) Ownr;
-  return Ownr;
+  Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (myLastPicked);
+  return anOwner->Shape();
 }
 
-
 //=======================================================================
 //function : HilightNextDetected
-//purpose  : 
+//purpose  :
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::HilightNextDetected(const Handle(V3d_View)& V)
+Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
+                                                              const Standard_Boolean  theToRedrawImmediate)
 {
-  if(!HasOpenedContext())
+  myMainPM->ClearImmediateDraw();
+  if (myDetectedSeq.IsEmpty())
+  {
     return 0;
-  return myLocalContexts(myCurLocalIndex)->HilightNextDetected(V);
-    
-}
+  }
 
-//=======================================================================
-//function : HilightNextDetected
-//purpose  : 
-//=======================================================================
-Standard_Integer AIS_InteractiveContext::HilightPreviousDetected(const Handle(V3d_View)& V)
-{
-  if(!HasOpenedContext())
+  if (++myCurHighlighted > myDetectedSeq.Upper())
+  {
+    myCurHighlighted = myDetectedSeq.Lower();
+  }
+  const Handle(SelectMgr_EntityOwner)& anOwner = myMainSel->Picked (myDetectedSeq (myCurHighlighted));
+  if (anOwner.IsNull())
+  {
     return 0;
-  return myLocalContexts(myCurLocalIndex)->HilightPreviousDetected(V);
-    
-}
+  }
 
-#ifdef IMP150501
-void AIS_InteractiveContext::Drag(
-                                const Handle(V3d_View)& aView,
-                                const Handle(AIS_InteractiveObject)& anObject,
-                                const Handle(Geom_Transformation)& aTrsf,
-                                const Standard_Boolean postConcatenate,
-                                const Standard_Boolean update,
-                                const Standard_Boolean zBuffer) {
-
-  if( anObject.IsNull() || aView.IsNull() ) return;
-
-  if( update ) {
-    anObject->SetTransformation(aTrsf,postConcatenate,Standard_True);
-    aView->Update();
-  } else if( Visual3d_TransientManager::BeginDraw(aView->View(),
-                                        zBuffer,Standard_False) ) {
-    Handle(Prs3d_Presentation) P = anObject->Presentation();
-    if( !P.IsNull() ) {
-      if( postConcatenate ) P->Multiply(aTrsf);
-      else                  P->Transform(aTrsf);
-      Visual3d_TransientManager::DrawStructure(P);
-    }
-    Visual3d_TransientManager::EndDraw(Standard_True);
+  highlightWithColor (anOwner, theView->Viewer());
+  myLastPicked = anOwner;
+
+  if (theToRedrawImmediate)
+  {
+    myMainPM->RedrawImmediate (theView->Viewer());
+    myMainVwr->RedrawImmediate();
   }
+
+  return myCurHighlighted;
 }
-#endif
 
-#ifdef IMP160701
 //=======================================================================
-//function : InitDetected
+//function : HilightPreviousDetected
 //purpose  :
 //=======================================================================
-void AIS_InteractiveContext::InitDetected()
+Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
+                                                                  const Standard_Boolean  theToRedrawImmediate)
 {
-  if(HasOpenedContext())
+  myMainPM->ClearImmediateDraw();
+  if (myDetectedSeq.IsEmpty())
   {
-    myLocalContexts(myCurLocalIndex)->InitDetected();
-    return;
+    return 0;
   }
 
-  if(myAISDetectedSeq.Length() != 0)
-    myAISCurDetected = 1;
+  if (--myCurHighlighted < myDetectedSeq.Lower())
+  {
+    myCurHighlighted = myDetectedSeq.Upper();
+  }
+  const Handle(SelectMgr_EntityOwner)& anOwner = myMainSel->Picked (myDetectedSeq (myCurHighlighted));
+  if (anOwner.IsNull())
+  {
+    return 0;
+  }
 
-}
+  highlightWithColor (anOwner, theView->Viewer());
+  myLastPicked = anOwner;
 
-//=======================================================================
-//function : MoreDetected
-//purpose  :
-//=======================================================================
-Standard_Boolean AIS_InteractiveContext::MoreDetected() const
-{
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->MoreDetected();
+  if (theToRedrawImmediate)
+  {
+    myMainPM->RedrawImmediate (theView->Viewer());
+    myMainVwr->RedrawImmediate();
+  }
 
-  return (myAISCurDetected>0 &&myAISCurDetected <= myAISDetectedSeq.Length()) ?
-          Standard_True : Standard_False;
+  return myCurHighlighted;
 }
 
 //=======================================================================
-//function : NextDetected
+//function : DetectedCurrentOwner
 //purpose  :
 //=======================================================================
-void AIS_InteractiveContext::NextDetected()
+Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedCurrentOwner() const
 {
-  if(HasOpenedContext())
-  {
-    myLocalContexts(myCurLocalIndex)->NextDetected();
-    return;
-  }
-
-  myAISCurDetected++;
+  return MoreDetected()
+       ? myMainSel->Picked (myDetectedSeq (myCurDetected))
+       : Handle(SelectMgr_EntityOwner)();
 }
 
 //=======================================================================
 //function : DetectedCurrentShape
 //purpose  :
 //=======================================================================
-
 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
 {
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
-
-  static TopoDS_Shape bidsh;
-  if(myAISCurDetected > 0 &&
-     myAISCurDetected <= myAISDetectedSeq.Length())
-    return Handle(AIS_Shape)::DownCast(myAISDetectedSeq(myAISCurDetected))->Shape();
-  return bidsh;
+  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 : DetectedCurrentObject
 //purpose  :
 //=======================================================================
-
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const {
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->DetectedCurrentObject();
-
-  Handle(AIS_InteractiveObject) aBad;
-  if(myAISCurDetected > 0 &&
-     myAISCurDetected <= myAISDetectedSeq.Length())
-      return myAISDetectedSeq(myAISCurDetected);
-  else
-    return aBad;
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
+{
+  return MoreDetected()
+       ? Handle(AIS_InteractiveObject)::DownCast (myMainSel->Picked (myDetectedSeq (myCurDetected))->Selectable())
+       : Handle(AIS_InteractiveObject)();
 }
-#endif