0026172: Visualization, AIS_LocalContext - locally selected object should not stay...
[occt.git] / src / AIS / AIS_LocalContext_1.cxx
index 6ef3585..04c094d 100644 (file)
 //              - use of optimisation in SelectMgr_ViewerSelector
 //              -> Best management in detected entities...
 
-#define BUC60569       //GG_051199 Enable to select the local context 
-//                     in any case and especially in multi selection mode.
-//                     Note that right now when an hilighted owner is selected
-//                     this owner is unhilighted,this permits to see the selection!
-//                     Principle : an owner can have 3 state :
-//                     1 : The owner is selected and no more highlightable
-//                     0 : The owner is NOT selected
-//                     -1: The owner is selected but stay highlightable (NEW)
-
-// IMP230600   //GG Add protection on selection methodes
-//                     when nothing is selected
-
-#define BUC60726        //GG_040900 When nothing is detected, 
-//                     Clear the last temporary stuff in any case
-
-#define BUC60765       //GG_121000 Avoid to raise when the same selection 
-//                     is attached to several local context.
-
-#define BUC60771       //GG_261000     Avoid to crash after closing a view
-//                     containing a selected entity and creating a new one.
-
-#define BUC60774       //GG_261000     Returns right select status on
-//                     bounding-box selection type.
-
-#define BUC60818       //GG_300101     Enable detection even if
-//                     SetAutomaticHilight(FALSE) has been used.
-
-#define IMP300101       //GG Enable to use polygon highlighting
-
-#define BUC60876       //GG_050401 Clear selection always even
-//                     if the current highlight mode is not 0.
-
-#define BUC60953        //SAV_060701 For Select optimization. Selection by rectangle case.
-// for single selection no optimization done.
-
-#define IMP120701      //SZV made a shape valid for selection
-//                     when required.
-
-#define IMP160701       //SZV Add InitDetected(),MoreDetected(),NextDetected(),
-//                       DetectedCurrentShape(),DetectedCurrentObject()
-//                       methods
-
-#define OCC138          //VTN Avoding infinit loop in AddOrRemoveSelected method.
-
-#define OCC189          //SAV: 18/03/02 AIS_Selection::Objects() returns ListOfTransient
-// instead of array.
-
-#define USE_MAP         //san : 18/04/03 USE_MAP - additional datamap is used to speed up access 
-//to certain owners in AIS_Selection::myresult list  
-
-#define OCC9026                //AEL Performance optimization of the FindSelectedOwnerFromShape() method.
-
 #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_LocalStatus.hxx>
-#include <StdPrs_WFShape.hxx>
 #include <Graphic3d_ArrayOfTriangles.hxx>
 #include <Graphic3d_Group.hxx>
 #include <Select3D_SensitiveTriangulation.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
 #include <SelectBasics_SensitiveEntity.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <NCollection_Map.hxx>
+#include <Visual3d_View.hxx>
 
-#ifdef OCC9026
-#include <SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive.hxx>
-#endif
 #include <SelectMgr_Selection.hxx>
 #include <SelectMgr_SequenceOfOwner.hxx>
 #include <OSD_Environment.hxx>
 #include <Geom_Transformation.hxx>
 #include <AIS_Selection.hxx>
 #include <Aspect_Grid.hxx>
-#ifdef IMP120701
 #include <AIS_Shape.hxx>
-#endif
-
 
 static Standard_Integer GetHiMod(const Handle(AIS_InteractiveObject)& IO)
 {
@@ -126,6 +71,7 @@ AIS_StatusOfDetection AIS_LocalContext::MoveTo (const Standard_Integer  theXpix,
 
   myCurDetected = 0;
   myDetectedSeq.Clear();
+  myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
   myMainVS->Pick (theXpix, theYpix, theView);
 
   const Standard_Integer aDetectedNb = myMainVS->NbPicked();
@@ -151,6 +97,7 @@ AIS_StatusOfDetection AIS_LocalContext::MoveTo (const Standard_Integer  theXpix,
   {
     if (mylastindex != 0 && mylastindex <= myMapOfOwner.Extent())
     {
+      myMainPM->ClearImmediateDraw();
       Unhilight (myMapOfOwner (mylastindex), theView);
       if (theToRedrawImmediate)
       {
@@ -374,6 +321,7 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Boolean toUpdateV
 
     if(myAutoHilight)
     {
+      myMainPM->ClearImmediateDraw();
       const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
       for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
       {
@@ -393,12 +341,10 @@ AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Boolean toUpdateV
       }
     } 
 
-#ifdef BUC60774
     Standard_Integer NS = AIS_Selection::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;
-#endif
   }
   return AIS_SOP_Error;
 }
@@ -543,7 +489,6 @@ void AIS_LocalContext::Unhilight (const Handle(SelectMgr_EntityOwner)& theOwner,
     return;
   }
 
-  myMainPM->ClearImmediateDraw();
   const Standard_Integer aHilightMode = GetHiMod (Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()));
   if (IsSelected (theOwner))
   {
@@ -564,12 +509,8 @@ void AIS_LocalContext::Unhilight (const Handle(SelectMgr_EntityOwner)& theOwner,
 //=======================================================================
 void AIS_LocalContext::HilightPicked(const Standard_Boolean updateviewer)
 {
-  Standard_Boolean updMain(Standard_False);
-
   Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
-#ifdef BUC60765
   if( Sel.IsNull() ) return;
-#endif
 
   typedef NCollection_DataMap <Handle(SelectMgr_SelectableObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > SelectMgr_DataMapOfObjectOwners;
   SelectMgr_DataMapOfObjectOwners aMap;
@@ -577,18 +518,11 @@ void AIS_LocalContext::HilightPicked(const Standard_Boolean updateviewer)
   Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
   
   // to avoid problems when there is a loop searching for selected objects...
-#if !defined OCC189 && !defined USE_MAP
-  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
-  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++)
-  {
-    const Handle(Standard_Transient)& Tr = Obj(i);
-#else
   const AIS_NListTransient& Obj = Sel->Objects();
   AIS_NListTransient::Iterator anIter( Obj );
   for(; anIter.More(); anIter.Next())
   {
     const Handle(Standard_Transient)& Tr = anIter.Value();
-#endif
     if(!Tr.IsNull()){
       const Handle(SelectMgr_EntityOwner)& Ownr =
         *((const Handle(SelectMgr_EntityOwner)*) &Tr);
@@ -598,13 +532,8 @@ void AIS_LocalContext::HilightPicked(const Standard_Boolean updateviewer)
        if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
          Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
          IO = *((Handle(AIS_InteractiveObject)*)&SO);
-         updMain = Standard_True;
        }
-       else
-         updMain = Standard_True;
       }
-      else
-       updMain = Standard_True;
       Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
       Standard_Integer HM = GetHiMod(*((Handle(AIS_InteractiveObject)*)&SO));
       if ( Ownr->IsAutoHilight() )
@@ -639,48 +568,28 @@ void AIS_LocalContext::UnhilightPicked (const Standard_Boolean updateviewer)
 {
   myMainPM->ClearImmediateDraw();
 
-  Standard_Boolean updMain(Standard_False);
-
   Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
-#ifdef BUC60765
   if( Sel.IsNull() ) return;
-#endif
   Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
   NCollection_Map<Handle(SelectMgr_SelectableObject)> anObjMap;
   
-#if !defined OCC189 && !defined USE_MAP  
-  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
-  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
-    const Handle(Standard_Transient)& Tr = Obj(i);
-#else
   const AIS_NListTransient& Obj = Sel->Objects();
   AIS_NListTransient::Iterator anIter( Obj );
   for(; anIter.More(); anIter.Next()){
     const Handle(Standard_Transient)& Tr = anIter.Value();
-#endif
     if(!Tr.IsNull()){
       const Handle(SelectMgr_EntityOwner)& Ownr =
         *((const Handle(SelectMgr_EntityOwner)*) &Tr);
       Standard_Integer HM(0);
       if(Ownr->HasSelectable()){
-#ifdef BUC60876
        Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
        Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&SO);
         anObjMap.Add (IO);
 
         HM = GetHiMod(IO);
-#endif
        Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
        if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
-#ifndef BUC60876
-         Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
-         Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&SO);
-         HM = GetHiMod(IO);
-#endif
-         updMain = Standard_True;
        }
-       else
-         updMain = Standard_True;
       }
       Ownr->Unhilight(PM,HM);
     }
@@ -693,14 +602,8 @@ void AIS_LocalContext::UnhilightPicked (const Standard_Boolean updateviewer)
       anIter1.Key()->ClearSelected();
   }
 
-  if(updateviewer){
-#ifdef BUC60774
+  if(updateviewer)
     myCTX->CurrentViewer()->Update();
-#else
-    if(updMain) myCTX->CurrentViewer()->Update();
-#endif
-  }
-  
 }
 
 //=======================================================================
@@ -770,6 +673,28 @@ HasShape() const
   return (hasshape&&comes);
 }
 
+//================================================================
+// Function : HasSelectedShape
+// Purpose  : Checks if there is a selected shape regardless of its decomposition status
+//================================================================
+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())
+  {
+    return Standard_False;
+  }
+  return aBrepOwner->HasShape();
+}
+
 //==================================================
 // Function: 
 // Purpose :
@@ -783,6 +708,7 @@ TopoDS_Shape AIS_LocalContext::SelectedShape() const
   {
     return TopoDS_Shape();
   }
+
   return aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
 }
 
@@ -895,16 +821,10 @@ void AIS_LocalContext::ClearSelected (const Standard_Boolean updateviewer)
   AIS_Selection::SetCurrentSelection(mySelName.ToCString());
 
   Handle(AIS_Selection) Sel = AIS_Selection::CurrentSelection();
-#if !defined OCC189 && !defined USE_MAP   
-  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
-  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
-    const Handle(Standard_Transient)& Tr = Obj(i);
-#else
   const AIS_NListTransient& Obj = Sel->Objects();
   AIS_NListTransient::Iterator anIter( Obj );
   for(; anIter.More(); anIter.Next()){
     const Handle(Standard_Transient)& Tr = anIter.Value();
-#endif
     if(!Tr.IsNull())
     {
       (*((const Handle(SelectMgr_EntityOwner)*)&Tr))->SetSelected (Standard_False);
@@ -915,28 +835,78 @@ void AIS_LocalContext::ClearSelected (const Standard_Boolean updateviewer)
 }
 
 //==================================================
-// Function: ClearSelected
+// Function: ClearOutdatedSelection
 // Purpose :
 //==================================================
-void AIS_LocalContext::ClearSelected (const Handle(AIS_InteractiveObject)& theIO,
-                                      const Standard_Boolean toUpdateViewer)
+void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObject)& theIO,
+                                               const Standard_Boolean toClearDeactivated)
 {
+  // 1. Collect selectable entities
+  SelectMgr_IndexedMapOfOwner aValidOwners;
+
+  const TColStd_ListOfInteger& aModes = SelectionModes (theIO);
+
+  TColStd_ListIteratorOfListOfInteger aModeIter (aModes);
+  for (; aModeIter.More(); aModeIter.Next())
+  {
+    int aMode = aModeIter.Value();
+    if (!theIO->HasSelection(aMode))
+    {
+      continue;
+    }
+
+    if (toClearDeactivated && !mySM->IsActivated(theIO, aMode, myMainVS))
+    {
+      continue;
+    }
+
+    Handle(SelectMgr_Selection) aSelection = theIO->Selection(aMode);
+    for (aSelection->Init(); aSelection->More(); aSelection->Next())
+    {
+      Handle(SelectBasics_SensitiveEntity) anEntity = aSelection->Sensitive()->BaseSensitive();
+      if (anEntity.IsNull())
+      {
+        continue;
+      }
+
+      Handle(SelectMgr_EntityOwner) anOwner =
+        Handle(SelectMgr_EntityOwner)::DownCast (anEntity->OwnerId());
+
+      if (anOwner.IsNull())
+      {
+        continue;
+      }
+
+      aValidOwners.Add(anOwner);
+    }
+  }
+
+  // 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();
 
-  // Remove the interactive object from detected sequence
-  for (Standard_Integer anIdx = 1; anIdx <= myAISDetectedSeq.Length(); ++anIdx)
+  // Remove entity owners from detected sequences
+  for (Standard_Integer anIdx = 1; anIdx <= myDetectedSeq.Length(); ++anIdx)
   {
-    Handle(AIS_InteractiveObject) aDetectedIO = myAISDetectedSeq.Value (anIdx);
-    if (!aDetectedIO.IsNull() && aDetectedIO == theIO)
+    Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (anIdx));
+    if (anOwner.IsNull() || anOwner->Selectable() != theIO || aValidOwners.Contains (anOwner))
     {
-      myAISDetectedSeq.Remove (anIdx--);
+      continue;
+    }
+
+    myDetectedSeq.Remove (anIdx--);
+
+    if (anIdx < myCurDetected)
+    {
+      myCurDetected--;
     }
   }
+  myCurDetected = Max (myCurDetected, 1);
 
-  Standard_Integer aHilightMode = theIO->HasHilightMode() ? theIO->HilightMode() : 0;
+  Standard_Boolean isAISRemainsDetected = Standard_False;
 
-  // 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;
@@ -948,11 +918,18 @@ void AIS_LocalContext::ClearSelected (const Handle(AIS_InteractiveObject)& theIO
       continue;
     }
 
-    aRemoveEntites.Append (anOwner);
-
-    if (IsSelected (anOwner))
+    if (aValidOwners.Contains (anOwner))
+    {
+      isAISRemainsDetected = Standard_True;
+    }
+    else
     {
-      anOwner->Unhilight (myMainPM, aHilightMode);
+      aRemoveEntites.Append (anOwner);
+      anOwner->SetSelected (Standard_False);
+      for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
+      {
+        Unhilight (anOwner, aViewer->ActiveView());
+      }
     }
   }
   AIS_NListTransient::Iterator anIterRemove (aRemoveEntites);
@@ -961,9 +938,8 @@ void AIS_LocalContext::ClearSelected (const Handle(AIS_InteractiveObject)& theIO
     aSelection->Select (anIterRemove.Value());
   }
 
-  // Remove entity owners from myMapOfOwner
+  // 4. AIS_LocalContext - myMapOfOwner : remove entity owners from myMapOfOwner
   SelectMgr_IndexedMapOfOwner anOwnersToKeep;
-  const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
   for (Standard_Integer anIdx = 1; anIdx <= myMapOfOwner.Extent(); anIdx++)
   {
     Handle(SelectMgr_EntityOwner) anOwner = myMapOfOwner (anIdx);
@@ -972,7 +948,7 @@ void AIS_LocalContext::ClearSelected (const Handle(AIS_InteractiveObject)& theIO
       continue;
     }
 
-    if (anOwner->Selectable() != theIO)
+    if (anOwner->Selectable() != theIO || aValidOwners.Contains (anOwner))
     {
       anOwnersToKeep.Add (anOwner);
     }
@@ -987,10 +963,30 @@ void AIS_LocalContext::ClearSelected (const Handle(AIS_InteractiveObject)& theIO
   myMapOfOwner.Clear();
   myMapOfOwner.Assign (anOwnersToKeep);
   mylastindex = myMapOfOwner.FindIndex (aLastPicked);
+  if (!IsValidIndex (mylastindex))
+  {
+    myMainPM->ClearImmediateDraw();
+  }
 
-  if (toUpdateViewer)
+  if (!isAISRemainsDetected)
   {
-    aViewer->Update();
+    // Remove the interactive object from detected sequences
+    for (Standard_Integer anIdx = 1; anIdx <= myAISDetectedSeq.Length(); ++anIdx)
+    {
+      Handle(AIS_InteractiveObject) aDetectedIO = myAISDetectedSeq.Value (anIdx);
+      if (aDetectedIO.IsNull() || aDetectedIO != theIO)
+      {
+        continue;
+      }
+
+      myAISDetectedSeq.Remove (anIdx--);
+
+      if (anIdx < myAISCurDetected)
+      {
+        myAISCurDetected--;
+      }
+    }
+    myAISCurDetected = Max (myAISCurDetected, 1);
   }
 }
 
@@ -1017,7 +1013,7 @@ void AIS_LocalContext::SetSelected(const Handle(AIS_InteractiveObject)& anIObj,
       const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
       SIOBJ->Init();
       if(SIOBJ->More()){
-       Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->OwnerId();
+        Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->BaseSensitive()->OwnerId();
        EO = *((Handle(SelectMgr_EntityOwner)*)&BO);
       }
     }
@@ -1054,10 +1050,9 @@ void AIS_LocalContext::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)&
     {
       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);
+      if(SIOBJ->More()){
+        Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->BaseSensitive()->OwnerId();
+       EO = *((Handle(SelectMgr_EntityOwner)*)&BO);
       }
     }
     if(EO.IsNull())
@@ -1124,6 +1119,7 @@ void AIS_LocalContext::manageDetected (const Handle(SelectMgr_EntityOwner)& theP
 {
   if (thePickOwner.IsNull())
   {
+    myMainPM->ClearImmediateDraw();
     if (theToRedrawImmediate)
     {
       theView->RedrawImmediate();
@@ -1175,6 +1171,7 @@ void AIS_LocalContext::manageDetected (const Handle(SelectMgr_EntityOwner)& theP
   if (aNewIndex != mylastindex
    || thePickOwner->IsForcedHilight())
   {
+    myMainPM->ClearImmediateDraw();
     if (mylastindex != 0
      && mylastindex <= myMapOfOwner.Extent())
     {
@@ -1197,7 +1194,7 @@ void AIS_LocalContext::manageDetected (const Handle(SelectMgr_EntityOwner)& theP
     mylastindex = aNewIndex;
   }
 
-  if (mylastindex)
+  if (mylastindex != 0)
   {
     mylastgood = mylastindex;
   }
@@ -1275,27 +1272,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  : 
@@ -1333,12 +1309,10 @@ Standard_Boolean AIS_LocalContext::IsShape(const Standard_Integer Index) const
 Standard_Boolean AIS_LocalContext::IsValidForSelection(const Handle(AIS_InteractiveObject)& anIObj) const 
 {
 
-#ifdef IMP120701
   // 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));
-#endif
   return myFilters->IsOk(new SelectMgr_EntityOwner(anIObj));
 }
 
@@ -1361,7 +1335,7 @@ Standard_Integer AIS_LocalContext::HilightNextDetected (const Handle(V3d_View)&
   {
     myCurDetected = 1;
   }
-  Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myCurDetected);
+  Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (myCurDetected));
   if (anOwner.IsNull())
   {
     return 0;
@@ -1382,11 +1356,12 @@ Standard_Integer AIS_LocalContext::HilightPreviousDetected (const Handle(V3d_Vie
     return 0;
   }
 
+  const Standard_Integer aLen = myDetectedSeq.Length();
   if (--myCurDetected < 1)
   {
-    myCurDetected = 1;
+    myCurDetected = aLen;
   }
-  Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myCurDetected);
+  Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (myCurDetected));
   if (anOwner.IsNull())
   {
     return 0;
@@ -1431,23 +1406,17 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromIO
   
   Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
   if(Sel.IsNull()) {
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "
         <<mySelName<<" Nulle "<<endl;
 #endif
     return EO;
   }
   Standard_Boolean found(Standard_False);
-#if !defined OCC189 && !defined USE_MAP     
-  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
-  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
-    const Handle(Standard_Transient)& Tr = Obj(i);
-#else
   const AIS_NListTransient& Obj = Sel->Objects();
   AIS_NListTransient::Iterator anIter( Obj );
   for(; anIter.More(); anIter.Next()){
     const Handle(Standard_Transient)& Tr = anIter.Value();
-#endif
     if(!Tr.IsNull()){
       EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
       if(EO->HasSelectable()){
@@ -1471,16 +1440,12 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromIO
 //=======================================================================
 Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromShape(const TopoDS_Shape& sh) const 
 {
-#ifdef OCC9026
   Handle(SelectMgr_EntityOwner) EO, bid;
-#else
-  Handle(SelectMgr_EntityOwner) EO;
-#endif
   if (sh.IsNull()) return EO;
   
   Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
   if(Sel.IsNull()) {
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "<<mySelName<<" Nulle "<<endl;
 #endif
     return EO;
@@ -1488,11 +1453,10 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromShape(const
   
   Standard_Boolean found(Standard_False);
 
-#ifdef OCC9026
   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)>::Iterator anOwnersIt (myMainVS->ActiveOwners());
+    for (; 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;
@@ -1500,33 +1464,11 @@ Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromShape(const
       }
     }
   }
-#else
-#if !defined OCC189 && !defined USE_MAP   
-  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
-  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
-    const Handle(Standard_Transient)& Tr = Obj(i);
-#else
-  const AIS_NListTransient& Obj = Sel->Objects();
-  AIS_NListTransient::Iterator anIter( Obj );
-  for(; anIter.More(); anIter.Next()){
-    const Handle(Standard_Transient)& Tr = anIter.Value();
-#endif
-    if(!Tr.IsNull()){
-      
-      EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
-      if(EO->HasShape())
-       if ( EO->Shape() == sh)
-         found =Standard_True;
-          break;
-    }
-  }
-#endif
 
   if(found)  return EO;
   return bid;
 }
 
-#ifdef IMP160701
 //=======================================================================
 //function : AIS_LocalContext::InitDetected
 //purpose  :
@@ -1579,4 +1521,3 @@ Handle(AIS_InteractiveObject) AIS_LocalContext::DetectedCurrentObject() const
 {
   return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
 }
-#endif