0024023: Revamp the OCCT Handle -- downcast (automatic)
[occt.git] / src / AIS / AIS_LocalContext.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 4997c38..b9f6b74
@@ -1,39 +1,21 @@
 // Created on: 1997-01-17
 // Created by: Robert COUBLANC
 // Copyright (c) 1997-1999 Matra Datavision
-// Copyright (c) 1999-2012 OPEN CASCADE SAS
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 //Modified by ROB : Traque des UpdateConversion intempestifs.
 
-#define BUC60688       //GG 25/05/00 Add SetSensitivity() methods.
-
-#define BUC60722        //GG_040900 Disable detection on an unviewable object
-
-#define IMP160701      //SZV Add InitDetected(),MoreDetected(),NextDetected(),
-//                       DetectedCurrentShape(),DetectedCurrentObject()
-//                       methods
-
-#define ALE70590       //GG  Avoid raise especially under W2000-SP2
-//             when opening many local context due to a
-//             system error in the selection name computation routine.
-//             Many thanks to Philippe CARRET for the helpfull he has 
-//             give to accelerate the resolution of this problem.
-
 #include <AIS_LocalContext.ixx>
 #include <SelectMgr_OrFilter.hxx>
 #include <SelectMgr_CompositionFilter.hxx>
 #include <Prs3d_Presentation.hxx>
 #include <Aspect_TypeOfMarker.hxx>
 #include <StdSelect_ShapeTypeFilter.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
 #include <AIS_Selection.hxx>
 #include <V3d_Viewer.hxx>
 #include <V3d_View.hxx>
-#include <Visual3d_TransientManager.hxx>
 #include <Visual3d_View.hxx>
+#include <AIS_InteractiveObject.hxx>
 
-#ifdef ALE70590
 #include <stdio.h>
-#else
-#include <Standard_SStream.hxx>
-#endif
 
 static TCollection_AsciiString AIS_Local_SelName(const Standard_Address address,
                                                  const Standard_Integer anIndex)
 {
 //  TCollection_AsciiString SelName;
-#ifdef ALE70590
   char string[100];
   sprintf(string,"%p_%d", address, anIndex);   // works under any system 
   TCollection_AsciiString SelName(string);
-#else
-  Standard_SStream stream;
-  stream<<address;     // something is wrong here using the SStream because
-//             the following access to rdbuf crash for an unknown reason 
-//             especially under W2000 with SP2 and sometime under WNT and W98.
-//             NOTE that stream is not ended by a NULL char and it's probably
-//             one of the reasons why this crash.
-//             In any case the resulting ascii string give a wrong and random
-//             name under WINDOWS !
-  TCollection_AsciiString SelName(stream.rdbuf()->str());
-//  SelName = TCollection_AsciiString("AIS_Local_");
-  TCollection_AsciiString theind(anIndex);
-  SelName += "_";
-  SelName += theind;
-#endif
   return SelName;
 }
 
@@ -109,28 +72,29 @@ myLoadDisplayed(LoadDisplayed),
 myAcceptStdMode(AcceptStandardModes),
 myAcceptErase(AcceptEraseOfTemp),
 mySM(aCtx->SelectionManager()),
-myMainVS(new StdSelect_ViewerSelector3d(aCtx->MainSelector()->Projector())),
+myMainVS(aCtx->MainSelector()),
 myFilters(new SelectMgr_OrFilter()),
 myAutoHilight(Standard_True),
+myMapOfOwner (new SelectMgr_IndexedMapOfOwner()),
 mylastindex(0),
 mylastgood(0),
-myCurDetected(0)
-#ifdef IMP160701
-,myAISCurDetected(0)
-#endif
+myCurDetected(0),
+myAISCurDetected(0)
+
 {
   // bind self to AIS_InteractiveContext::myLocalContexts. Further, the
   // constructor executes logic that implies that the context is already
   // created and mapped.
   aCtx->myLocalContexts.Bind (Index, this);
 
+  myMainVS->ResetSelectionActivationStatus();
   myMainPM = aCtx->MainPrsMgr();
   mySelName = AIS_Local_SelName(this, Index);
   AIS_Selection::CreateSelection(mySelName.ToCString());
 
   mySM->Add(myMainVS);
   if(myLoadDisplayed) LoadContextObjects();
-  Process(Standard_False);
+  Process();
 
 }
 
@@ -235,7 +199,24 @@ Load(const Handle(AIS_InteractiveObject)& anInteractive,
      const Standard_Boolean AllowShapeDecomposition,
      const Standard_Integer ActivationMode)
 {
-  if(myActiveObjects.IsBound(anInteractive)) return Standard_False;
+  if (myActiveObjects.IsBound (anInteractive))
+  {
+    if (anInteractive->HasSelection (ActivationMode))
+    {
+      const Handle(SelectMgr_Selection)& aSel = anInteractive->Selection (ActivationMode);
+      if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
+      {
+        if (!myMainVS->Contains (anInteractive))
+        {
+          mySM->Load (anInteractive, myMainVS);
+        }
+        mySM->Activate (anInteractive, ActivationMode, myMainVS);
+        return Standard_True;
+      }
+    }
+    return Standard_False;
+  }
+
   Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
   
   if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
@@ -317,20 +298,27 @@ Erase(const Handle(AIS_InteractiveObject)& anInteractive)
       AddOrRemoveSelected(anInteractive);
     if(myMainPM->IsHighlighted(anInteractive,STAT->HilightMode()))
       myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
-    myMainPM->Erase(anInteractive,STAT->DisplayMode());
+    myMainPM->SetVisibility (anInteractive, STAT->DisplayMode(), Standard_False);
     STAT->SetDisplayMode(-1);
     status = Standard_True;
   }
   if(STAT->IsTemporary()){
     if(myMainPM->IsDisplayed(anInteractive,STAT->HilightMode()))
-      myMainPM->Erase(anInteractive,STAT->HilightMode());
+      myMainPM->SetVisibility (anInteractive, STAT->HilightMode(), Standard_False);
   }
-  //selection step
-  
-  TColStd_ListIteratorOfListOfInteger It(STAT->SelectionModes());
-  for(;It.More();It.Next())
-    mySM->Deactivate(anInteractive,It.Value(),myMainVS);
-  //  STAT->ClearSelectionModes();
+
+  // Deactivate selectable entities of interactive object
+  if (mySM->Contains (anInteractive))
+  {
+    TColStd_ListIteratorOfListOfInteger aModeIter (STAT->SelectionModes());
+    for (; aModeIter.More(); aModeIter.Next())
+    {
+      mySM->Deactivate (anInteractive, aModeIter.Value(), myMainVS);
+    }
+  }
+
+  ClearOutdatedSelection (anInteractive, Standard_True);
+
   return status;
 }
 
@@ -384,7 +372,6 @@ void AIS_LocalContext::Clear(const AIS_ClearMode aType)
   case AIS_CM_TemporaryShapePrs:
     ClearDetected();
   }
-  UpdateSort();
 }
 //=======================================================================
 //function : ActivateMode
@@ -401,7 +388,6 @@ void AIS_LocalContext::ActivateMode(const Handle(AIS_InteractiveObject)& aSelect
     myActiveObjects(aSelectable)->AddSelectionMode(aMode);
     mySM->Activate(aSelectable,aMode,myMainVS);
   }
-  UpdateSort();
 }
 //=======================================================================
 //function : ActivateMode
@@ -417,8 +403,6 @@ void AIS_LocalContext::DeactivateMode(const Handle(AIS_InteractiveObject)& aSele
   
   myActiveObjects(aSelectable)->RemoveSelectionMode(aMode);
   mySM->Deactivate(aSelectable,aMode,myMainVS);
-  UpdateSort();
-  
 }
 //=======================================================================
 //function : ActivateMode
@@ -429,9 +413,8 @@ void AIS_LocalContext::Deactivate(const Handle(AIS_InteractiveObject)& aSelectab
 {
   if(!myActiveObjects.IsBound(aSelectable)) return;
   
-  mySM->Deactivate(aSelectable,myMainVS);
+  mySM->Deactivate(aSelectable, -1, myMainVS);
   myActiveObjects(aSelectable)->ClearSelectionModes();
-  UpdateSort();
 }
 
 //=======================================================================
@@ -441,119 +424,66 @@ void AIS_LocalContext::Deactivate(const Handle(AIS_InteractiveObject)& aSelectab
 
 Standard_Boolean AIS_LocalContext::Remove(const Handle(AIS_InteractiveObject)& aSelectable)
 {
-  if(!myActiveObjects.IsBound(aSelectable)) return Standard_False;
+  if(!myActiveObjects.IsBound (aSelectable))
+  {
+    return Standard_False;
+  }
+
+  if (IsSelected (aSelectable))
+  {
+    AddOrRemoveSelected (aSelectable, Standard_False);
+  }
+
+  const Handle(AIS_LocalStatus)& Att = myActiveObjects (aSelectable);
 
-  if(IsSelected(aSelectable))
-    AddOrRemoveSelected(aSelectable,Standard_False);
-    
-  const Handle(AIS_LocalStatus)& Att = myActiveObjects(aSelectable);
-  
   TColStd_ListIteratorOfListOfInteger It;
-  Standard_Boolean jobdone(Standard_False);
-  // it is checked which were the temporary attributes 
-  // and they are set to 0
 
-  // desactivate standard modes
-  if(Att->Decomposed()){
-    for(It.Initialize(myListOfStandardMode);It.More();It.Next()){
-      mySM->Deactivate(aSelectable,It.Value(),myMainVS);
+  // Deactivate standard modes
+  if (Att->Decomposed())
+  {
+    for (It.Initialize (myListOfStandardMode); It.More(); It.Next())
+    {
+      mySM->Deactivate (aSelectable, It.Value(), myMainVS);
     }
   }
-  
-  // if object or temporary presentations...
-  if(Att->IsTemporary())
+
+  // If object or temporary presentations
+  if (Att->IsTemporary())
+  {
+    if (Att->IsSubIntensityOn())
     {
-      if(Att->IsSubIntensityOn())
-       myMainPM->Unhighlight(aSelectable,Att->HilightMode());
-      
-      // remove if bug on clear correct...
-      myMainPM->Erase(aSelectable,Att->DisplayMode());
-      myMainPM->Clear(aSelectable,Att->DisplayMode());
-      if(myMainPM->IsDisplayed(aSelectable,Att->HilightMode()))
-       myMainPM->Erase(aSelectable,Att->HilightMode());
-      //       myMainPM->Clear(aSelectable,Att->HilightMode());
-      jobdone = Standard_True;
+      myMainPM->Unhighlight (aSelectable, Att->HilightMode());
     }
-  // if below intensity
-  else
+
+    myMainPM->Erase (aSelectable, Att->DisplayMode());
+    myMainPM->Clear (aSelectable, Att->DisplayMode());
+    if (myMainPM->IsDisplayed (aSelectable, Att->HilightMode()))
     {
-      if(Att->IsSubIntensityOn())
-       myCTX->SubIntensityOff(aSelectable);
+      myMainPM->Erase (aSelectable, Att->HilightMode());
     }
-  // desactivate stored proper modes
-  for(It.Initialize(Att->SelectionModes());It.More();It.Next()){
-    mySM->Deactivate(aSelectable,It.Value(),myMainVS);
   }
-// pop : si je laisses cela plantes dans les elements de construction  
-//       alors a toi de jouer ROB
-//  RemoveSelected(aSelectable);
-
-  if(IsSelected(aSelectable))
-    AddOrRemoveSelected(aSelectable);
-  myActiveObjects.UnBind(aSelectable);
-
-  //Last detected object keeps for lastindex initialization.
-  Handle(SelectMgr_EntityOwner) aLastPicked = myMainVS->OnePicked();
-
-  UpdateSort();
-
-  //Object removes from SelectMgr
-  if( mySM->Contains(aSelectable) )
-    mySM->Remove(aSelectable);
-
-  //Object removes from Detected sequence
-  AIS_SequenceOfInteractive detectAIS;
-
-  Standard_Integer i = 1;
-  for(i = 1 ; i < myAISDetectedSeq.Length(); i++)
+  // If subintensity used
+  else if (Att->IsSubIntensityOn())
   {
-    Handle(AIS_InteractiveObject) anObj = DetectedCurrentObject();
-    if( !anObj.IsNull() && anObj != aSelectable )
-      myAISDetectedSeq.Remove( i );
+    myCTX->SubIntensityOff (aSelectable);
   }
 
-  Standard_Integer aHM = aSelectable->HasHilightMode() ? aSelectable->HilightMode() : 0;
-
-  //EntityOwners remove from AIS_Selection
-  Handle(AIS_Selection) aSel = AIS_Selection::Selection(mySelName.ToCString());
-  AIS_NListTransient::Iterator anIter(aSel->Objects()); 
-  AIS_NListTransient removeEntites;
-  for(; anIter.More(); anIter.Next()){
-    const Handle(Standard_Transient)& Tr = anIter.Value();
-    if (!Tr.IsNull()){
-      const Handle(SelectMgr_EntityOwner)& anOwnr = *((const Handle(SelectMgr_EntityOwner)*) &Tr);
-      if(anOwnr->Selectable() == aSelectable){
-        removeEntites.Append(Tr);
-        if(IsSelected(anOwnr))
-          anOwnr->Unhilight(myMainPM, aHM);//Unhilight selected
-      }
-    }
+  // Deactivate stored selection modes
+  for (It.Initialize (Att->SelectionModes()); It.More(); It.Next())
+  {
+    mySM->Deactivate (aSelectable, It.Value(), myMainVS);
   }
-  AIS_NListTransient::Iterator anIterRemove(removeEntites); 
-  for(; anIterRemove.More(); anIterRemove.Next())
-    aSel->Select(anIterRemove.Value());//EntityOwner removes from the selection data
-
-  //EntityOwners remove from myMapOfOwner
-  SelectMgr_IndexedMapOfOwner ownersToKeep; 
-  const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
-  for(i = 1; i <= myMapOfOwner.Extent(); i++){
-    const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner(i) ;
-    if(!anOwner.IsNull())
-      if(anOwner->Selectable() != aSelectable)
-        ownersToKeep.Add(anOwner);
-      else
-      {
-        if(anOwner->IsHilighted(myMainPM, aHM))
-        {
-          for(aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
-            Unhilight(anOwner, aViewer->ActiveView());
-        }
-      }
+
+  // Remove the interactive object from selection manager
+  if (mySM->Contains (aSelectable))
+  {
+    mySM->Remove (aSelectable);
   }
-  myMapOfOwner.Clear();
-  myMapOfOwner.Assign(ownersToKeep);
-  mylastindex = myMapOfOwner.FindIndex(aLastPicked);
+  ClearOutdatedSelection (aSelectable, Standard_True);
 
+  // This should be done at the very end because most methods use
+  // myActiveObjects even during clean-up
+  myActiveObjects.UnBind (aSelectable);
   return Standard_True;
 }
 
@@ -589,13 +519,7 @@ void AIS_LocalContext::ActivateStandardMode(const TopAbs_ShapeEnum aType)
   AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
 
   for(;ItM.More();ItM.Next()){
-#ifdef BUC60722
-    AIS_DisplayStatus DS = 
-       myCTX->DisplayStatus(Handle(AIS_InteractiveObject)::DownCast(ItM.Key()));
-    if( ItM.Value()->Decomposed() && (DS != AIS_DS_FullErased)  )
-#else
     if(ItM.Value()->Decomposed())
-#endif
       myCTX->SelectionManager()->Activate(ItM.Key(),
                                          IMode,
                                          myMainVS);
@@ -627,10 +551,8 @@ void AIS_LocalContext::DeactivateStandardMode(const TopAbs_ShapeEnum aType)
       myListOfStandardMode.Remove(It);
       if(myFilters->IsIn(myStdFilters[IMode]))
        myFilters->Remove(myStdFilters[IMode]);
-      UpdateSort();
       return;
     }  
-  UpdateSort();
 }
 
 //=======================================================================
@@ -677,46 +599,20 @@ void AIS_LocalContext::RemoveFilter(const Handle(SelectMgr_Filter)& aFilter)
   }
 }
 
-
-
-Standard_Boolean AIS_LocalContext::HasSameProjector(const Handle(Select3D_Projector)& thePrj) const
-{
-  const Handle(Select3D_Projector)& aCurPrj = myMainVS->Projector();
-  if (aCurPrj->Perspective() != thePrj->Perspective())
-    return Standard_False;  
-  if (aCurPrj->Perspective() && aCurPrj->Focus() != thePrj->Focus())
-    return Standard_False;
-  const gp_GTrsf& aCurTrsf = aCurPrj->Transformation();
-  const gp_GTrsf& aPrjTrsf = thePrj->Transformation();
-
-  for (Standard_Integer i = 1; i <= 3; ++i)
-  {
-    for (Standard_Integer j = 1; j <= 3 ; ++j)
-    {
-      if (aCurTrsf.Value (i, j) != aPrjTrsf.Value (i, j))
-        return Standard_False;
-    }
-  }
-
-  return Standard_True;
-}
-
-
 //=======================================================================
 //function : Terminate
-//purpose  : 
+//purpose  :
 //=======================================================================
 
-void AIS_LocalContext::Terminate( const Standard_Boolean updateviewer )
+void AIS_LocalContext::Terminate (const Standard_Boolean theToUpdate)
 {
   ClearDetected();
   Clear();
-  myMapOfOwner.Clear();
+  myMapOfOwner->Clear();
   
   mylastindex=0;
   // clear the selector...
   myMainVS->Clear();
-  myCTX->SelectionManager()->Remove(myMainVS);
   
 
   AIS_Selection::SetCurrentSelection(mySelName.ToCString());
@@ -724,30 +620,27 @@ void AIS_LocalContext::Terminate( const Standard_Boolean updateviewer )
   Handle(Standard_Transient) Tr;
   for(S->Init();S->More();S->Next()){
     Tr = S->Value();
-    (*((Handle(SelectMgr_EntityOwner)*)&Tr))->State(0);
+    Handle(SelectMgr_EntityOwner)::DownCast (Tr)->SetSelected (Standard_False);
   }
 
       
   AIS_Selection::Select();
   AIS_Selection::Remove(mySelName.ToCString());
 
-  // CLE
-  // const Handle(V3d_Viewer)& Vwr = myCTX->CurrentViewer();
-  Handle(V3d_Viewer) Vwr = myCTX->CurrentViewer();
-  // ENDCLE
-  Handle(V3d_View) curV;
-  for(Vwr->InitActiveViews();Vwr->MoreActiveViews();Vwr->NextActiveViews()){
-    curV = Vwr->ActiveView(); 
-    Visual3d_TransientManager::ClearDraw( curV->View(), updateviewer );
+  Handle(V3d_Viewer) aViewer = myCTX->CurrentViewer();
+  for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
+  {
+    Handle(V3d_View) aView = aViewer->ActiveView();
+    aView->View()->ClearImmediate();
   }
 
+  Handle(V3d_View) aDummyView;
+  myMainVS->ClearSensitive (aDummyView);
 
-#ifdef DEB
-  Handle(V3d_View) BidV;
-  myMainVS->ClearAreas(BidV);
-  myMainVS->ClearSensitive(BidV);
-  
-#endif
+  if (theToUpdate)
+  {
+    myCTX->UpdateCurrentViewer();
+  }
 }
 
 
@@ -843,8 +736,8 @@ void AIS_LocalContext::Unhilight(const Handle(AIS_InteractiveObject)& anObject)
   myMainPM->Unhighlight(anObject,Att->HilightMode());
   if(Att->IsTemporary() && Att->DisplayMode()==-1)
     if(!IsSomeWhereElse)
-      myMainPM->Erase(anObject,Att->HilightMode());
-  
+      myMainPM->SetVisibility (anObject, Att->HilightMode(), Standard_False);
+
   Att->SubIntensityOff();
   Att->SetHilightColor(Quantity_NOC_WHITE);
 }
@@ -984,12 +877,17 @@ void AIS_LocalContext::LoadContextObjects()
     myCTX->DisplayedObjects(LL,Standard_True);
     Handle(AIS_LocalStatus) Att;
     for (It.Initialize(LL);It.More();It.Next()){
+      const Handle(AIS_InteractiveObject)& anObj = It.Value();
       Att= new AIS_LocalStatus();
-      Att->SetDecomposition((It.Value()->AcceptShapeDecomposition() && myAcceptStdMode));
+      Att->SetDecomposition((anObj->AcceptShapeDecomposition() && myAcceptStdMode));
       Att->SetTemporary(Standard_False);
-      Att->SetHilightMode(It.Value()->HasHilightMode()? It.Value()->HilightMode(): 0);
-      
-      myActiveObjects.Bind(It.Value(),Att);
+      Att->SetHilightMode(anObj->HasHilightMode()? anObj->HilightMode(): 0);
+      for (anObj->Init(); anObj->More(); anObj->Next())
+      {
+        const Handle(SelectMgr_Selection)& aSel = anObj->CurrentSelection();
+        aSel->SetSelectionState (SelectMgr_SOS_Deactivated);
+      }
+      myActiveObjects.Bind(anObj,Att);
     }
   }
 }
@@ -1013,17 +911,16 @@ void AIS_LocalContext::UnloadContextObjects()
 //purpose  : 
 //=======================================================================
 
-void AIS_LocalContext::Process(const Handle(SelectMgr_SelectableObject)& anObject,
-                              const Standard_Boolean WithProj)
+void AIS_LocalContext::Process(const Handle(SelectMgr_SelectableObject)& anObject)
 { 
   if(!myActiveObjects.IsBound(anObject)) return;
   if(myActiveObjects(anObject)->Decomposed())
-    ActivateStandardModes(anObject,WithProj);
+    ActivateStandardModes(anObject);
   else
     {
       TColStd_ListIteratorOfListOfInteger It(myActiveObjects(anObject)->SelectionModes());
       for(;It.More();It.Next())
-       myCTX->SelectionManager()->Activate(anObject,It.Value(),myMainVS,WithProj);
+       myCTX->SelectionManager()->Activate(anObject,It.Value(),myMainVS);
     }
 }
 
@@ -1032,7 +929,7 @@ void AIS_LocalContext::Process(const Handle(SelectMgr_SelectableObject)& anObjec
 //purpose  : 
 //=======================================================================
 
-void AIS_LocalContext::Process(const Standard_Boolean WithProj)
+void AIS_LocalContext::Process()
 { 
 
   myMainVS->Clear();
@@ -1042,11 +939,11 @@ void AIS_LocalContext::Process(const Standard_Boolean WithProj)
   for(;It.More();It.Next()){
     myCTX->SelectionManager()->Load(It.Key(),myMainVS);
     if(It.Value()->Decomposed()) 
-      ActivateStandardModes(It.Key(),WithProj);
+      ActivateStandardModes(It.Key());
     else if( myCTX->GetAutoActivateSelection() )
     {
       It.Value()->AddSelectionMode(0);
-      myCTX->SelectionManager()->Activate(It.Key(),0,myMainVS,WithProj);
+      myCTX->SelectionManager()->Activate(It.Key(),0,myMainVS);
     }
   }
 
@@ -1057,8 +954,7 @@ void AIS_LocalContext::Process(const Standard_Boolean WithProj)
 //purpose  : 
 //=======================================================================
 
-void AIS_LocalContext::ActivateStandardModes(const Handle(SelectMgr_SelectableObject)& anObject,
-                                            const Standard_Boolean WithProj)
+void AIS_LocalContext::ActivateStandardModes(const Handle(SelectMgr_SelectableObject)& anObject)
 { 
   if(!myActiveObjects.IsBound(anObject)) return;
   
@@ -1067,7 +963,7 @@ void AIS_LocalContext::ActivateStandardModes(const Handle(SelectMgr_SelectableOb
   const Handle(AIS_LocalStatus)&  LS = myActiveObjects(anObject);
   if(LS->Decomposed()){
     for(;itl.More();itl.Next()){
-      myCTX->SelectionManager()->Activate(anObject,itl.Value(),myMainVS,WithProj);
+      myCTX->SelectionManager()->Activate(anObject,itl.Value(),myMainVS);
       LS->AddSelectionMode(itl.Value());
     }
   }
@@ -1114,17 +1010,26 @@ void AIS_LocalContext::ClearObjects()
          myMainPM->Erase(SO,CurAtt->DisplayMode());
       }
       
-      TColStd_ListIteratorOfListOfInteger ITL(CurAtt->SelectionModes());
-      for(;ITL.More();ITL.Next())
-       mySM->Deactivate(SO,ITL.Value(),myMainVS);
-      
-      if(CurAtt->IsTemporary())
-       mySM->Remove(SO,myMainVS);
-      
+      TColStd_ListIteratorOfListOfInteger aSelModeIter (CurAtt->SelectionModes());
+      for ( ; aSelModeIter.More(); aSelModeIter.Next())
+      {
+        Standard_Integer aSelMode = aSelModeIter.Value();
+        mySM->Deactivate (SO, aSelMode, myMainVS);
+      }
+
     }
   ClearSelected( Standard_False );
+
+  // Clear selection structures for temporary objects, created in local context
+  for (AIS_DataMapIteratorOfDataMapOfSelStat anIter (myActiveObjects); anIter.More(); anIter.Next())
+  {
+    if (anIter.Value()->IsTemporary())
+    {
+      mySM->Remove (anIter.Key(), myMainVS);
+    }
+  }
+
   myActiveObjects.Clear();
-//  myMainVS->ClearAreas();myMainVS->ClearSensitive();
 }
 
 
@@ -1147,145 +1052,101 @@ HasFilters(const TopAbs_ShapeEnum aType) const
 
 void AIS_LocalContext::ClearDetected()
 {
-  for(Standard_Integer I=1;I<=myMapOfOwner.Extent();I++){
-    
-    if(!myMapOfOwner(I).IsNull()){
-      if(myMapOfOwner(I)->IsHilighted(myMainPM))
-       myMapOfOwner(I)->Unhilight(myMainPM);
-      else if (myMapOfOwner(I)->IsHilighted(myCTX->CollectorPrsMgr()))
-       myMapOfOwner(I)->Unhilight(myCTX->CollectorPrsMgr());
-      
-      else{
-       const Handle(SelectMgr_SelectableObject)& SO = 
-         myMapOfOwner.FindKey(I)->Selectable();
-       if(myActiveObjects.IsBound(SO)){
-         const Handle(AIS_LocalStatus)& Att = myActiveObjects(SO);
-         
-         if(Att->IsTemporary() &&
-            Att->DisplayMode()==-1 && 
-            Att->SelectionModes().IsEmpty()){
-           myMapOfOwner(I)->Clear(myMainPM);
-           //myMapOfOwner(I)->Clear();//rob-jmi...
-         }
-       }
+  for(Standard_Integer I=1;I<=myMapOfOwner->Extent();I++)
+  {
+    if(!myMapOfOwner->FindKey (I).IsNull())
+    {
+      if(myMapOfOwner->FindKey (I)->IsHilighted(myMainPM))
+        myMapOfOwner->FindKey (I)->Unhilight(myMainPM);
+      else
+      {
+        const Handle(SelectMgr_SelectableObject)& SO = 
+          myMapOfOwner->FindKey (I)->Selectable();
+        if(myActiveObjects.IsBound(SO))
+        {
+          const Handle(AIS_LocalStatus)& Att = myActiveObjects(SO);
+
+          if(Att->IsTemporary() &&
+             Att->DisplayMode()==-1 && 
+             Att->SelectionModes().IsEmpty())
+          {
+            myMapOfOwner->FindKey (I)->Clear(myMainPM);
+          }
+        }
       }
     }
   }
-
-}
-
-void AIS_LocalContext::UpdateConversion()
-{
-  myMainVS->UpdateConversion();
 }
 
-void AIS_LocalContext::UpdateSort()
-{
-  myMainVS->UpdateSort();
-}
-
-
-
 //=======================================================================
-//function : IMMEDIATE MODE
-//purpose  : 
+//function : BeginImmediateDraw
+//purpose  :
 //=======================================================================
-
-Standard_Boolean  AIS_LocalContext::BeginImmediateDraw()
+Standard_Boolean AIS_LocalContext::BeginImmediateDraw()
 {
-  if(myMainPM->IsImmediateModeOn()){
-    myMainPM->BeginDraw();
+  if (myMainPM->IsImmediateModeOn())
+  {
+    myMainPM->BeginImmediateDraw();
     return Standard_True;
   }
   return Standard_False;
 }
 
-
-Standard_Boolean AIS_LocalContext::ImmediateAdd(const Handle(AIS_InteractiveObject)& anIObj,
-                                                     const Standard_Integer aMode)
+//=======================================================================
+//function : ImmediateAdd
+//purpose  :
+//=======================================================================
+Standard_Boolean AIS_LocalContext::ImmediateAdd (const Handle(AIS_InteractiveObject)& theObj,
+                                                 const Standard_Integer               theMode)
 {
-  if(!myMainPM->IsImmediateModeOn())
+  if (!myMainPM->IsImmediateModeOn())
+  {
     return Standard_False;
-  myMainPM->Add(anIObj,aMode);
-  return Standard_True;
-}
+  }
 
-Standard_Boolean AIS_LocalContext::ImmediateRemove(const Handle(AIS_InteractiveObject)& anIObj,
-                                                  const Standard_Integer aMode)
-{
-  if(!myMainPM->IsImmediateModeOn())   return Standard_False;
-  myMainPM->Remove(anIObj,aMode);
+  myMainPM->AddToImmediateList (myMainPM->Presentation (theObj, theMode)->Presentation());
   return Standard_True;
 }
 
-Standard_Boolean AIS_LocalContext::EndImmediateDraw(const Handle(V3d_View)& aView,
-                                                         const Standard_Boolean DoubleBuf)
+//=======================================================================
+//function : EndImmediateDraw
+//purpose  :
+//=======================================================================
+Standard_Boolean AIS_LocalContext::EndImmediateDraw (const Handle(V3d_View)& theView)
 {
-  if(!myMainPM->IsImmediateModeOn()) return Standard_False;
-  myMainPM->EndDraw(aView,DoubleBuf);
-  return Standard_True;
-}
-
-Standard_Boolean AIS_LocalContext::IsImmediateModeOn() const
-{return myMainPM->IsImmediateModeOn();}
-
-void AIS_LocalContext::SetSensitivityMode(const StdSelect_SensitivityMode aMode) {
-
-  myMainVS->SetSensitivityMode(aMode);
-}
-
-StdSelect_SensitivityMode AIS_LocalContext::SensitivityMode() const {
-
-  return myMainVS->SensitivityMode();
-}
-
-void AIS_LocalContext::SetSensitivity(const Standard_Real aPrecision) {
-
-  myMainVS->SetSensitivity(aPrecision);
-}
-
-Standard_Real AIS_LocalContext::Sensitivity() const {
-
-  return myMainVS->Sensitivity();
-}
-
-void AIS_LocalContext::SetPixelTolerance(const Standard_Integer aPrecision) {
+  if (!myMainPM->IsImmediateModeOn())
+  {
+    return Standard_False;
+  }
 
-  myMainVS->SetPixelTolerance(aPrecision);
+  myMainPM->EndImmediateDraw (theView);
+  return Standard_True;
 }
 
-Standard_Integer AIS_LocalContext::PixelTolerance() const {
-
-  return myMainVS->PixelTolerance();
+// =======================================================================
+// function : ClearImmediateDraw
+// purpose  :
+// =======================================================================
+void AIS_LocalContext::ClearImmediateDraw()
+{
+  myMainPM->ClearImmediateDraw();
 }
 
 //=======================================================================
-//function : SetZLayer
+//function : IsImmediateModeOn
 //purpose  :
 //=======================================================================
-
-void AIS_LocalContext::SetZLayer (const Handle(AIS_InteractiveObject)& theIObj,
-                                  const Standard_Integer theLayerId)
+Standard_Boolean AIS_LocalContext::IsImmediateModeOn() const
 {
-  if (!myActiveObjects.IsBound (theIObj)) 
-    return;
+  return myMainPM->IsImmediateModeOn();
+}
 
-  const Handle(AIS_LocalStatus)& aStatus = myActiveObjects (theIObj);
-  if (aStatus->DisplayMode () == -1)
-    return;
+void AIS_LocalContext::SetPixelTolerance(const Standard_Real aPrecision) {
 
-  theIObj->SetZLayer (myMainPM, theLayerId);
+  myMainVS->SetPixelTolerance(aPrecision);
 }
 
-//=======================================================================
-//function : GetZLayer
-//purpose  : 
-//=======================================================================
-
-Standard_Integer AIS_LocalContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
-{
-  if (!myActiveObjects.IsBound (theIObj)) 
-    return -1;
+Standard_Real AIS_LocalContext::PixelTolerance() const {
 
-  return theIObj->GetZLayer (myMainPM);
+  return myMainVS->PixelTolerance();
 }