0031153: Visualization - Non clear highlighting of selected trihedron elements
[occt.git] / src / AIS / AIS_InteractiveContext.cxx
index dd649e4..5d4454d 100644 (file)
@@ -5,8 +5,8 @@
 //
 // This file is part of Open CASCADE Technology software library.
 //
-// This library is free software; you can redistribute it and / or modify it
-// under the terms of the GNU Lesser General Public version 2.1 as published
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
 // by the Free Software Foundation, with special exception defined in the file
 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
 // distribution for complete text of the license and disclaimer of any warranty.
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-// Modified by  XAB & Serguei Dec 97 (angle &deviation coeffts)
+#include <AIS_InteractiveContext.hxx>
 
-#define BUC60577        //GG_101099     Enable to compute correctly
-//                      transparency with more than one object in the view.
-
-#define BUC60632        //GG 15/03/00 Add protection on SetDisplayMode()
-//                      method, compute only authorized presentation.
-
-#define BUC60688        //GG 25/05/00 Add SetSensitivity() methods.
-
-#define BUC60722  //GG 04/09/00 Always enable viewer update when erasing something
-
-#define IMP051001       //GG Adds SetZDetected() and ZDetected() methods
-
-#define OCC172          //SAV clear static map before destroying context.
-
-#define OCC204          //SAV 26/02/02 : pass <updateviewer> flag to 
-// AddOrRemoveCurrentObject method from ClearGlobal.
-
-#define OCC4373         //SAN 10/11/03 : improve display mode management in
-// Display( IO, updateviewer ) and 
-// SetDisplayMode( IO, mode, updateviewer ) methods
-
-#include <AIS_InteractiveContext.ixx>
-
-//#include <AIS_DataMapIteratorOfDataMapOfInteractiveInteger.hxx>
-#include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <TColStd_MapIteratorOfMapOfTransient.hxx>
-#include <AIS_LocalContext.hxx>
-#include <AIS_LocalStatus.hxx>
-#include <Precision.hxx>
-#include <AIS_Selection.hxx>
 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
-#include <AIS_ConnectedShape.hxx>
-#include <AIS_MultipleConnectedShape.hxx>
-#include <AIS_DataMapIteratorOfDataMapOfILC.hxx>
+#include <AIS_ConnectedInteractive.hxx>
 #include <AIS_GlobalStatus.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_ListIteratorOfListOfInteractive.hxx>
 #include <AIS_MapIteratorOfMapOfInteractive.hxx>
-#include <PrsMgr_ModedPresentation.hxx>
-#include <Visual3d_ViewManager.hxx>
-#include <Prs3d_ShadingAspect.hxx>
+#include <AIS_MultipleConnectedInteractive.hxx>
 #include <AIS_Shape.hxx>
+#include <AIS_Trihedron.hxx>
+#include <Geom_Axis2Placement.hxx>
 #include <Graphic3d_AspectFillArea3d.hxx>
 #include <HLRBRep.hxx>
-#include <Prs3d_IsoAspect.hxx>
+#include <OSD_Environment.hxx>
+#include <Precision.hxx>
+#include <Prs3d_BasicAspect.hxx>
 #include <Prs3d_DatumAspect.hxx>
+#include <Prs3d_IsoAspect.hxx>
+#include <Prs3d_LineAspect.hxx>
 #include <Prs3d_PlaneAspect.hxx>
+#include <Prs3d_PointAspect.hxx>
+#include <Prs3d_ShadingAspect.hxx>
 #include <PrsMgr_PresentableObject.hxx>
+#include <Quantity_Color.hxx>
+#include <SelectMgr_EntityOwner.hxx>
+#include <SelectMgr_Filter.hxx>
+#include <SelectMgr_OrFilter.hxx>
+#include <SelectMgr_SelectionManager.hxx>
 #include <Standard_Atomic.hxx>
+#include <Standard_Transient.hxx>
+#include <Standard_Type.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TCollection_ExtendedString.hxx>
+#include <TColStd_ListIteratorOfListOfInteger.hxx>
+#include <TColStd_MapIteratorOfMapOfTransient.hxx>
+#include <TopLoc_Location.hxx>
+#include <TopoDS_Shape.hxx>
 #include <UnitsAPI.hxx>
+#include <V3d_View.hxx>
+#include <V3d_Viewer.hxx>
 
-#include <AIS_Trihedron.hxx>
-#include <Geom_Axis2Placement.hxx>
-#include <OSD_Environment.hxx>
-
-#include <AIS_ListIteratorOfListOfInteractive.hxx>
-
-// In the method GetDefModes() the returned value of the selection mode 
-// is always equal to 0 if it is -1.
-#define BUC61051
-
-// The local context is closed the method ::ResetOriginalState() sets the selection mode equal to 0
-// in spite of the selection mode of the interactive object in Natural Point.
-#define OCC166
-
-static Standard_Boolean AISDebugModeOn()
-{
-//  static OSD_Environment aisdb("AISDEBUGMODE");
-//  return !aisdb.Value().IsEmpty();
-  static Standard_Integer isDebugMode(-1);
-  if (isDebugMode < 0) {
-    isDebugMode = 1;
-    OSD_Environment aisdb("AISDEBUGMODE");
-    if (aisdb.Value().IsEmpty())
-      isDebugMode = 0;
-  }
-  return (isDebugMode != 0);
-}
+IMPLEMENT_STANDARD_RTTIEXT(AIS_InteractiveContext, Standard_Transient)
 
 namespace
 {
-  static volatile Standard_Integer THE_AIS_INDEX_SEL = 0;
-  static volatile Standard_Integer THE_AIS_INDEX_CUR = 0;
+  typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners;
+  typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners;
 
-  static TCollection_AsciiString AIS_Context_NewSelName()
+  //! Initialize default highlighting attributes.
+  static void initDefaultHilightAttributes (const Handle(Prs3d_Drawer)& theDrawer,
+                                            const Quantity_Color& theColor)
   {
-    return TCollection_AsciiString ("AIS_SelContext_")
-         + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_SEL));
-  }
+    theDrawer->SetMethod (Aspect_TOHM_COLOR);
+    theDrawer->SetDisplayMode (0);
+    theDrawer->SetColor (theColor);
+
+    theDrawer->SetupOwnShadingAspect();
+    theDrawer->SetupOwnPointAspect();
+    theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
+    *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
+    theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
+    *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
+    theDrawer->SetPlaneAspect (new Prs3d_PlaneAspect());
+    *theDrawer->PlaneAspect()->EdgesAspect() = *theDrawer->Link()->PlaneAspect()->EdgesAspect();
+    theDrawer->SetFreeBoundaryAspect   (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
+    *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
+    theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
+    *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
+    theDrawer->SetDatumAspect (new Prs3d_DatumAspect());
+
+    theDrawer->ShadingAspect()->SetColor (theColor);
+    theDrawer->WireAspect()->SetColor (theColor);
+    theDrawer->LineAspect()->SetColor (theColor);
+    theDrawer->PlaneAspect()->ArrowAspect()->SetColor (theColor);
+    theDrawer->PlaneAspect()->IsoAspect()->SetColor (theColor);
+    theDrawer->PlaneAspect()->EdgesAspect()->SetColor (theColor);
+    theDrawer->FreeBoundaryAspect()->SetColor (theColor);
+    theDrawer->UnFreeBoundaryAspect()->SetColor (theColor);
+    theDrawer->PointAspect()->SetColor (theColor);
+    for (Standard_Integer aPartIter = 0; aPartIter < Prs3d_DP_None; ++aPartIter)
+    {
+      if (Handle(Prs3d_LineAspect) aLineAsp = theDrawer->DatumAspect()->LineAspect ((Prs3d_DatumParts )aPartIter))
+      {
+        aLineAsp->SetColor (theColor);
+      }
+    }
 
-  static TCollection_AsciiString AIS_Context_NewCurName()
-  {
-    return TCollection_AsciiString ("AIS_CurContext_")
-         + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_CUR));
+    theDrawer->WireAspect()->SetWidth (2.0);
+    theDrawer->LineAspect()->SetWidth (2.0);
+    theDrawer->PlaneAspect()->EdgesAspect()->SetWidth (2.0);
+    theDrawer->FreeBoundaryAspect()  ->SetWidth (2.0);
+    theDrawer->UnFreeBoundaryAspect()->SetWidth (2.0);
+    theDrawer->PointAspect()->SetTypeOfMarker (Aspect_TOM_O_POINT);
+    theDrawer->PointAspect()->SetScale (2.0);
+
+    // the triangulation should be computed using main presentation attributes,
+    // and should not be overridden by highlighting
+    theDrawer->SetAutoTriangulation (Standard_False);
   }
-};
+}
 
 //=======================================================================
 //function : AIS_InteractiveContext
@@ -117,72 +123,108 @@ namespace
 //=======================================================================
 
 AIS_InteractiveContext::AIS_InteractiveContext(const Handle(V3d_Viewer)& MainViewer):
-mgrSelector(new SelectMgr_SelectionManager()),
-myMainPM(new PrsMgr_PresentationManager3d(MainViewer->Viewer())),
+myMainPM(new PrsMgr_PresentationManager3d(MainViewer->StructureManager())),
 myMainVwr(MainViewer),
 myMainSel(new StdSelect_ViewerSelector3d()),
-myToHilightSelected( Standard_False ),
+myToHilightSelected(Standard_True),
+mySelection(new AIS_Selection()),
 myFilters(new SelectMgr_OrFilter()),
 myDefaultDrawer(new Prs3d_Drawer()),
-myDefaultColor(Quantity_NOC_GOLDENROD),
-myHilightColor(Quantity_NOC_CYAN1),
-mySelectionColor(Quantity_NOC_GRAY80),
-myPreselectionColor(Quantity_NOC_GREEN),
-mySubIntensity(Quantity_NOC_GRAY40),
-myDisplayMode(0),
-myCurLocalIndex(0),
-#ifdef IMP051001
-myZDetectionFlag(0),
-#endif
-myIsAutoActivateSelMode( Standard_True )
-{ 
+myCurDetected(0),
+myCurHighlighted(0),
+myPickingStrategy (SelectMgr_PickingStrategy_FirstAcceptable),
+myAutoHilight(Standard_True),
+myIsAutoActivateSelMode(Standard_True)
+{
+  mgrSelector = new SelectMgr_SelectionManager (myMainSel);
+
+  myStyles[Prs3d_TypeOfHighlight_None]          = myDefaultDrawer;
+  myStyles[Prs3d_TypeOfHighlight_Selected]      = new Prs3d_Drawer();
+  myStyles[Prs3d_TypeOfHighlight_Dynamic]       = new Prs3d_Drawer();
+  myStyles[Prs3d_TypeOfHighlight_LocalSelected] = new Prs3d_Drawer();
+  myStyles[Prs3d_TypeOfHighlight_LocalDynamic]  = new Prs3d_Drawer();
+  myStyles[Prs3d_TypeOfHighlight_SubIntensity]  = new Prs3d_Drawer();
+
+  myDefaultDrawer->SetZLayer(Graphic3d_ZLayerId_Default);
+  myDefaultDrawer->SetDisplayMode(0);
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_Dynamic];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle, Quantity_NOC_CYAN1);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_Top);
+  }
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_LocalDynamic];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle, Quantity_NOC_CYAN1);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_Topmost);
+  }
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_Selected];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle, Quantity_NOC_GRAY80);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
+  }
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_LocalSelected];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle, Quantity_NOC_GRAY80);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
+  }
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_SubIntensity];
+    aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
+    aStyle->SetMethod(Aspect_TOHM_COLOR);
+    aStyle->SetColor (Quantity_NOC_GRAY40);
+  }
+
   InitAttributes();
 }
 
-void AIS_InteractiveContext::Delete() const
+//=======================================================================
+//function : ~AIS_InteractiveContext
+//purpose  :
+//=======================================================================
+AIS_InteractiveContext::~AIS_InteractiveContext()
 {
-  //Clear the static current selection. Else the memory
-  //is not release
-  AIS_Selection::ClearCurrentSelection();
-#ifdef OCC172
-  // to avoid an exception
-  if ( AIS_Selection::Find( mySelectionName.ToCString() ) )
-    AIS_Selection::Remove( mySelectionName.ToCString() );
-
-  // to avoid an exception
-  if ( AIS_Selection::Find( myCurrentName.ToCString() ) )
-    AIS_Selection::Remove( myCurrentName.ToCString() );
+  // clear the current selection
+  mySelection->Clear();
+  mgrSelector.Nullify();
 
-  // let's remove one reference explicitly. this operation's supposed to
-  // be performed when mgrSelector will be destroyed but anyway...
-  mgrSelector->Remove( myMainSel );
-#endif
-  AIS_ListOfInteractive aList;
-  
-  AIS_DataMapIteratorOfDataMapOfIOStatus anIt(myObjects);
   Handle(AIS_InteractiveContext) aNullContext;
-  for(; anIt.More() ; anIt.Next())
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
   {
-    Handle(AIS_InteractiveObject) anObj = anIt.Key();
-    anObj->SetContext(aNullContext);
-    
+    const Handle(AIS_InteractiveObject)& anObj = anObjIter.Key();
+    anObj->SetContext (aNullContext);
+    for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
+    {
+      aSelIter.Value()->UpdateBVHStatus (SelectMgr_TBU_Renew);
+    }
   }
-  MMgt_TShared::Delete();
 }
 
 //=======================================================================
-//function : AIS_SelectionName
-//purpose  : 
+//function : LastActiveView
+//purpose  :
 //=======================================================================
-const TCollection_AsciiString& AIS_InteractiveContext::SelectionName() const 
+Handle(V3d_View) AIS_InteractiveContext::LastActiveView() const
 {
-  if(!HasOpenedContext())
-    return mySelectionName;
-  return myLocalContexts(myCurLocalIndex)->SelectionName();
-
-} 
-
+  if (myLastActiveView == NULL
+   || myMainVwr.IsNull())
+  {
+    return Handle(V3d_View)();
+  }
 
+  // as a precaution - check that myLastActiveView pointer is a valid active View
+  for (V3d_ListOfViewIterator aViewIter = myMainVwr->ActiveViewIterator(); aViewIter.More(); aViewIter.Next())
+  {
+    if (aViewIter.Value() == myLastActiveView)
+    {
+      return aViewIter.Value();
+    }
+  }
+  return Handle(V3d_View)();
+}
 
 //=======================================================================
 //function : UpdateCurrentViewer
@@ -195,1046 +237,768 @@ void AIS_InteractiveContext::UpdateCurrentViewer()
     myMainVwr->Update();
 }
 
-
-//=======================================================================
-//function : DomainOfMainViewer
-//purpose  : 
-//=======================================================================
-
-Standard_CString AIS_InteractiveContext::DomainOfMainViewer() const 
-{
-  return myMainVwr->Domain();
-  
-}
-
 //=======================================================================
 //function : DisplayedObjects
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::DisplayedObjects(AIS_ListOfInteractive& aListOfIO,
-                                              const Standard_Boolean OnlyFromNeutral) const 
+void AIS_InteractiveContext::DisplayedObjects (AIS_ListOfInteractive& theListOfIO) const
 {
-#ifdef DEBUG
-  cout<<"AIS_IC::DisplayedObjects"<<endl;
-#endif
-
-  AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);
-  if(!HasOpenedContext() || OnlyFromNeutral){
-    for(;It.More();It.Next()){
-      if(It.Value()->GraphicStatus()==AIS_DS_Displayed)
-        aListOfIO.Append(It.Key());
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    if (anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed)
+    {
+      theListOfIO.Append (anObjIter.Key());
     }
   }
-  else{
-    TColStd_MapOfTransient theMap;
-    // neutral point
-    for(;It.More();It.Next()){
-      if(It.Value()->GraphicStatus()==AIS_DS_Displayed)
-        theMap.Add(It.Key());
-    }
-#ifdef DEBUG
-    cout<<"\tFrom Neutral Point : "<<theMap.Extent()<<endl;
-#endif
-
-    //parse all local contexts...
-#ifdef DEBUG
-    Standard_Integer NbDisp;
-    for(AIS_DataMapIteratorOfDataMapOfILC it1(myLocalContexts);it1.More();it1.Next()){
-      const Handle(AIS_LocalContext)& LC = it1.Value();
-      NbDisp =  LC->DisplayedObjects(theMap);
-      cout<<"\tIn Local Context "<<it1.Key()<<" : "<<NbDisp<<endl;
-    }
-#endif
-    Handle(AIS_InteractiveObject) curIO;
-    Handle(Standard_Transient) Tr;
-      for(TColStd_MapIteratorOfMapOfTransient it2(theMap);it2.More();it2.Next()){
-        Tr = it2.Key();
-        curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
-        aListOfIO.Append(curIO);
-      }
-    }
 }
+
 //=======================================================================
 //function : DisplayedObjects
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::DisplayedObjects(const AIS_KindOfInteractive TheKind,
-                                              const Standard_Integer TheSign,
-                                              AIS_ListOfInteractive& aListOfIO,
-                                              const Standard_Boolean /*OnlyFromNeutral*/) const 
+void AIS_InteractiveContext::DisplayedObjects (const AIS_KindOfInteractive theKind,
+                                               const Standard_Integer      theSign,
+                                               AIS_ListOfInteractive&      theListOfIO) const
 {
-  ObjectsByDisplayStatus( TheKind, TheSign, AIS_DS_Displayed, aListOfIO );
+  ObjectsByDisplayStatus (theKind, theSign, AIS_DS_Displayed, theListOfIO);
 }
 
 //=======================================================================
 //function : ErasedObjects
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ErasedObjects(AIS_ListOfInteractive& theListOfIO) const 
+void AIS_InteractiveContext::ErasedObjects (AIS_ListOfInteractive& theListOfIO) const
 {
-  ObjectsByDisplayStatus( AIS_DS_Erased, theListOfIO );
+  ObjectsByDisplayStatus (AIS_DS_Erased, theListOfIO);
 }
 
 //=======================================================================
 //function : ErasedObjects
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ErasedObjects(const AIS_KindOfInteractive TheKind,
-                                           const Standard_Integer TheSign,
-                                           AIS_ListOfInteractive& theListOfIO) const 
+void AIS_InteractiveContext::ErasedObjects (const AIS_KindOfInteractive theKind,
+                                            const Standard_Integer      theSign,
+                                            AIS_ListOfInteractive&      theListOfIO) const
 {
-  ObjectsByDisplayStatus( TheKind, TheSign, AIS_DS_Erased, theListOfIO );
+  ObjectsByDisplayStatus (theKind, theSign, AIS_DS_Erased, theListOfIO);
 }
 
 //=======================================================================
 //function : ObjectsByDisplayStatus
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ObjectsByDisplayStatus(const AIS_DisplayStatus theStatus,
-                                                    AIS_ListOfInteractive&  theListOfIO) const 
+void AIS_InteractiveContext::ObjectsByDisplayStatus (const AIS_DisplayStatus theStatus,
+                                                     AIS_ListOfInteractive&  theListOfIO) const
 {
-  AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);
-  for(;It.More();It.Next()){
-    if(It.Value()->GraphicStatus() == theStatus)
-      theListOfIO.Append(It.Key());
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    if (anObjIter.Value()->GraphicStatus() == theStatus)
+    {
+      theListOfIO.Append (anObjIter.Key());
+    }
   }
 }
 
 //=======================================================================
 //function : ObjectsByDisplayStatus
-//purpose  : 
+//purpose  :
 //=======================================================================
+void AIS_InteractiveContext::ObjectsByDisplayStatus (const AIS_KindOfInteractive theKind,
+                                                     const Standard_Integer      theSign,
+                                                     const AIS_DisplayStatus     theStatus,
+                                                     AIS_ListOfInteractive&      theListOfIO) const
+{
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    if (theStatus != AIS_DS_None
+     && anObjIter.Value()->GraphicStatus() != theStatus)
+    {
+      continue;
+    }
+    else if (anObjIter.Key()->Type() != theKind)
+    {
+      continue;
+    }
 
-void AIS_InteractiveContext::ObjectsByDisplayStatus(const AIS_KindOfInteractive TheKind,
-                                                    const Standard_Integer TheSign,
-                                                    const AIS_DisplayStatus theStatus,
-                                                    AIS_ListOfInteractive& theListOfIO) const 
-{
-  AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);
-  for(;It.More();It.Next()){
-    if(It.Value()->GraphicStatus()==theStatus){
-      if(It.Key()->Type()==TheKind){
-        if(TheSign ==-1)
-          theListOfIO.Append(It.Key());
-        else{
-          if(It.Key()->Signature()==TheSign)
-            theListOfIO.Append(It.Key());
-        }
-      }
+    if (theSign == -1
+     || anObjIter.Key()->Signature() == theSign)
+    {
+      theListOfIO.Append (anObjIter.Key());
     }
   }
 }
 
 //=======================================================================
 //function : ObjectsInside
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ObjectsInside(AIS_ListOfInteractive& aListOfIO,
-                                           const AIS_KindOfInteractive TheKind,
-                                           const Standard_Integer TheSign) const 
+void AIS_InteractiveContext::ObjectsInside (AIS_ListOfInteractive&      theListOfIO,
+                                            const AIS_KindOfInteractive theKind,
+                                            const Standard_Integer      theSign) const
 {
-  AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);
-  if(TheKind==AIS_KOI_None && TheSign ==-1){
-    for(;It.More();It.Next()){
-        aListOfIO.Append(It.Key());
+  if (theKind == AIS_KOI_None
+   && theSign == -1)
+  {
+    for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+    {
+      theListOfIO.Append (anObjIter.Key());
     }
+    return;
   }
-  else{
-    for(;It.More();It.Next()){
-      if(It.Key()->Type()==TheKind){
-        if(TheSign ==-1)
-          aListOfIO.Append(It.Key());
-        else{
-          if(It.Key()->Signature()==TheSign)
-            aListOfIO.Append(It.Key());
-        }
-      }
+
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    if (anObjIter.Key()->Type() != theKind)
+    {
+      continue;
+    }
+
+    if (theSign == -1
+     || anObjIter.Key()->Signature() == theSign)
+    {
+      theListOfIO.Append (anObjIter.Key());
+    }
+  }
+}
+
+//=======================================================================
+//function : ObjectsForView
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::ObjectsForView (AIS_ListOfInteractive&  theListOfIO,
+                                             const Handle(V3d_View)& theView,
+                                             const Standard_Boolean  theIsVisibleInView,
+                                             const AIS_DisplayStatus theStatus) const
+{
+  Handle(Graphic3d_CView) aViewImpl = theView->View();
+  const Standard_Integer  aViewId   = aViewImpl->Identification();
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    if (theStatus != AIS_DS_None
+     && anObjIter.Value()->GraphicStatus() != theStatus)
+    {
+      theListOfIO.Append (anObjIter.Key());
+      continue;
+    }
+
+    Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->StructureManager()->ObjectAffinity (anObjIter.Key());
+    const Standard_Boolean isVisible = anAffinity->IsVisible (aViewId);
+    if (isVisible == theIsVisibleInView)
+    {
+      theListOfIO.Append (anObjIter.Key());
     }
   }
 }
 
 //=======================================================================
 //function : Display
-//purpose  : 
+//purpose  :
 //=======================================================================
+void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIObj,
+                                      const Standard_Boolean               theToUpdateViewer)
+{
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  Standard_Integer aDispMode = 0, aHiMod = -1, aSelMode = -1;
+  GetDefModes (theIObj, aDispMode, aHiMod, aSelMode);
+  Display (theIObj, aDispMode, myIsAutoActivateSelMode ? aSelMode : -1, theToUpdateViewer);
+}
 
-void AIS_InteractiveContext::Display(const Handle(AIS_InteractiveObject)& anIObj,
-                                     const Standard_Boolean updateviewer )
+//=======================================================================
+//function : SetViewAffinity
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::SetViewAffinity (const Handle(AIS_InteractiveObject)& theIObj,
+                                              const Handle(V3d_View)&              theView,
+                                              const Standard_Boolean               theIsVisible)
 {
-  if(anIObj.IsNull()) return;
-  
-  Standard_Integer DispMode,HiMod,SelMode;
-  
-  GetDefModes(anIObj,DispMode,HiMod,SelMode);
-  
-  Handle (AIS_InteractiveContext) aThis = this;
-  if(!anIObj->HasInteractiveContext()) 
-    anIObj->SetContext(aThis);
-  
-  //NO LOCAL CONTEXT OPEN
-  if(!HasOpenedContext()) {
-#ifndef OCC4373
-    // SAN : Do not return here. Perform advanced display mode analysis a bit later...
-    if(IsDisplayed(anIObj)) return;
-#endif
-
-    // it did not yet exist
-    if(!myObjects.IsBound(anIObj)){
-      
-      Handle(AIS_GlobalStatus) STATUS= 
-#ifdef OCC166
-        new AIS_GlobalStatus(AIS_DS_Displayed,DispMode,SelMode);
-#else
-      new AIS_GlobalStatus(AIS_DS_Displayed,DispMode,0);
-#endif
-      myObjects.Bind (anIObj,STATUS);
-      myMainPM->Display(anIObj,DispMode);
-      if( myIsAutoActivateSelMode )
-      {
-        if(!mgrSelector->Contains(anIObj))
-           mgrSelector->Load(anIObj);
-        mgrSelector->Activate(anIObj,SelMode,myMainSel);
-      }
-        
-     if(updateviewer) myMainVwr->Update();
-    }
-    // it is somewhere else...
-    else {
-      // CLE
-      // const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-      Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-      // ENDCLE
-      if (STATUS->GraphicStatus() == AIS_DS_Displayed || STATUS->GraphicStatus() == AIS_DS_Erased)
-      {
-        // SAN : erase presentations for all display modes different from <DispMode>;
-        //       then make sure <DispMode> is displayed and maybe highlighted;
-        //       Finally, activate selection mode <SelMode> if not yet activated.
-        TColStd_ListOfInteger aModesToRemove;
-        TColStd_ListIteratorOfListOfInteger ItL (STATUS->DisplayedModes());
-        for(;ItL.More();ItL.Next()){
-
-          Standard_Integer OldMode = ItL.Value();
-
-          if(OldMode!=DispMode){
-            aModesToRemove.Append(OldMode);
-            if(myMainPM->IsHighlighted(anIObj,OldMode))
-              myMainPM->Unhighlight(anIObj,OldMode);
-            myMainPM->Erase(anIObj,OldMode);
-          }
-        }
-
-        for(ItL.Initialize(aModesToRemove);ItL.More();ItL.Next())
-          STATUS->RemoveDisplayMode(ItL.Value());
-
-        if(!STATUS->IsDModeIn(DispMode))
-          STATUS->AddDisplayMode(DispMode);
-
-        myMainPM->Display(anIObj,DispMode);
-        if (STATUS->GraphicStatus() == AIS_DS_Erased)
-        {
-          STATUS->SetGraphicStatus(AIS_DS_Displayed);
-        }
-        if(STATUS->IsHilighted()){
-          myMainPM->Highlight(anIObj,HiMod);
-        }
-        if( myIsAutoActivateSelMode )
-        {
-           if(!mgrSelector->Contains(anIObj))
-              mgrSelector->Load(anIObj);
-           if(!mgrSelector->IsActivated(anIObj,SelMode))
-              mgrSelector->Activate(anIObj,SelMode,myMainSel);
-        }
-      }
-    } 
-    
-    if(anIObj->IsTransparent() && !myMainVwr->Viewer()->Transparency())
-      myMainVwr->Viewer()->SetTransparency(Standard_True);
-    if(updateviewer) myMainVwr->Update();
+  if (theIObj.IsNull()
+  || !myObjects.IsBound (theIObj))
+  {
+    return;
+  }
+
+  Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->StructureManager()->ObjectAffinity (theIObj);
+  Handle(Graphic3d_CView) aViewImpl = theView->View();
+  anAffinity->SetVisible (aViewImpl->Identification(), theIsVisible == Standard_True);
+  if (theIsVisible)
+  {
+    theView->View()->ChangeHiddenObjects()->Remove (theIObj.get());
   }
-  
-  //  LOCAL CONTEXT OPEN
   else
   {
-    myLocalContexts(myCurLocalIndex)->Display(anIObj,DispMode,anIObj->AcceptShapeDecomposition(),SelMode);
-    
-    if(anIObj->IsTransparent() && !myMainVwr->Viewer()->Transparency())
-      myMainVwr->Viewer()->SetTransparency(Standard_True);
-    
-    
-    if(updateviewer) myMainVwr->Update();
+    theView->View()->ChangeHiddenObjects()->Add (theIObj.get());
   }
 }
 
 //=======================================================================
 //function : Display
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Display(const Handle(AIS_InteractiveObject)& anIObj,
-                                    const Standard_Integer aDisplayMode, 
-                                    const Standard_Integer aSelectionMode, 
-                                    const Standard_Boolean updateviewer, 
-                                    const Standard_Boolean allowdecomposition)
+void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIObj,
+                                      const Standard_Integer               theDispMode,
+                                      const Standard_Integer               theSelectionMode,
+                                      const Standard_Boolean               theToUpdateViewer,
+                                      const AIS_DisplayStatus              theDispStatus)
 {
-  if(anIObj.IsNull()) return;
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
-  
-  // if no local context...
-  if(!HasOpenedContext()) {
-    //    if(!anIObj->HasDisplayMode())
-    //      anIObj->SetDisplayMode(aDisplayMode);
-  
-    if(!myObjects.IsBound(anIObj)){
-      Handle(AIS_GlobalStatus) STATUS= 
-        new AIS_GlobalStatus(AIS_DS_Displayed,aDisplayMode,aSelectionMode);
-      myObjects.Bind (anIObj,STATUS);
-      myMainPM->Display(anIObj,aDisplayMode);
-      if(aSelectionMode!=-1){
-        if(!mgrSelector->Contains(anIObj))
-          mgrSelector->Load(anIObj,aSelectionMode);
-        mgrSelector->Activate(anIObj,aSelectionMode,myMainSel);
-      }
-      if(updateviewer) myMainVwr->Update();
+  if (theDispStatus == AIS_DS_Erased)
+  {
+    Erase  (theIObj, theToUpdateViewer);
+    Load (theIObj, theSelectionMode);
+    if (Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (theIObj))
+    {
+      (*aStatusPtr)->SetDisplayMode (theDispMode);
     }
-    //    anIObj->SelectionMode(aSelectionMode);
-    else{
-      // CLE
-      // const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-      Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-      // ENDCLE
-      if(!STATUS->IsDModeIn(aDisplayMode)){
-        myMainPM->Display(anIObj,aDisplayMode);
-        STATUS->AddDisplayMode(aDisplayMode);
-        if(STATUS->IsSubIntensityOn())
-          myMainPM->Color(anIObj,mySubIntensity,aDisplayMode);
-      }
-      if(aSelectionMode!=-1){
-        STATUS->AddSelectionMode(aSelectionMode);
-        mgrSelector->Activate(anIObj,aSelectionMode,myMainSel);
+    return;
+  }
+
+  setContextToObject (theIObj);
+  if (!myObjects.IsBound (theIObj))
+  {
+    setObjectStatus (theIObj, AIS_DS_Displayed, theDispMode, theSelectionMode);
+    myMainVwr->StructureManager()->RegisterObject (theIObj);
+    myMainPM->Display(theIObj, theDispMode);
+    if (theSelectionMode != -1)
+    {
+      const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+      if (!mgrSelector->Contains (anObj))
+      {
+        mgrSelector->Load (theIObj);
       }
-      if(updateviewer) myMainVwr->Update();
-      
+      mgrSelector->Activate (theIObj, theSelectionMode);
     }
   }
   else
+  {
+    Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
+
+    // Mark the presentation modes hidden of interactive object different from aDispMode.
+    // Then make sure aDispMode is displayed and maybe highlighted.
+    // Finally, activate selection mode <SelMode> if not yet activated.
+    const Standard_Integer anOldMode = aStatus->DisplayMode();
+    if (anOldMode != theDispMode)
     {
-      myLocalContexts(myCurLocalIndex)->Display(anIObj,aDisplayMode,allowdecomposition,aSelectionMode);
-      if(updateviewer) myMainVwr->Update();
-    }  
-}
+      if(myMainPM->IsHighlighted (theIObj, anOldMode))
+      {
+        unhighlightGlobal (theIObj);
+      }
+      myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
+    }
+
+    aStatus->SetDisplayMode (theDispMode);
+
+    myMainPM->Display (theIObj, theDispMode);
+    aStatus->SetGraphicStatus (AIS_DS_Displayed);
+    if (aStatus->IsHilighted())
+    {
+      highlightGlobal (theIObj, aStatus->HilightStyle(), theDispMode);
+    }
+    if (theSelectionMode != -1)
+    {
+      const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+      if (!mgrSelector->Contains (anObj))
+      {
+        mgrSelector->Load (theIObj);
+      }
+      if (!mgrSelector->IsActivated (theIObj, theSelectionMode))
+      {
+        if (!aStatus->IsSModeIn (theSelectionMode))
+          aStatus->AddSelectionMode (theSelectionMode);
+        mgrSelector->Activate (theIObj, theSelectionMode);
+      }
+    }
+  }
 
+  if (theToUpdateViewer)
+  {
+    myMainVwr->Update();
+  }
+}
 
 //=======================================================================
 //function : Load
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Load(const Handle(AIS_InteractiveObject)& anIObj,
-                                  const Standard_Integer SelMode,
-                                  const Standard_Boolean AllowDecomposition)
+void AIS_InteractiveContext::Load (const Handle(AIS_InteractiveObject)& theIObj,
+                                   const Standard_Integer               theSelMode)
 {
-  if(anIObj.IsNull()) return;
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
-  
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  if(!HasOpenedContext()) {
-    if(SelMode==-1 && !AllowDecomposition){
-      Standard_Integer DispMode,HiMod,SelModeDef;
-      
-      GetDefModes(anIObj,DispMode,HiMod,SelModeDef);
-      Handle(AIS_GlobalStatus) STATUS= 
-        new AIS_GlobalStatus(AIS_DS_Erased,DispMode,SelModeDef);
-      myObjects.Bind (anIObj,STATUS);      
-      return;
-    }
+  setContextToObject (theIObj);
+  if (!myObjects.IsBound (theIObj))
+  {
+    Standard_Integer aDispMode, aHiMod, aSelModeDef;
+    GetDefModes (theIObj, aDispMode, aHiMod, aSelModeDef);
+    setObjectStatus (theIObj, AIS_DS_Erased, aDispMode, theSelMode != -1 ? theSelMode : aSelModeDef);
+    myMainVwr->StructureManager()->RegisterObject (theIObj);
   }
-  myLocalContexts(myCurLocalIndex)->Load(anIObj,AllowDecomposition,SelMode);
-}
 
+  // Register theIObj in the selection manager to prepare further activation of selection
+  const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+  if (!mgrSelector->Contains (anObj))
+  {
+    mgrSelector->Load (theIObj);
+  }
+}
 
 //=======================================================================
 //function : Erase
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Erase(const Handle(AIS_InteractiveObject)& anIObj, 
-                                   const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::Erase (const Handle(AIS_InteractiveObject)& theIObj,
+                                    const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return;
-  
-  if ( !anIObj->IsAutoHilight() )
-    anIObj->ClearSelected();
+  if (theIObj.IsNull())
+  {
+    return;
+  }
   
-  if(!HasOpenedContext()){
-    EraseGlobal(anIObj,updateviewer);
+  if (!theIObj->IsAutoHilight())
+  {
+    theIObj->ClearSelected();
+  }
+
+  EraseGlobal (theIObj, Standard_False);
+  if (theToUpdateViewer)
+  {
+    myMainVwr->Update();
   }
-  else
-    {
-      // First it is checked if it is possible to remove in the current local context
-      // then one tries to remove in other local contexts, if they allow it...
-      
-      Standard_Boolean WasInCtx = myLocalContexts(myCurLocalIndex)->Erase(anIObj);
-//      if(!WasInCtx) {
-      AIS_DataMapIteratorOfDataMapOfILC It(myLocalContexts);
-      for (;It.More();It.Next()){
-        if(It.Value()->AcceptErase())
-#ifdef BUC60722
-          WasInCtx |= It.Value()->Erase(anIObj);
-#else
-          WasInCtx = It.Value()->Erase(anIObj);
-#endif
-      }
-      
-      if(!WasInCtx)
-        EraseGlobal(anIObj,updateviewer);
-      else
-        if(updateviewer) myMainVwr->Update();
-    }
 }
 
 //=======================================================================
 //function : EraseAll
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::EraseAll(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::EraseAll (const Standard_Boolean theToUpdateViewer)
 {
-  if(!HasOpenedContext())
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
   {
-    for (AIS_DataMapIteratorOfDataMapOfIOStatus ItM(myObjects); ItM.More(); ItM.Next())
-    {
-      if(ItM.Value()->GraphicStatus() == AIS_DS_Displayed)
-        Erase(ItM.Key(),Standard_False);
-    }
-    if(updateviewer)
+    if (anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed)
     {
-      myMainVwr->Update();
+      Erase (anObjIter.Key(), Standard_False);
     }
   }
+
+  if (theToUpdateViewer)
+  {
+    myMainVwr->Update();
+  }
 }
 
 //=======================================================================
 //function : DisplayAll
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::DisplayAll(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::DisplayAll (const Standard_Boolean theToUpdateViewer)
 {
-  if(!HasOpenedContext()){
-
-    AIS_DisplayStatus aStatus;
-
-    if (!HasOpenedContext())
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    const AIS_DisplayStatus aStatus = anObjIter.Value()->GraphicStatus();
+    if (aStatus == AIS_DS_Erased)
     {
-      for (AIS_DataMapIteratorOfDataMapOfIOStatus ItM (myObjects); ItM.More(); ItM.Next())
-      {
-        aStatus = ItM.Value()->GraphicStatus();
-        if (aStatus == AIS_DS_Erased)
-          Display(ItM.Key(),Standard_False);
-      }
-      if(updateviewer)
-      {
-        myMainVwr->Update();
-      }
+      Display (anObjIter.Key(), Standard_False);
     }
   }
-  
+
+  if (theToUpdateViewer)
+  {
+    myMainVwr->Update();
+  }
 }
 
 //=======================================================================
 //function : DisplaySelected
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::DisplaySelected(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::DisplaySelected (const Standard_Boolean theToUpdateViewer)
 {
-  
-  if (!HasOpenedContext())
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
   {
-    Standard_Boolean found = Standard_False;
-    Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-    Handle(AIS_InteractiveObject) iObj;
-    for (sel->Init(); sel->More(); sel->Next())
-    {
-      iObj = Handle(AIS_InteractiveObject)::DownCast(sel->Value());
-      Display(iObj,Standard_False);
-      found = Standard_True;
-    }
-    if (found && updateviewer)
-    {
-      myMainVwr->Update();
-    }
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
+    Display (anObj, Standard_False);
   }
-}
 
+  if (theToUpdateViewer && !mySelection->Objects().IsEmpty())
+  {
+    myMainVwr->Update();
+  }
+}
 
 //=======================================================================
 //function : EraseSelected
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::EraseSelected(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::EraseSelected (const Standard_Boolean theToUpdateViewer)
 {
-  if (!HasOpenedContext())
+  Standard_Boolean isFound = Standard_False;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Init (mySelection->Objects()))
   {
-    Standard_Boolean found = Standard_False;
-    Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
-    Handle(AIS_InteractiveObject) iObj;
-    for (sel->Init();sel->More();sel->Next())
-    {
-      iObj = Handle(AIS_InteractiveObject)::DownCast(sel->Value());
-      Erase(iObj,Standard_False);
-      found = Standard_True;
-    }
-    if(found && updateviewer)
-    {
-      myMainVwr->Update();
-    }
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
+    Erase (anObj, Standard_False);
+    isFound = Standard_True;
   }
-}
-//=======================================================================
-//function : 
-//purpose  : 
-//=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::KeepTemporary(const Handle(AIS_InteractiveObject)& anIObj,
-                                                       const Standard_Integer WhichContext)
-{
-  if(anIObj.IsNull()) return Standard_False;
 
-  if(!HasOpenedContext()) return Standard_False;
-  if(myObjects.IsBound(anIObj)) return Standard_False;
-  if(WhichContext!=-1 && !myLocalContexts.IsBound(WhichContext)) return Standard_False;
-  
-  // Protection : if one tries to preserve a temporary object
-  // which is not in the local active context... rob 11-06-97
-
-  Standard_Integer IsItInLocal = myCurLocalIndex;
-  Standard_Boolean Found(Standard_False);
-
-  while(IsItInLocal>0 && !Found){
-    if(!myLocalContexts.IsBound(IsItInLocal))
-      IsItInLocal--;
-    else if(myLocalContexts(IsItInLocal)->IsIn(anIObj))
-      Found = Standard_True;
-    else
-      IsItInLocal--;
+  if (isFound && theToUpdateViewer)
+  {
+    myMainVwr->Update();
   }
-
-  if(!Found) return Standard_False;
-  
-
-//  const Handle(AIS_LocalStatus)& LS = (WhichContext== -1) ? 
-//    myLocalContexts(IsItInLocal)->Status(anIObj):myLocalContexts(WhichContext)->Status(anIObj);
-  // CLE
-  // const Handle(AIS_LocalStatus)& LS = myLocalContexts(IsItInLocal)->Status(anIObj);
-  Handle(AIS_LocalStatus) LS = myLocalContexts(IsItInLocal)->Status(anIObj);
-  // ENDCLE
-  
-  
-  if(LS->IsTemporary()){
-    Standard_Integer DM,HM,SM;
-    GetDefModes(anIObj,DM,HM,SM);
-    
-    SM = LS->SelectionModes().IsEmpty() ? SM : LS->SelectionModes().First();
-    if(LS->DisplayMode()!= DM ){
-      Standard_Integer LSM =  LS->SelectionModes().IsEmpty() ? -1 : LS->SelectionModes().First();
-      myLocalContexts(IsItInLocal)->Display(anIObj,DM,LS->Decomposed(),LSM);
-    }
-
-    Handle (AIS_GlobalStatus) GS = new AIS_GlobalStatus(AIS_DS_Displayed,
-                                                        DM,
-                                                        SM,
-                                                        Standard_False);
-//    GS->SubIntensityOn();
-    myObjects.Bind(anIObj,GS);
-    mgrSelector->Load(anIObj);
-    mgrSelector->Activate(anIObj,SM,myMainSel);
-    
-    LS->SetTemporary(Standard_False);
-  }                                 
-  return Standard_True;
 }
 
-
 //=======================================================================
-//purpose  : 
+//function : DisplayStatus
+//purpose  :
 //=======================================================================
-
-AIS_DisplayStatus AIS_InteractiveContext::DisplayStatus(const Handle(AIS_InteractiveObject)& anIObj) const 
+AIS_DisplayStatus AIS_InteractiveContext::DisplayStatus (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  if(anIObj.IsNull()) return AIS_DS_None;
-
-  if(myObjects.IsBound(anIObj))
-    return myObjects(anIObj)->GraphicStatus();
-
-  AIS_DataMapIteratorOfDataMapOfILC ItM(myLocalContexts);
-  for(;ItM.More();ItM.Next()){
-    if(ItM.Value()->IsIn(anIObj))
-      return AIS_DS_Temporary;
+  if (theIObj.IsNull())
+  {
+    return AIS_DS_None;
   }
-  
-  return AIS_DS_None;
-  
-}
-
-
-
-
-//=======================================================================
-//function : DisplayedModes
-//purpose  : 
-//=======================================================================
-
-const TColStd_ListOfInteger& AIS_InteractiveContext::
-DisplayedModes(const Handle(AIS_InteractiveObject)& anIObj) const 
-{
-  return myObjects(anIObj)->DisplayedModes();
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
+  return aStatus != NULL ? (*aStatus)->GraphicStatus() : AIS_DS_None;
 }
 
-
 //=======================================================================
 //function : Remove
-//purpose  : 
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::Remove(const Handle(AIS_InteractiveObject)& anIObj,
-                                    const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::Remove (const Handle(AIS_InteractiveObject)& theIObj,
+                                     const Standard_Boolean               theToUpdateViewer)
 {
-
-  if(anIObj.IsNull()) return;
-
-  if(!HasOpenedContext()){ 
-    ClearGlobal(anIObj,updateviewer);
+  if (theIObj.IsNull())
+  {
+    return;
   }
-  else
+
+  if (theIObj->HasInteractiveContext())
+  {
+    if (theIObj->myCTXPtr != this)
     {
-      myLocalContexts(myCurLocalIndex)->Remove(anIObj);
-      AIS_DataMapIteratorOfDataMapOfILC It(myLocalContexts);
-      for (;It.More() ;It.Next()){
-        if(It.Value()->AcceptErase())
-          It.Value()->Remove(anIObj);
-        
-      }
-      //      if(!WasInCtx)
-      ClearGlobal(anIObj,updateviewer);
-//      else
-//      if(updateviewer) myMainVwr->Update();
+      throw Standard_ProgramError("AIS_InteractiveContext - object has been displayed in another context!");
     }
+    theIObj->SetContext (Handle(AIS_InteractiveContext)());
+  }
+  ClearGlobal (theIObj, theToUpdateViewer);
 }
 
 //=======================================================================
 //function : RemoveAll
-//purpose  : 
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::RemoveAll(const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::RemoveAll (const Standard_Boolean theToUpdateViewer)
 {
   AIS_ListOfInteractive aList;
-  ObjectsInside(aList);
-  AIS_ListIteratorOfListOfInteractive aListIterator;
-  for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
-    Remove(aListIterator.Value(), Standard_False);
+  ObjectsInside (aList);
+  for (AIS_ListIteratorOfListOfInteractive aListIterator (aList); aListIterator.More(); aListIterator.Next())
+  {
+    Remove (aListIterator.Value(), Standard_False);
   }
-       
-  if(updateviewer)
+
+  if (theToUpdateViewer)
+  {
     myMainVwr->Update();
+  }
 }
 
 //=======================================================================
-//function : Clear
+//function : HilightWithColor
 //purpose  : 
 //=======================================================================
-
-void AIS_InteractiveContext::Clear(const Handle(AIS_InteractiveObject)& anIObj,
-                                   const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject)& theObj,
+                                              const Handle(Prs3d_Drawer)& theStyle,
+                                              const Standard_Boolean theIsToUpdate)
 {
-  if(anIObj.IsNull()) return;
-
-  if(!HasOpenedContext()){ 
-    ClearGlobal(anIObj,updateviewer);
-  }
-  else
-    {
-      
-      Standard_Boolean  WasInCtx = myLocalContexts(myCurLocalIndex)->Remove(anIObj);
-      AIS_DataMapIteratorOfDataMapOfILC It(myLocalContexts);
-      for (;It.More() ;It.Next()){
-        if(It.Value()->AcceptErase())
-          WasInCtx = It.Value()->Remove(anIObj);
-        
-      }
-      if(!WasInCtx)
-        ClearGlobal(anIObj,updateviewer);
-      else
-        if(updateviewer) myMainVwr->Update();
-    }
-}
-
-
-
+  if (theObj.IsNull())
+    return;
 
-//=======================================================================
-//function : ClearPrs
-//purpose  : 
-//=======================================================================
+  setContextToObject (theObj);
+  if (!myObjects.IsBound (theObj))
+    return;
 
-void AIS_InteractiveContext::ClearPrs(const Handle(AIS_InteractiveObject)& anIObj,
-                                      const Standard_Integer aMode,
-                                      const Standard_Boolean updateviewer)
-{
-  if(anIObj.IsNull()) return;
+  const Handle(AIS_GlobalStatus)& aStatus = myObjects (theObj);
+  aStatus->SetHilightStatus (Standard_True);
 
-  if(!HasOpenedContext()){
-    ClearGlobalPrs(anIObj,aMode,updateviewer);
+  if (aStatus->GraphicStatus() == AIS_DS_Displayed)
+  {
+    highlightGlobal (theObj, theStyle, aStatus->DisplayMode());
+    aStatus->SetHilightStyle (theStyle);
   }
-  else
-    {
-      Standard_Boolean  WasInCtx = myLocalContexts(myCurLocalIndex)->ClearPrs(anIObj,aMode);
-      AIS_DataMapIteratorOfDataMapOfILC It(myLocalContexts);
-      for (;It.More() ;It.Next()){
-        if(It.Value()->AcceptErase())
-          WasInCtx = It.Value()->ClearPrs(anIObj,aMode);
-      }
-      if(!WasInCtx)
-        ClearGlobalPrs(anIObj,aMode,updateviewer);
-      else
-        if(updateviewer) myMainVwr->Update();
-      
-    }
+
+  if (theIsToUpdate)
+    myMainVwr->Update();
 }
 
 //=======================================================================
-//function : Hilight
+//function : Unhilight
 //purpose  : 
 //=======================================================================
-
-void AIS_InteractiveContext::Hilight(const Handle(AIS_InteractiveObject)& anIObj,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::Unhilight(const Handle(AIS_InteractiveObject)& anIObj, const Standard_Boolean updateviewer)
 {
   if(anIObj.IsNull()) return;
+  if(!myObjects.IsBound(anIObj)) return;
 
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
-  if (!HasOpenedContext())
-  {
-    if(!myObjects.IsBound(anIObj)) return;
-
-    // CLE
-    // const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-    // const TColStd_ListOfInteger& LL = STATUS->DisplayedModes();
-    Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-
-    // ENDCLE
-    STATUS->SetHilightStatus (Standard_True);
+  const Handle(AIS_GlobalStatus)& aStatus = myObjects(anIObj);
+  aStatus->SetHilightStatus (Standard_False);
+  aStatus->SetHilightStyle (Handle(Prs3d_Drawer)());
 
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed)
-    {
-      Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
-      myMainPM->Highlight (anIObj, aHilightMode);
-    }
-  }
-  else
+  if (aStatus->GraphicStatus() == AIS_DS_Displayed)
   {
-    myLocalContexts(myCurLocalIndex)->Hilight(anIObj);
+    unhighlightGlobal (anIObj);
   }
 
   if(updateviewer) myMainVwr->Update();
 }
+
 //=======================================================================
-//function : Hilight
-//purpose  : 
+//function : IsHilighted
+//purpose  : Returns true if the objects global status is set to highlighted.
 //=======================================================================
+Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(AIS_InteractiveObject)& theObj) const
+{
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
+  return aStatus != NULL
+      && (*aStatus)->IsHilighted();
+}
 
-void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject)& anIObj,
-                                              const Quantity_NameOfColor aCol,
-                                              const Standard_Boolean updateviewer)
+//=======================================================================
+//function : IsHilighted
+//purpose  : Returns true if the owner is highlighted with selection style.
+//=======================================================================
+Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(SelectMgr_EntityOwner)& theOwner) const
 {
-  if(anIObj.IsNull()) return;
+  if (theOwner.IsNull() || !theOwner->HasSelectable())
+    return Standard_False;
 
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
+  const Handle(AIS_InteractiveObject) anObj =
+    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
 
-  if (!HasOpenedContext())
+  if (anObj->GlobalSelOwner() == theOwner)
   {
-    if(!myObjects.IsBound(anIObj)) return;
-
-    const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-    STATUS->SetHilightStatus (Standard_True);
+    if (!myObjects.IsBound (anObj))
+      return Standard_False;
 
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed)
-    {
-      Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
-      myMainPM->Color (anIObj, aCol, aHilightMode);
-      STATUS->SetHilightColor (aCol);
-    }
-  }
-  else
-  {
-    myLocalContexts(myCurLocalIndex)->Hilight(anIObj,aCol);
+    return myObjects (anObj)->IsHilighted();
   }
-  if(updateviewer) myMainVwr->Update();
+
+  const Handle(Prs3d_Drawer)& aStyle = getSelStyle (anObj, theOwner);
+  const Standard_Integer aHiMode = getHilightMode (anObj, aStyle, -1);
+  return theOwner->IsHilighted (myMainPM, aHiMode);
 }
 
 //=======================================================================
-//function : Unhilight
-//purpose  : 
+//function : HighlightStyle
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Unhilight(const Handle(AIS_InteractiveObject)& anIObj, const Standard_Boolean updateviewer)
+Standard_Boolean AIS_InteractiveContext::HighlightStyle (const Handle(AIS_InteractiveObject)& theObj,
+                                                         Handle(Prs3d_Drawer)& theStyle) const
 {
-  if(anIObj.IsNull()) return;
-
-  if (!HasOpenedContext())
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
+  if (aStatus != NULL
+   && (*aStatus)->IsHilighted())
   {
-    if(!myObjects.IsBound(anIObj)) return;
-
-    const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-    STATUS->SetHilightStatus (Standard_False);
-    STATUS->SetHilightColor(Quantity_NOC_WHITE);
-
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed)
-    {
-      Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
-      myMainPM->Unhighlight (anIObj, aHilightMode);
-    }
+    theStyle = (*aStatus)->HilightStyle();
+    return Standard_True;
   }
-  else
-  {
-    myLocalContexts(myCurLocalIndex)->Unhilight(anIObj);
-  }
-  if(updateviewer) myMainVwr->Update();
+
+  theStyle.Nullify();
+  return Standard_False;
 }
 
 //=======================================================================
-//function : IsHilighted
-//purpose  : 
+//function : HighlightStyle
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::IsHilighted(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Boolean AIS_InteractiveContext::HighlightStyle (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                         Handle(Prs3d_Drawer)& theStyle) const
 {
-  if(anIObj.IsNull()) return Standard_False;
-
-  if (!HasOpenedContext()){
-    if(!myObjects.IsBound(anIObj)) 
-      return Standard_False;
-    return myObjects(anIObj)->IsHilighted();
-  }
-  AIS_DataMapIteratorOfDataMapOfILC ItM(myLocalContexts);
-  for(;ItM.More();ItM.Next()){
-    if(ItM.Value()->IsHilighted(anIObj))
-      return Standard_True;
-  }
-  return Standard_False;
-}
+  if (theOwner.IsNull() || !theOwner->HasSelectable())
+    return Standard_False;
 
-Standard_Boolean AIS_InteractiveContext::IsHilighted(const Handle(AIS_InteractiveObject)& anIObj,
-                                                     Standard_Boolean& WithColor,
-                                                     Quantity_NameOfColor& TheHiCol) const
-{
-  if(!HasOpenedContext()){
-    if(myObjects.IsBound(anIObj)){
-      const Handle(AIS_GlobalStatus)& STAT = myObjects(anIObj);
-      if(STAT->IsHilighted()){
-        if(STAT->HilightColor()!=Quantity_NOC_WHITE){
-          WithColor=Standard_True;
-          TheHiCol = STAT->HilightColor();
-        }
-        else
-          WithColor = Standard_False;
-        return Standard_True;
-      }
+  if (IsHilighted (theOwner))
+  {
+    const Handle(AIS_InteractiveObject) anObj =
+      Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+    if (anObj->GlobalSelOwner() == theOwner)
+    {
+      theStyle = myObjects (anObj)->HilightStyle();
     }
-    return Standard_False;
-  }
-  Standard_Integer MaxIndex = HighestIndex();
-  for(Standard_Integer i=MaxIndex;i>=1 ; i--){
-    if(myLocalContexts.IsBound(i)){
-      if(myLocalContexts(i)->IsHilighted(anIObj,WithColor,TheHiCol))
-        return Standard_True;
+    else
+    {
+      // since part selection style is not stored in global status,
+      // check if the object has own selection style. If not, it can
+      // only be highlighted with default selection style (because
+      // sub-intensity does not modify any selection states)
+      theStyle = getSelStyle (anObj, theOwner);
     }
-    
+    return Standard_True;
+  }
+  else
+  {
+    theStyle.Nullify();
+    return Standard_False;
   }
-  return Standard_False;
 }
 
-
-
 //=======================================================================
 //function : IsDisplayed
 //purpose  : 
 //=======================================================================
 
-Standard_Boolean AIS_InteractiveContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Boolean AIS_InteractiveContext::IsDisplayed(const Handle(AIS_InteractiveObject)& theObj) const 
 {
-  if(anIObj.IsNull()) return Standard_False;
-
+  if(theObj.IsNull()) return Standard_False;
 
-  if(myObjects.IsBound(anIObj)) 
-    if(myObjects(anIObj)->GraphicStatus()==AIS_DS_Displayed)
-      return Standard_True;
-  
-  AIS_DataMapIteratorOfDataMapOfILC ItM(myLocalContexts);
-  for(;ItM.More();ItM.Next()){
-    if(ItM.Value()->IsDisplayed(anIObj))
-      return Standard_True;
-  }
-  return Standard_False;
-  
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
+  return aStatus != NULL
+    && (*aStatus)->GraphicStatus() == AIS_DS_Displayed; 
 }
 
 //=======================================================================
 //function : IsDisplayed
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::
-IsDisplayed(const Handle(AIS_InteractiveObject)& anIObj,
-            const Standard_Integer aMode) const 
+Standard_Boolean AIS_InteractiveContext::IsDisplayed (const Handle(AIS_InteractiveObject)& theIObj,
+                                                      const Standard_Integer               theMode) const
 {
-  if(anIObj.IsNull()) return Standard_False;
-  if(myObjects.IsBound(anIObj)) {
-    if(myObjects(anIObj)->GraphicStatus()==AIS_DS_Displayed &&
-       myObjects(anIObj)->IsDModeIn(aMode)) 
-      return Standard_True;
-  }
-  
-  AIS_DataMapIteratorOfDataMapOfILC ItM(myLocalContexts);
-  for(;ItM.More();ItM.Next()){
-    if(ItM.Value()->IsDisplayed(anIObj,aMode))
-      return Standard_True;
+  if (theIObj.IsNull())
+  {
+    return Standard_False;
   }
-  return Standard_False;
-}
 
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
+  return aStatus != NULL
+      && (*aStatus)->GraphicStatus() == AIS_DS_Displayed
+      && (*aStatus)->DisplayMode() == theMode;
+}
 
 //=======================================================================
 //function : DisplayPriority
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Integer AIS_InteractiveContext::
-DisplayPriority(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Integer AIS_InteractiveContext::DisplayPriority (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  if(anIObj.IsNull()) return -1;
+  if (theIObj.IsNull())
+  {
+    return -1;
+  }
 
-  if (myObjects.IsBound(anIObj))
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
+  if (aStatus != NULL
+   && ((*aStatus)->GraphicStatus() == AIS_DS_Displayed
+    || (*aStatus)->GraphicStatus() == AIS_DS_Erased))
   {
-    Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed || STATUS->GraphicStatus() == AIS_DS_Erased)
-    {
-      Standard_Integer aDispMode = anIObj->HasDisplayMode() ? anIObj->DisplayMode() : 
-                                   (anIObj->AcceptDisplayMode(myDisplayMode)? myDisplayMode : 0);
-      return myMainPM->DisplayPriority (anIObj, aDispMode);
-    }
+    Standard_Integer aDispMode = theIObj->HasDisplayMode()
+                               ? theIObj->DisplayMode()
+                               : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
+                                ? myDefaultDrawer->DisplayMode()
+                                : 0);
+    return myMainPM->DisplayPriority (theIObj, aDispMode);
   }
   return 0;
 }
+
 //=======================================================================
 //function : SetDisplayPriority
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDisplayPriority(const Handle(AIS_InteractiveObject)& anIObj,
-                                               const Standard_Integer aPriority)
+void AIS_InteractiveContext::SetDisplayPriority (const Handle(AIS_InteractiveObject)& theIObj,
+                                                 const Standard_Integer               thePriority)
 {
-  if(anIObj.IsNull())
-    return;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  if(myObjects.IsBound(anIObj))
+  if (theIObj.IsNull())
   {
-    Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed || STATUS->GraphicStatus() == AIS_DS_Erased)
-    {
-      Standard_Integer aDisplayMode = anIObj->HasDisplayMode() ? anIObj->DisplayMode() : 
-                                      (anIObj->AcceptDisplayMode(myDisplayMode)? myDisplayMode : 0);
-      myMainPM->SetDisplayPriority (anIObj, aDisplayMode, aPriority);
-    }
+    return;
   }
-  else if (HasOpenedContext())
+
+  setContextToObject (theIObj);
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
+  if (aStatus != NULL
+   && ((*aStatus)->GraphicStatus() == AIS_DS_Displayed
+    || (*aStatus)->GraphicStatus() == AIS_DS_Erased))
   {
-    myLocalContexts(myCurLocalIndex)->SetDisplayPriority(anIObj,aPriority);
+    Standard_Integer aDisplayMode = theIObj->HasDisplayMode()
+                                  ? theIObj->DisplayMode()
+                                  : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
+                                    ? myDefaultDrawer->DisplayMode()
+                                    : 0);
+    myMainPM->SetDisplayPriority (theIObj, aDisplayMode, thePriority);
   }
 }
 
 //=======================================================================
 //function : Redisplay
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Redisplay(const Handle(AIS_InteractiveObject)& anIObj,
-                                      const Standard_Boolean updateviewer,
-                                      const Standard_Boolean allmodes)
+void AIS_InteractiveContext::Redisplay (const Handle(AIS_InteractiveObject)& theIObj,
+                                        const Standard_Boolean               theToUpdateViewer,
+                                        const Standard_Boolean               theAllModes)
 {
-  if(AISDebugModeOn()){
-    cout<<"===>AIS_InteractiveContext::Redisplay de :";
-    cout<<anIObj->DynamicType()->Name()<<endl;
-  }
-
-  RecomputePrsOnly(anIObj,updateviewer,allmodes);
-  RecomputeSelectionOnly(anIObj);
+  RecomputePrsOnly (theIObj, theToUpdateViewer, theAllModes);
+  RecomputeSelectionOnly (theIObj);
 }
 
 //=======================================================================
 //function : Redisplay
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Redisplay(const AIS_KindOfInteractive KOI,
-                                       const Standard_Integer /*Sign*/,
-                                      const Standard_Boolean updateviewer)
-{
-  Standard_Boolean found_viewer(Standard_False);
-  // update
-  for(AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);It.More();It.Next()){
-    // CLE
-    // const Handle(AIS_InteractiveObject)& IO = It.Key();
-    Handle(AIS_InteractiveObject) IO = It.Key();
-    // ENDCLE
-    if(IO->Type()== KOI){ 
-#ifdef DEB
-//      Standard_Boolean good = (Sign==-1)? Standard_True : 
-//        ((IO->Signature()==Sign)? Standard_True:Standard_False);
-#endif
-      Redisplay(IO,Standard_False);
-      if (It.Value()->GraphicStatus() == AIS_DS_Displayed)
-      {
-        found_viewer = Standard_True;
-      }
+void AIS_InteractiveContext::Redisplay (const AIS_KindOfInteractive theKOI,
+                                        const Standard_Integer    /*theSign*/,
+                                        const Standard_Boolean      theToUpdateViewer)
+{
+  Standard_Boolean isRedisplayed = Standard_False;
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
+    if (anObj->Type() != theKOI)
+    {
+      continue;
     }
+
+    Redisplay (anObj, Standard_False);
+    isRedisplayed = anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed
+                 || isRedisplayed;
   }
-  // update viewer...
-  if(updateviewer && found_viewer)
+
+  if (theToUpdateViewer
+   && isRedisplayed)
   {
     myMainVwr->Update();
   }
 }
 
-
 //=======================================================================
 //function : RecomputePrsOnly
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::RecomputePrsOnly(const Handle(AIS_InteractiveObject)& anIObj,
-                                              const Standard_Boolean updateviewer,
-                                              const Standard_Boolean allmodes)
+void AIS_InteractiveContext::RecomputePrsOnly (const Handle(AIS_InteractiveObject)& theIObj,
+                                               const Standard_Boolean               theToUpdateViewer,
+                                               const Standard_Boolean               theAllModes)
 {
-  if(anIObj.IsNull()) return;
-  anIObj->Update(allmodes);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  if (!updateviewer)
+  theIObj->SetToUpdate();
+  theIObj->UpdatePresentations (theAllModes);
+  if (!theToUpdateViewer)
   {
     return;
   }
 
-  if (HasOpenedContext() ||
-     (myObjects.IsBound(anIObj) && myObjects(anIObj)->GraphicStatus() == AIS_DS_Displayed))
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
+  if (aStatus != NULL
+   && (*aStatus)->GraphicStatus() == AIS_DS_Displayed)
   {
     myMainVwr->Update();
   }
@@ -1243,203 +1007,197 @@ void AIS_InteractiveContext::RecomputePrsOnly(const Handle(AIS_InteractiveObject
 //function : RecomputeSelectionOnly
 //purpose  : 
 //=======================================================================
-void AIS_InteractiveContext::RecomputeSelectionOnly(const Handle(AIS_InteractiveObject)& anIObj)
+void AIS_InteractiveContext::RecomputeSelectionOnly (const Handle(AIS_InteractiveObject)& theIO)
 {
-  if(anIObj.IsNull()) return;
-  mgrSelector->RecomputeSelection(anIObj);
-
+  if (theIO.IsNull())
+  {
+    return;
+  }
 
+  mgrSelector->RecomputeSelection (theIO);
 
-  TColStd_ListOfInteger LI;
-  TColStd_ListIteratorOfListOfInteger Lit;
-  ActivatedModes(anIObj,LI);
-  if(!HasOpenedContext()){
-    if(!myObjects.IsBound(anIObj)) return;
+  const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIO);
+  if (aStatus == NULL
+  || (*aStatus)->GraphicStatus() != AIS_DS_Displayed)
+  {
+    return;
+  }
 
-    if (myObjects(anIObj)->GraphicStatus() == AIS_DS_Displayed)
-    {
-      for(Lit.Initialize(LI);Lit.More();Lit.Next())
-      {
-        mgrSelector->Activate(anIObj,Lit.Value(),myMainSel);
-      }
-    }
+  TColStd_ListOfInteger aModes;
+  ActivatedModes (theIO, aModes);
+  TColStd_ListIteratorOfListOfInteger aModesIter (aModes);
+  for (; aModesIter.More(); aModesIter.Next())
+  {
+    mgrSelector->Activate (theIO, aModesIter.Value());
   }
 }
 
 //=======================================================================
 //function : Update
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::Update(const Handle(AIS_InteractiveObject)& anIObj,
-                                    const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::Update (const Handle(AIS_InteractiveObject)& theIObj,
+                                     const Standard_Boolean               theUpdateViewer)
 {
-  if(anIObj.IsNull()) return;
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  
+  theIObj->UpdatePresentations();
+  mgrSelector->Update(theIObj);
 
-  TColStd_ListOfInteger LL;
-  anIObj->ToBeUpdated(LL);
-  TColStd_ListIteratorOfListOfInteger ITI(LL);
-  Standard_Boolean wasupdated(Standard_False);
-  
-  for (;ITI.More();ITI.Next()){
-    anIObj->Update(ITI.Value(),Standard_False);
-    wasupdated = Standard_True;
-  }
-  
-  if(wasupdated)
-    mgrSelector->Update(anIObj);
-  
-  if(updateviewer){
-    if(!myObjects.IsBound(anIObj)) return;
-    switch(myObjects(anIObj)->GraphicStatus()){
-    case AIS_DS_Displayed:
-    case AIS_DS_Temporary:
+  if (theUpdateViewer)
+  {
+    const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
+    if (aStatus != NULL
+     && (*aStatus)->GraphicStatus() == AIS_DS_Displayed)
+    {
       myMainVwr->Update();
-      break;
-    default:
-      break;
     }
   }
 }
 
-
-
 //=======================================================================
 //function : SetLocation
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetLocation(const Handle(AIS_InteractiveObject)& anIObj,
-                                         const TopLoc_Location& aLoc)
+void AIS_InteractiveContext::SetLocation (const Handle(AIS_InteractiveObject)& theIObj,
+                                          const TopLoc_Location&               theLoc)
 {
-  if(anIObj.IsNull()) return;
-
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  if(anIObj->HasLocation() && aLoc.IsIdentity()){
-    anIObj->ResetLocation();
-    mgrSelector->Update(anIObj,Standard_False);
+  if (theIObj->HasTransformation()
+   && theLoc.IsIdentity())
+  {
+    theIObj->ResetTransformation();
+    mgrSelector->Update (theIObj, Standard_False);
+    return;
+  }
+  else if (theLoc.IsIdentity())
+  {
     return;
   }
-  if(aLoc.IsIdentity()) return ;
 
   // first reset the previous location to properly clean everything...
-  if(anIObj->HasLocation())
-    anIObj->ResetLocation();
+  if (theIObj->HasTransformation())
+  {
+    theIObj->ResetTransformation();
+  }
 
+  theIObj->SetLocalTransformation (theLoc.Transformation());
 
-  anIObj->SetLocation(aLoc);
-  
-  if(!HasOpenedContext())
-    mgrSelector->Update(anIObj,Standard_False);
-  else
-    {
-      // CLE
-      // const Handle(StdSelect_ViewerSelector3d)& tempSel = myLocalContexts(myCurLocalIndex)->MainSelector();
-      Handle(StdSelect_ViewerSelector3d) tempSel = myLocalContexts(myCurLocalIndex)->MainSelector();
-      // ENDCLE
-      mgrSelector->Update(anIObj,tempSel,Standard_False);
-    }
+  mgrSelector->Update (theIObj, Standard_False);
+
+  // if the object or its part is highlighted dynamically, it is necessary to apply location transformation
+  // to its highlight structure immediately
+  if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (theIObj))
+  {
+    const Standard_Integer aHiMod = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+    myLastPicked->UpdateHighlightTrsf (myMainVwr,
+                                       myMainPM,
+                                       aHiMod);
+  }
 }
+
 //=======================================================================
 //function : ResetLocation
-//purpose  : 
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::ResetLocation(const Handle(AIS_InteractiveObject)& anIObj)
+void AIS_InteractiveContext::ResetLocation (const Handle(AIS_InteractiveObject)& theIObj)
 {
-  if(anIObj.IsNull()) return;
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  anIObj->ResetLocation();
-  mgrSelector->Update(anIObj,Standard_False);
+  theIObj->ResetTransformation();
+  mgrSelector->Update (theIObj, Standard_False);
 }
 
 //=======================================================================
 //function : HasLocation
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::
-HasLocation(const Handle(AIS_InteractiveObject)& anIObj) const
+Standard_Boolean AIS_InteractiveContext::HasLocation (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  if(anIObj.IsNull()) return Standard_False;
-
-  return anIObj->HasLocation();
+  return !theIObj.IsNull()
+       && theIObj->HasTransformation();
 }
 
-const TopLoc_Location& AIS_InteractiveContext::
-Location(const Handle(AIS_InteractiveObject)& anIObj) const
+//=======================================================================
+//function : Location
+//purpose  :
+//=======================================================================
+TopLoc_Location AIS_InteractiveContext::Location (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  return anIObj->Location();
+  return theIObj->Transformation();
 }
 
 //=======================================================================
 //function : SetDeviationCoefficient
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDeviationCoefficient(const Standard_Real aCoefficient)
+void AIS_InteractiveContext::SetDeviationCoefficient (const Standard_Real theCoefficient)
 {
-  myDefaultDrawer->SetDeviationCoefficient(aCoefficient);
+  myDefaultDrawer->SetDeviationCoefficient (theCoefficient);
 }
+
 //=======================================================================
 //function : SetDeviationAngle
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDeviationAngle(const Standard_Real anAngle)
+void AIS_InteractiveContext::SetDeviationAngle (const Standard_Real theAngle)
 {
-  myDefaultDrawer->SetDeviationCoefficient(anAngle);
+  myDefaultDrawer->SetDeviationAngle (theAngle);
 }
 
 //=======================================================================
 //function : DeviationAngle
 //purpose  : Gets  deviationAngle
 //=======================================================================
-
 Standard_Real AIS_InteractiveContext::DeviationAngle() const
 {
-  //return M_PI/180.0e0 ;
-    return myDefaultDrawer->DeviationAngle();
+  return myDefaultDrawer->DeviationAngle();
 }
 
 //=======================================================================
 //function : DeviationCoefficient
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Real AIS_InteractiveContext::DeviationCoefficient() const 
+Standard_Real AIS_InteractiveContext::DeviationCoefficient() const
 {
   return myDefaultDrawer->DeviationCoefficient();
 }
+
 //=======================================================================
 //function : SetHLRDeviationCoefficient
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetHLRDeviationCoefficient(const Standard_Real aCoefficient)
+void AIS_InteractiveContext::SetHLRDeviationCoefficient (const Standard_Real theCoefficient)
 {
-  myDefaultDrawer->SetHLRDeviationCoefficient(aCoefficient);
+  myDefaultDrawer->SetHLRDeviationCoefficient (theCoefficient);
 }
 
 //=======================================================================
 //function : HLRDeviationCoefficient
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Real AIS_InteractiveContext::HLRDeviationCoefficient() const 
+Standard_Real AIS_InteractiveContext::HLRDeviationCoefficient() const
 {
   return myDefaultDrawer->HLRDeviationCoefficient();
 }
 
 //=======================================================================
 //function : SetHLRAngle
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetHLRAngle(const Standard_Real anAngle)
+void AIS_InteractiveContext::SetHLRAngle (const Standard_Real theAngle)
 {
-  myDefaultDrawer->SetHLRAngle(anAngle);
+  myDefaultDrawer->SetHLRAngle (theAngle);
 }
 
 //=======================================================================
@@ -1448,23 +1206,19 @@ void AIS_InteractiveContext::SetHLRAngle(const Standard_Real anAngle)
 //           and set them in myHLRAngle and in myHLRDeviationCoefficient
 //           of myDefaultDrawer 
 //=======================================================================
-
-void AIS_InteractiveContext::SetHLRAngleAndDeviation(const Standard_Real anAngle)
+void AIS_InteractiveContext::SetHLRAngleAndDeviation (const Standard_Real theAngle)
 {
+  Standard_Real anOutAngl, anOutDefl;
+  HLRBRep::PolyHLRAngleAndDeflection (theAngle, anOutAngl, anOutDefl);
 
-  Standard_Real OutAngl,OutDefl;
-  HLRBRep::PolyHLRAngleAndDeflection(anAngle,OutAngl,OutDefl);
-
-  myDefaultDrawer->SetHLRAngle(OutAngl);
-  myDefaultDrawer->SetHLRDeviationCoefficient(OutDefl);
-
+  myDefaultDrawer->SetHLRAngle                (anOutAngl);
+  myDefaultDrawer->SetHLRDeviationCoefficient (anOutDefl);
 }
 
 //=======================================================================
 //function : HLRAngle
-//purpose  : 
+//purpose  :
 //=======================================================================
-
 Standard_Real AIS_InteractiveContext::HLRAngle() const 
 {
   return myDefaultDrawer->HLRAngle();
@@ -1472,1396 +1226,1381 @@ Standard_Real AIS_InteractiveContext::HLRAngle() const
 
 //=======================================================================
 //function : SetDisplayMode
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDisplayMode(const AIS_DisplayMode aMode,
-                                           const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetDisplayMode(const Standard_Integer theMode,
+                                            const Standard_Boolean theToUpdateViewer)
 {
-  if(aMode==myDisplayMode) return;
-  AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);
+  if (theMode == myDefaultDrawer->DisplayMode())
+  {
+    return;
+  }
 
-  for(;It.More();It.Next()){
-    // CLE
-    // const Handle(AIS_InteractiveObject)& anObj = It.Key();
-    Handle(AIS_InteractiveObject) anObj = It.Key();
-    // ENDCLE
-    Standard_Boolean Processed = (anObj->IsKind(STANDARD_TYPE(AIS_Shape)) ||
-                                  anObj->IsKind(STANDARD_TYPE(AIS_ConnectedShape)) ||
-                                  anObj->IsKind(STANDARD_TYPE(AIS_MultipleConnectedShape)) );
+  for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
+  {
+    Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
+    Standard_Boolean toProcess = anObj->IsKind (STANDARD_TYPE(AIS_Shape))
+                              || anObj->IsKind (STANDARD_TYPE(AIS_ConnectedInteractive))
+                              || anObj->IsKind (STANDARD_TYPE(AIS_MultipleConnectedInteractive));
     
-    if ((!anObj->HasDisplayMode()) && Processed) 
+    if (!toProcess
+     ||  anObj->HasDisplayMode()
+     || !anObj->AcceptDisplayMode (theMode))
+    {
+      continue;
+    }
+
+    Handle(AIS_GlobalStatus) aStatus = anObjIter.Value();
+    aStatus->SetDisplayMode (theMode);
+
+    if (aStatus->GraphicStatus() == AIS_DS_Displayed)
+    {
+      myMainPM->Display (anObj, theMode);
+      if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (anObj))
       {
-        if(anObj->AcceptDisplayMode(aMode)){
-          // CLE
-          // const Handle(AIS_GlobalStatus)& STATUS = It.Value();
-          Handle(AIS_GlobalStatus) STATUS = It.Value();
-          // ENDCLE
-          if(STATUS->IsDModeIn(myDisplayMode))
-            STATUS->RemoveDisplayMode(myDisplayMode);
-          
-          STATUS->AddDisplayMode(aMode);
-          
-          if(STATUS->GraphicStatus()== AIS_DS_Displayed){
-            myMainPM->SetVisibility (anObj, myDisplayMode, Standard_False);
-            myMainPM->Display(anObj, aMode);
-            if(STATUS->IsSubIntensityOn())
-              myMainPM->Color(anObj,mySubIntensity,aMode);
-          }
-          //  myDisplayMode = aMode;
-        }
+        myMainPM->BeginImmediateDraw();
+        unhighlightGlobal (anObj);
+        myMainPM->EndImmediateDraw (myMainVwr);
       }
-    
-  } 
-  myDisplayMode = aMode;
-  if(updateviewer) myMainVwr->Update();
+      if (aStatus->IsSubIntensityOn())
+      {
+        highlightWithSubintensity (anObj, theMode);
+      }
+      myMainPM->SetVisibility (anObj, myDefaultDrawer->DisplayMode(), Standard_False);
+    }
+  }
+
+  myDefaultDrawer->SetDisplayMode (theMode);
+  if (theToUpdateViewer)
+  {
+    myMainVwr->Update();
+  }
 }
 
 //=======================================================================
 //function : SetDisplayMode
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDisplayMode(const Handle(AIS_InteractiveObject)& anIObj, 
-                                           const Standard_Integer aMode,
-                                           const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetDisplayMode (const Handle(AIS_InteractiveObject)& theIObj,
+                                             const Standard_Integer               theMode,
+                                             const Standard_Boolean               theToUpdateViewer)
 {
-  
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  if(!HasOpenedContext()){
-    
-    if(!myObjects.IsBound(anIObj)) 
-      anIObj->SetDisplayMode(aMode);
-    else if( anIObj->AcceptDisplayMode(aMode) ) 
+  setContextToObject (theIObj);
+  if (!myObjects.IsBound (theIObj))
+  {
+    theIObj->SetDisplayMode (theMode);
+    return;
+  }
+  else if (!theIObj->AcceptDisplayMode (theMode))
+  {
+    return;
+  }
+
+  Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
+  if (aStatus->GraphicStatus() != AIS_DS_Displayed)
+  {
+    aStatus->SetDisplayMode (theMode);
+    theIObj->SetDisplayMode (theMode);
+    return;
+  }
+
+  // erase presentations for all display modes different from <aMode>
+  const Standard_Integer anOldMode = aStatus->DisplayMode();
+  if (anOldMode != theMode)
+  {
+    if (myMainPM->IsHighlighted (theIObj, anOldMode))
     {
-      // CLE
-      // const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-      Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-      // ENDCLE
-      // SAN : erase presentations for all display modes different from <aMode>
-      if(STATUS->GraphicStatus()==AIS_DS_Displayed){
-        TColStd_ListOfInteger aModesToRemove;
-        TColStd_ListIteratorOfListOfInteger ItL (STATUS->DisplayedModes());
-        for(;ItL.More();ItL.Next()){
-
-          Standard_Integer OldMode = ItL.Value();
-
-          if(OldMode!=aMode){
-            aModesToRemove.Append(OldMode);
-            if(myMainPM->IsHighlighted(anIObj,OldMode))
-              myMainPM->Unhighlight(anIObj,OldMode);
-            myMainPM->SetVisibility (anIObj, OldMode, Standard_False);
-          }
-        }
-
-        for(ItL.Initialize(aModesToRemove);ItL.More();ItL.Next())
-          STATUS->RemoveDisplayMode(ItL.Value());
-
-        if(!STATUS->IsDModeIn(aMode))
-          STATUS->AddDisplayMode(aMode);
-
-        myMainPM->Display(anIObj,aMode);
-        Standard_Integer DM,HM,SM;
-        GetDefModes(anIObj,DM,HM,SM);
-        if(STATUS->IsHilighted()){
-          myMainPM->Highlight(anIObj,HM);
-        }
-        if(STATUS->IsSubIntensityOn()){
-          myMainPM->Color(anIObj,mySubIntensity,aMode);
-        }
-        if(anIObj->IsTransparent() && !myMainVwr->Viewer()->Transparency())
-          myMainVwr->Viewer()->SetTransparency(Standard_True);
-        
-        if(updateviewer) myMainVwr->Update();
-      }
-      anIObj->SetDisplayMode(aMode);
+      unhighlightGlobal (theIObj);
     }
+    myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
   }
+
+  aStatus->SetDisplayMode (theMode);
+
+  myMainPM->Display (theIObj, theMode);
+  if (aStatus->IsHilighted())
+  {
+    highlightGlobal (theIObj, getSelStyle (theIObj, theIObj->GlobalSelOwner()), theMode);
+  }
+  if (aStatus->IsSubIntensityOn())
+  {
+    highlightWithSubintensity (theIObj, theMode);
+  }
+
+  if (theToUpdateViewer)
+  {
+    myMainVwr->Update();
+  }
+  theIObj->SetDisplayMode (theMode);
 }
 
 //=======================================================================
 //function : UnsetDisplayMode
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::
-UnsetDisplayMode(const Handle(AIS_InteractiveObject)& anIObj,
-                 const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnsetDisplayMode (const Handle(AIS_InteractiveObject)& theIObj,
+                                               const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasDisplayMode()) return;
-  
-  if(!myObjects.IsBound(anIObj))
-    anIObj->UnsetDisplayMode();
-  else
+  if (theIObj.IsNull()
+  || !theIObj->HasDisplayMode())
+  {
+    return;
+  }
+
+  if (!myObjects.IsBound (theIObj))
+  {
+    theIObj->UnsetDisplayMode();
+    return;
+  }
+
+  const Standard_Integer anOldMode = theIObj->DisplayMode();
+  if (myDefaultDrawer->DisplayMode() == anOldMode)
+  {
+    return;
+  }
+
+  const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
+  aStatus->SetDisplayMode (myDefaultDrawer->DisplayMode());
+
+  if (aStatus->GraphicStatus() == AIS_DS_Displayed)
+  {
+    if (myMainPM->IsHighlighted (theIObj, anOldMode))
     {
-      Standard_Integer OldMode = anIObj->DisplayMode();
-      if(myDisplayMode==OldMode) return;
-      const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-      STATUS->RemoveDisplayMode(OldMode);
-      if(!STATUS->IsDModeIn(myDisplayMode))
-        STATUS->AddDisplayMode(myDisplayMode);
-      if(STATUS->GraphicStatus()==AIS_DS_Displayed){
-        if(myMainPM->IsHighlighted(anIObj,OldMode))
-          myMainPM->Unhighlight(anIObj,OldMode);
-        myMainPM->SetVisibility (anIObj, OldMode, Standard_False);
-        myMainPM->Display(anIObj,myDisplayMode);
-        Standard_Integer DM,HM,SM;
-        GetDefModes(anIObj,DM,HM,SM);
-        if(STATUS->IsHilighted()){
-          myMainPM->Highlight(anIObj,HM);
-        }
-        if(STATUS->IsSubIntensityOn()){
-          myMainPM->Color(anIObj,mySubIntensity,myDisplayMode);
-        }
-        if(updateviewer) myMainVwr->Update();
-      }
-      anIObj->UnsetDisplayMode();
-      
+      unhighlightGlobal (theIObj);
+    }
+    myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
+    myMainPM->Display (theIObj, myDefaultDrawer->DisplayMode());
+    if (aStatus->IsHilighted())
+    {
+      highlightSelected (theIObj->GlobalSelOwner());
+    }
+    if (aStatus->IsSubIntensityOn())
+    {
+      highlightWithSubintensity (theIObj, myDefaultDrawer->DisplayMode());
     }
+
+    if (theToUpdateViewer)
+    {
+      myMainVwr->Update();
+    }
+  }
+
+  theIObj->UnsetDisplayMode();
 }
 
 //=======================================================================
 //function : SetCurrentFacingModel
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetCurrentFacingModel(
-                        const Handle(AIS_InteractiveObject)& anIObj,
-                        const Aspect_TypeOfFacingModel aModel)
+void AIS_InteractiveContext::SetCurrentFacingModel (const Handle(AIS_InteractiveObject)& theIObj,
+                                                    const Aspect_TypeOfFacingModel       theModel)
 {
-  if (  !anIObj.IsNull ()  )
-    anIObj->SetCurrentFacingModel(aModel);
+  if (!theIObj.IsNull())
+  {
+    theIObj->SetCurrentFacingModel (theModel);
+  }
 }
 
 //=======================================================================
 //function : SetColor
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetColor(const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Quantity_NameOfColor aColor,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theIObj,
+                                       const Quantity_Color&                theColor,
+                                       const Standard_Boolean               theToUpdateViewer)
 {
-  SetColor(anIObj,Quantity_Color(aColor),updateviewer);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  setContextToObject (theIObj);
+  theIObj->SetColor (theColor);
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
-void AIS_InteractiveContext::SetColor(const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Quantity_Color &aColor,
-                                     const Standard_Boolean updateviewer)
+//=======================================================================
+//function : SetIsoOnTriangulation
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::IsoOnTriangulation (const Standard_Boolean theIsEnabled,
+                                                 const Handle(AIS_InteractiveObject)& theObject)
 {
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  anIObj->SetColor(aColor);
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp=0;
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-      anIObj->SetRecomputeOk();
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-    }
-
+  if (theObject.IsNull())
+  {
+    return;
+  }
 
-#ifdef DEB
-//   // pour isg
-//   if(anIObj->Type()==AIS_KOI_Datum && anIObj->Signature()==3){
-//     Handle(AIS_Trihedron) Tr = *((Handle(AIS_Trihedron)*)&anIObj);
-//     Standard_Real Lx = myDefaultDrawer->DatumAspect()->FirstAxisLength();
-//     gp_Trsf T; 
-//     gp_Dir D =Tr->Component()->XDirection();
-//     gp_Pnt O = Tr->Component()->Location();
-//     gp_Vec V(D);V*=Lx/5.;
-//     T.SetRotation(gp_Ax1(O,D),M_PI/6.);
-//     T.SetTranslationPart(V);
-//     TopLoc_Location L,IncLoc(T);
-   
-//     for(Standard_Integer I=1;I<90;I++){
-//       if(anIObj->HasLocation())
-//      L = anIObj->Location() * IncLoc;
-//       else
-//      L = IncLoc;
-//       SetLocation(anIObj,L);
-//       UpdateCurrentViewer();
-//     }
-//      for(I=1;I<90;I++){
-//        L = anIObj->Location() /IncLoc;
-//       SetLocation(anIObj,L);
-//       UpdateCurrentViewer();
-//     }
-//    ResetLocation(anIObj);
-//   }
- #endif
-  if(updateviewer) UpdateCurrentViewer();
+  theObject->SetIsoOnTriangulation (theIsEnabled);
 }
 
 //=======================================================================
 //function : SetDeviationCoefficient
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDeviationCoefficient(
-                                     const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Standard_Real   aCoefficient,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetDeviationCoefficient (const Handle(AIS_InteractiveObject)& theIObj,
+                                                      const Standard_Real                  theCoefficient,
+                                                      const Standard_Boolean               theToUpdateViewer)
 {
-//  cout<<" Coefficient:"<< aCoefficient <<endl;
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-
-  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
-  if(anIObj->Type()!=AIS_KOI_Object && anIObj->Type()!=AIS_KOI_Shape) return;
-  if(anIObj->Signature()!=0) return;
-  (*((Handle(AIS_Shape)*)&anIObj))->SetOwnDeviationCoefficient(aCoefficient);
-
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
+  // to be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
+  if (theIObj->Type() != AIS_KOI_Object
+   && theIObj->Type() != AIS_KOI_Shape)
+  {
+    return;
+  }
+  else if (theIObj->Signature() != 0)
+  {
+    return;
+  }
 
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp=0;
-      TColStd_ListOfInteger LL;
-      anIObj->ToBeUpdated(LL);
-      TColStd_ListIteratorOfListOfInteger ITI(LL);
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-      anIObj->SetRecomputeOk();
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-    }
-  if(updateviewer) UpdateCurrentViewer();
+  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
+  aShape->SetOwnDeviationCoefficient (theCoefficient);
+  aShape->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : SetHLRDeviationCoefficient
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetHLRDeviationCoefficient(
-                                     const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Standard_Real    aCoefficient,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetHLRDeviationCoefficient (const Handle(AIS_InteractiveObject)& theIObj,
+                                                         const Standard_Real                  theCoefficient,
+                                                         const Standard_Boolean               theToUpdateViewer)
 {
-//  cout<<" HLRCoefficient:"<< aCoefficient <<endl;
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
-  if(anIObj->Type()!=AIS_KOI_Object && anIObj->Type()!=AIS_KOI_Shape) return;
-  if(anIObj->Signature()!=0) return;
-  (*((Handle(AIS_Shape)*)&anIObj))->SetOwnHLRDeviationCoefficient(aCoefficient);
-
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
+  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
+  if (theIObj->Type() != AIS_KOI_Object
+   && theIObj->Type() != AIS_KOI_Shape)
+  {
+    return;
+  }
+  else if (theIObj->Signature() != 0)
+  {
+    return;
+  }
 
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp=0;
-
-      TColStd_ListOfInteger LL;
-      anIObj->ToBeUpdated(LL);
-      TColStd_ListIteratorOfListOfInteger ITI(LL);
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-      anIObj->SetRecomputeOk();
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-    }
-  if(updateviewer) UpdateCurrentViewer();
+  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
+  aShape->SetOwnHLRDeviationCoefficient (theCoefficient);
+  aShape->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
-
 //=======================================================================
 //function : SetDeviationAngle
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetDeviationAngle(
-                                     const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Standard_Real   anAngle,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetDeviationAngle (const Handle(AIS_InteractiveObject)& theIObj,
+                                                const Standard_Real                  theAngle,
+                                                const Standard_Boolean               theToUpdateViewer)
 {
-//  cout<<" Angle:"<< anAngle <<endl;
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
-  if(anIObj->Type()!=AIS_KOI_Shape) return;
-  if(anIObj->Signature()!=0) return;
-  (*((Handle(AIS_Shape)*)&anIObj))->SetOwnDeviationAngle(anAngle);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
+  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
+  if (theIObj->Type() != AIS_KOI_Shape)
+  {
+    return;
+  }
+  else if (theIObj->Signature() != 0)
+  {
+    return;
+  }
 
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp=0;
-      TColStd_ListOfInteger LL;
-      anIObj->ToBeUpdated(LL);
-      TColStd_ListIteratorOfListOfInteger ITI(LL);
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-      anIObj->SetRecomputeOk();
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-    }
-  if(updateviewer) UpdateCurrentViewer();
+  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
+  aShape->SetOwnDeviationAngle (theAngle);
+  aShape->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
+
 //=======================================================================
-//function : SetDeviationAngle
-//purpose  : 
+//function : SetAngleAndDeviation
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetAngleAndDeviation(
-                                     const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Standard_Real   anAngle,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetAngleAndDeviation (const Handle(AIS_InteractiveObject)& theIObj,
+                                                   const Standard_Real                  theAngle,
+                                                   const Standard_Boolean               theToUpdateViewer)
 {
-//  cout<<" Angle:"<< anAngle <<endl;
-  if(anIObj.IsNull()) return ;
-//   Standard_Real anAngleRad = M_PI*anAngle/180; test rob...
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
-  if(anIObj->Type()!=AIS_KOI_Shape) return;
-  if(anIObj->Signature()!=0) return;
-  (*((Handle(AIS_Shape)*)&anIObj))->SetAngleAndDeviation(anAngle);
-
+  setContextToObject (theIObj);
+  if (theIObj->Type() != AIS_KOI_Shape)
+  {
+    return;
+  }
+  if (theIObj->Signature() != 0)
+  {
+    return;
+  }
 
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    Update(anIObj,updateviewer);
+  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
+  aShape->SetAngleAndDeviation (theAngle);
+  aShape->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
-//function : SetDeviationAngle
-//purpose  : 
+//function : SetHLRAngleAndDeviation
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetHLRAngleAndDeviation(
-                                     const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Standard_Real   anAngle,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetHLRAngleAndDeviation (const Handle(AIS_InteractiveObject)& theIObj,
+                                                      const Standard_Real                  theAngle,
+                                                      const Standard_Boolean               theToUpdateViewer)
 {
-//  cout<<" Angle:"<< anAngle <<endl;
-
-  if(anIObj.IsNull()) return ;
-   if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
-  if(anIObj->Type()!=AIS_KOI_Shape) return;
-  if(anIObj->Signature()!=0) return;
-  (*((Handle(AIS_Shape)*)&anIObj))->SetHLRAngleAndDeviation(anAngle);
-
-
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp=0;
-
-      TColStd_ListOfInteger LL;
-      anIObj->ToBeUpdated(LL);
-      TColStd_ListIteratorOfListOfInteger ITI(LL);
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-      anIObj->SetRecomputeOk();
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-    }
-  if(updateviewer) UpdateCurrentViewer();
+  setContextToObject (theIObj);
+  if (theIObj->Type() != AIS_KOI_Shape)
+  {
+    return;
+  }
+  if (theIObj->Signature() != 0)
+  {
+    return;
+  }
+  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
+  aShape->SetHLRAngleAndDeviation (theAngle);
+  aShape->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : SetHLRDeviationAngle
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetHLRDeviationAngle(
-                                     const Handle(AIS_InteractiveObject)& anIObj, 
-                                     const Standard_Real   anAngle,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetHLRDeviationAngle (const Handle(AIS_InteractiveObject)& theIObj,
+                                                   const Standard_Real                  theAngle,
+                                                   const Standard_Boolean               theToUpdateViewer)
 {
-//  cout<<" HLRAngle:"<< anAngle <<endl;
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
-  if( anIObj->Type()!=AIS_KOI_Shape) return;
-  if(anIObj->Signature()!=0) return;
-  (*((Handle(AIS_Shape)*)&anIObj))->SetOwnHLRDeviationAngle(anAngle);
-
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp=0;
-      TColStd_ListOfInteger LL;
-      anIObj->ToBeUpdated(LL);
-      TColStd_ListIteratorOfListOfInteger ITI(LL);
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-      anIObj->SetRecomputeOk();
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-    }
-  if(updateviewer) UpdateCurrentViewer();
+  // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
+  if (theIObj->Type() != AIS_KOI_Shape)
+  {
+    return;
+  }
+  if (theIObj->Signature() != 0)
+  {
+    return;
+  }
+  Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
+  aShape->SetOwnHLRDeviationAngle (theAngle);
+  aShape->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : UnsetColor
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::UnsetColor(const Handle(AIS_InteractiveObject)& anIObj,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnsetColor (const Handle(AIS_InteractiveObject)& theIObj,
+                                         const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-   anIObj->UnsetColor();
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      Standard_Integer NbDisp =0;
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-      anIObj->SetRecomputeOk();
-    }
-  if(updateviewer) 
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  theIObj->UnsetColor();
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
     UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : HasColor
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::HasColor(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Boolean AIS_InteractiveContext::HasColor (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  return anIObj->HasColor();
-
+  return theIObj->HasColor();
 }
 
 //=======================================================================
 //function : Color
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Quantity_NameOfColor AIS_InteractiveContext::Color(const Handle(AIS_InteractiveObject)& anIObj) const 
-{
-  return anIObj->Color();
-}
-
-void AIS_InteractiveContext::Color(const Handle(AIS_InteractiveObject)& anIObj,
-                                   Quantity_Color &aColor) const
+void AIS_InteractiveContext::Color (const Handle(AIS_InteractiveObject)& theIObj,
+                                    Quantity_Color&                      theColor) const
 {
-  anIObj->Color(aColor);
+  theIObj->Color (theColor);
 }
 
 //=======================================================================
 //function : Width
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Real AIS_InteractiveContext::Width(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Real AIS_InteractiveContext::Width (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  return anIObj->Width();
+  return theIObj->Width();
 }
 
 //=======================================================================
 //function : SetWidth
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetWidth(const Handle(AIS_InteractiveObject)& anIObj,
-                                     const Standard_Real aValue,
-                                     const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetWidth (const Handle(AIS_InteractiveObject)& theIObj,
+                                       const Standard_Real                  theWidth,
+                                       const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  anIObj->SetWidth(aValue);
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  setContextToObject (theIObj);
+  theIObj->SetWidth (theWidth);
+  theIObj->UpdatePresentations();
+  if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (theIObj))
+  {
+    if (myLastPicked->IsAutoHilight())
+    {
+      const Standard_Integer aHiMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+      myLastPicked->HilightWithColor (myMainPM,
+                                      myLastPicked->IsSelected() ? getSelStyle (theIObj, myLastPicked) : getHiStyle (theIObj, myLastPicked),
+                                      aHiMode);
+    }
+    else
     {
-      Standard_Integer NbDisp=0;
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-          NbDisp++;
-        }
-#ifdef DEB
-      cout<<"nb of modes to recalculate : "<<NbDisp<<endl;
-#endif
-      anIObj->SetRecomputeOk();
+      theIObj->HilightOwnerWithColor (myMainPM,
+                                      myLastPicked->IsSelected() ? getSelStyle (theIObj, myLastPicked) : getHiStyle (theIObj, myLastPicked),
+                                      myLastPicked);
     }
-   if(updateviewer) 
+  }
+  if (theToUpdateViewer)
+  {
     UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : UnsetWidth
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::UnsetWidth(const Handle(AIS_InteractiveObject)& anIObj,
-                                       const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnsetWidth (const Handle(AIS_InteractiveObject)& theIObj,
+                                         const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-   anIObj->UnsetWidth();
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-        }
-      anIObj->SetRecomputeOk();
-    }
-   if(updateviewer) 
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  theIObj->UnsetWidth();
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
     UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : SetMaterial
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetMaterial(const Handle(AIS_InteractiveObject)& anIObj,
-//POP pour K4L
-                                        const Graphic3d_NameOfMaterial aName,
-//                                      const Graphic3d_NameOfPhysicalMaterial aName,
-                                        const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
+                                          const Graphic3d_MaterialAspect&      theMaterial,
+                                          const Standard_Boolean               theToUpdateViewer)
 {
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  anIObj->SetMaterial(aName);
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-        }
-      anIObj->SetRecomputeOk();
-    }
-   if(updateviewer)
-    UpdateCurrentViewer();
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
+  setContextToObject (theIObj);
+  theIObj->SetMaterial (theMaterial);
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
+    UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : UnsetMaterial
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::UnsetMaterial(const Handle(AIS_InteractiveObject)& anIObj,
-                                          const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnsetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
+                                            const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-  anIObj->UnsetMaterial();
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-        }
-      anIObj->SetRecomputeOk();
-    }
-  if(updateviewer)
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+  theIObj->UnsetMaterial();
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
     UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : SetTransparency
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetTransparency(const Handle(AIS_InteractiveObject)& anIObj,
-                                            const Standard_Real aValue,
-                                            const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetTransparency (const Handle(AIS_InteractiveObject)& theIObj,
+                                              const Standard_Real                  theValue,
+                                              const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-   if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
 
-#ifdef BUC60577 //right optimization
-  if(!anIObj->IsTransparent() && aValue<=0.05) return;
-#else  
-//  if(!anIObj->IsTransparent() && aValue<=0.05) return;
-#endif
-  if(aValue<=0.05){
-    UnsetTransparency(anIObj,updateviewer);
+  setContextToObject (theIObj);
+  if (!theIObj->IsTransparent()
+    && theValue <= 0.005)
+  {
     return;
   }
-  
-  if(!myMainVwr->Viewer()->Transparency())
-    myMainVwr->Viewer()->SetTransparency(Standard_True);
-  anIObj->SetTransparency(aValue);
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-        }
-      anIObj->SetRecomputeOk();
-    }
-   if(updateviewer)
+
+  if (theValue <= 0.005)
+  {
+    UnsetTransparency (theIObj, theToUpdateViewer);
+    return;
+  }
+
+  theIObj->SetTransparency (theValue);
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
     UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : UnsetTransparency
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::UnsetTransparency(const Handle(AIS_InteractiveObject)& anIObj,
-                                              const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::UnsetTransparency (const Handle(AIS_InteractiveObject)& theIObj,
+                                                const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-   anIObj->UnsetTransparency();
-  if(anIObj->RecomputeEveryPrs())
-    anIObj->Redisplay();
-  else
-    {
-      TColStd_ListIteratorOfListOfInteger ITI(anIObj->ListOfRecomputeModes());
-      for (;ITI.More();ITI.Next())
-        {
-          anIObj->Update(ITI.Value(),Standard_False);
-        }
-      anIObj->SetRecomputeOk();
-    }
-
-  // To Unset transparency in the viewer, if no other object is transparent ...(Speed)
-  AIS_DataMapIteratorOfDataMapOfIOStatus It(myObjects);
-  Standard_Boolean FoundTransp(Standard_False);
-  for(;It.More() && !FoundTransp ;It.Next()){
-    if(It.Key()->IsTransparent())
-      FoundTransp = Standard_True;
+  if (theIObj.IsNull())
+  {
+    return;
   }
-  if(!FoundTransp)
-    myMainVwr->Viewer()->SetTransparency(Standard_False);
-  
-  
-  if(updateviewer)
+
+  theIObj->UnsetTransparency();
+  theIObj->UpdatePresentations();
+  if (theToUpdateViewer)
+  {
     UpdateCurrentViewer();
+  }
 }
 
 //=======================================================================
 //function : SetSelectedAspect
-//purpose  : 
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::SetSelectedAspect (const Handle(Prs3d_BasicAspect)& theAspect,
+                                                const Standard_Boolean           theToUpdateViewer)
+{
+  Standard_DISABLE_DEPRECATION_WARNINGS
+  Standard_Boolean isFound = Standard_False;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    isFound = Standard_True;
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
+    anObj->SetAspect (theAspect);
+  }
+  Standard_ENABLE_DEPRECATION_WARNINGS
+
+  if (isFound && theToUpdateViewer)
+  {
+    myMainVwr->Update();
+  }
+}
+
+//=======================================================================
+//function : SetLocalAttributes
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::SetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
+                                                 const Handle(Prs3d_Drawer)&          theDrawer,
+                                                 const Standard_Boolean               theToUpdateViewer)
+{
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  setContextToObject (theIObj);
+  theIObj->SetAttributes (theDrawer);
+  Update (theIObj, theToUpdateViewer);
+}
+
+//=======================================================================
+//function : UnsetLocalAttributes
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::UnsetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
+                                                   const Standard_Boolean               theToUpdateViewer)
+{
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  setContextToObject (theIObj);
+  theIObj->UnsetAttributes();
+  Update (theIObj, theToUpdateViewer);
+}
+
+//=======================================================================
+//function : Status
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetSelectedAspect(
-                                const Handle(Prs3d_BasicAspect)& anAspect,
-                                const Standard_Boolean globalChange,
-                                const Standard_Boolean updateViewer)
-{
-  if( !HasOpenedContext() ) {
-    Standard_Boolean found = Standard_False;
-    Handle(AIS_Selection) sel = 
-                AIS_Selection::Selection(myCurrentName.ToCString());
-    Handle(AIS_InteractiveObject) object;
-    for( sel->Init() ; sel->More() ; sel->Next()) {
-      found = Standard_True;
-      object = Handle(AIS_InteractiveObject)::DownCast(sel->Value());
-      object->SetAspect(anAspect,globalChange);
+void AIS_InteractiveContext::Status (const Handle(AIS_InteractiveObject)& theIObj,
+                                     TCollection_ExtendedString&          theStatus) const
+{
+  theStatus = "";
+  if (theIObj.IsNull()
+  || !myObjects.IsBound (theIObj))
+  {
+    return;
+  }
+
+  theStatus += "\t ____________________________________________";
+  theStatus += "\t| Known at Neutral Point:\n\tDisplayStatus:";
+  const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
+  switch (aStatus->GraphicStatus())
+  {
+    case AIS_DS_Displayed:
+    {
+      theStatus += "\t| -->Displayed\n";
+      break;
     }
-    if( found && updateViewer) {
-      myMainVwr->Update();
+    case AIS_DS_Erased:
+    {
+      theStatus += "\t| -->Erased\n";
+      break;
     }
+    default:
+      break;
   }
+
+  theStatus += "\t| Active Display Modes in the MainViewer :\n";
+  theStatus += "\t|\t Mode ";
+  theStatus += TCollection_AsciiString (aStatus->DisplayMode());
+  theStatus += "\n";
+
+  if (IsSelected(theIObj)) theStatus +="\t| Selected\n";
+
+  theStatus += "\t| Active Selection Modes in the MainViewer :\n";
+  for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
+  {
+    theStatus += "\t\t Mode ";
+    theStatus += TCollection_AsciiString (aSelModeIter.Value());
+    theStatus += "\n";
+  }
+  theStatus += "\t ____________________________________________";
 }
 
 //=======================================================================
-//function : SetLocalAttributes
-//purpose  : 
+//function : GetDefModes
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::GetDefModes (const Handle(AIS_InteractiveObject)& theIObj,
+                                          Standard_Integer&                    theDispMode,
+                                          Standard_Integer&                    theHiMode,
+                                          Standard_Integer&                    theSelMode) const
+{
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  theDispMode = theIObj->HasDisplayMode()
+              ? theIObj->DisplayMode()
+              : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
+               ? myDefaultDrawer->DisplayMode()
+               : 0);
+  theHiMode  = theIObj->HasHilightMode()   ? theIObj->HilightMode()   : theDispMode;
+  theSelMode = theIObj->GlobalSelectionMode();
+}
+
 //=======================================================================
+//function : EraseGlobal
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::EraseGlobal (const Handle(AIS_InteractiveObject)& theIObj,
+                                          const Standard_Boolean               theToUpdateviewer)
+{
+  Handle(AIS_GlobalStatus) aStatus;
+  if (theIObj.IsNull()
+  || !myObjects.Find (theIObj, aStatus)
+  ||  aStatus->GraphicStatus() == AIS_DS_Erased)
+  {
+    return;
+  }
+
+  const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+  unselectOwners (theIObj);
+  myMainPM->SetVisibility (theIObj, aStatus->DisplayMode(), Standard_False);
+
+  if (!myLastPicked.IsNull()
+    && myLastPicked->IsSameSelectable (theIObj))
+  {
+    clearDynamicHighlight();
+  }
+
+  // make sure highlighting presentations are properly erased
+  theIObj->ErasePresentations (false);
 
-void AIS_InteractiveContext::SetLocalAttributes(const Handle(AIS_InteractiveObject)& anIObj, 
-                                                const Handle(AIS_Drawer)& aDrawer,
-                                                const Standard_Boolean updateviewer)
+  if (IsSelected (theIObj)
+   && aStatus->DisplayMode() != aDispMode)
+  {
+    myMainPM->SetVisibility (theIObj, aDispMode, Standard_False);
+  }
+
+  for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
+  {
+    mgrSelector->Deactivate (theIObj, aSelModeIter.Value());
+  }
+  aStatus->ClearSelectionModes();
+  aStatus->SetGraphicStatus (AIS_DS_Erased);
+
+  if (theToUpdateviewer)
+  {
+    myMainVwr->Update();
+  }
+}
+
+//=======================================================================
+//function : unselectOwners
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unselectOwners (const Handle(AIS_InteractiveObject)& theObject)
 {
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  anIObj->SetAttributes(aDrawer);
-  Update(anIObj,updateviewer);
-  
+  SelectMgr_SequenceOfOwner aSeq;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    if (aSelIter.Value()->IsSameSelectable (theObject))
+    {
+      aSeq.Append (aSelIter.Value());
+    }
+  }
+  for (SelectMgr_SequenceOfOwner::Iterator aDelIter (aSeq); aDelIter.More(); aDelIter.Next())
+  {
+    AddOrRemoveSelected (aDelIter.Value(), Standard_False);
+  }
 }
 
 //=======================================================================
-//function : UnsetLocalAttributes
-//purpose  : 
+//function : ClearGlobal
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::UnsetLocalAttributes(const Handle(AIS_InteractiveObject)& anIObj,
-                                                  const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::ClearGlobal (const Handle(AIS_InteractiveObject)& theIObj,
+                                          const Standard_Boolean               theToUpdateviewer)
 {
-  if(anIObj.IsNull()) return ;
-  if(!anIObj->HasInteractiveContext())
-    anIObj->SetContext(this);
-  anIObj->UnsetAttributes();
-  Update(anIObj,updateviewer);
-}
+  Handle(AIS_GlobalStatus) aStatus;
+  if (theIObj.IsNull()
+  || !myObjects.Find (theIObj, aStatus))
+  {
+    // for cases when reference shape of connected interactives was not displayed
+    // but its selection primitives were calculated
+    const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+    mgrSelector->Remove (anObj);
+    return;
+  }
 
+  unselectOwners (theIObj);
 
-//=======================================================================
-//function : Status
-//purpose  : 
-//=======================================================================
+  myMainPM->Erase (theIObj, -1);
+  theIObj->ErasePresentations (true); // make sure highlighting presentations are properly erased
 
-void AIS_InteractiveContext::Status(const Handle(AIS_InteractiveObject)& anIObj, TCollection_ExtendedString& astatus) const 
-{
-  astatus = "";
+  // Object removes from Detected sequence
+  Standard_DISABLE_DEPRECATION_WARNINGS
+  for (Standard_Integer aDetIter = myDetectedSeq.Lower(); aDetIter <= myDetectedSeq.Upper();)
+  {
+    Handle(SelectMgr_EntityOwner) aPicked = myMainSel->Picked (myDetectedSeq (aDetIter));
+    Handle(AIS_InteractiveObject) anObj;
+    if (!aPicked.IsNull())
+    {
+      anObj = Handle(AIS_InteractiveObject)::DownCast (aPicked->Selectable());
+    }
 
-  if(anIObj.IsNull()) return ;
-  if(myObjects.IsBound(anIObj)){
-    astatus += "\t ____________________________________________";
-    astatus += "\t| Known at Neutral Point:\n\tDisplayStatus:";
-    const Handle(AIS_GlobalStatus)& ST = myObjects(anIObj);
-    switch(ST->GraphicStatus()){
-    case AIS_DS_Displayed:
+    if (!anObj.IsNull()
+      && anObj == theIObj)
+    {
+      myDetectedSeq.Remove (aDetIter);
+      if (myCurDetected == aDetIter)
       {
-        astatus +="\t| -->Displayed\n";
-        break;
+        myCurDetected = Min (myDetectedSeq.Upper(), aDetIter);
       }
-    case AIS_DS_Erased:
+      if (myCurHighlighted == aDetIter)
       {
-        astatus +="\t| -->Erased\n";
-        break;
+        myCurHighlighted = 0;
       }
-    default:
-      break;
     }
-    astatus += "\t| Active Display Modes in the MainViewer :\n";
-    TColStd_ListIteratorOfListOfInteger ItL (ST->DisplayedModes());
-    for(;ItL.More();ItL.Next()){
-      astatus += "\t|\t Mode ";
-      astatus += TCollection_AsciiString(ItL.Value());
-      astatus+="\n";
-    }   
-    if(IsCurrent(anIObj)) astatus +="\t| Current\n";
-    if(IsSelected(anIObj)) astatus +="\t| Selected\n";
-
-    astatus += "\t| Active Selection Modes in the MainViewer :\n";
-    for(ItL.Initialize(ST->SelectionModes());ItL.More();ItL.Next()){
-      astatus += "\t\t Mode ";
-      astatus += TCollection_AsciiString(ItL.Value());
-      astatus+="\n";
-    }   
-    astatus += "\t ____________________________________________";
-      
+    else
+    {
+      aDetIter++;
     }
-}
-
-
-//=======================================================================
-//function : GetDefModes
-//purpose  : 
-//=======================================================================
-
-void AIS_InteractiveContext::GetDefModes(const Handle(AIS_InteractiveObject)& anIObj,
-                                     Standard_Integer& DispMode,
-                                     Standard_Integer& HiMode,
-                                     Standard_Integer& SelMode) const 
-{
-  if(anIObj.IsNull()) return ;
-  DispMode = anIObj->HasDisplayMode() ? anIObj->DisplayMode() : 
-  (anIObj->AcceptDisplayMode(myDisplayMode)? myDisplayMode : 0);
-  
-  HiMode = anIObj->HasHilightMode()? anIObj->HilightMode():DispMode;
-#ifdef BUC61051
-  SelMode = anIObj->HasSelectionMode()? anIObj->SelectionMode() : -1;
-#else
-  SelMode = anIObj->HasSelectionMode()? anIObj->SelectionMode() : 0;
-#endif
-}
+  }
+  Standard_ENABLE_DEPRECATION_WARNINGS
 
+  // remove IO from the selection manager to avoid memory leaks
+  const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+  mgrSelector->Remove (anObj);
 
-//=======================================================================
-//function : EraseGlobal
-//purpose  : 
-//=======================================================================
+  setObjectStatus (theIObj, AIS_DS_None, -1, -1);
+  myMainVwr->StructureManager()->UnregisterObject (theIObj);
 
-void AIS_InteractiveContext::EraseGlobal(const Handle(AIS_InteractiveObject)& anIObj, 
-                                         const Standard_Boolean updateviewer)
-{
-  if(anIObj.IsNull()) return ;
-  if(!myObjects.IsBound(anIObj)) return;
+  for (V3d_ListOfViewIterator aDefViewIter (myMainVwr->DefinedViewIterator()); aDefViewIter.More(); aDefViewIter.Next())
+  {
+    aDefViewIter.Value()->View()->ChangeHiddenObjects()->Remove (theIObj.get());
+  }
 
-  // CLE
-  // const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-  Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-  // ENDCLE
-  Standard_Integer Dmode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
-  if(STATUS->GraphicStatus()==AIS_DS_Displayed){
-    
-    TColStd_ListIteratorOfListOfInteger ItL (STATUS->DisplayedModes());
-    for(;ItL.More();ItL.Next()){
-      if(myMainPM->IsHighlighted(anIObj,ItL.Value()))
-        myMainPM->Unhighlight(anIObj,ItL.Value());
-      myMainPM->SetVisibility (anIObj, ItL.Value(), Standard_False);
+  if (!myLastPicked.IsNull())
+  {
+    if (myLastPicked->IsSameSelectable (theIObj))
+    {
+      clearDynamicHighlight();
+      myLastPicked.Nullify();
     }
-    if(IsCurrent(anIObj) && !STATUS->IsDModeIn(Dmode))
-      myMainPM->SetVisibility (anIObj, Dmode, Standard_False);
-    
-    for(ItL.Initialize(STATUS->SelectionModes());ItL.More();ItL.Next())
-      mgrSelector->Deactivate(anIObj,ItL.Value(),myMainSel);
-    if(updateviewer) myMainVwr->Update();
   }
-  STATUS->SetGraphicStatus(AIS_DS_Erased);
-  
+
+  if (theToUpdateviewer && aStatus->GraphicStatus() == AIS_DS_Displayed)
+  {
+    myMainVwr->Update();
+  }
 }
 
 //=======================================================================
-//function : ClearGlobal
-//purpose  : 
+//function : ClearGlobalPrs
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ClearGlobal(const Handle(AIS_InteractiveObject)& anIObj, 
-                                         const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::ClearGlobalPrs (const Handle(AIS_InteractiveObject)& theIObj,
+                                             const Standard_Integer               theMode,
+                                             const Standard_Boolean               theToUpdateViewer)
 {
-  if(anIObj.IsNull()) return ;
-  if(!myObjects.IsBound(anIObj)) return;
-  // CLE
-  // const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-  Handle(AIS_GlobalStatus) STATUS = myObjects(anIObj);
-  // ENDCLE
-   TColStd_ListIteratorOfListOfInteger ItL (STATUS->DisplayedModes());
-   for(;ItL.More();ItL.Next()){
-     if(STATUS->IsHilighted()){
-       if(IsCurrent(anIObj))
-#ifdef OCC204
-         AddOrRemoveCurrentObject( anIObj, updateviewer );
-#else
-         AddOrRemoveCurrentObject(anIObj);
-#endif
-       else if(myMainPM->IsHighlighted(anIObj,ItL.Value()))
-         myMainPM->Unhighlight(anIObj,ItL.Value());
-     }
-     myMainPM->Erase(anIObj,ItL.Value());
-     myMainPM->Clear(anIObj,ItL.Value());
-     if(anIObj->HasHilightMode()){
-       Standard_Integer im = anIObj->HilightMode();
-       myMainPM->Unhighlight(anIObj,im);
-       myMainPM->Erase(anIObj,im);
-  
-     }
-   }
-
-  //Object removes from Detected sequence
-  Standard_Integer i = 1;
-  for(i = 1; i < myAISDetectedSeq.Length(); i++)
+  if (theIObj.IsNull()
+  || !myObjects.IsBound (theIObj))
   {
-    Handle(AIS_InteractiveObject) anObj = DetectedCurrentObject();
-    if( !anObj.IsNull() && anObj != anIObj )
-      myAISDetectedSeq.Remove( i );
+    return;
   }
 
-  if(myLastinMain == anIObj)
-    myLastinMain.Nullify();
-
-  if(myLastPicked == anIObj)
-    myLastPicked.Nullify();
+  const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
+  if (aStatus->DisplayMode() == theMode)
+  {
+    const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+    if (aDispMode == theMode
+     && myMainPM->IsHighlighted (theIObj, theMode))
+    {
+      unhighlightGlobal (theIObj);
+    }
 
-  // OCC21671: Remove IO from the selection manager in any case
-  // to avoid memory leaks
-  mgrSelector->Remove(anIObj);
+    myMainPM->Erase (theIObj, theMode);
+  }
 
-  if (updateviewer && (STATUS->GraphicStatus() == AIS_DS_Displayed))
+  if (aStatus->GraphicStatus() == AIS_DS_Displayed
+   && theToUpdateViewer)
   {
     myMainVwr->Update();
   }
-
-  myObjects.UnBind(anIObj);
-  
 }
 
 //=======================================================================
-//function : ClearGlobalPrs
-//purpose  : 
+//function : ClearDetected
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::ClearGlobalPrs(const Handle(AIS_InteractiveObject)& anIObj, 
-                                            const Standard_Integer aMode,
-                                            const Standard_Boolean updateviewer)
+Standard_Boolean AIS_InteractiveContext::ClearDetected (Standard_Boolean theToRedrawImmediate)
 {
-  if(anIObj.IsNull()) return ;
-  if(!myObjects.IsBound(anIObj)) return;
-  const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-  
-  Standard_Integer DM = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0; 
-  if(STATUS->IsDModeIn(aMode)){
-    if(DM==aMode && myMainPM->IsHighlighted(anIObj,aMode))
-      myMainPM->Unhighlight(anIObj,aMode);
-    
-    myMainPM->Erase(anIObj,aMode);
-    myMainPM->Clear(anIObj,aMode);
+  myCurDetected = 0;
+  myCurHighlighted = 0;
+  myDetectedSeq.Clear();
+  Standard_Boolean toUpdate = Standard_False;
+  if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
+  {
+    toUpdate = Standard_True;
+    clearDynamicHighlight();
   }
-  
-
-  if(STATUS->GraphicStatus()==AIS_DS_Displayed && updateviewer)
-    myMainVwr->Update();
+  myLastPicked.Nullify();
+  myMainSel->ClearPicked();
+  if (toUpdate && theToRedrawImmediate)
+  {
+    myMainVwr->RedrawImmediate();
+  }
+  return toUpdate;
 }
 
 //=======================================================================
 //function : DrawHiddenLine
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::DrawHiddenLine () const {
-
+Standard_Boolean AIS_InteractiveContext::DrawHiddenLine() const
+{
   return myDefaultDrawer->DrawHiddenLine();
 }
 
 //=======================================================================
 //function : EnableDrawHiddenLine
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::EnableDrawHiddenLine () const {
+void AIS_InteractiveContext::EnableDrawHiddenLine() const
+{
   myDefaultDrawer->EnableDrawHiddenLine();
 }
 
 //=======================================================================
 //function : DisableDrawHiddenLine 
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::DisableDrawHiddenLine () const {
+void AIS_InteractiveContext::DisableDrawHiddenLine() const
+{
   myDefaultDrawer->DisableDrawHiddenLine();
 }
 
 //=======================================================================
 //function : HiddenLineAspect
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Handle (Prs3d_LineAspect) AIS_InteractiveContext::HiddenLineAspect () const  {
+Handle (Prs3d_LineAspect) AIS_InteractiveContext::HiddenLineAspect() const
+{
   return myDefaultDrawer->HiddenLineAspect();
 }
 
 //=======================================================================
 //function : SetHiddenLineAspect
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetHiddenLineAspect ( const Handle(Prs3d_LineAspect)& anAspect) const {
-  myDefaultDrawer->SetHiddenLineAspect(anAspect);
+void AIS_InteractiveContext::SetHiddenLineAspect (const Handle(Prs3d_LineAspect)& theAspect) const
+{
+  myDefaultDrawer->SetHiddenLineAspect (theAspect);
 }
 
 //=======================================================================
 //function : SetIsoNumber
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetIsoNumber(const Standard_Integer Nb,const AIS_TypeOfIso Type)
+void AIS_InteractiveContext::SetIsoNumber (const Standard_Integer theNb,
+                                           const AIS_TypeOfIso    theType)
 {
-  switch(Type){
-  case AIS_TOI_IsoU:
-    myDefaultDrawer->UIsoAspect()->SetNumber(Nb);
-    break;
-  case AIS_TOI_IsoV:
-    myDefaultDrawer->VIsoAspect()->SetNumber(Nb);
-    break;
-  case AIS_TOI_Both:
-    myDefaultDrawer->UIsoAspect()->SetNumber(Nb);
-    myDefaultDrawer->VIsoAspect()->SetNumber(Nb);
-    break;
+  switch (theType)
+  {
+    case AIS_TOI_IsoU:
+      myDefaultDrawer->UIsoAspect()->SetNumber (theNb);
+      break;
+    case AIS_TOI_IsoV:
+      myDefaultDrawer->VIsoAspect()->SetNumber (theNb);
+      break;
+    case AIS_TOI_Both:
+      myDefaultDrawer->UIsoAspect()->SetNumber (theNb);
+      myDefaultDrawer->VIsoAspect()->SetNumber (theNb);
+      break;
   }
 }
+
 //=======================================================================
 //function : IsoNumber
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Integer AIS_InteractiveContext::IsoNumber(const AIS_TypeOfIso Type)
+Standard_Integer AIS_InteractiveContext::IsoNumber (const AIS_TypeOfIso theType)
 {
-  
-  switch(Type){
-  case AIS_TOI_IsoU:
-    return myDefaultDrawer->UIsoAspect()->Number();
-  case AIS_TOI_IsoV:
-    return myDefaultDrawer->VIsoAspect()->Number();
-  case AIS_TOI_Both:
-    return (myDefaultDrawer->UIsoAspect()->Number()==
-            myDefaultDrawer->VIsoAspect()->Number()) ? 
-              myDefaultDrawer->UIsoAspect()->Number(): -1;
+  switch (theType)
+  {
+    case AIS_TOI_IsoU: return myDefaultDrawer->UIsoAspect()->Number();
+    case AIS_TOI_IsoV: return myDefaultDrawer->VIsoAspect()->Number();
+    case AIS_TOI_Both: return myDefaultDrawer->UIsoAspect()->Number() == myDefaultDrawer->VIsoAspect()->Number()
+                            ? myDefaultDrawer->UIsoAspect()->Number()
+                            : -1;
   }
   return 0;
 }
 
 //=======================================================================
 //function : IsoOnPlane
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::IsoOnPlane(const Standard_Boolean SwitchOn)
+void AIS_InteractiveContext::IsoOnPlane (const Standard_Boolean theToSwitchOn)
 {
-  myDefaultDrawer->SetIsoOnPlane(SwitchOn);
+  myDefaultDrawer->SetIsoOnPlane (theToSwitchOn);
 }
 
 //=======================================================================
 //function : IsoOnPlane
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::IsoOnPlane() const 
+Standard_Boolean AIS_InteractiveContext::IsoOnPlane() const
 {
   return myDefaultDrawer->IsoOnPlane();
 }
 
 //=======================================================================
-//function : SetSelectionMode
-//purpose  : 
+//function : IsoOnTriangulation
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetSelectionMode(const Handle_AIS_InteractiveObject&, const Standard_Integer )
+void AIS_InteractiveContext::IsoOnTriangulation (const Standard_Boolean theToSwitchOn)
 {
+  myDefaultDrawer->SetIsoOnTriangulation (theToSwitchOn);
 }
 
 //=======================================================================
-//function : UnsetSelectionMode
-//purpose  : 
+//function : IsoOnTriangulation
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::UnsetSelectionMode(const Handle_AIS_InteractiveObject&)
+Standard_Boolean AIS_InteractiveContext::IsoOnTriangulation() const
 {
-}
-
-//=======================================================================
-//function : SetSensitivityMode
-//purpose  : 
-//=======================================================================
-
-void AIS_InteractiveContext::SetSensitivityMode(const StdSelect_SensitivityMode aMode) {
-
-  if( HasOpenedContext() )
-        myLocalContexts(myCurLocalIndex)->SetSensitivityMode(aMode);
-  else {
-    myMainSel->SetSensitivityMode(aMode);
-  }
-}
-
-//=======================================================================
-//function : SensitivityMode
-//purpose  : 
-//=======================================================================
-
-StdSelect_SensitivityMode AIS_InteractiveContext::SensitivityMode() const {
-
-  if( HasOpenedContext() )
-        return myLocalContexts(myCurLocalIndex)->SensitivityMode();
-  return myMainSel->SensitivityMode();
-}
-
-//=======================================================================
-//function : SetSensitivity
-//purpose  : 
-//=======================================================================
-
-void AIS_InteractiveContext::SetSensitivity(const Standard_Real aPrecision) {
-
-  if( HasOpenedContext() )
-        myLocalContexts(myCurLocalIndex)->SetSensitivity(aPrecision);
-  else {
-    myMainSel->SetSensitivity(aPrecision);
-  }
-}
-
-//=======================================================================
-//function : Sensitivity
-//purpose  : 
-//=======================================================================
-
-Standard_Real AIS_InteractiveContext::Sensitivity() const {
-
-  if( HasOpenedContext() )
-        return myLocalContexts(myCurLocalIndex)->Sensitivity();
-  return myMainSel->Sensitivity();
+  return myDefaultDrawer->IsoOnTriangulation();
 }
 
 //=======================================================================
 //function : SetPixelTolerance
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetPixelTolerance(const Standard_Integer aPrecision) {
-
-  if( HasOpenedContext() )
-        myLocalContexts(myCurLocalIndex)->SetPixelTolerance(aPrecision);
-  else {
-    myMainSel->SetPixelTolerance(aPrecision);
-  }
+void AIS_InteractiveContext::SetPixelTolerance (const Standard_Integer thePrecision)
+{
+  myMainSel->SetPixelTolerance (thePrecision);
 }
 
 //=======================================================================
 //function : PixelTolerance
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Integer AIS_InteractiveContext::PixelTolerance() const {
-
-  if( HasOpenedContext() )
-        return myLocalContexts(myCurLocalIndex)->PixelTolerance();
+Standard_Integer AIS_InteractiveContext::PixelTolerance() const
+{
   return myMainSel->PixelTolerance();
 }
 
 //=======================================================================
-//function : IsInLocal
-//purpose  : 
+//function : SetSelectionSensitivity
+//purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::IsInLocal(const Handle_AIS_InteractiveObject& anIObj,
-                                                   Standard_Integer& TheIndex) const 
+void AIS_InteractiveContext::SetSelectionSensitivity (const Handle(AIS_InteractiveObject)& theObject,
+                                                      const Standard_Integer theMode,
+                                                      const Standard_Integer theNewSensitivity)
 {
-  if(anIObj.IsNull()) return Standard_False;
-  // if it exists at neutral point 0 index is returned
-  if(myObjects.IsBound(anIObj)) {
-    TheIndex = 0;
-    return Standard_False;
-  }
-  for(Standard_Integer I=1;I<=myLocalContexts.Extent();I++){
-    if(myLocalContexts.IsBound(I)){
-      if(myLocalContexts(I)->IsIn(anIObj)){
-        TheIndex = I;
-        return Standard_True;
-        
-      }
-    }
-  }
-  TheIndex =-1;
-  return Standard_False;
-}  
-  
+  mgrSelector->SetSelectionSensitivity (theObject, theMode, theNewSensitivity);
+}
+
 //=======================================================================
 //function : InitAttributes
-//purpose  : 
+//purpose  :
 //=======================================================================
-
 void AIS_InteractiveContext::InitAttributes()
 {
-
-  mgrSelector->Add(myMainSel);
-  myCurrentName   = AIS_Context_NewCurName();
-  mySelectionName = AIS_Context_NewSelName();
-
-  AIS_Selection::CreateSelection(mySelectionName.ToCString());
-  AIS_Selection::CreateSelection(myCurrentName.ToCString());
-
-  
-  myDefaultDrawer->SetShadingAspectGlobal(Standard_False);
-  Graphic3d_MaterialAspect aMat(Graphic3d_NOM_BRASS);
-  myDefaultDrawer->ShadingAspect()->SetMaterial(aMat);
+  Graphic3d_MaterialAspect aMat (Graphic3d_NOM_BRASS);
+  myDefaultDrawer->ShadingAspect()->SetMaterial (aMat);
 
 //  myDefaultDrawer->ShadingAspect()->SetColor(Quantity_NOC_GRAY70);
-  Handle (Prs3d_LineAspect) HLA = myDefaultDrawer->HiddenLineAspect();
-  HLA->SetColor(Quantity_NOC_GRAY20);
-  HLA->SetWidth(1);
-  HLA->SetTypeOfLine(Aspect_TOL_DASH);
+  Handle(Prs3d_LineAspect) aLineAspect = myDefaultDrawer->HiddenLineAspect();
+  aLineAspect->SetColor      (Quantity_NOC_GRAY20);
+  aLineAspect->SetWidth      (1.0);
+  aLineAspect->SetTypeOfLine (Aspect_TOL_DASH);
 
-  // tolerance to 4 pixels...
-  SetPixelTolerance();
+  // tolerance to 2 pixels...
+  SetPixelTolerance (2);
 
   // Customizing the drawer for trihedrons and planes...
+  Handle(Prs3d_DatumAspect) aTrihAspect = myDefaultDrawer->DatumAspect();
+  const Standard_Real aLength = 100.0;
+  aTrihAspect->SetAxisLength (aLength, aLength, aLength);
+  const Quantity_Color aColor = Quantity_NOC_LIGHTSTEELBLUE4;
+  aTrihAspect->LineAspect(Prs3d_DP_XAxis)->SetColor (aColor);
+  aTrihAspect->LineAspect(Prs3d_DP_YAxis)->SetColor (aColor);
+  aTrihAspect->LineAspect(Prs3d_DP_ZAxis)->SetColor (aColor);
 
-  Handle (Prs3d_DatumAspect) DA = myDefaultDrawer->DatumAspect();
-  Standard_Real aLength(100.);
-  DA->SetAxisLength(aLength,aLength,aLength);
-  Quantity_NameOfColor col = Quantity_NOC_LIGHTSTEELBLUE4;
-  DA->FirstAxisAspect()->SetColor(col);
-  DA->SecondAxisAspect()->SetColor(col);
-  DA->ThirdAxisAspect()->SetColor(col);
-
-  Handle(Prs3d_PlaneAspect)PA = myDefaultDrawer->PlaneAspect();
-  aLength =200.;
-  PA->SetPlaneLength(aLength,aLength);
-  PA->EdgesAspect()->SetColor(Quantity_NOC_SKYBLUE);
-  
-
+  Handle(Prs3d_PlaneAspect) aPlaneAspect = myDefaultDrawer->PlaneAspect();
+  const Standard_Real aPlaneLength = 200.0;
+  aPlaneAspect->SetPlaneLength (aPlaneLength, aPlaneLength);
+  aPlaneAspect->EdgesAspect()->SetColor (Quantity_NOC_SKYBLUE);
 }
 
-
 //=======================================================================
 //function : TrihedronSize
-//purpose  : 
+//purpose  :
 //=======================================================================
 Standard_Real AIS_InteractiveContext::TrihedronSize() const
 {
-  return myDefaultDrawer->DatumAspect()->FirstAxisLength();
+  return myDefaultDrawer->DatumAspect()->AxisLength(Prs3d_DP_XAxis);
 }
+
 //=======================================================================
 //function : SetTrihedronSize
-//purpose  : 
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetTrihedronSize(const Standard_Real aVal,const Standard_Boolean /*updateviewer*/)
+void AIS_InteractiveContext::SetTrihedronSize (const Standard_Real    theVal,
+                                               const Standard_Boolean /*updateviewer*/)
 {
-  myDefaultDrawer->DatumAspect()->SetAxisLength(aVal,aVal,aVal);
-  Redisplay(AIS_KOI_Datum,3,Standard_False);
-  Redisplay(AIS_KOI_Datum,4,Standard_True);
+  myDefaultDrawer->DatumAspect()->SetAxisLength (theVal, theVal, theVal);
+  Redisplay (AIS_KOI_Datum, 3, Standard_False);
+  Redisplay (AIS_KOI_Datum, 4, Standard_True);
 }
 
-
-
 //=======================================================================
 //function : SetPlaneSize
-//purpose  : 
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetPlaneSize(const Standard_Real aValX,
-                                          const Standard_Real aValY,
-                                          const Standard_Boolean /*updateviewer*/)
+void AIS_InteractiveContext::SetPlaneSize(const Standard_Real    theValX,
+                                          const Standard_Real    theValY,
+                                          const Standard_Boolean theToUpdateViewer)
 {
-  myDefaultDrawer->PlaneAspect()->SetPlaneLength(aValX,aValY);
-  Redisplay(AIS_KOI_Datum,7);
+  myDefaultDrawer->PlaneAspect()->SetPlaneLength (theValX, theValY);
+  Redisplay (AIS_KOI_Datum, 7, theToUpdateViewer);
 }
 
 //=======================================================================
 //function : SetPlaneSize
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-void AIS_InteractiveContext::SetPlaneSize(const Standard_Real aVal,
-                                          const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::SetPlaneSize (const Standard_Real    theVal,
+                                           const Standard_Boolean theToUpdateViewer)
 {
-  SetPlaneSize(aVal,aVal,updateviewer);
+  SetPlaneSize (theVal, theVal, theToUpdateViewer);
 }
 
 //=======================================================================
 //function : PlaneSize
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::PlaneSize(Standard_Real& LX,Standard_Real& LY) const
+Standard_Boolean AIS_InteractiveContext::PlaneSize (Standard_Real& theX,
+                                                    Standard_Real& theY) const
 {
-  LX = myDefaultDrawer->PlaneAspect()->PlaneXLength();
-  LY =  myDefaultDrawer->PlaneAspect()->PlaneYLength();
-  return (Abs(LX-LY)<=Precision::Confusion());
+  theX = myDefaultDrawer->PlaneAspect()->PlaneXLength();
+  theY = myDefaultDrawer->PlaneAspect()->PlaneYLength();
+  return (Abs (theX - theY) <= Precision::Confusion());
 }
 
+//=======================================================================
+//function : SetZLayer
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::SetZLayer (const Handle(AIS_InteractiveObject)& theIObj,
+                                        const Graphic3d_ZLayerId theLayerId)
+{
+  if (theIObj.IsNull())
+    return;
 
+  theIObj->SetZLayer (theLayerId);
+}
 
 //=======================================================================
-//function :
-//purpose  : 
+//function : GetZLayer
+//purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetAutoActivateSelection( const Standard_Boolean Auto )
+Graphic3d_ZLayerId AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
 {
-  myIsAutoActivateSelMode = Auto;
+  return !theIObj.IsNull()
+       ?  theIObj->ZLayer()
+       :  Graphic3d_ZLayerId_UNKNOWN;
 }
 
 //=======================================================================
-//function :
-//purpose  : 
+//function : RebuildSelectionStructs
+//purpose  : Rebuilds 1st level of BVH selection forcibly
 //=======================================================================
-Standard_Boolean AIS_InteractiveContext::GetAutoActivateSelection() const
+void AIS_InteractiveContext::RebuildSelectionStructs()
 {
-  return myIsAutoActivateSelMode;
+  myMainSel->RebuildObjectsTree (Standard_True);
 }
 
 //=======================================================================
-//function : SetZLayer
-//purpose  : 
+//function : Disconnect
+//purpose  : Disconnects selectable object from an assembly and updates selection structures
 //=======================================================================
-
-void AIS_InteractiveContext::SetZLayer (const Handle(AIS_InteractiveObject)& theIObj,
-                                        const Standard_Integer theLayerId)
+void AIS_InteractiveContext::Disconnect (const Handle(AIS_InteractiveObject)& theAssembly,
+                                         const Handle(AIS_InteractiveObject)& theObjToDisconnect)
 {
-  if (theIObj.IsNull ())
+  if (theAssembly->IsInstance ("AIS_MultipleConnectedInteractive"))
+  {
+    Handle(AIS_MultipleConnectedInteractive) theObj (Handle(AIS_MultipleConnectedInteractive)::DownCast (theAssembly));
+    theObj->Disconnect (theObjToDisconnect);
+    if (!myObjects.IsBound (theObjToDisconnect))
+    {
+      // connected presentation might contain displayed presentations
+      myMainPM->Erase (theObjToDisconnect, -1);
+      theObjToDisconnect->ErasePresentations (true);
+    }
+
+    const Handle(SelectMgr_SelectableObject)& anObj = theObjToDisconnect; // to avoid ambiguity
+    mgrSelector->Remove (anObj);
+  }
+  else if (theAssembly->IsInstance ("AIS_ConnectedInteractive") && theObjToDisconnect.IsNull())
+  {
+    Handle(AIS_ConnectedInteractive) theObj (Handle(AIS_ConnectedInteractive)::DownCast (theAssembly));
+    theObj->Disconnect();
+    const Handle(SelectMgr_SelectableObject)& anObj = theObj; // to avoid ambiguity
+    mgrSelector->Remove (anObj);
+  }
+  else
     return;
+}
+
+//=======================================================================
+//function : FitSelected
+//purpose  : Fits the view corresponding to the bounds of selected objects
+//=======================================================================
+void AIS_InteractiveContext::FitSelected (const Handle(V3d_View)& theView)
+{
+  FitSelected (theView, 0.01, Standard_True);
+}
 
-  if (myObjects.IsBound (theIObj))
+//=======================================================================
+//function : BoundingBoxOfSelection
+//purpose  :
+//=======================================================================
+Bnd_Box AIS_InteractiveContext::BoundingBoxOfSelection() const
+{
+  Bnd_Box aBndSelected;
+  AIS_MapOfObjectOwners anObjectOwnerMap;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
   {
-    if (myObjects (theIObj)->GraphicStatus() == AIS_DS_Displayed || myObjects (theIObj)->GraphicStatus() == AIS_DS_Erased)
+    const Handle(SelectMgr_EntityOwner)& anOwner = aSelIter.Value();
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
+    if (anObj->IsInfinite())
+    {
+      continue;
+    }
+
+    if (anOwner == anObj->GlobalSelOwner())
+    {
+      Bnd_Box aTmpBnd;
+      anObj->BoundingBox (aTmpBnd);
+      aBndSelected.Add (aTmpBnd);
+    }
+    else
     {
-      theIObj->SetZLayer (myMainPM, theLayerId);
+      Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
+      if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
+      {
+        anOwnerMap = new SelectMgr_IndexedMapOfOwner();
+        anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
+      }
+
+      anOwnerMap->Add (anOwner);
     }
   }
-  else if (HasOpenedContext ())
+
+  for (AIS_MapIteratorOfMapOfObjectOwners anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
   {
-    myLocalContexts (myCurLocalIndex)->SetZLayer (theIObj, theLayerId);
+    const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
+    Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
+    aBndSelected.Add (aTmpBox);
   }
+
+  return aBndSelected;
 }
 
 //=======================================================================
-//function : GetZLayer
-//purpose  : 
+//function : FitSelected
+//purpose  : Fits the view corresponding to the bounds of selected objects
 //=======================================================================
+void AIS_InteractiveContext::FitSelected (const Handle(V3d_View)& theView,
+                                          const Standard_Real theMargin,
+                                          const Standard_Boolean theToUpdate)
+{
+  Bnd_Box aBndSelected = BoundingBoxOfSelection();
+  if (!aBndSelected.IsVoid())
+  {
+    theView->FitAll (aBndSelected, theMargin, theToUpdate);
+  }
+}
 
-Standard_Integer AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
+//=======================================================================
+//function : SetTransformPersistence
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::SetTransformPersistence (const Handle(AIS_InteractiveObject)& theObject,
+                                                      const Handle(Graphic3d_TransformPers)& theTrsfPers)
 {
-  if (theIObj.IsNull ())
-    return -1;
+  theObject->SetTransformPersistence (theTrsfPers);
+  if (!myObjects.IsBound (theObject))
+  {
+    return;
+  }
 
-  if (myObjects.IsBound (theIObj))
+  mgrSelector->UpdateSelection (theObject);
+
+  const Standard_Integer    aLayerId   = myObjects.Find (theObject)->GetLayerIndex();
+  const Handle(V3d_Viewer)& aCurViewer = CurrentViewer();
+  for (V3d_ListOfViewIterator anActiveViewIter (aCurViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
   {
-    return theIObj->GetZLayer (myMainPM);
+    anActiveViewIter.Value()->View()->InvalidateBVHData (aLayerId);
+    anActiveViewIter.Value()->View()->InvalidateZLayerBoundingBox (aLayerId);
   }
-  else if (HasOpenedContext ())
+}
+
+//=======================================================================
+//function : GravityPoint
+//purpose  :
+//=======================================================================
+gp_Pnt AIS_InteractiveContext::GravityPoint (const Handle(V3d_View)& theView) const
+{
+  return theView->GravityPoint();
+}
+//=======================================================================
+//function : setObjectStatus
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::setObjectStatus (const Handle(AIS_InteractiveObject)& theIObj,
+                                              const AIS_DisplayStatus theStatus,
+                                              const Standard_Integer theDispMode,
+                                              const Standard_Integer theSelectionMode)
+{
+  if (theStatus != AIS_DS_None)
   {
-    return myLocalContexts (myCurLocalIndex)->GetZLayer (theIObj);
+    Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus (AIS_DS_Displayed, theDispMode, theSelectionMode);
+    myObjects.Bind (theIObj, aStatus);
   }
+  else
+    myObjects.UnBind (theIObj);
+
+  for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (theIObj->Children()); aPrsIter.More(); aPrsIter.Next())
+  {
+    Handle(AIS_InteractiveObject) aChild (Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value()));
+    if (aChild.IsNull())
+      continue;
 
-  return -1;
+    setObjectStatus (aChild, theStatus, theDispMode, theSelectionMode);
+  }
 }