0027893: Visualization - AIS_InteractiveContext::SetSelected does not work
[occt.git] / src / AIS / AIS_LocalContext_1.cxx
index cbf86d8..6a2a088 100644 (file)
 //              - use of optimisation in SelectMgr_ViewerSelector
 //              -> Best management in detected entities...
 
-#include <AIS_LocalContext.jxx>
-#include <StdSelect_BRepOwner.hxx>
-#include <TColStd_ListOfInteger.hxx>
-#include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <TColStd_MapOfTransient.hxx>
-#include <TColStd_MapIteratorOfMapOfTransient.hxx>
-#include <Prs3d_Presentation.hxx>
-#include <Prs3d_Drawer.hxx>
-#include <Prs3d_ShadingAspect.hxx>
+#include <AIS_InteractiveContext.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_LocalContext.hxx>
 #include <AIS_LocalStatus.hxx>
+#include <AIS_Selection.hxx>
+#include <AIS_Shape.hxx>
+#include <Aspect_Grid.hxx>
+#include <Geom_Transformation.hxx>
 #include <Graphic3d_ArrayOfTriangles.hxx>
 #include <Graphic3d_Group.hxx>
+#include <NCollection_Map.hxx>
+#include <OSD_Environment.hxx>
+#include <Prs3d_Drawer.hxx>
+#include <Prs3d_Presentation.hxx>
+#include <Prs3d_ShadingAspect.hxx>
 #include <Select3D_SensitiveTriangulation.hxx>
 #include <SelectBasics_SensitiveEntity.hxx>
-#include <TCollection_AsciiString.hxx>
-#include <NCollection_Map.hxx>
-#include <Visual3d_View.hxx>
-
-#include <SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive.hxx>
+#include <SelectMgr_EntityOwner.hxx>
+#include <SelectMgr_Filter.hxx>
+#include <SelectMgr_OrFilter.hxx>
+#include <SelectMgr_SelectableObject.hxx>
 #include <SelectMgr_Selection.hxx>
+#include <SelectMgr_SelectionManager.hxx>
 #include <SelectMgr_SequenceOfOwner.hxx>
-#include <OSD_Environment.hxx>
+#include <Standard_Transient.hxx>
+#include <StdSelect_BRepOwner.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TColStd_ListIteratorOfListOfInteger.hxx>
+#include <TColStd_ListOfInteger.hxx>
+#include <TColStd_MapIteratorOfMapOfTransient.hxx>
+#include <TColStd_MapOfTransient.hxx>
+#include <V3d_View.hxx>
 
-#include <Geom_Transformation.hxx>
-#include <AIS_Selection.hxx>
-#include <Aspect_Grid.hxx>
-#include <AIS_Shape.hxx>
+namespace
+{
+  TopoDS_Shape AIS_myDummyShape;
+}
 
 static Standard_Integer GetHiMod(const Handle(AIS_InteractiveObject)& IO)
 {
   return IO->HasHilightMode() ? IO->HilightMode():0;
 }
 
+//=======================================================================
+//function : getHiStyle
+//purpose  :
+//=======================================================================
+const Handle(Graphic3d_HighlightStyle)& AIS_LocalContext::getHiStyle (const Handle(SelectMgr_SelectableObject)& theObj) const
+{
+  const Handle(Prs3d_Drawer)& aHiDrawer = theObj->HilightAttributes();
+  return !aHiDrawer.IsNull() && aHiDrawer->HasOwnHighlightStyle()
+    ? aHiDrawer->HighlightStyle() : myCTX->HighlightStyle();
+}
+
+//=======================================================================
+//function : getSelStyle
+//purpose  :
+//=======================================================================
+const Handle(Graphic3d_HighlightStyle)& AIS_LocalContext::getSelStyle (const Handle(AIS_InteractiveObject)& theObj) const
+{
+  const Handle(Prs3d_Drawer)& aHiDrawer = theObj->HilightAttributes();
+  return !aHiDrawer.IsNull() && aHiDrawer->HasOwnSelectionStyle()
+    ? aHiDrawer->SelectionStyle() : myCTX->SelectionStyle();
+}
+
 //==================================================
 // Function: MoveTo
 // Purpose :
@@ -95,10 +128,10 @@ AIS_StatusOfDetection AIS_LocalContext::MoveTo (const Standard_Integer  theXpix,
   // result of courses..
   if (aDetectedNb == 0 || myDetectedSeq.IsEmpty())
   {
-    if (mylastindex != 0 && mylastindex <= myMapOfOwner.Extent())
+    if (mylastindex != 0 && mylastindex <= myMapOfOwner->Extent())
     {
       myMainPM->ClearImmediateDraw();
-      Unhilight (myMapOfOwner (mylastindex), theView);
+      Unhilight (myMapOfOwner->FindKey (mylastindex), theView);
       if (theToRedrawImmediate)
       {
         theView->RedrawImmediate();
@@ -128,6 +161,20 @@ AIS_StatusOfDetection AIS_LocalContext::MoveTo (const Standard_Integer  theXpix,
   }
 }
 
+//=======================================================================
+//function : AddSelect
+//purpose  : 
+//=======================================================================
+AIS_StatusOfPick AIS_LocalContext::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
+{
+  mySelection->AddSelect (theObject);
+
+  Standard_Integer aSelNum = mySelection->Extent();
+  return (aSelNum == 1) ? AIS_SOP_OneSelected
+                        : (aSelNum > 1) ? AIS_SOP_SeveralSelected
+                                        : AIS_SOP_Error;
+}
+
 //=======================================================================
 //function : Select
 //purpose  : 
@@ -139,31 +186,29 @@ AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Boolean toUpdateViewer
     UnhilightPicked (Standard_False);
   }
 
-  AIS_Selection::SetCurrentSelection (mySelName.ToCString());
-
   Standard_Integer aDetIndex = DetectedIndex();
   if (aDetIndex <= 0)
   {
     ClearSelected (toUpdateViewer);
-    return (AIS_Selection::Extent() == 0) ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
+    return mySelection->IsEmpty() ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
   }
 
-  const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner (aDetIndex);
+  const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner->FindKey (aDetIndex);
 
   ClearSelected (Standard_False);
 
   if (!anOwner->IsSelected()) // anOwner is not selected
   {
     anOwner->SetSelected (Standard_True);
-    AIS_Selection::Select (anOwner);
+    mySelection->Select (anOwner);
   }
 
   if (myAutoHilight)
   {
     const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
-    for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
+    for (V3d_ListOfViewIterator anActiveViewIter (aViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
     {
-      Unhilight (anOwner, aViewer->ActiveView());
+      Unhilight (anOwner, anActiveViewIter.Value());
     }
 
     // advanced selection highlighting mechanism
@@ -179,7 +224,7 @@ AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Boolean toUpdateViewer
     }
   }
 
-  return (AIS_Selection::Extent() == 1) ? AIS_SOP_OneSelected : AIS_SOP_SeveralSelected;
+  return (mySelection->Extent() == 1) ? AIS_SOP_OneSelected : AIS_SOP_SeveralSelected;
 }
 
 //=======================================================================
@@ -201,11 +246,8 @@ AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Integer  theXPMin,
       UnhilightPicked (Standard_False);
     }
 
-    AIS_Selection::SetCurrentSelection (mySelName.ToCString());
-    Standard_Integer aSelNum = AIS_Selection::Extent();
-
-    myMainVS->Init();
-    if (!myMainVS->More())
+    Standard_Integer aSelNum = mySelection->Extent();
+    if (myMainVS->NbPicked() == 0)
     {
       ClearSelected (toUpdateViewer);
       mylastindex = 0;
@@ -214,16 +256,16 @@ AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Integer  theXPMin,
 
     ClearSelected (Standard_False);
 
-    for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
+    for (Standard_Integer aPickIter = 1; aPickIter <= myMainVS->NbPicked(); ++aPickIter)
     {
-      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
+      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked (aPickIter);
       if (myFilters->IsOk (anOwner))
       {
         // it can be helpful to classify this owner immediately...
         if (!anOwner->IsSelected())
         {
           anOwner->SetSelected (Standard_True);
-          AIS_Selection::Select (anOwner);
+          mySelection->Select (anOwner);
         }
       }
     }
@@ -234,7 +276,7 @@ AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Integer  theXPMin,
     }
   }
 
-  Standard_Integer aSelNum = AIS_Selection::Extent();
+  Standard_Integer aSelNum = mySelection->Extent();
 
   return (aSelNum == 1) ? AIS_SOP_OneSelected
                         : (aSelNum > 1) ? AIS_SOP_SeveralSelected
@@ -253,11 +295,8 @@ AIS_StatusOfPick AIS_LocalContext::Select (const TColgp_Array1OfPnt2d& thePolyli
   {
     myMainVS->Pick (thePolyline, theView);
 
-    AIS_Selection::SetCurrentSelection (mySelName.ToCString());
-
-    Standard_Integer aLastSelNum = AIS_Selection::Extent();
-    myMainVS->Init();
-    if (!myMainVS->More())
+    Standard_Integer aLastSelNum = mySelection->Extent();
+    if (myMainVS->NbPicked() == 0)
     {
       // Nothing is selected clear selection.
       ClearSelected (toUpdateViewer);
@@ -275,15 +314,15 @@ AIS_StatusOfPick AIS_LocalContext::Select (const TColgp_Array1OfPnt2d& thePolyli
     // Clear previous selection without update to process this selection
     ClearSelected (Standard_False);
 
-    for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
+    for (Standard_Integer aPickIter = 1; aPickIter <= myMainVS->NbPicked(); ++aPickIter)
     {
-      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
+      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked (aPickIter);
       if (myFilters->IsOk (anOwner))
       {
         // it can be helpful to classify this owner immediately...
         if (!anOwner->IsSelected())
         {
-          AIS_Selection::AddSelect (anOwner);
+          mySelection->AddSelect (anOwner);
           anOwner->SetSelected (Standard_True);
         }
       }
@@ -295,7 +334,7 @@ AIS_StatusOfPick AIS_LocalContext::Select (const TColgp_Array1OfPnt2d& thePolyli
     }
   }
 
-  Standard_Integer aSelNum = AIS_Selection::Extent();
+  Standard_Integer aSelNum = mySelection->Extent();
   return (aSelNum == 1) ? AIS_SOP_OneSelected
                         : (aSelNum > 1) ? AIS_SOP_SeveralSelected
                                         : AIS_SOP_Error;
@@ -308,24 +347,22 @@ AIS_StatusOfPick AIS_LocalContext::Select (const TColgp_Array1OfPnt2d& thePolyli
 AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
 {
   Standard_Integer aDetIndex = DetectedIndex();
-  AIS_Selection::SetCurrentSelection (mySelName.ToCString());
 
   if(aDetIndex > 0)
   {
-    AIS_Selection::SetCurrentSelection (mySelName.ToCString());
-    Standard_Integer aSelNum = AIS_Selection::Extent();
-    const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner (aDetIndex);
+    Standard_Integer aSelNum = mySelection->Extent();
+    const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner->FindKey (aDetIndex);
     Standard_Boolean toSelect = anOwner->IsSelected() ? Standard_False : Standard_True;
-    AIS_Selection::Select (anOwner);
+    mySelection->Select (anOwner);
     anOwner->SetSelected (toSelect);
 
     if(myAutoHilight)
     {
       myMainPM->ClearImmediateDraw();
       const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
-      for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
+      for (V3d_ListOfViewIterator anActiveViewIter (aViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
       {
-        Unhilight (anOwner, aViewer->ActiveView());
+        Unhilight (anOwner, anActiveViewIter.Value());
       }
 
       // advanced selection highlighting mechanism
@@ -341,7 +378,7 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Boolean toUpdateV
       }
     } 
 
-    Standard_Integer NS = AIS_Selection::Extent();
+    Standard_Integer NS = mySelection->Extent();
     if( NS == 1 ) return AIS_SOP_OneSelected;
     else if( NS > 1 ) return AIS_SOP_SeveralSelected;
     return aSelNum == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
@@ -366,11 +403,8 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Integer  theXPMin
   {
     myMainVS->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
 
-    AIS_Selection::SetCurrentSelection (mySelName.ToCString());
-    Standard_Integer aLastSelNum = AIS_Selection::Extent();
-
-    myMainVS->Init();
-    if (!myMainVS->More())
+    Standard_Integer aLastSelNum = mySelection->Extent();
+    if (myMainVS->NbPicked() == 0)
     {
       // Nothing is selected clear selection, but don't clear the selection
       // as it is shift selection and previous selection matters.
@@ -383,13 +417,13 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Integer  theXPMin
       UnhilightPicked (Standard_False);
     }
 
-    for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
+    for (Standard_Integer aPickIter = 1; aPickIter <= myMainVS->NbPicked(); ++aPickIter)
     {
-      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
+      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked (aPickIter);
       if(myFilters->IsOk (anOwner))
       {
         Standard_Boolean toSelect = anOwner->IsSelected() ? Standard_False : Standard_True;
-        AIS_Selection::Select (anOwner);
+        mySelection->Select (anOwner);
         anOwner->SetSelected (toSelect);
       }
     }
@@ -400,7 +434,7 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Integer  theXPMin
     }
   }
 
-  Standard_Integer aSelNum = AIS_Selection::Extent();
+  Standard_Integer aSelNum = mySelection->Extent();
 
   return (aSelNum == 1) ? AIS_SOP_OneSelected
                         : (aSelNum > 1) ? AIS_SOP_SeveralSelected
@@ -419,11 +453,8 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const TColgp_Array1OfPnt2d& theP
   {
     myMainVS->Pick (thePolyline, theView);
 
-    AIS_Selection::SetCurrentSelection (mySelName.ToCString());
-
-    Standard_Integer aLastSelNum = AIS_Selection::Extent();
-    myMainVS->Init();
-    if(!myMainVS->More())
+    Standard_Integer aLastSelNum = mySelection->Extent();
+    if (myMainVS->NbPicked() == 0)
     {
       // Nothing is selected clear selection, but don't clear the selection
       // as it is shift selection and previous selection matters.
@@ -436,13 +467,13 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const TColgp_Array1OfPnt2d& theP
       UnhilightPicked (Standard_False);
     }
 
-    for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
+    for (Standard_Integer aPickIter = 1; aPickIter <= myMainVS->NbPicked(); ++aPickIter)
     {
-      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
+      const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked (aPickIter);
       if (myFilters->IsOk (anOwner))
       {
         Standard_Boolean toSelect = anOwner->IsSelected() ? Standard_False : Standard_True;
-        AIS_Selection::Select (anOwner);
+        mySelection->Select (anOwner);
         anOwner->SetSelected (toSelect);
       }
     }
@@ -452,7 +483,7 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const TColgp_Array1OfPnt2d& theP
     }
   }
 
-  Standard_Integer aSelNum = AIS_Selection::Extent();
+  Standard_Integer aSelNum = mySelection->Extent();
 
   return (aSelNum == 1) ? AIS_SOP_OneSelected
                         : (aSelNum > 1) ? AIS_SOP_SeveralSelected
@@ -473,8 +504,8 @@ void AIS_LocalContext::Hilight (const Handle(SelectMgr_EntityOwner)& theOwner,
 
   const Standard_Integer aHilightMode = GetHiMod (Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()));
   myMainPM->BeginImmediateDraw();
-  theOwner->HilightWithColor (myMainPM, myCTX->HilightColor(), aHilightMode);
-  myMainPM->EndImmediateDraw (theView);
+  theOwner->HilightWithColor (myMainPM, getHiStyle (theOwner->Selectable()), aHilightMode);
+  myMainPM->EndImmediateDraw (theView->Viewer());
 }
 
 //==================================================
@@ -489,12 +520,13 @@ void AIS_LocalContext::Unhilight (const Handle(SelectMgr_EntityOwner)& theOwner,
     return;
   }
 
-  const Standard_Integer aHilightMode = GetHiMod (Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()));
+  const Handle(AIS_InteractiveObject)& anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+  const Standard_Integer aHilightMode = GetHiMod (anObj);
   if (IsSelected (theOwner))
   {
     if (theOwner->IsAutoHilight())
     {
-      theOwner->HilightWithColor (myMainPM, myCTX->SelectionColor(), aHilightMode);
+      theOwner->HilightWithColor (myMainPM, getSelStyle (anObj), aHilightMode);
     }
   }
   else
@@ -505,105 +537,92 @@ void AIS_LocalContext::Unhilight (const Handle(SelectMgr_EntityOwner)& theOwner,
 
 //=======================================================================
 //function : HilightPicked
-//purpose  : 
+//purpose  :
 //=======================================================================
-void AIS_LocalContext::HilightPicked(const Standard_Boolean updateviewer)
+void AIS_LocalContext::HilightPicked (const Standard_Boolean theToUpdateviewer)
 {
-  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
-  if( Sel.IsNull() ) return;
-
-  typedef NCollection_DataMap <Handle(SelectMgr_SelectableObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > SelectMgr_DataMapOfObjectOwners;
+  if (mySelection.IsNull())
+  {
+    return;
+  }
+  
+  typedef NCollection_Shared<SelectMgr_SequenceOfOwner> SelectMgr_HSequenceOfOwner;
+  typedef NCollection_DataMap <Handle(SelectMgr_SelectableObject), Handle(SelectMgr_HSequenceOfOwner) > SelectMgr_DataMapOfObjectOwners;
   SelectMgr_DataMapOfObjectOwners aMap;
 
-  Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
-  
   // to avoid problems when there is a loop searching for selected objects...
-  const AIS_NListTransient& Obj = Sel->Objects();
-  AIS_NListTransient::Iterator anIter( Obj );
-  for(; anIter.More(); anIter.Next())
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
   {
-    const Handle(Standard_Transient)& Tr = anIter.Value();
-    if(!Tr.IsNull()){
-      const Handle(SelectMgr_EntityOwner)& Ownr =
-        *((const Handle(SelectMgr_EntityOwner)*) &Tr);
-      Handle(AIS_InteractiveObject) IO;
-      if(Ownr->HasSelectable()){
-       Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
-       if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
-         Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
-         IO = *((Handle(AIS_InteractiveObject)*)&SO);
-       }
-      }
-      Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
-      Standard_Integer HM = GetHiMod(*((Handle(AIS_InteractiveObject)*)&SO));
-      if ( Ownr->IsAutoHilight() )
-        Ownr->HilightWithColor(PM,myCTX->SelectionColor(),HM);
-      else if ( aMap.IsBound (SO) )
-        aMap(SO)->Append ( Ownr );        
-      else {
-        NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq = new SelectMgr_SequenceOfOwner;
-        aSeq->Append ( Ownr );
-        aMap.Bind ( SO, aSeq );
-      }      
+    const Handle(SelectMgr_EntityOwner)& anOwner = aSelIter.Value();
+    Handle(SelectMgr_SelectableObject) aSelObj = anOwner->Selectable();
+    if (anOwner->IsAutoHilight())
+    {
+      Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(aSelObj);
+      const Standard_Integer aHighMode = GetHiMod (anIO);
+      anOwner->HilightWithColor (myMainPM, getSelStyle (anIO), aHighMode);
+      continue;
+    }
+
+    Handle(SelectMgr_HSequenceOfOwner) aSeq;
+    if (aMap.Find (aSelObj, aSeq))
+    {
+      aSeq->Append (anOwner);
+    }
+    else
+    {
+      aSeq = new SelectMgr_HSequenceOfOwner();
+      aSeq->Append (anOwner);
+      aMap.Bind (aSelObj, aSeq);
     }
   }
 
-  for ( SelectMgr_DataMapOfObjectOwners::Iterator aMapIter(aMap); 
-        aMapIter.More(); aMapIter.Next() )
+  for (SelectMgr_DataMapOfObjectOwners::Iterator aMapIter (aMap); aMapIter.More(); aMapIter.Next())
   {
     aMapIter.Key()->HilightSelected (myMainPM, *aMapIter.Value());
   }
 
-  if (updateviewer)
+  if (theToUpdateviewer)
   {
     myCTX->CurrentViewer()->Update();
   }
 }
 
 //==================================================
-// Function: 
+// Function: UnhilightPicked
 // Purpose :
 //==================================================
-void AIS_LocalContext::UnhilightPicked (const Standard_Boolean updateviewer)
+void AIS_LocalContext::UnhilightPicked (const Standard_Boolean theToUpdateViewer)
 {
   myMainPM->ClearImmediateDraw();
+  if (mySelection.IsNull())
+  {
+    return;
+  }
 
-  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
-  if( Sel.IsNull() ) return;
-  Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
   NCollection_Map<Handle(SelectMgr_SelectableObject)> anObjMap;
-  
-  const AIS_NListTransient& Obj = Sel->Objects();
-  AIS_NListTransient::Iterator anIter( Obj );
-  for(; anIter.More(); anIter.Next()){
-    const Handle(Standard_Transient)& Tr = anIter.Value();
-    if(!Tr.IsNull()){
-      const Handle(SelectMgr_EntityOwner)& Ownr =
-        *((const Handle(SelectMgr_EntityOwner)*) &Tr);
-      Standard_Integer HM(0);
-      if(Ownr->HasSelectable()){
-       Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
-       Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&SO);
-        anObjMap.Add (IO);
-
-        HM = GetHiMod(IO);
-       Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
-       if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
-       }
-      }
-      Ownr->Unhilight(PM,HM);
-    }
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    const Handle(SelectMgr_EntityOwner)& anOwner = aSelIter.Value();
+    Handle(SelectMgr_SelectableObject) aSelObj = anOwner->Selectable();
+    Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (aSelObj);
+    anObjMap.Add (aSelObj);
+    Standard_Integer aHighMode = GetHiMod (anIO);
+    anOwner->Unhilight (myMainPM, aHighMode);
   }
-  
-  for (NCollection_Map<Handle(SelectMgr_SelectableObject)>::Iterator anIter1 ( anObjMap ); 
-        anIter1.More(); anIter1.Next() )
+
+  for (NCollection_Map<Handle(SelectMgr_SelectableObject)>::Iterator aMapIter (anObjMap);
+       aMapIter.More(); aMapIter.Next())
   {
-    if ( !anIter1.Key()->IsAutoHilight() )
-      anIter1.Key()->ClearSelected();
+    if (!aMapIter.Key()->IsAutoHilight())
+    {
+      aMapIter.Key()->ClearSelected();
+    }
   }
 
-  if(updateviewer)
+  if (theToUpdateViewer)
+  {
     myCTX->CurrentViewer()->Update();
+  }
 }
 
 //=======================================================================
@@ -632,8 +651,7 @@ Standard_Boolean AIS_LocalContext::IsSelected (const Handle(SelectMgr_EntityOwne
 void AIS_LocalContext::
 InitSelected()
 {
-  AIS_Selection::SetCurrentSelection(mySelName.ToCString());
-  AIS_Selection::CurrentSelection()->Init();
+  mySelection->Init();
 }
 
 //==================================================
@@ -643,7 +661,7 @@ InitSelected()
 Standard_Boolean AIS_LocalContext::
 MoreSelected() const 
 {
-  return AIS_Selection::CurrentSelection()->More();
+  return mySelection->More();
 }
 
 //==================================================
@@ -653,24 +671,24 @@ MoreSelected() const
 void AIS_LocalContext::
 NextSelected()
 {
-  AIS_Selection::CurrentSelection()->Next();
+  mySelection->Next();
 }
 
 //==================================================
-// Function: 
+// Function: HasShape
 // Purpose :
 //==================================================
-Standard_Boolean AIS_LocalContext::
-HasShape() const 
+Standard_Boolean AIS_LocalContext::HasShape() const
 {
-  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
-  if( Tr.IsNull() ) return Standard_False;
-  Handle(SelectMgr_EntityOwner) EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
-  Handle(StdSelect_BRepOwner) BRO = Handle(StdSelect_BRepOwner)::DownCast(EO);
-  if(BRO.IsNull()) return Standard_False;
-  Standard_Boolean hasshape = BRO->HasShape();
-  Standard_Boolean comes = BRO->ComesFromDecomposition();
-  return (hasshape&&comes);
+  if (!mySelection->More())
+  {
+    return Standard_False;
+  }
+
+  Handle(StdSelect_BRepOwner) aBROwner = Handle(StdSelect_BRepOwner)::DownCast(mySelection->Value());
+  return !aBROwner.IsNull()
+       && aBROwner->HasShape()
+       && aBROwner->ComesFromDecomposition();
 }
 
 //================================================================
@@ -679,32 +697,29 @@ HasShape() const
 //================================================================
 Standard_Boolean AIS_LocalContext::HasSelectedShape() const
 {
-  if (AIS_Selection::CurrentSelection()->Extent() == 0)
-    return Standard_False;
-
-  Handle(Standard_Transient) aCurSelection = AIS_Selection::CurrentSelection()->Value();
-  if (aCurSelection.IsNull())
-    return Standard_False;
-
-  Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aCurSelection);
-  Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
-  if (aBrepOwner.IsNull())
+  if (!mySelection->More())
   {
     return Standard_False;
   }
-  return aBrepOwner->HasShape();
+
+  Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
+  return !aBrepOwner.IsNull()
+      && aBrepOwner->HasShape();
 }
 
 //==================================================
-// Function: 
+// Function: SelectedShape
 // Purpose :
 //==================================================
 TopoDS_Shape AIS_LocalContext::SelectedShape() const 
 {
-  Handle(Standard_Transient) aTr = AIS_Selection::CurrentSelection()->Value();
-  Handle(SelectMgr_EntityOwner) anEO = *((Handle(SelectMgr_EntityOwner)*)&aTr);
-  Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(anEO);
-  if( aBRO.IsNull() ) 
+  if (!mySelection->More())
+  {
+    return TopoDS_Shape();
+  }
+
+  Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(mySelection->Value());
+  if (aBRO.IsNull())
   {
     return TopoDS_Shape();
   }
@@ -713,48 +728,36 @@ TopoDS_Shape AIS_LocalContext::SelectedShape() const
 }
 
 //==================================================
-// Function: 
+// Function: SelectedInteractive
 // Purpose :
 //==================================================
-Handle(AIS_InteractiveObject) AIS_LocalContext::
-SelectedInteractive() const 
+Handle(AIS_InteractiveObject) AIS_LocalContext::SelectedInteractive() const
 {
-  Handle(AIS_InteractiveObject) IO;
-  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
-  if( !Tr.IsNull() ) {
-    Handle(SelectMgr_EntityOwner) EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
-    Handle(SelectMgr_SelectableObject) SO;
-    if(EO->HasSelectable()){
-      SO = EO->Selectable();
-      IO = *((Handle(AIS_InteractiveObject)*)&SO);
-    }
-  }
-  return IO;
+  return !mySelection->More()
+       ?  Handle(AIS_InteractiveObject)()
+       :  Handle(AIS_InteractiveObject)::DownCast (mySelection->Value()->Selectable());
 }
+
 //==================================================
-// Function: 
+// Function: SelectedOwner
 // Purpose :
 //==================================================
-Handle(SelectMgr_EntityOwner) AIS_LocalContext::
-SelectedOwner() const 
+Handle(SelectMgr_EntityOwner) AIS_LocalContext::SelectedOwner() const
 {
-  Handle(SelectMgr_EntityOwner) EO;
-  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
-  if( !Tr.IsNull() )
-       EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
-  return EO;
+  return !mySelection->More()
+       ?  Handle(SelectMgr_EntityOwner)()
+       :  mySelection->Value();
 }
 
 //==================================================
 // Function: 
 // Purpose :
 //==================================================
-Standard_Boolean AIS_LocalContext::
-HasApplicative() const 
+Standard_Boolean AIS_LocalContext::HasApplicative() const
 {
-  Handle(AIS_InteractiveObject) IO = SelectedInteractive();
-  if( IO.IsNull() ) return Standard_False;
-  return IO->HasOwner();
+  Handle(AIS_InteractiveObject) anIO = SelectedInteractive();
+  return !anIO.IsNull()
+       && anIO->HasOwner();
 }
 
 //==================================================
@@ -790,15 +793,13 @@ void AIS_LocalContext::UpdateSelected(const Handle(AIS_InteractiveObject)& anobj
   if (anobj.IsNull() || anobj->IsAutoHilight())
     return;
 
-  AIS_Selection::SetCurrentSelection(mySelName.ToCString());
-  Handle(AIS_Selection) Sel = AIS_Selection::CurrentSelection();
-
   SelectMgr_SequenceOfOwner aSeq;
-  for ( Sel->Init(); Sel->More(); Sel->Next() ){
-    Handle(SelectMgr_EntityOwner) aOwner = Handle(SelectMgr_EntityOwner)::DownCast(Sel->Value());
-
-    if ( !aOwner.IsNull() && aOwner->HasSelectable() && aOwner->Selectable() == anobj )
-      aSeq.Append( aOwner );
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    if (aSelIter.Value()->IsSameSelectable (anobj))
+    {
+      aSeq.Append (aSelIter.Value());
+    }
   }
 
   if ( aSeq.Length() )
@@ -818,19 +819,11 @@ void AIS_LocalContext::UpdateSelected(const Handle(AIS_InteractiveObject)& anobj
 void AIS_LocalContext::ClearSelected (const Standard_Boolean updateviewer)
 {
   UnhilightPicked(updateviewer);
-  AIS_Selection::SetCurrentSelection(mySelName.ToCString());
-
-  Handle(AIS_Selection) Sel = AIS_Selection::CurrentSelection();
-  const AIS_NListTransient& Obj = Sel->Objects();
-  AIS_NListTransient::Iterator anIter( Obj );
-  for(; anIter.More(); anIter.Next()){
-    const Handle(Standard_Transient)& Tr = anIter.Value();
-    if(!Tr.IsNull())
-    {
-      (*((const Handle(SelectMgr_EntityOwner)*)&Tr))->SetSelected (Standard_False);
-    }
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    aSelIter.Value()->SetSelected (Standard_False);
   }
-  AIS_Selection::Select();
+  mySelection->Clear();
   mylastindex = 0;
 }
 
@@ -855,7 +848,7 @@ void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObjec
       continue;
     }
 
-    if (toClearDeactivated && !mySM->IsActivated (theIO, myMainVS, aMode))
+    if (toClearDeactivated && !mySM->IsActivated(theIO, aMode, myMainVS))
     {
       continue;
     }
@@ -863,7 +856,7 @@ void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObjec
     Handle(SelectMgr_Selection) aSelection = theIO->Selection(aMode);
     for (aSelection->Init(); aSelection->More(); aSelection->Next())
     {
-      Handle(SelectBasics_SensitiveEntity) anEntity = aSelection->Sensitive();
+      Handle(SelectBasics_SensitiveEntity) anEntity = aSelection->Sensitive()->BaseSensitive();
       if (anEntity.IsNull())
       {
         continue;
@@ -881,15 +874,19 @@ void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObjec
     }
   }
 
-  // 2. Refresh context's detection and selection and keep only active owners
+  // 2. Refresh context's detection and selection and keep only active owners.
   // Keep last detected object for lastindex initialization.
-  Handle(SelectMgr_EntityOwner) aLastPicked = myMainVS->OnePicked();
+  Handle(SelectMgr_EntityOwner) aLastPicked;
+  if (IsValidIndex (mylastindex))
+  {
+    aLastPicked = myMapOfOwner->FindKey (mylastindex);
+  }
 
   // Remove entity owners from detected sequences
   for (Standard_Integer anIdx = 1; anIdx <= myDetectedSeq.Length(); ++anIdx)
   {
     Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (anIdx));
-    if (anOwner.IsNull() || anOwner->Selectable() != theIO || aValidOwners.Contains (anOwner))
+    if (anOwner.IsNull() || !anOwner->IsSameSelectable (theIO) || aValidOwners.Contains (anOwner))
     {
       continue;
     }
@@ -905,15 +902,13 @@ void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObjec
 
   Standard_Boolean isAISRemainsDetected = Standard_False;
 
-  // 3. Remove entity owners from AIS_Selection
+  // 3. AIS_Selection : remove entity owners from AIS_Selection
   const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
-  Handle(AIS_Selection) aSelection = AIS_Selection::Selection (mySelName.ToCString());
-  AIS_NListTransient::Iterator anIter (aSelection->Objects());
-  AIS_NListTransient aRemoveEntites;
-  for (; anIter.More(); anIter.Next())
+  NCollection_List<Handle(SelectMgr_EntityOwner)> aRemoveEntites;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
   {
-    Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anIter.Value());
-    if (anOwner.IsNull() || anOwner->Selectable() != theIO)
+    Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
+    if (!anOwner->IsSameSelectable (theIO))
     {
       continue;
     }
@@ -922,51 +917,83 @@ void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObjec
     {
       isAISRemainsDetected = Standard_True;
     }
-
-    aRemoveEntites.Append (anOwner);
-    anOwner->SetSelected (Standard_False);
-    for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
+    else
     {
-      Unhilight (anOwner, aViewer->ActiveView());
+      aRemoveEntites.Append (anOwner);
+      anOwner->SetSelected (Standard_False);
+      for (V3d_ListOfViewIterator anActiveViewIter (aViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
+      {
+        Unhilight (anOwner, anActiveViewIter.Value());
+      }
     }
   }
 
-  AIS_NListTransient::Iterator anIterRemove (aRemoveEntites);
-  for (; anIterRemove.More(); anIterRemove.Next())
+  for (NCollection_List<Handle(SelectMgr_EntityOwner)>::Iterator anIterRemove (aRemoveEntites);
+       anIterRemove.More(); anIterRemove.Next())
   {
-    aSelection->Select (anIterRemove.Value());
+    mySelection->Select (anIterRemove.Value());
   }
 
-  // 4. Remove entity owners from myMapOfOwner
+  // 4. AIS_LocalContext - myMapOfOwner : remove entity owners from myMapOfOwner
   SelectMgr_IndexedMapOfOwner anOwnersToKeep;
-  for (Standard_Integer anIdx = 1; anIdx <= myMapOfOwner.Extent(); anIdx++)
+  for (Standard_Integer anIdx = 1; anIdx <= myMapOfOwner->Extent(); anIdx++)
   {
-    Handle(SelectMgr_EntityOwner) anOwner = myMapOfOwner (anIdx);
+    Handle(SelectMgr_EntityOwner) anOwner = myMapOfOwner->FindKey (anIdx);
     if (anOwner.IsNull())
     {
       continue;
     }
 
-    if (anOwner->Selectable() != theIO || aValidOwners.Contains (anOwner))
+    if (!anOwner->IsSameSelectable (theIO) || aValidOwners.Contains (anOwner))
     {
       anOwnersToKeep.Add (anOwner);
     }
     else
     {
-      for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
+      for (V3d_ListOfViewIterator anActiveViewIter (aViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
       {
-        Unhilight (anOwner, aViewer->ActiveView());
+        Unhilight (anOwner, anActiveViewIter.Value());
       }
     }
   }
-  myMapOfOwner.Clear();
-  myMapOfOwner.Assign (anOwnersToKeep);
-  mylastindex = myMapOfOwner.FindIndex (aLastPicked);
-  if (!IsValidIndex (mylastindex))
+  myMapOfOwner->Clear();
+  myMapOfOwner->Assign (anOwnersToKeep);
+
+  if (myDetectedSeq.IsEmpty() && !aLastPicked.IsNull())
   {
     myMainPM->ClearImmediateDraw();
+    mylastindex = 0;
+  }
+  else if (!aLastPicked.IsNull())
+  {
+    // For a case when the last detected owner was unhilighted and removed as outdated we
+    // need to check if there were other detected owners with less priority. If yes then
+    // one from the remaining should be treated.
+    Standard_Integer anIndex = 1, aDetectedSeqLength = myDetectedSeq.Length();
+    for (; anIndex <= aDetectedSeqLength; anIndex++)
+    {
+      if (aLastPicked == myMainVS->Picked (myDetectedSeq.Value(anIndex)))
+      {
+        break; // detected owner was not removed
+      }
+    }
+    if (anIndex <= aDetectedSeqLength)
+    {
+      // Last detected owner was not removed, update mylastindex variable
+      mylastindex = myMapOfOwner->FindIndex (aLastPicked);
+    }
+    else
+    {
+      // Last detected owner was removed. First object from sequence become detected.
+      // Pass any active view because in current implementation the highlighting is
+      // synchronized in all view.
+      manageDetected (myMainVS->Picked (myDetectedSeq.First()),
+                      aViewer->ActiveViewIterator().Value(),
+                      Standard_False);
+    }
   }
 
+  // Renew iterator of ::DetectedCurrentObject()
   if (!isAISRemainsDetected)
   {
     // Remove the interactive object from detected sequences
@@ -1002,27 +1029,20 @@ void AIS_LocalContext::SetSelected(const Handle(AIS_InteractiveObject)& anIObj,
   //1st case, owner already <anIObj> as owner  
   // and not separated is found...
 
-  Handle(AIS_Selection) sel = AIS_Selection::Selection(mySelName.ToCString());
-  //Standard_Boolean found(Standard_False);
-  Handle(Standard_Transient) Tr;
   Handle(SelectMgr_EntityOwner) EO = FindSelectedOwnerFromIO(anIObj);
   if(EO.IsNull()){
-    //check if in selection number 0 there is an owner that can be triturated...
-    if(anIObj->HasSelection(0)){
-      const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
-      SIOBJ->Init();
-      if(SIOBJ->More()){
-       Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->OwnerId();
-       EO = *((Handle(SelectMgr_EntityOwner)*)&BO);
-      }
+    //check if global selection there is an owner that can be triturated...
+    if (anIObj->HasSelection (anIObj->GlobalSelectionMode()))
+    {
+      EO = anIObj->GlobalSelOwner();
     }
     if(EO.IsNull()) 
-      EO = new SelectMgr_EntityOwner(anIObj);
+      EO = new SelectMgr_EntityOwner((const Handle(SelectMgr_SelectableObject)&)anIObj);
   }
   
   ClearSelected(Standard_False);
 
-  AIS_Selection::Select(EO);
+  mySelection->Select(EO);
   EO->SetSelected (Standard_True);
 
   HilightPicked(updateviewer);
@@ -1045,28 +1065,19 @@ void AIS_LocalContext::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)&
 
   if (EO.IsNull())
   {
-    if(anIObj->HasSelection(0))
+    if(anIObj->HasSelection (anIObj->GlobalSelectionMode()))
     {
-      const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
-      SIOBJ->Init();
-      if(SIOBJ->More())
-      {
-        Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->OwnerId();
-        EO = *((Handle(SelectMgr_EntityOwner)*)&BO);
-      }
+      EO = anIObj->GlobalSelOwner();
     }
     if(EO.IsNull())
     {
-      EO = new SelectMgr_EntityOwner(anIObj);
+      EO = new SelectMgr_EntityOwner((const Handle(SelectMgr_SelectableObject)&)anIObj);
     }
   }
-  
-//  cout<<"AIS_LocalContext::AddOrRemoveSelected : Selection = "<<mySelName<<endl;
-  const Handle(AIS_Selection)& S = AIS_Selection::Selection(mySelName.ToCString());
-  
-  if (!S.IsNull())
+
+  if (!mySelection.IsNull())
   {
-    AIS_SelectStatus aStatus = S->Select(EO);
+    AIS_SelectStatus aStatus = mySelection->Select(EO);
     EO->SetSelected (aStatus == AIS_SS_Added);
   }
 
@@ -1084,7 +1095,7 @@ void AIS_LocalContext::AddOrRemoveSelected(const TopoDS_Shape& Sh,
   Handle(SelectMgr_EntityOwner) EO = FindSelectedOwnerFromShape(Sh);
   if (!EO.IsNull())
   {
-    AIS_Selection::Selection(mySelName.ToCString())->Select(EO);
+    mySelection->Select(EO);
     EO->SetSelected (Standard_True);
   }
   HilightPicked (updateviewer);
@@ -1100,7 +1111,7 @@ void AIS_LocalContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)&
 
   Standard_Boolean toSelect = theOwner->IsSelected() ? Standard_False : Standard_True;
 
-  AIS_Selection::Selection (mySelName.ToCString())->Select (theOwner);
+  mySelection->Select(theOwner);
   theOwner->SetSelected (toSelect);
 
   if(myAutoHilight)
@@ -1159,9 +1170,9 @@ void AIS_LocalContext::manageDetected (const Handle(SelectMgr_EntityOwner)& theP
   //
   //=======================================================================================================
 
-  const Standard_Integer aNewIndex = myMapOfOwner.Contains  (thePickOwner)
-                                   ? myMapOfOwner.FindIndex (thePickOwner)
-                                   : myMapOfOwner.Add       (thePickOwner);
+  const Standard_Integer aNewIndex = myMapOfOwner->Contains  (thePickOwner)
+                                   ? myMapOfOwner->FindIndex (thePickOwner)
+                                   : myMapOfOwner->Add       (thePickOwner);
 
   // For the advanced mesh selection mode the owner indices comparison
   // is not effective because in that case only one owner manage the
@@ -1173,9 +1184,9 @@ void AIS_LocalContext::manageDetected (const Handle(SelectMgr_EntityOwner)& theP
   {
     myMainPM->ClearImmediateDraw();
     if (mylastindex != 0
-     && mylastindex <= myMapOfOwner.Extent())
+     && mylastindex <= myMapOfOwner->Extent())
     {
-      const Handle(SelectMgr_EntityOwner)& aLastOwner = myMapOfOwner (mylastindex);
+      const Handle(SelectMgr_EntityOwner)& aLastOwner = myMapOfOwner->FindKey (mylastindex);
       Unhilight (aLastOwner, theView);
     }
 
@@ -1219,14 +1230,13 @@ Standard_Boolean AIS_LocalContext::HasDetectedShape() const
 const TopoDS_Shape&
 AIS_LocalContext::DetectedShape() const
 {
-  static TopoDS_Shape bidsh;
   if(mylastindex != 0)
   {
-    Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(myMapOfOwner(mylastindex));
-    if(BROwnr.IsNull()) return bidsh;
+    Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(myMapOfOwner->FindKey (mylastindex));
+    if(BROwnr.IsNull()) return AIS_myDummyShape;
     return BROwnr->Shape();
   }
-  return bidsh;
+  return AIS_myDummyShape;
 }                                          
 
 //=======================================================================
@@ -1239,8 +1249,8 @@ AIS_LocalContext::DetectedInteractive() const
 {
   Handle(AIS_InteractiveObject) Iobj;
   if(IsValidIndex(mylastindex)){
-    Handle(SelectMgr_SelectableObject) SO = myMapOfOwner.FindKey(mylastindex)->Selectable();
-    Iobj = *((Handle(AIS_InteractiveObject)*) &SO);
+    Handle(SelectMgr_SelectableObject) SO = myMapOfOwner->FindKey(mylastindex)->Selectable();
+    Iobj = Handle(AIS_InteractiveObject)::DownCast (SO);
   }
   return Iobj;
 }
@@ -1252,7 +1262,7 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::DetectedOwner() const
 {
   Handle(SelectMgr_EntityOwner) bid;
   if(!IsValidIndex(mylastindex)) return bid;
-  return myMapOfOwner.FindKey(mylastindex);
+  return myMapOfOwner->FindKey(mylastindex);
 }
 
 
@@ -1263,7 +1273,7 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::DetectedOwner() const
 
 Standard_Boolean AIS_LocalContext::ComesFromDecomposition(const Standard_Integer PickedIndex) const 
 {
-  const Handle(SelectMgr_EntityOwner)& OWN = myMapOfOwner.FindKey(PickedIndex);
+  const Handle(SelectMgr_EntityOwner)& OWN = myMapOfOwner->FindKey(PickedIndex);
   Handle(SelectMgr_SelectableObject) aSel  = OWN->Selectable();
   if (myActiveObjects.IsBound (aSel)) { // debug of jmi
     const Handle(AIS_LocalStatus)& Stat      = myActiveObjects(aSel);    
@@ -1272,27 +1282,6 @@ Standard_Boolean AIS_LocalContext::ComesFromDecomposition(const Standard_Integer
   return Standard_False;
 }
 
-
-//=======================================================================
-//function : DisplayAreas
-//purpose  : 
-//=======================================================================
-
-void AIS_LocalContext::DisplayAreas(const Handle(V3d_View)& aviou)
-{
-    myMainVS->DisplayAreas(aviou);
-}
-
-//=======================================================================
-//function : ClearAreas
-//purpose  : 
-//=======================================================================
-
-void AIS_LocalContext::ClearAreas(const Handle(V3d_View)& aviou)
-{
-    myMainVS->ClearAreas(aviou);
-}
-
 //=======================================================================
 //function : DisplaySensitive
 //purpose  : 
@@ -1320,8 +1309,8 @@ void AIS_LocalContext::ClearSensitive(const Handle(V3d_View)& aviou)
 //=======================================================================
 Standard_Boolean AIS_LocalContext::IsShape(const Standard_Integer Index) const
 {
-  
-  if(Handle(StdSelect_BRepOwner)::DownCast(myMapOfOwner.FindKey(Index)).IsNull())
+  Handle(SelectMgr_EntityOwner) aEO (myMapOfOwner->FindKey(Index));
+  if (aEO.IsNull() || ! aEO->IsKind(STANDARD_TYPE(StdSelect_BRepOwner)))
     return Standard_False;
   return 
     ComesFromDecomposition(Index);
@@ -1329,12 +1318,12 @@ Standard_Boolean AIS_LocalContext::IsShape(const Standard_Integer Index) const
 
 Standard_Boolean AIS_LocalContext::IsValidForSelection(const Handle(AIS_InteractiveObject)& anIObj) const 
 {
-
+  const Handle(SelectMgr_SelectableObject)& aSelObj = anIObj; // to avoid ambiguity
   // Shape was not transfered from AIS_Shape to EntityOwner
   Handle(AIS_Shape) shape = Handle(AIS_Shape)::DownCast(anIObj);
   if( !shape.IsNull() ) 
-    return myFilters->IsOk(new StdSelect_BRepOwner(shape->Shape(),shape));
-  return myFilters->IsOk(new SelectMgr_EntityOwner(anIObj));
+    return myFilters->IsOk(new StdSelect_BRepOwner(shape->Shape(), aSelObj));
+  return myFilters->IsOk(new SelectMgr_EntityOwner(aSelObj));
 }
 
 
@@ -1404,13 +1393,13 @@ Standard_Boolean AIS_LocalContext::UnhilightLastDetected (const Handle(V3d_View)
   }
 
   myMainPM->BeginImmediateDraw();
-  const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner (mylastindex);
+  const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner->FindKey (mylastindex);
   const Standard_Integer aHilightMode = anOwner->HasSelectable()
                                       ? GetHiMod (Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable()))
                                       : 0;
 
-  myMapOfOwner (mylastindex)->Unhilight (myMainPM, aHilightMode);
-  myMainPM->EndImmediateDraw (theView);
+  myMapOfOwner->FindKey (mylastindex)->Unhilight (myMainPM, aHilightMode);
+  myMainPM->EndImmediateDraw (theView->Viewer());
   mylastindex = 0;
   return Standard_True;
 }
@@ -1419,40 +1408,30 @@ Standard_Boolean AIS_LocalContext::UnhilightLastDetected (const Handle(V3d_View)
 //function : FindSelectedOwnerFromIO
 //purpose  : it is checked if one of the selected owners really presents IObj
 //=======================================================================
-Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromIO
-                          (const Handle(AIS_InteractiveObject)& anIObj) const 
+Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromIO (const Handle(AIS_InteractiveObject)& theObj) const
 {
   Handle(SelectMgr_EntityOwner) EO,bid;
-  if (anIObj.IsNull()) return EO;
-  
-  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
-  if(Sel.IsNull()) {
-#ifdef OCCT_DEBUG
-    cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "
-        <<mySelName<<" Nulle "<<endl;
-#endif
-    return EO;
+  if (theObj.IsNull()
+   || mySelection.IsNull())
+  {
+    return Handle(SelectMgr_EntityOwner)();
   }
-  Standard_Boolean found(Standard_False);
-  const AIS_NListTransient& Obj = Sel->Objects();
-  AIS_NListTransient::Iterator anIter( Obj );
-  for(; anIter.More(); anIter.Next()){
-    const Handle(Standard_Transient)& Tr = anIter.Value();
-    if(!Tr.IsNull()){
-      EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
-      if(EO->HasSelectable()){
-       Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(EO);
-       if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
-         if (anIObj == EO->Selectable()){
-           found =Standard_True;
-           break;
-         }
-       }
-      }
+
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    if (!aSelIter.Value()->IsSameSelectable (theObj))
+    {
+      continue;
+    }
+
+    Handle(StdSelect_BRepOwner) aBROwner = Handle(StdSelect_BRepOwner)::DownCast(aSelIter.Value());
+    if (aBROwner.IsNull()
+    || !aBROwner->ComesFromDecomposition())
+    {
+      return aSelIter.Value();
     }
   }
-  if(found)  return EO;
-  return bid;
+  return Handle(SelectMgr_EntityOwner)();
 }
 
 //=======================================================================
@@ -1464,20 +1443,18 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromShape(const
   Handle(SelectMgr_EntityOwner) EO, bid;
   if (sh.IsNull()) return EO;
   
-  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
-  if(Sel.IsNull()) {
-#ifdef OCCT_DEBUG
-    cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "<<mySelName<<" Nulle "<<endl;
-#endif
+  if(mySelection.IsNull()) {
     return EO;
   }
   
   Standard_Boolean found(Standard_False);
 
   if (!found) {
-    SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive aSensitiveIt (myMainVS->Primitives());
-    for (; aSensitiveIt.More(); aSensitiveIt.Next()) {
-      EO = Handle(SelectMgr_EntityOwner)::DownCast (aSensitiveIt.Value()->OwnerId());
+    NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
+    myMainVS->ActiveOwners (anActiveOwners);
+    for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners); anOwnersIt.More(); anOwnersIt.Next())
+    {
+      EO = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
       Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(EO);
       if (!BROwnr.IsNull() && BROwnr->HasShape() && BROwnr->Shape() == sh) {
         found = Standard_True;
@@ -1523,13 +1500,11 @@ void AIS_LocalContext::NextDetected()
 //=======================================================================
 const TopoDS_Shape& AIS_LocalContext::DetectedCurrentShape() const
 {
-  static TopoDS_Shape aDummyShape;
-
   Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
 
   if (aCurrentShape.IsNull())
   {
-    return aDummyShape;
+    return AIS_myDummyShape;
   }
 
   return aCurrentShape->Shape();
@@ -1542,3 +1517,19 @@ Handle(AIS_InteractiveObject) AIS_LocalContext::DetectedCurrentObject() const
 {
   return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
 }
+
+//=======================================================================
+//function : RestoreActivatedModes
+//purpose  :
+//=======================================================================
+void AIS_LocalContext::RestoreActivatedModes() const
+{
+  for (AIS_DataMapOfSelStat::Iterator anIter (myActiveObjects); anIter.More(); anIter.Next())
+  {
+    const TColStd_ListOfInteger& anActivatedModes = anIter.Value()->SelectionModes();
+    for (TColStd_ListIteratorOfListOfInteger aModesIter (anActivatedModes); aModesIter.More(); aModesIter.Next())
+    {
+      mySM->Activate (anIter.Key(), aModesIter.Value(), myMainVS);
+    }
+  }
+}