0027893: Visualization - AIS_InteractiveContext::SetSelected does not work
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
old mode 100755 (executable)
new mode 100644 (file)
index c4674d5..efbfd12
-// File:       AIS_InteractiveContext_1.cxx
-// Created:    Wed Jan 29 10:29:35 1997
-// Author:     Robert COUBLANC
-//             <rob@robox.paris1.matra-dtv.fr>
+// Created on: 1997-01-29
+// Created by: Robert COUBLANC
+// Copyright (c) 1997-1999 Matra Datavision
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
-#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_LocalContext.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 <SelectBasics_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_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;
+  const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
 
-#define IMP191001      //GG Avoid to raise when switching with the 
-//                     SetAutomaticHilight() method.
-//                     Thanks to IFO of SAMTECH company for this improvment.
+  myMainPM->BeginImmediateDraw();
+  theOwner->HilightWithColor (myMainPM, getHiStyle (anObj), aHiMode);
+  myMainPM->EndImmediateDraw (theViewer.IsNull() ? myMainVwr : theViewer);
+}
 
-#define OCC138          //VTN Avoding infinit loop in AddOrRemoveCurrentObject method.
+//=======================================================================
+//function : highlightSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner)
+{
+  const Handle(AIS_InteractiveObject) anObj =
+    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  if (anObj.IsNull())
+    return;
+  const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
 
-#define OCC9657
+  if (!theOwner->IsAutoHilight())
+  {
+    SelectMgr_SequenceOfOwner aSeq;
+    for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+    {
+      if (aSelIter.Value()->IsSameSelectable (anObj))
+      {
+        aSeq.Append (aSelIter.Value());
+      }
+    }
+    anObj->HilightSelected (myMainPM, aSeq);
+  }
+  else
+  {
+    theOwner->HilightWithColor (myMainPM, getSelStyle (anObj), aHiMode);
+  }
+}
 
-#include <AIS_InteractiveContext.jxx>
-#include <SelectMgr_EntityOwner.hxx>
-#include <AIS_Selection.hxx>
-#include <AIS_StatusOfDetection.hxx>
-#include <AIS_StatusOfPick.hxx>
+//=======================================================================
+//function : highlightGlobal
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::highlightGlobal (const Handle(AIS_InteractiveObject)& theObj,
+                                              const Handle(Graphic3d_HighlightStyle)& theStyle,
+                                              const Standard_Integer theMode) const
+{
+  if (theObj.IsNull())
+    return;
+  const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
 
-#include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
-#include <AIS_LocalContext.hxx>
-#include <AIS_GlobalStatus.hxx>
-#include <AIS_Shape.hxx>
+  if (aGlobOwner.IsNull())
+  {
+    myMainPM->Color (theObj, theStyle, theMode);
+    return;
+  }
 
-#include <V3d_Light.hxx>
-#include <V3d_PositionalLight.hxx>
-#include <V3d_SpotLight.hxx>
-#include <V3d_DirectionalLight.hxx>
-#include <V3d_AmbientLight.hxx>
+  if (!aGlobOwner->IsAutoHilight())
+  {
+    SelectMgr_SequenceOfOwner aSeq;
+    for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+    {
+      if (aSelIter.Value()->IsSameSelectable (theObj))
+      {
+        aSeq.Append (aSelIter.Value());
+      }
+    }
+    theObj->HilightSelected (myMainPM, aSeq);
+  }
+  else
+  {
+    aGlobOwner->HilightWithColor (myMainPM, theStyle, theMode);
+  }
+}
 
-#include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <SelectMgr_Selection.hxx>
-#include <SelectBasics_SensitiveEntity.hxx>
+//=======================================================================
+//function : unhighlightSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
+{
+  NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    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);
+      if (theIsToHilightSubIntensity)
+      {
+        if (myObjects.IsBound (anInteractive) && myObjects (anInteractive)->IsSubIntensityOn())
+        {
+          highlightWithSubintensity (anOwner, aHiMode);
+        }
+      }
+    }
+    else
+    {
+      if (!anObjToClear.Contains (anInteractive))
+        anObjToClear.Add (anInteractive);
+    }
+    anOwner->State (0);
+    if (anOwner == anInteractive->GlobalSelOwner())
+    {
+      myObjects.ChangeFind (anInteractive)->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);
+    anObj->ClearSelected();
+  }
+}
 
-#ifdef IMP150501
-#include <Visual3d_TransientManager.hxx>
-#include <Prs3d_Presentation.hxx>
-#endif
+//=======================================================================
+//function : unhighlightGlobal
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightGlobal (const Handle(AIS_InteractiveObject)& theObj,
+                                                const Standard_Integer theMode) const
+{
+  if (theObj.IsNull())
+    return;
+  const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
 
-#ifdef OCC9657
-#include <AIS_MapOfInteractive.hxx>
-#include <AIS_MapIteratorOfMapOfInteractive.hxx>
-#endif
+  if (aGlobOwner.IsNull())
+  {
+    myMainPM->Unhighlight (theObj, theMode);
+    return;
+  }
+
+  if (aGlobOwner->IsAutoHilight())
+  {
+    aGlobOwner->Unhilight (myMainPM, theMode);
+  }
+  else
+  {
+    myMainPM->Unhighlight (theObj, theMode);
+    theObj->ClearSelected();
+  }
+}
 
-//unused
-/*#ifdef DEB
-static void InfoOnLight(const Handle(V3d_View) aView)
+//=======================================================================
+//function : turnOnSubintensity
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::turnOnSubintensity (const Handle(AIS_InteractiveObject)& theObject,
+                                                 const Standard_Integer theDispMode,
+                                                 const Standard_Boolean theIsDisplayedOnly) const
 {
-  Standard_Real V1=0.,V2=0.,V3=0.;
-  Standard_Integer IndexOfLit(0);
-  
-  static Standard_Boolean FirstTime (Standard_True);
-  if(FirstTime)
+  // the only differ with selection highlight is color, so
+  // sync transparency values
+  mySubintStyle->SetTransparency (mySelStyle->Transparency());
+
+  if (theObject.IsNull())
+  {
+    for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjsIter (myObjects); anObjsIter.More(); anObjsIter.Next())
     {
-      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;
-         }
-       }
+      const Handle(AIS_GlobalStatus)& aStatus = anObjsIter.Value();
+      if (aStatus->GraphicStatus() != AIS_DS_Displayed && theIsDisplayedOnly)
+        continue;
+
+      aStatus->SubIntensityOn();
+
+      if (theDispMode == -1)
+      {
+        myMainPM->Color (anObjsIter.Key(), mySubintStyle, aStatus->DisplayMode());
       }
+      else
+        myMainPM->Color (anObjsIter.Key(), mySubintStyle, theDispMode);
     }
+  }
+  else
+  {
+    Handle(AIS_GlobalStatus) aStatus;
+    if (!myObjects.Find (theObject, aStatus))
+      return;
+
+    if (aStatus->GraphicStatus() != AIS_DS_Displayed && theIsDisplayedOnly)
+        return;
+
+    aStatus->SubIntensityOn();
+
+    if (theDispMode == -1)
+    {
+      myMainPM->Color (theObject, mySubintStyle, aStatus->DisplayMode());
+    }
+    else
+      myMainPM->Color (theObject, mySubintStyle, theDispMode);
+  }
 }
-#endif
-*/
+
 //=======================================================================
-//function : MoveTo
-//purpose  : 
+//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
+  mySubintStyle->SetTransparency (mySelStyle->Transparency());
+
+  myMainPM->Color (theObject, mySubintStyle, 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
+  mySubintStyle->SetTransparency (mySelStyle->Transparency());
+
+  theOwner->HilightWithColor (myMainPM, mySubintStyle, theMode);
+}
 
-AIS_StatusOfDetection AIS_InteractiveContext::MoveTo(const Standard_Integer XPix, 
-                                  const Standard_Integer YPix, 
-                                  const Handle(V3d_View)& aView)
+//=======================================================================
+//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(HasOpenedContext()&& aView->Viewer()!=myCollectorVwr){
+  if (HasOpenedContext())
+  {
     myWasLastMain = Standard_True;
-    return myLocalContexts(myCurLocalIndex)->MoveTo(XPix,YPix,aView);
+    return myLocalContexts (myCurLocalIndex)->MoveTo (theXPix, theYPix, theView, theToRedrawOnUpdate);
   }
 
-#ifdef IMP160701
-    //Nullify class members storing information about detected AIS objects.
   myAISCurDetected = 0;
   myAISDetectedSeq.Clear();
-#endif
 
-  // 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 if (aView->Viewer()== myCollectorVwr){
-    pmgr = myCollectorPM;
-    selector=myCollectorSel;
-    myLastPicked = myLastinColl;
-    ismain = Standard_False;
-    myWasLastMain = Standard_False;
-  }
-  else 
+  if (theView->Viewer() != myMainVwr)
+  {
     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))
-      {
-        Handle(AIS_InteractiveObject) anObj = 
-               Handle(AIS_InteractiveObject)::DownCast(anEntityOwner->Selectable());
-        if(!Handle(AIS_Shape)::DownCast(anObj).IsNull())
-          myAISDetectedSeq.Append(anObj);
-      }
   }
-#endif
 
-  selector->Init();
-  if ( selector->More() )
+  // preliminaires
+  myLastPicked  = myLastinMain;
+  myWasLastMain = Standard_True;
+  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;
+  for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
   {
-    if ( HasOpenedContext() )
-      if ( !myFilters->IsOk( selector->OnePicked() ) ) 
-       return AIS_SOD_AllBad;
-      else
-        if ( !myLocalContexts( myCurLocalIndex )->Filter()->IsOk( selector->OnePicked() ) )
-          return AIS_SOD_AllBad;
-    
-    // Does nothing if previously detected object is equal to the current one
-    if ( selector->OnePicked()->Selectable() == myLastPicked )
-      return AIS_SOD_OnlyOneDetected;
+    Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
+    if (anOwner.IsNull()
+     || !myFilters->IsOk (anOwner))
+    {
+      continue;
+    }
+
+    if (aNewDetected < 1)
+    {
+      aNewDetected = aDetIter;
+    }
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    if (!anObj.IsNull())
+    {
+      myAISDetectedSeq.Append (anObj);
+    }
+  }
+
+  if (aNewDetected >= 1)
+  {
+    // 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())
+    {
+      return myLastPicked->IsSelected()
+           ? AIS_SOD_Selected
+           : 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 )
+      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())
       {
-       pmgr->Unhighlight( myLastPicked, HiMod );
-        UpdVwr = Standard_True;
+        myLastPicked->Unhilight (myMainPM, aHiMod);
+        toUpdateViewer = Standard_True;
       }
-      else if ( myToHilightSelected )
+      else if (myToHilightSelected)
       {
-        pmgr->Color( myLastPicked, mySelectionColor, HiMod );
-        UpdVwr = Standard_True;
+        highlightSelected (aNewPickedOwner);
+        toUpdateViewer = Standard_True;
       }
     }
-    
-    // Initialize myLastPicked field with currently detected object
-    Handle(SelectMgr_SelectableObject) SO = selector->OnePicked()->Selectable();
-    myLastPicked = *((Handle(AIS_InteractiveObject)*)&SO);
-    
-    if ( ismain )
-      myLastinMain = myLastPicked;
-    else 
-      myLastinColl = 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
+
+    // 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())
     {
-      Standard_Integer HiMod =  
-        myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
-      pmgr->Color( myLastPicked, myHilightColor, HiMod );
-      UpdVwr = Standard_True;
+      if (!myLastPicked->IsSelected() || myToHilightSelected)
+      {
+        highlightWithColor (myLastPicked, theView->Viewer());
+        toUpdateViewer = Standard_True;
+      }
+
+      aStatus = myLastPicked->IsSelected()
+              ? AIS_SOD_Selected
+              : AIS_SOD_OnlyOneDetected;
     }
-    
-    if ( myLastPicked->State()==1 )
-      TheStat = AIS_SOD_Selected;
   }
-  else 
+  else
   {
-    // 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() )
+    // 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())
     {
-      Standard_Integer HiMod =  
-        myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
-      if ( myLastPicked->State() != 1 )
+      myMainPM->ClearImmediateDraw();
+      const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
+      Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
+      if (!myLastPicked->IsSelected())
       {
-       pmgr->Unhighlight( myLastPicked, HiMod );
-        UpdVwr = Standard_True;
+        if (myLastPicked->IsAutoHilight())
+        {
+          myLastPicked->Unhilight (myMainPM, aHiMod);
+        }
+        toUpdateViewer = Standard_True;
       }
-      else if ( myToHilightSelected )
+      else if (myToHilightSelected)
       {
-        pmgr->Color( myLastPicked, mySelectionColor, HiMod );
-        UpdVwr = Standard_True;
+        highlightSelected (myLastPicked);
+        toUpdateViewer = Standard_True;
       }
     }
-    
-    if ( ismain )
-      myLastinMain.Nullify();
-    else
-      myLastinColl.Nullify();
+
+    myLastinMain.Nullify();
+    myLastPicked.Nullify();
   }
-  
-  if(UpdVwr) aView->Viewer()->Update();
-  myLastPicked.Nullify();
-  
-  mylastmoveview = aView;
-  return TheStat;
+
+  if (toUpdateViewer
+   && theToRedrawOnUpdate)
+  {
+    theView->Viewer()->Update();
+  }
+
+  mylastmoveview = theView;
+  return aStatus;
 }
 
 //=======================================================================
-//function : Select
+//function : AddSelect
 //purpose  : 
 //=======================================================================
+AIS_StatusOfPick AIS_InteractiveContext::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
+{
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->AddSelect (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)
 {
   // all objects detected by the selector are taken, previous current objects are emptied,
   // new objects are put...
 
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->Select(XPMin,YPMin,XPMax,YPMax,aView,updateviewer);
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->Select (theXPMin, theYPMin,
+                                                     theXPMax, theYPMax,
+                                                     theView, toUpdateViewer);
+  }
 
-  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;}
-  
-  else if (aView->Viewer()==myCollectorVwr){
-    selector= myCollectorSel;
-    myWasLastMain = Standard_False;}
-  
-  
-  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
-    }
+  ClearSelected (Standard_False);
+
+  Handle(StdSelect_ViewerSelector3d) aSelector;
+
+  if (theView->Viewer() == myMainVwr)
+  {
+    aSelector = myMainSel;
+    myWasLastMain = Standard_True;
   }
 
-#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;
+  aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= aSelector->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner)& aCurOwner = aSelector->Picked (aPickIter);
+    if (aCurOwner.IsNull() || !aCurOwner->HasSelectable() || !myFilters->IsOk (aCurOwner))
+      continue;
+
+    mySelection->Select (aCurOwner);
+    aCurOwner->State (1);
+  }
+
+  HilightSelected (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbSelected();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
   
 }
 
@@ -360,69 +517,46 @@ 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)
 {
   // all objects detected by the selector are taken, previous current objects are emptied,
   // new objects are put...
 
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->Select(aPolyline,aView,updateviewer);
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->Select (thePolyline, theView, toUpdateViewer);
+  }
 
-  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;}
-  
-  else if (aView->Viewer()==myCollectorVwr){
-    selector= myCollectorSel;
-    myWasLastMain = Standard_False;}
-  
-  
-  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
-    }
+  ClearSelected (Standard_False);
+
+  Handle(StdSelect_ViewerSelector3d) aSelector;
+
+  if (theView->Viewer() == myMainVwr)
+  {
+    aSelector = myMainSel;
+    myWasLastMain = Standard_True;
   }
-#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;
+
+  aSelector->Pick (thePolyline, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= aSelector->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked (aPickIter);
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+      continue;
+
+    mySelection->Select (anOwner);
+    anOwner->State (1);
+  }
+
+  HilightSelected (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbSelected();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
   
 }
 
@@ -430,618 +564,625 @@ 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 (HasOpenedContext())
+  {
     if(myWasLastMain)
-      return myLocalContexts(myCurLocalIndex)->Select(updateviewer);
+    {
+      return myLocalContexts(myCurLocalIndex)->Select (toUpdateViewer);
+    }
     else
-      // picking was done in the collector, special processing is required...
-      {
-       myLocalContexts(myCurLocalIndex)->SetSelected(myLastPicked,updateviewer);
-       return AIS_SOP_OneSelected;
-      }
+    {
+      myLocalContexts(myCurLocalIndex)->SetSelected (Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable()), toUpdateViewer);
+      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 if (!myWasLastMain && !myLastinColl.IsNull()){
-    if(myLastinColl->State()!=1){
-      SetCurrentObject(myLastinColl,Standard_False);
-      if(updateviewer)
-       UpdateCollector();}
-  }
-  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);
+
+  myMainPM->ClearImmediateDraw();
+  if (myWasLastMain && !myLastinMain.IsNull())
+  {
+    if(!myLastinMain->IsSelected())
+    {
+      SetSelected (myLastinMain, Standard_False);
+      if(toUpdateViewer)
+      {
+        UpdateCurrentViewer();
       }
     }
-    
-    AIS_Selection::Select();
-    if(updateviewer){
-      if(myWasLastMain)
-       UpdateCurrentViewer();
-      else
-       UpdateCollector();
+  }
+  else
+  {
+    unhighlightSelected (Standard_True);
+
+    mySelection->Clear();
+    if (toUpdateViewer && myWasLastMain)
+    {
+        UpdateCurrentViewer();
     }
   }
-  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_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
 {
-  if(HasOpenedContext()){
+  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;
+    {
+      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;
     }
   }
-  if(myWasLastMain && !myLastinMain.IsNull())
-    AddOrRemoveCurrentObject(myLastinMain,updateviewer);
-  else if (!myWasLastMain && !myLastinColl.IsNull())
-    AddOrRemoveCurrentObject(myLastinColl,updateviewer);
-  
-  
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-}
 
+  myMainPM->ClearImmediateDraw();
+  if (myWasLastMain && !myLastinMain.IsNull())
+  {
+    AddOrRemoveSelected (myLastinMain, toUpdateViewer);
+  }
+
+  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_Integer theXPMin,
+                                                      const Standard_Integer theYPMin,
+                                                      const Standard_Integer theXPMax,
+                                                      const Standard_Integer theYPMax,
+                                                      const Handle(V3d_View)& theView,
+                                                      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 if (aView->Viewer()==myCollectorVwr){
-    selector= myCollectorSel;
-    myWasLastMain = Standard_False;}
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->ShiftSelect (theXPMin, theYPMin, theXPMax, theYPMax,
+                                                          theView, toUpdateViewer);
+  }
+
+  UnhilightSelected (Standard_False);
+
+  Handle(StdSelect_ViewerSelector3d) aSelector;
+  if (theView->Viewer() == myMainVwr)
+  {
+    aSelector = 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);
-    }
   }
-  
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS =NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-  
+
+  aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= aSelector->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked (aPickIter);
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+      continue;
+
+    AIS_SelectStatus aSelStatus = mySelection->Select (anOwner);
+    Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
+    anOwner->State (aState);
+  }
+
+  HilightSelected (toUpdateViewer);
+
+  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 TColgp_Array1OfPnt2d& aPolyline,
-                                                      const Handle(V3d_View)& aView,
-                                                      const Standard_Boolean updateviewer )
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean toUpdateViewer)
 {
-    if( HasOpenedContext() )
-        return myLocalContexts( myCurLocalIndex )->ShiftSelect( aPolyline, aView, updateviewer );
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->ShiftSelect (thePolyline, theView, toUpdateViewer);
+  }
 
-    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 if ( aView->Viewer() == myCollectorVwr ) {
-        selector= myCollectorSel;
-        myWasLastMain = Standard_False;
-    }
-    else
-        return AIS_SOP_NothingSelected;
+  UnhilightSelected (Standard_False);
 
-    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;
+  Handle(StdSelect_ViewerSelector3d) aSelector;
+
+  if (theView->Viewer() == myMainVwr)
+  {
+    aSelector= myMainSel;
+    myWasLastMain = Standard_True;
+  }
+  else
+  {
+    return AIS_SOP_NothingSelected;
+  }
+
+  aSelector->Pick (thePolyline, theView);
+  for (Standard_Integer aPickIter = 1; aPickIter <= aSelector->NbPicked(); ++aPickIter)
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked (aPickIter);
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
+      continue;
+
+    AIS_SelectStatus aSelStatus = mySelection->Select (anOwner);
+    Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
+    anOwner->State (aState);
+  }
+
+  HilightSelected (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbSelected();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
 }
 
 //=======================================================================
 //function : SetCurrentObject
-//purpose  : 
+//purpose  : OBSOLETE, please use SetSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
-void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
-                                             const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetCurrentObject (const Handle(AIS_InteractiveObject)& theObject,
+                                               const Standard_Boolean theToUpdateViewer)
 {
-  // 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
-      }
-    }
+  if (HasOpenedContext())
     return;
-  }
 
-
-  // 
-  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();
-  }
-  else{
-#ifdef DEB
-    cout<<"Nothing Done : Opened Local Context"<<endl;
-#endif
-  }
+  SetSelected (theObject, theToUpdateViewer);
 }
 
 //=======================================================================
 //function : AddOrRemoveCurrentObject
-//purpose  : 
+//purpose  : OBSOLETE, please use AddOrRemoveSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
-void AIS_InteractiveContext::
-AddOrRemoveCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
-                        const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::AddOrRemoveCurrentObject (const Handle(AIS_InteractiveObject)& theObj,
+                                                       const Standard_Boolean theIsToUpdateViewer)
 {
-  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());
+  if (HasOpenedContext())
+    return;
 
-    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
-  }
+  AddOrRemoveSelected (theObj, theIsToUpdateViewer);
 }
 //=======================================================================
 //function : UpdateCurrent
-//purpose  : 
+//purpose  : OBSOLETE, please use UpdateSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
 void AIS_InteractiveContext::UpdateCurrent()
 {
-  HilightCurrents();
+  UpdateSelected();
 }
 
 //=======================================================================
 //function : IsCurrent
-//purpose  : 
+//purpose  : OBSOLETE, please use IsSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::IsCurrent(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Boolean AIS_InteractiveContext::IsCurrent (const Handle(AIS_InteractiveObject)& theObject) const
 {
-
-  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;
-
+  return IsSelected (theObject);
 }
 
 //=======================================================================
 //function : InitCurrent
-//purpose  : 
+//purpose  : OBSOLETE, please use InitSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
 void AIS_InteractiveContext::InitCurrent()
 {
-  AIS_Selection::Selection(myCurrentName.ToCString())->Init();
+  if (HasOpenedContext())
+    return;
+
+  InitSelected();
 }
 
 //=======================================================================
 //function : MoreCurrent
-//purpose  : 
+//purpose  : OBSOLETE, please use MoreSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
 Standard_Boolean AIS_InteractiveContext::MoreCurrent() const 
 {
-  return AIS_Selection::Selection(myCurrentName.ToCString())->More();
-
+  return !HasOpenedContext() && MoreSelected();
 }
 
 //=======================================================================
 //function : NextCurrent
-//purpose  : 
+//purpose  : OBSOLETE, please use NextSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
 void AIS_InteractiveContext::NextCurrent()
 {
-  AIS_Selection::Selection(myCurrentName.ToCString())->Next();
+  if (HasOpenedContext())
+    return;
+
+  NextSelected();
 }
 
 //=======================================================================
 //function : Current
-//purpose  : 
+//purpose  : OBSOLETE, please use SelectedInteractive() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-
 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;
+  return HasOpenedContext() ? NULL : SelectedInteractive();
 }
 
 //=======================================================================
-//function : FirstCurrentObject
-//purpose  : 
+//function : NbCurrents
+//purpose  : OBSOLETE, please use NbSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstCurrentObject()  
+Standard_Integer AIS_InteractiveContext::NbCurrents()
 {
-  Handle(AIS_InteractiveObject) IO ;
-  InitCurrent();
-  if(MoreCurrent()){
-    return Current();
-  }
-  return IO;
+  return HasOpenedContext() ? -1 : NbSelected();
 }
 
 //=======================================================================
-//function : NbCurrents
-//purpose  : 
+//function : HilightCurrents
+//purpose  : OBSOLETE, please use HilightSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::NbCurrents()
+void AIS_InteractiveContext::HilightCurrents (const Standard_Boolean theToUpdateViewer)
 {
-  Standard_Integer i(0);
-  for(InitCurrent();MoreCurrent();NextCurrent())
-    {i++;}
-  return i;
+  if (HasOpenedContext())
+    return;
+
+  HilightSelected (theToUpdateViewer);
 }
 
 //=======================================================================
-//function : HilightCurrents
-//purpose  : 
+//function : UnhilightCurrents
+//purpose  : OBSOLETE, please use UnhilightSelected() instead
+//TODO     : Remove in process of local context deletion
 //=======================================================================
-void AIS_InteractiveContext::HilightCurrents(const Standard_Boolean updateviewer)
-{
-  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) 
-    UpdateCurrentViewer();
+void AIS_InteractiveContext::UnhilightCurrents (const Standard_Boolean theToUpdateViewer)
+{
+  if (HasOpenedContext())
+    return;
+
+  UnhilightSelected (theToUpdateViewer);
 }
 
 //=======================================================================
-//function : UnhilightCurrents
-//purpose  : 
+//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);
+}
 
-void AIS_InteractiveContext::UnhilightCurrents(const Standard_Boolean updateviewer)
+
+//=======================================================================
+//function : HilightSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::HilightSelected (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 (HasOpenedContext())
+  {
+    return myLocalContexts (myCurLocalIndex)->HilightPicked (theToUpdateViewer);
   }
-  if(updateviewer) 
+
+  // In case of selection without using local context
+  myMainPM->ClearImmediateDraw();
+  AIS_MapOfObjSelectedOwners anObjOwnerMap;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    const Handle(Graphic3d_HighlightStyle)& anObjSelStyle = getSelStyle (anObj);
+    if (anOwner == anObj->GlobalSelOwner())
+    {
+      Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObj);
+      aState->SetHilightStatus (Standard_True);
+      aState->SetHilightStyle (anObjSelStyle);
+    }
+    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, anObjSelStyle, 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 : ClearCurrents
-//purpose  : 
+//function : UnhilightSelected
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnhilightSelected (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);
+  if (HasOpenedContext())
+  {
+    return myLocalContexts (myCurLocalIndex)->UnhilightPicked (theToUpdateViewer);
+  }
+
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    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);
   }
-  AIS_Selection::Select();
-  if(updateviewer)
+
+  if (theToUpdateViewer)
     UpdateCurrentViewer();
 }
 
 
 //=======================================================================
-//function : HilightSelected
-//purpose  : 
+//function : ClearSelected
+//purpose  :
 //=======================================================================
+void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateViewer)
+{
+  if (HasOpenedContext())
+    return myLocalContexts (myCurLocalIndex)->ClearSelected (theToUpdateViewer);
 
-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();
+  if (NbSelected() == 0)
+    return;
+
+  unhighlightSelected();
+
+  mySelection->Clear();
+  myMainPM->ClearImmediateDraw();
+
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
+}
+
+//=======================================================================
+//function : UpdateSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::UpdateSelected (const Standard_Boolean theToUpdateViewer)
+{
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->UpdateSelected (theToUpdateViewer);
+  }
+
+  HilightSelected (theToUpdateViewer);
+}
+
+//=======================================================================
+//function : SetSelected
+//purpose  : Sets the whole object as selected and highlights it with selection color
+//=======================================================================
+void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& theObject,
+                                          const Standard_Boolean theToUpdateViewer)
+{
+  if(HasOpenedContext())
+  {
+    return myLocalContexts (myCurLocalIndex)->SetSelected (theObject, theToUpdateViewer);
+  }
+
+  if (theObject.IsNull())
+    return;
+  if(!myObjects.IsBound (theObject))
+    Display (theObject, Standard_False);
+  if (!theObject->HasSelection (theObject->GlobalSelectionMode()))
+    return;
+
+  const Handle(Graphic3d_HighlightStyle)& anObjSelStyle =
+    getSelStyle (theObject);
+
+  if (NbSelected() == 1 && myObjects (theObject)->IsHilighted())
+  {
+    Handle(Graphic3d_HighlightStyle) aCustomStyle;
+    if (HighlightStyle (theObject, aCustomStyle))
+    {
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+      {
+        HilightWithColor (theObject, anObjSelStyle, theToUpdateViewer);
+      }
+    }
+    return;
+  }
+
+  for (mySelection->Init(); mySelection->More(); mySelection->Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner = mySelection->Value();
+    if (!myFilters->IsOk (anOwner))
+    {
+      continue;
     }
-  }
-  else
+
+    Handle(AIS_InteractiveObject) aSelectable =
+      Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    Unhilight (aSelectable, Standard_False);
+    anOwner->State (0);
+    if (anOwner == aSelectable->GlobalSelOwner())
     {
-      myLocalContexts(myCurLocalIndex)->HilightPicked(updateviewer);
+      myObjects.ChangeFind (aSelectable)->SetHilightStatus (Standard_False);
     }
-  if(updateviewer)
-    UpdateCurrentViewer();
-}
+  }
 
-//=======================================================================
-//function : UnhilightSelected
-//purpose  : 
-//=======================================================================
+  // added to avoid untimely viewer update...
+  Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
+  if (anOwner.IsNull())
+    return;
+  mySelection->ClearAndSelect (anOwner);
 
-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(Graphic3d_HighlightStyle) aCustomStyle;
+  if (HighlightStyle (theObject, aCustomStyle))
+  {
+    if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+    {
+      HilightWithColor (theObject, anObjSelStyle, Standard_False);
     }
-    if(updateviewer)
-      UpdateCurrentViewer();
   }
   else
-    {
-      myLocalContexts(myCurLocalIndex)->UnhilightPicked(updateviewer);
-    }
-}
+  {
+    HilightWithColor (theObject, anObjSelStyle, Standard_False);
+  }
+  anOwner->State (1);
 
+  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;
 
-//=======================================================================
-//function : SetSelectedCurrent
-//purpose  : 
-//=======================================================================
+  const Handle(AIS_InteractiveObject) anObject =
+    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  const Handle(Graphic3d_HighlightStyle)& anObjSelStyle =
+    getSelStyle (anObject);
 
-void AIS_InteractiveContext::SetSelectedCurrent()
-{
+  if (NbSelected() == 1 && theOwner->IsSelected())
+  {
+    Handle(Graphic3d_HighlightStyle) aCustomStyle;
+    if (HighlightStyle (theOwner, aCustomStyle))
+    {
+      if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
+      {
+        const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
+        theOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
+      }
+    }
+    return;
+  }
 
+  if (!myObjects.IsBound (anObject))
+    Display (anObject, Standard_False);
 
-#ifdef DEB
-  cout<<"Not Yet Implemented"<<endl;
-#endif
-}
+  unhighlightSelected();
 
-//=======================================================================
-//function : UpdateSelection
-//purpose  : 
-//=======================================================================
+  mySelection->ClearAndSelect (theOwner);
+  Handle(Graphic3d_HighlightStyle) aCustomStyle;
+  if (!HighlightStyle (theOwner, aCustomStyle) ||
+    (!aCustomStyle.IsNull() && aCustomStyle != anObjSelStyle))
+  {
+    highlightSelected (theOwner);
+  }
 
-void AIS_InteractiveContext::UpdateSelected(const Standard_Boolean updateviewer)
-{
-  if(!HasOpenedContext()) {
-    HilightSelected();
-#ifndef BUG
-    return;
-#endif
+  theOwner->State (1);
+  if (theOwner == anObject->GlobalSelOwner())
+  {
+    Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObject);
+    aState->SetHilightStatus (Standard_True);
+    aState->SetHilightStyle (anObjSelStyle);
   }
-  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);
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
 }
 
 //=======================================================================
 //function : AddOrRemoveSelected
-//purpose  : 
+//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::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)& anIObj,
-                                                const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
+                                                  const Standard_Boolean theToUpdateViewer)
 {
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
-  if(!HasOpenedContext())
-    AddOrRemoveCurrentObject(anIObj,updateviewer);
-  else
-    myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(anIObj,updateviewer);
-  
+  if (theObject.IsNull())
+    return;
+
+  if (HasOpenedContext())
+    return myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (theObject, theToUpdateViewer);
+
+  const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
+  if (!myObjects.IsBound (theObject) || !theObject->HasSelection (aGlobalSelMode))
+    return;
+
+  if (!theObject->HasInteractiveContext())
+    theObject->SetContext (this);
+
+  const Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
+
+  if (anOwner.IsNull() || !anOwner->HasSelectable())
+    return;
+
+  AddOrRemoveSelected (anOwner, theToUpdateViewer);
 }
 //=======================================================================
 //function : AddOrRemoveSelected
 //purpose  : 
 //=======================================================================
 
-void AIS_InteractiveContext::AddOrRemoveSelected(const TopoDS_Shape& aShap,
+void AIS_InteractiveContext::AddOrRemoveSelected (const TopoDS_Shape& aShap,
                                            const Standard_Boolean updateviewer)
 { 
   if(!HasOpenedContext()) {
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
 #endif
     return;
@@ -1051,163 +1192,216 @@ void AIS_InteractiveContext::AddOrRemoveSelected(const TopoDS_Shape& aShap,
   if(updateviewer) UpdateCurrentViewer();
   
 }
+
 //=======================================================================
 //function : AddOrRemoveSelected
-//purpose  : 
+//purpose  : Allows to highlight or unhighlight the owner given depending on
+//           its selection status
 //=======================================================================
+void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                  const Standard_Boolean theToUpdateViewer)
+{
+  if (HasOpenedContext())
+    return myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected (theOwner, theToUpdateViewer);
 
-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 (theOwner.IsNull() || !theOwner->HasSelectable())
     return;
+
+  AIS_SelectStatus aSelStat = 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)
+  {
+    highlightSelected (theOwner);
+    if (isGlobal)
+    {
+      aStatus->SetHilightStatus (Standard_True);
+      aStatus->SetHilightStyle (getSelStyle (anObj));
+    }
+  }
+  else
+  {
+    if (theOwner->IsAutoHilight())
+      theOwner->Unhilight (myMainPM, aHiMode);
+    else
+      anObj->ClearSelected();
+    aStatus->SetHilightStatus (Standard_False);
+    aStatus->SetHilightStyle (new Graphic3d_HighlightStyle());
   }
-  
-  myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(Own,Standard_False);
-  if(updateviewer) UpdateCurrentViewer();
-  
-}
 
+  if (theToUpdateViewer)
+    UpdateCurrentViewer();
+}
 
 
 //=======================================================================
 //function : IsSelected
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::
-IsSelected(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
 {
-  if (anIObj.IsNull()) return Standard_False;
-  
-  if(!HasOpenedContext()) 
-    return (anIObj->State()==1);
-  else 
-    return myLocalContexts(myCurLocalIndex)->IsSelected(anIObj);
+  if(HasOpenedContext())
+    return myLocalContexts(myCurLocalIndex)->IsSelected (theObj);
+
+  if (theObj.IsNull() || !myObjects.IsBound (theObj))
+    return Standard_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 (aModeIter.Value() == aGlobalSelMode)
+    {
+      isGlobalModeActivated = Standard_True;
+      break;
+    }
+  }
+  if (!theObj->HasSelection (aGlobalSelMode) || !isGlobalModeActivated || theObj->GlobalSelOwner().IsNull())
+    return Standard_False;
+
+  return theObj->GlobalSelOwner()->State() == 1;
 }
 
+//=======================================================================
+//function : IsSelected
+//purpose  : Returns true is the owner given is selected
+//=======================================================================
+Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(SelectMgr_EntityOwner)& theOwner) const
+{
+  if (HasOpenedContext())
+    return myLocalContexts(myCurLocalIndex)->IsSelected (theOwner);
+
+  if (theOwner.IsNull())
+    return Standard_False;
+
+  return theOwner->IsSelected();
+}
 
 //=======================================================================
 //function : InitSelected
-//purpose  : 
+//purpose  :
 //=======================================================================
-
 void AIS_InteractiveContext::InitSelected()
 {
-  if(!HasOpenedContext())
-    AIS_Selection::Selection(myCurrentName.ToCString())->Init();
-  else
-    myLocalContexts(myCurLocalIndex)->InitSelected();
+  if (HasOpenedContext())
+  {
+    myLocalContexts (myCurLocalIndex)->InitSelected();
+    return;
+  }
+
+  mySelection->Init();
 }
 
 //=======================================================================
 //function : MoreSelected
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::MoreSelected() const 
+Standard_Boolean AIS_InteractiveContext::MoreSelected() const
 {
-  if(!HasOpenedContext())
-    return AIS_Selection::Selection(myCurrentName.ToCString())->More();
-  return myLocalContexts(myCurLocalIndex)->MoreSelected();
+  if (HasOpenedContext())
+    return myLocalContexts (myCurLocalIndex)->MoreSelected();
+
+  return mySelection->More();
 }
 
 //=======================================================================
 //function : NextSelected
-//purpose  : 
+//purpose  :
 //=======================================================================
-
 void AIS_InteractiveContext::NextSelected()
 {
-  if(!HasOpenedContext())
-    AIS_Selection::Selection(myCurrentName.ToCString())->Next();
-  else
-    myLocalContexts(myCurLocalIndex)->NextSelected();
+  if(HasOpenedContext())
+  {
+    return myLocalContexts (myCurLocalIndex)->NextSelected();
+  }
+
+  mySelection->Next();
 }
 
 //=======================================================================
 //function : HasSelectedShape
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const 
+Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
 {
-  if(!HasOpenedContext()) {
-#ifdef IMP280200
-    Handle(AIS_Shape) shape = 
-       Handle(AIS_Shape)::DownCast(SelectedInteractive());
-    if( !shape.IsNull() ) return Standard_True;
-#endif
-    return Standard_False;
+  if(HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->HasSelectedShape();
   }
-  return myLocalContexts(myCurLocalIndex)->HasShape();
-  
-  
+  if (!mySelection->More())
+    return Standard_False;
+
+  const Handle(StdSelect_BRepOwner) anOwner =
+    Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+
+  return !anOwner.IsNull() && anOwner->HasShape();
 }
 
 //=======================================================================
 //function : SelectedShape
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-TopoDS_Shape AIS_InteractiveContext::SelectedShape() const 
+TopoDS_Shape AIS_InteractiveContext::SelectedShape() const
 {
+  if (HasOpenedContext())
+  {
+    return myLocalContexts (myCurLocalIndex)->SelectedShape();
+  }
 
-  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();
-}
+  if (!mySelection->More())
+    return TopoDS_Shape();
 
-//=======================================================================
-//function : Interactive
-//purpose  : 
-//=======================================================================
+  const Handle(StdSelect_BRepOwner) anOwner =
+    Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+  if (anOwner.IsNull() || !anOwner->HasSelectable())
+    return TopoDS_Shape();
 
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::Interactive() const 
-{
-  return SelectedInteractive();
+  return anOwner->Shape().Located (anOwner->Location() * anOwner->Shape().Location());
 }
 
+//=======================================================================
+//function : SelectedInteractive
+//purpose  :
+//=======================================================================
 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();
-  
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->SelectedInteractive();
+  }
+
+  return !mySelection->More()
+       ? Handle(AIS_InteractiveObject)()
+       : Handle(AIS_InteractiveObject)::DownCast (mySelection->Value()->Selectable());
 }
 //=======================================================================
-//function : Interactive
-//purpose  : 
+//function : SelectedOwner
+//purpose  :
 //=======================================================================
-
-Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const 
+Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const
 {
-  if(!HasOpenedContext()){
-    Handle(SelectMgr_EntityOwner) Ownr;
-    return Ownr;
+  if(HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->SelectedOwner();
   }
-  return  myLocalContexts(myCurLocalIndex)->SelectedOwner();
-  
+
+  return !mySelection->More()
+       ? Handle(SelectMgr_EntityOwner)()
+       : mySelection->Value();
 }
 
 //=======================================================================
 //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 
 {
@@ -1220,6 +1414,9 @@ void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners
   else
     aModes.Append( theMode );
 
+  if (theOwners.IsNull())
+    theOwners = new SelectMgr_IndexedMapOfOwner();
+
   TColStd_ListIteratorOfListOfInteger anItr( aModes );
   for (; anItr.More(); anItr.Next() )
   {
@@ -1227,62 +1424,65 @@ void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners
     if ( !theIObj->HasSelection( aMode ) )
       continue;
 
-    Handle(SelectMgr_Selection) aSel = theIObj->Selection( aMode );
+    Handle(SelectMgr_Selection) aSel = theIObj->Selection(aMode);
 
     for ( aSel->Init(); aSel->More(); aSel->Next() )
     {
-      Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive();
+      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 );
+       theOwners->Add( aOwner );
     }
   }
 }
 
-Standard_Integer AIS_InteractiveContext::NbSelected() 
+//=======================================================================
+//function : NbSelected
+//purpose  :
+//=======================================================================
+Standard_Integer AIS_InteractiveContext::NbSelected()
 {
-  Standard_Integer i(0);
-  for(InitSelected();MoreSelected();NextSelected())
-    {i++;}
-  return i;
+  Standard_Integer aNbSelected = 0;
+  for (InitSelected(); MoreSelected(); NextSelected())
+  {
+    aNbSelected++;
+  }
+
+  return aNbSelected;
 }
 
 //=======================================================================
 //function : HasApplicative
-//purpose  : 
+//purpose  :
 //=======================================================================
-
   Standard_Boolean AIS_InteractiveContext::HasApplicative() const 
 {
-  return Interactive()->HasOwner();
+  return SelectedInteractive()->HasOwner();
 }
 
 //=======================================================================
 //function : Applicative
-//purpose  : 
+//purpose  :
 //=======================================================================
-
 Handle(Standard_Transient) AIS_InteractiveContext::Applicative() const 
 {
-  return Interactive()->GetOwner();
+  return SelectedInteractive()->GetOwner();
 }
 
-
-
 //==================================================
-// Function: 
+// Function: HasDetected
 // Purpose :
 //==================================================
-Standard_Boolean AIS_InteractiveContext::HasDetected() const 
+Standard_Boolean AIS_InteractiveContext::HasDetected() const
 {
   if(HasOpenedContext())
     return myLocalContexts(myCurLocalIndex)->HasDetected();
-  else
-    return !myLastPicked.IsNull();
+
+  return !myLastPicked.IsNull();
 }
 
 //=======================================================================
@@ -1310,15 +1510,14 @@ AIS_InteractiveContext::DetectedShape() const
 
 //=======================================================================
 //function : DetectedInteractive
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Handle(AIS_InteractiveObject) 
-AIS_InteractiveContext::DetectedInteractive() const 
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedInteractive() const
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
     return myLocalContexts(myCurLocalIndex)->DetectedInteractive();
-  return myLastPicked;
+
+  return Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
 }
 
 
@@ -1338,80 +1537,54 @@ Standard_Boolean AIS_InteractiveContext::HasNextDetected() const
 //=======================================================================
 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
     return myLocalContexts(myCurLocalIndex)->DetectedOwner();
-  Handle(SelectMgr_EntityOwner) Ownr;
-  return Ownr;
-}
 
+  return myLastPicked;
+}
 
 //=======================================================================
 //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())
-    return 0;
-  return myLocalContexts(myCurLocalIndex)->HilightNextDetected(V);
+  return HasOpenedContext()
+       ? myLocalContexts (myCurLocalIndex)->HilightNextDetected (theView, theToRedrawImmediate)
+       : 0;
     
 }
 
 //=======================================================================
 //function : HilightNextDetected
-//purpose  : 
+//purpose  :
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::HilightPreviousDetected(const Handle(V3d_View)& V)
+Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
+                                                                  const Standard_Boolean  theToRedrawImmediate)
 {
-  if(!HasOpenedContext())
-    return 0;
-  return myLocalContexts(myCurLocalIndex)->HilightPreviousDetected(V);
+  return HasOpenedContext()
+       ? myLocalContexts (myCurLocalIndex)->HilightPreviousDetected (theView, theToRedrawImmediate)
+       : 0;
     
 }
 
-#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);
-  }
-}
-#endif
-
-#ifdef IMP160701
 //=======================================================================
 //function : InitDetected
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::InitDetected()
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
   {
     myLocalContexts(myCurLocalIndex)->InitDetected();
     return;
   }
 
   if(myAISDetectedSeq.Length() != 0)
+  {
     myAISCurDetected = 1;
-
+  }
 }
 
 //=======================================================================
@@ -1420,10 +1593,12 @@ void AIS_InteractiveContext::InitDetected()
 //=======================================================================
 Standard_Boolean AIS_InteractiveContext::MoreDetected() const
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
+  {
     return myLocalContexts(myCurLocalIndex)->MoreDetected();
+  }
 
-  return (myAISCurDetected>0 &&myAISCurDetected <= myAISDetectedSeq.Length()) ?
+  return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length()) ?
           Standard_True : Standard_False;
 }
 
@@ -1446,34 +1621,61 @@ void AIS_InteractiveContext::NextDetected()
 //function : DetectedCurrentShape
 //purpose  :
 //=======================================================================
-
 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
+  {
     return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
+  }
+
+  Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
+
+  if (aCurrentShape.IsNull())
+  {
+    return AIS_myDummyShape;
+  }
 
-  static TopoDS_Shape bidsh;
-  if(myAISCurDetected > 0 &&
-     myAISCurDetected <= myAISDetectedSeq.Length())
-    return Handle(AIS_Shape)::DownCast(myAISDetectedSeq(myAISCurDetected))->Shape();
-  return bidsh;
+  return aCurrentShape->Shape();
 }
 
 //=======================================================================
 //function : DetectedCurrentObject
 //purpose  :
 //=======================================================================
-
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const {
-  if(HasOpenedContext())
+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;
+  return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
+}
+
+//=======================================================================
+//function : FirstSelectedObject
+//purpose  :
+//=======================================================================
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject()
+{
+  Handle(AIS_InteractiveObject) anObject;
+
+  if (HasOpenedContext())
+    return anObject;
+
+  InitSelected();
+  if (MoreSelected())
+  {
+    return SelectedInteractive();
+  }
+  return anObject;
+}
+
+//=======================================================================
+//function : RedrawImmediate
+//purpose  : Redisplays immediate strucures of the viewer given according to their visibility
+//=======================================================================
+void AIS_InteractiveContext::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
+{
+  myMainPM->RedrawImmediate (theViewer);
 }
-#endif