0028726: Quantity_NameOfColor should be replaced by Quantity_Color in function input...
[occt.git] / src / AIS / AIS_InteractiveContext.cxx
index 8f3e0bd..3fcae94 100644 (file)
 // 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>
 
-#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_DataMapIteratorOfDataMapOfILC.hxx>
 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
 #include <AIS_ConnectedInteractive.hxx>
-#include <AIS_MultipleConnectedInteractive.hxx>
-#include <AIS_DataMapIteratorOfDataMapOfILC.hxx>
 #include <AIS_GlobalStatus.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_ListIteratorOfListOfInteractive.hxx>
+#include <AIS_LocalContext.hxx>
+#include <AIS_LocalStatus.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_ModedPresentation.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>
+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)
   {
-    return TCollection_AsciiString ("AIS_SelContext_")
-         + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_SEL));
-  }
+    theDrawer->SetMethod (Aspect_TOHM_COLOR);
+    theDrawer->SetDisplayMode (0);
 
-  static TCollection_AsciiString AIS_Context_NewCurName()
-  {
-    return TCollection_AsciiString ("AIS_CurContext_")
-         + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_CUR));
+    theDrawer->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_BLACK, 1.0));
+    *theDrawer->PointAspect()->Aspect() = *theDrawer->Link()->PointAspect()->Aspect();
+    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->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);
   }
 }
 
@@ -75,69 +108,93 @@ 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_True ),
+myWasLastMain(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),
-myZDetectionFlag(0),
-myIsAutoActivateSelMode( Standard_True )
-{ 
-  InitAttributes();
-}
-
-void AIS_InteractiveContext::Delete() const
+myCurDetected(0),
+myCurHighlighted(0),
+myPickingStrategy (SelectMgr_PickingStrategy_FirstAcceptable),
+myIsAutoActivateSelMode(Standard_True)
 {
-  // clear the static current selection
-  AIS_Selection::ClearCurrentSelection();
+  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();
 
-  // to avoid an exception
-  if (AIS_Selection::Find (mySelectionName.ToCString()))
+  myDefaultDrawer->SetZLayer(Graphic3d_ZLayerId_Default);
+  myDefaultDrawer->SetDisplayMode(0);
   {
-    AIS_Selection::Remove (mySelectionName.ToCString());
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_Dynamic];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_Top);
+    aStyle->SetColor (Quantity_NOC_CYAN1);
+  }
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_LocalDynamic];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_Topmost);
+    aStyle->SetColor (Quantity_NOC_CYAN1);
   }
-
-  // to avoid an exception
-  if (AIS_Selection::Find (myCurrentName.ToCString()))
   {
-    AIS_Selection::Remove (myCurrentName.ToCString());
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_Selected];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
+    aStyle->SetColor (Quantity_NOC_GRAY80);
   }
+  {
+    const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_LocalSelected];
+    aStyle->Link (myDefaultDrawer);
+    initDefaultHilightAttributes (aStyle);
+    aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
+    aStyle->SetColor (Quantity_NOC_GRAY80);
+  }
+  {
+    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();
+}
+
+//=======================================================================
+//function : ~AIS_InteractiveContext
+//purpose  :
+//=======================================================================
+AIS_InteractiveContext::~AIS_InteractiveContext()
+{
+  // clear the current selection
+  mySelection->Clear();
 
   // let's remove one reference explicitly. this operation's supposed to
   // be performed when mgrSelector will be destroyed but anyway...
-  mgrSelector->Remove (myMainSel);
+  const Handle(SelectMgr_ViewerSelector)& aSelector = myMainSel; // to avoid ambiguity
+  mgrSelector->Remove (aSelector);
 
   Handle(AIS_InteractiveContext) aNullContext;
   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
   {
     Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
     anObj->SetContext (aNullContext);
+    for (anObj->Init(); anObj->More(); anObj->Next())
+    {
+      anObj->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Renew);
+    }
   }
-  MMgt_TShared::Delete();
 }
 
-//=======================================================================
-//function : AIS_SelectionName
-//purpose  : 
-//=======================================================================
-const TCollection_AsciiString& AIS_InteractiveContext::SelectionName() const 
-{
-  if(!HasOpenedContext())
-    return mySelectionName;
-  return myLocalContexts(myCurLocalIndex)->SelectionName();
-
-} 
-
-
-
 //=======================================================================
 //function : UpdateCurrentViewer
 //purpose  : 
@@ -149,18 +206,6 @@ void AIS_InteractiveContext::UpdateCurrentViewer()
     myMainVwr->Update();
 }
 
-
-//=======================================================================
-//function : DomainOfMainViewer
-//purpose  : 
-//=======================================================================
-
-Standard_CString AIS_InteractiveContext::DomainOfMainViewer() const 
-{
-  return myMainVwr->Domain();
-  
-}
-
 //=======================================================================
 //function : DisplayedObjects
 //purpose  :
@@ -202,7 +247,7 @@ void AIS_InteractiveContext::DisplayedObjects (AIS_ListOfInteractive& theListOfI
   for (TColStd_MapIteratorOfMapOfTransient aDispMapIter (aDispMap); aDispMapIter.More(); aDispMapIter.Next())
   {
     const Handle(Standard_Transient)& aTransient = aDispMapIter.Key();
-    anObj = *((Handle(AIS_InteractiveObject)* )&aTransient);
+    anObj = Handle(AIS_InteractiveObject)::DownCast (aTransient);
     theListOfIO.Append (anObj);
   }
 }
@@ -266,8 +311,12 @@ void AIS_InteractiveContext::ObjectsByDisplayStatus (const AIS_KindOfInteractive
 {
   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
   {
-    if (anObjIter.Value()->GraphicStatus() != theStatus
-     || anObjIter.Key()->Type() != theKind)
+    if (theStatus != AIS_DS_None
+     && anObjIter.Value()->GraphicStatus() != theStatus)
+    {
+      continue;
+    }
+    else if (anObjIter.Key()->Type() != theKind)
     {
       continue;
     }
@@ -313,6 +362,35 @@ void AIS_InteractiveContext::ObjectsInside (AIS_ListOfInteractive&      theListO
   }
 }
 
+//=======================================================================
+//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  :
@@ -332,6 +410,33 @@ void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIO
            theToUpdateViewer, theIObj->AcceptShapeDecomposition());
 }
 
+//=======================================================================
+//function : SetViewAffinity
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::SetViewAffinity (const Handle(AIS_InteractiveObject)& theIObj,
+                                              const Handle(V3d_View)&              theView,
+                                              const Standard_Boolean               theIsVisible)
+{
+  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());
+  }
+  else
+  {
+    theView->View()->ChangeHiddenObjects()->Add (theIObj.get());
+  }
+}
+
 //=======================================================================
 //function : Display
 //purpose  :
@@ -340,36 +445,51 @@ void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIO
                                       const Standard_Integer               theDispMode,
                                       const Standard_Integer               theSelectionMode,
                                       const Standard_Boolean               theToUpdateViewer,
-                                      const Standard_Boolean               theToAllowDecomposition)
+                                      const Standard_Boolean               theToAllowDecomposition,
+                                      const AIS_DisplayStatus              theDispStatus)
 {
   if (theIObj.IsNull())
   {
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
+  if (theDispStatus == AIS_DS_Erased)
   {
-    theIObj->SetContext (this);
+    Erase  (theIObj, theToUpdateViewer);
+    Load (theIObj, theSelectionMode, theToAllowDecomposition);
+    return;
   }
 
-  if (HasOpenedContext())
+  setContextToObject (theIObj);
+  if (theDispStatus == AIS_DS_Temporary
+  && !HasOpenedContext())
   {
-    myLocalContexts (myCurLocalIndex)->Display (theIObj, theDispMode, theToAllowDecomposition, theSelectionMode);
-    if (theToUpdateViewer)
+    return;
+  }
+  else if (HasOpenedContext())
+  {
+    if (theDispStatus == AIS_DS_None
+     || theDispStatus == AIS_DS_Temporary)
     {
-      myMainVwr->Update();
+      myLocalContexts (myCurLocalIndex)->Display (theIObj, theDispMode, theToAllowDecomposition, theSelectionMode);
+      if (theToUpdateViewer)
+      {
+        myMainVwr->Update();
+      }
+      return;
     }
-    return;
   }
 
   if (!myObjects.IsBound (theIObj))
   {
     Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus (AIS_DS_Displayed, theDispMode, theSelectionMode);
     myObjects.Bind   (theIObj, aStatus);
+    Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->StructureManager()->RegisterObject (theIObj);
     myMainPM->Display(theIObj, theDispMode);
     if (theSelectionMode != -1)
     {
-      if (!mgrSelector->Contains (theIObj))
+      const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+      if (!mgrSelector->Contains (anObj))
       {
         mgrSelector->Load (theIObj);
       }
@@ -379,58 +499,43 @@ void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIO
   else
   {
     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
-    if (aStatus->GraphicStatus() != AIS_DS_Displayed
-     && aStatus->GraphicStatus() != AIS_DS_Erased)
+    if (aStatus->GraphicStatus() == AIS_DS_Temporary)
     {
       return;
     }
 
-    // Erase presentations for all display modes different from aDispMode.
+    // 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.
-    TColStd_ListOfInteger aModesToRemove;
-    for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
+    const Standard_Integer anOldMode = aStatus->DisplayMode();
+    if (anOldMode != theDispMode)
     {
-      const Standard_Integer anOldMode = aDispModeIter.Value();
-      if (anOldMode != theDispMode)
+      if(myMainPM->IsHighlighted (theIObj, anOldMode))
       {
-        aModesToRemove.Append (anOldMode);
-        if(myMainPM->IsHighlighted (theIObj, anOldMode))
-        {
-          myMainPM->Unhighlight (theIObj, anOldMode);
-        }
-        myMainPM->Erase (theIObj, anOldMode);
+        unhighlightGlobal (theIObj);
       }
+      myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
     }
 
-    for (TColStd_ListIteratorOfListOfInteger aRemModeIter (aModesToRemove); aRemModeIter.More(); aRemModeIter.Next())
-    {
-      aStatus->RemoveDisplayMode (aRemModeIter.Value());
-    }
-
-    if (!aStatus->IsDModeIn (theDispMode))
-    {
-      aStatus->AddDisplayMode (theDispMode);
-    }
+    aStatus->SetDisplayMode (theDispMode);
 
     myMainPM->Display (theIObj, theDispMode);
-    if (aStatus->GraphicStatus() == AIS_DS_Erased)
-    {
-      aStatus->SetGraphicStatus (AIS_DS_Displayed);
-    }
+    aStatus->SetGraphicStatus (AIS_DS_Displayed);
     if (aStatus->IsHilighted())
     {
-      const Standard_Integer aHiMod = theIObj->HasHilightMode() ? theIObj->HilightMode() : theDispMode;
-      myMainPM->Highlight (theIObj, aHiMod);
+      highlightGlobal (theIObj, aStatus->HilightStyle(), theDispMode);
     }
     if (theSelectionMode != -1)
     {
-      if (!mgrSelector->Contains (theIObj))
+      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, myMainSel);
       }
     }
@@ -455,11 +560,7 @@ void AIS_InteractiveContext::Load (const Handle(AIS_InteractiveObject)& theIObj,
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
+  setContextToObject (theIObj);
   if (HasOpenedContext())
   {
     myLocalContexts (myCurLocalIndex)->Load (theIObj, theToAllowDecomposition, theSelMode);
@@ -467,13 +568,22 @@ void AIS_InteractiveContext::Load (const Handle(AIS_InteractiveObject)& theIObj,
   }
 
   if (theSelMode == -1
-  && !theToAllowDecomposition
-  && !myObjects.IsBound (theIObj))
+  && !theToAllowDecomposition)
   {
-    Standard_Integer aDispMode, aHiMod, aSelModeDef;
-    GetDefModes (theIObj, aDispMode, aHiMod, aSelModeDef);
-    Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus (AIS_DS_Erased, aDispMode, aSelModeDef);
-    myObjects.Bind (theIObj, aStatus);
+    if (!myObjects.IsBound (theIObj))
+    {
+      Standard_Integer aDispMode, aHiMod, aSelModeDef;
+      GetDefModes (theIObj, aDispMode, aHiMod, aSelModeDef);
+      Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus (AIS_DS_Erased, aDispMode, aSelModeDef);
+      myObjects.Bind (theIObj, aStatus);
+    }
+
+    // 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);
+    }
   }
 }
 
@@ -583,10 +693,9 @@ void AIS_InteractiveContext::DisplaySelected (const Standard_Boolean theToUpdate
   }
 
   Standard_Boolean      isFound  = Standard_False;
-  Handle(AIS_Selection) aSelIter = AIS_Selection::Selection (myCurrentName.ToCString());
-  for (aSelIter->Init(); aSelIter->More(); aSelIter->Next())
+  for (mySelection->Init(); mySelection->More(); mySelection->Next())
   {
-    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (mySelection->Value()->Selectable());
     Display (anObj, Standard_False);
     isFound = Standard_True;
   }
@@ -609,12 +718,16 @@ void AIS_InteractiveContext::EraseSelected (const Standard_Boolean theToUpdateVi
   }
 
   Standard_Boolean      isFound  = Standard_False;
-  Handle(AIS_Selection) aSelIter = AIS_Selection::Selection(myCurrentName.ToCString());
-  for (aSelIter->Init(); aSelIter->More(); aSelIter->Next())
+  mySelection->Init();
+  while (mySelection->More())
   {
-    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
+    Handle(SelectMgr_EntityOwner) anOwner = mySelection->Value();
+    Handle(AIS_InteractiveObject) anObj   = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+
     Erase (anObj, Standard_False);
     isFound = Standard_True;
+
+    mySelection->Init();
   }
 
   if (isFound && theToUpdateViewer)
@@ -712,15 +825,6 @@ AIS_DisplayStatus AIS_InteractiveContext::DisplayStatus (const Handle(AIS_Intera
   return AIS_DS_None;
 }
 
-//=======================================================================
-//function : DisplayedModes
-//purpose  :
-//=======================================================================
-const TColStd_ListOfInteger& AIS_InteractiveContext::DisplayedModes (const Handle(AIS_InteractiveObject)& theIObj) const
-{
-  return myObjects (theIObj)->DisplayedModes();
-}
-
 //=======================================================================
 //function : Remove
 //purpose  :
@@ -733,6 +837,15 @@ void AIS_InteractiveContext::Remove (const Handle(AIS_InteractiveObject)& theIOb
     return;
   }
 
+  if (theIObj->HasInteractiveContext())
+  {
+    if (theIObj->myCTXPtr != this)
+    {
+      throw Standard_ProgramError("AIS_InteractiveContext - object has been displayed in another context!");
+    }
+    theIObj->SetContext (Handle(AIS_InteractiveContext)());
+  }
+
   if (HasOpenedContext())
   {
     myLocalContexts (myCurLocalIndex)->Remove (theIObj);
@@ -805,163 +918,176 @@ void AIS_InteractiveContext::ClearPrs (const Handle(AIS_InteractiveObject)& theI
 }
 
 //=======================================================================
-//function : Hilight
-//purpose  :
+//function : HilightWithColor
+//purpose  : 
 //=======================================================================
-void AIS_InteractiveContext::Hilight (const Handle(AIS_InteractiveObject)& theIObj,
-                                      const Standard_Boolean               theToUpdateViewer)
+void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject)& theObj,
+                                              const Handle(Prs3d_Drawer)& theStyle,
+                                              const Standard_Boolean theIsToUpdate)
 {
-  if (theIObj.IsNull())
-  {
+  if (theObj.IsNull())
     return;
-  }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
+  setContextToObject (theObj);
   if (!HasOpenedContext())
   {
-    if (!myObjects.IsBound (theIObj))
-    {
+    if (!myObjects.IsBound (theObj))
       return;
-    }
 
-    Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
+    const Handle(AIS_GlobalStatus)& aStatus = myObjects (theObj);
     aStatus->SetHilightStatus (Standard_True);
+
     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
     {
-      Standard_Integer aHilightMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
-      myMainPM->Highlight (theIObj, aHilightMode);
+      highlightGlobal (theObj, theStyle, aStatus->DisplayMode());
+      aStatus->SetHilightStyle (theStyle);
     }
   }
   else
   {
-    myLocalContexts (myCurLocalIndex)->Hilight (theIObj);
+    myLocalContexts (myCurLocalIndex)->Hilight (theObj, theStyle);
   }
 
-  if (theToUpdateViewer)
-  {
+  if (theIsToUpdate)
     myMainVwr->Update();
-  }
 }
+
 //=======================================================================
-//function : Hilight
+//function : Unhilight
 //purpose  : 
 //=======================================================================
-
-void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject)& anIObj,
-                                              const Quantity_NameOfColor aCol,
-                                              const Standard_Boolean updateviewer)
+void AIS_InteractiveContext::Unhilight(const Handle(AIS_InteractiveObject)& anIObj, const Standard_Boolean updateviewer)
 {
   if(anIObj.IsNull()) return;
 
-  if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
-
   if (!HasOpenedContext())
   {
     if(!myObjects.IsBound(anIObj)) return;
 
-    const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-    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)
+    if (aStatus->GraphicStatus() == AIS_DS_Displayed)
     {
-      Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
-      myMainPM->Color (anIObj, aCol, aHilightMode);
-      STATUS->SetHilightColor (aCol);
+      unhighlightGlobal (anIObj);
     }
   }
   else
   {
-    myLocalContexts(myCurLocalIndex)->Hilight(anIObj,aCol);
+    myLocalContexts(myCurLocalIndex)->Unhilight(anIObj);
   }
   if(updateviewer) myMainVwr->Update();
 }
 
 //=======================================================================
-//function : Unhilight
-//purpose  : 
+//function : IsHilighted
+//purpose  : Returns true if the objects global status is set to highlighted.
 //=======================================================================
-
-void AIS_InteractiveContext::Unhilight(const Handle(AIS_InteractiveObject)& anIObj, const Standard_Boolean updateviewer)
+Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(AIS_InteractiveObject)& theObj) const
 {
-  if(anIObj.IsNull()) return;
-
   if (!HasOpenedContext())
   {
-    if(!myObjects.IsBound(anIObj)) return;
+    if (!myObjects.IsBound (theObj))
+      return Standard_False;
 
-    const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-    STATUS->SetHilightStatus (Standard_False);
-    STATUS->SetHilightColor(Quantity_NOC_WHITE);
+    return myObjects (theObj)->IsHilighted();
+  }
 
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed)
+  Standard_DISABLE_DEPRECATION_WARNINGS
+  Standard_Integer aCtxIdx = HighestIndex();
+  Standard_ENABLE_DEPRECATION_WARNINGS
+  for (; aCtxIdx >= 1; aCtxIdx--)
+  {
+    if (myLocalContexts.IsBound (aCtxIdx))
     {
-      Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
-      myMainPM->Unhighlight (anIObj, aHilightMode);
+      if (myLocalContexts (aCtxIdx)->IsHilighted (theObj))
+        return Standard_True;
     }
   }
-  else
-  {
-    myLocalContexts(myCurLocalIndex)->Unhilight(anIObj);
-  }
-  if(updateviewer) myMainVwr->Update();
+
+  return Standard_False;
 }
 
 //=======================================================================
 //function : IsHilighted
-//purpose  : 
+//purpose  : Returns true if the owner is highlighted with selection style.
 //=======================================================================
-
-Standard_Boolean AIS_InteractiveContext::IsHilighted(const Handle(AIS_InteractiveObject)& anIObj) const 
+Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(SelectMgr_EntityOwner)& theOwner) const
 {
-  if(anIObj.IsNull()) return Standard_False;
+  if (theOwner.IsNull() || !theOwner->HasSelectable())
+    return Standard_False;
+
+  const Handle(AIS_InteractiveObject) anObj =
+    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
 
-  if (!HasOpenedContext()){
-    if(!myObjects.IsBound(anIObj)) 
+  if (anObj->GlobalSelOwner() == theOwner)
+  {
+    if (!myObjects.IsBound (anObj))
       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 myObjects (anObj)->IsHilighted();
   }
-  return Standard_False;
+
+  return theOwner->IsSelected();
 }
 
-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;
-      }
-    }
-    return Standard_False;
+//=======================================================================
+//function : HighlightStyle
+//purpose  :
+//=======================================================================
+Standard_Boolean AIS_InteractiveContext::HighlightStyle (const Handle(AIS_InteractiveObject)& theObj,
+                                                         Handle(Prs3d_Drawer)& theStyle) const
+{
+  if (HasOpenedContext())
+    myLocalContexts (myCurLocalIndex)->HighlightStyle (theObj, theStyle);
+
+  if (IsHilighted (theObj))
+  {
+    theStyle = myObjects (theObj)->HilightStyle();
+    return Standard_True;
   }
-  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
+  {
+    theStyle.Nullify();
+    return Standard_False;
   }
-  return Standard_False;
 }
 
+//=======================================================================
+//function : HighlightStyle
+//purpose  :
+//=======================================================================
+Standard_Boolean AIS_InteractiveContext::HighlightStyle (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                         Handle(Prs3d_Drawer)& theStyle) const
+{
+  if (theOwner.IsNull() || !theOwner->HasSelectable())
+    return Standard_False;
 
+  if (IsHilighted (theOwner))
+  {
+    const Handle(AIS_InteractiveObject) anObj =
+      Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+    if (anObj->GlobalSelOwner() == theOwner)
+    {
+      theStyle = myObjects (anObj)->HilightStyle();
+    }
+    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;
+  }
+}
 
 //=======================================================================
 //function : IsDisplayed
@@ -1002,7 +1128,7 @@ Standard_Boolean AIS_InteractiveContext::IsDisplayed (const Handle(AIS_Interacti
   {
     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
     if (aStatus->GraphicStatus() == AIS_DS_Displayed
-     && aStatus->IsDModeIn (theMode))
+     && aStatus->DisplayMode() == theMode)
     {
       return Standard_True;
     }
@@ -1039,8 +1165,8 @@ Standard_Integer AIS_InteractiveContext::DisplayPriority (const Handle(AIS_Inter
   {
     Standard_Integer aDispMode = theIObj->HasDisplayMode()
                                ? theIObj->DisplayMode()
-                               : (theIObj->AcceptDisplayMode (myDisplayMode)
-                                ? myDisplayMode
+                               : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
+                                ? myDefaultDrawer->DisplayMode()
                                 : 0);
     return myMainPM->DisplayPriority (theIObj, aDispMode);
   }
@@ -1059,11 +1185,7 @@ void AIS_InteractiveContext::SetDisplayPriority (const Handle(AIS_InteractiveObj
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
+  setContextToObject (theIObj);
   if (myObjects.IsBound (theIObj))
   {
     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
@@ -1072,8 +1194,8 @@ void AIS_InteractiveContext::SetDisplayPriority (const Handle(AIS_InteractiveObj
     {
       Standard_Integer aDisplayMode = theIObj->HasDisplayMode()
                                     ? theIObj->DisplayMode()
-                                    : (theIObj->AcceptDisplayMode (myDisplayMode)
-                                     ? myDisplayMode
+                                    : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
+                                     ? myDefaultDrawer->DisplayMode()
                                      : 0);
       myMainPM->SetDisplayPriority (theIObj, aDisplayMode, thePriority);
     }
@@ -1274,6 +1396,16 @@ void AIS_InteractiveContext::SetLocation (const Handle(AIS_InteractiveObject)& t
     Handle(StdSelect_ViewerSelector3d) aTempSel = myLocalContexts (myCurLocalIndex)->MainSelector();
     mgrSelector->Update (theIObj, aTempSel, 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);
+  }
 }
 
 //=======================================================================
@@ -1325,7 +1457,7 @@ void AIS_InteractiveContext::SetDeviationCoefficient (const Standard_Real theCoe
 //=======================================================================
 void AIS_InteractiveContext::SetDeviationAngle (const Standard_Real theAngle)
 {
-  myDefaultDrawer->SetDeviationCoefficient (theAngle);
+  myDefaultDrawer->SetDeviationAngle (theAngle);
 }
 
 //=======================================================================
@@ -1401,10 +1533,10 @@ Standard_Real AIS_InteractiveContext::HLRAngle() const
 //function : SetDisplayMode
 //purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetDisplayMode (const AIS_DisplayMode  theMode,
-                                             const Standard_Boolean theToUpdateViewer)
+void AIS_InteractiveContext::SetDisplayMode(const Standard_Integer theMode,
+                                            const Standard_Boolean theToUpdateViewer)
 {
-  if (theMode == myDisplayMode)
+  if (theMode == myDefaultDrawer->DisplayMode())
   {
     return;
   }
@@ -1424,24 +1556,26 @@ void AIS_InteractiveContext::SetDisplayMode (const AIS_DisplayMode  theMode,
     }
 
     Handle(AIS_GlobalStatus) aStatus = anObjIter.Value();
-    if (aStatus->IsDModeIn (myDisplayMode))
-    {
-      aStatus->RemoveDisplayMode (myDisplayMode);
-    }
+    aStatus->SetDisplayMode (theMode);
 
-    aStatus->AddDisplayMode (theMode);
     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
     {
-      myMainPM->SetVisibility (anObj, myDisplayMode, Standard_False);
       myMainPM->Display (anObj, theMode);
+      if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (anObj))
+      {
+        myMainPM->BeginImmediateDraw();
+        unhighlightGlobal (anObj);
+        myMainPM->EndImmediateDraw (myMainVwr);
+      }
       if (aStatus->IsSubIntensityOn())
       {
-        myMainPM->Color (anObj, mySubIntensity, theMode);
+        highlightWithSubintensity (anObj, theMode);
       }
+      myMainPM->SetVisibility (anObj, myDefaultDrawer->DisplayMode(), Standard_False);
     }
   }
 
-  myDisplayMode = theMode;
+  myDefaultDrawer->SetDisplayMode (theMode);
   if (theToUpdateViewer)
   {
     myMainVwr->Update();
@@ -1456,20 +1590,11 @@ void AIS_InteractiveContext::SetDisplayMode (const Handle(AIS_InteractiveObject)
                                              const Standard_Integer               theMode,
                                              const Standard_Boolean               theToUpdateViewer)
 {
-  if (!theIObj->HasInteractiveContext())
+  setContextToObject (theIObj);
+  if (!myObjects.IsBound (theIObj))
   {
-    theIObj->SetContext(this);
-  }
-
-  if (HasOpenedContext())
-  {
-    return;
-  }
-
-  if (!myObjects.IsBound (theIObj))
-  {
-    theIObj->SetDisplayMode (theMode);
-    return;
+    theIObj->SetDisplayMode (theMode);
+    return;
   }
   else if (!theIObj->AcceptDisplayMode (theMode))
   {
@@ -1479,46 +1604,32 @@ void AIS_InteractiveContext::SetDisplayMode (const Handle(AIS_InteractiveObject)
   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>
-  TColStd_ListOfInteger aModesToRemove;
-  for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
+  const Standard_Integer anOldMode = aStatus->DisplayMode();
+  if (anOldMode != theMode)
   {
-    const Standard_Integer anOldMode = aDispModeIter.Value();
-    if (anOldMode != theMode)
+    if (myMainPM->IsHighlighted (theIObj, anOldMode))
     {
-      aModesToRemove.Append (anOldMode);
-      if (myMainPM->IsHighlighted (theIObj, anOldMode))
-      {
-        myMainPM->Unhighlight (theIObj, anOldMode);
-      }
-      myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
+      unhighlightGlobal (theIObj);
     }
+    myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
   }
 
-  for (TColStd_ListIteratorOfListOfInteger aRemModeIter (aModesToRemove); aRemModeIter.More(); aRemModeIter.Next())
-  {
-    aStatus->RemoveDisplayMode (aRemModeIter.Value());
-  }
-
-  if (!aStatus->IsDModeIn (theMode))
-  {
-    aStatus->AddDisplayMode (theMode);
-  }
+  aStatus->SetDisplayMode (theMode);
 
   myMainPM->Display (theIObj, theMode);
-  Standard_Integer aDispMode, aHiMode, aSelMode;
-  GetDefModes (theIObj, aDispMode, aHiMode, aSelMode);
   if (aStatus->IsHilighted())
   {
-    myMainPM->Highlight (theIObj, aHiMode);
+    highlightGlobal (theIObj, getSelStyle (theIObj, theIObj->GlobalSelOwner()), theMode);
   }
   if (aStatus->IsSubIntensityOn())
   {
-    myMainPM->Color (theIObj, mySubIntensity, theMode);
+    highlightWithSubintensity (theIObj, theMode);
   }
 
   if (theToUpdateViewer)
@@ -1548,36 +1659,29 @@ void AIS_InteractiveContext::UnsetDisplayMode (const Handle(AIS_InteractiveObjec
   }
 
   const Standard_Integer anOldMode = theIObj->DisplayMode();
-  if (myDisplayMode == anOldMode)
+  if (myDefaultDrawer->DisplayMode() == anOldMode)
   {
     return;
   }
 
   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
-  aStatus->RemoveDisplayMode (anOldMode);
-  if (!aStatus->IsDModeIn(myDisplayMode))
-  {
-    aStatus->AddDisplayMode (myDisplayMode);
-  }
+  aStatus->SetDisplayMode (myDefaultDrawer->DisplayMode());
 
   if (aStatus->GraphicStatus() == AIS_DS_Displayed)
   {
     if (myMainPM->IsHighlighted (theIObj, anOldMode))
     {
-      myMainPM->Unhighlight (theIObj, anOldMode);
+      unhighlightGlobal (theIObj);
     }
     myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
-    myMainPM->Display (theIObj, myDisplayMode);
-
-    Standard_Integer aDispMode, aHiMode, aSelMode;
-    GetDefModes (theIObj, aDispMode, aHiMode, aSelMode);
+    myMainPM->Display (theIObj, myDefaultDrawer->DisplayMode());
     if (aStatus->IsHilighted())
     {
-      myMainPM->Highlight (theIObj, aHiMode);
+      highlightSelected (theIObj->GlobalSelOwner());
     }
     if (aStatus->IsSubIntensityOn())
     {
-      myMainPM->Color (theIObj, mySubIntensity, myDisplayMode);
+      highlightWithSubintensity (theIObj, myDefaultDrawer->DisplayMode());
     }
 
     if (theToUpdateViewer)
@@ -1611,7 +1715,8 @@ void AIS_InteractiveContext::redisplayPrsRecModes (const Handle(AIS_InteractiveO
 {
   if (theIObj->RecomputeEveryPrs())
   {
-    theIObj->Redisplay();
+    theIObj->Update (Standard_True);
+    theIObj->UpdateSelection();
   }
   else
   {
@@ -1619,6 +1724,7 @@ void AIS_InteractiveContext::redisplayPrsRecModes (const Handle(AIS_InteractiveO
     {
       theIObj->Update (aModes.Value(), Standard_False);
     }
+    theIObj->UpdateSelection();
     theIObj->SetRecomputeOk();
   }
 
@@ -1637,7 +1743,8 @@ void AIS_InteractiveContext::redisplayPrsModes (const Handle(AIS_InteractiveObje
 {
   if (theIObj->RecomputeEveryPrs())
   {
-    theIObj->Redisplay();
+    theIObj->Update (Standard_True);
+    theIObj->UpdateSelection();
   }
   else
   {
@@ -1661,31 +1768,32 @@ void AIS_InteractiveContext::redisplayPrsModes (const Handle(AIS_InteractiveObje
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theIObj,
-                                       const Quantity_NameOfColor           theColor,
+                                       const Quantity_Color&                theColor,
                                        const Standard_Boolean               theToUpdateViewer)
 {
-  SetColor (theIObj, Quantity_Color(theColor), theToUpdateViewer);
+  if (theIObj.IsNull())
+  {
+    return;
+  }
+
+  setContextToObject (theIObj);
+  theIObj->SetColor (theColor);
+  redisplayPrsRecModes (theIObj, theToUpdateViewer);
 }
 
 //=======================================================================
-//function : SetColor
+//function : SetIsoOnTriangulation
 //purpose  :
 //=======================================================================
-void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theIObj,
-                                       const Quantity_Color&                theColor,
-                                       const Standard_Boolean               theToUpdateViewer)
+void AIS_InteractiveContext::IsoOnTriangulation (const Standard_Boolean theIsEnabled,
+                                                 const Handle(AIS_InteractiveObject)& theObject)
 {
-  if (theIObj.IsNull())
+  if (theObject.IsNull())
   {
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-  theIObj->SetColor (theColor);
-  redisplayPrsRecModes (theIObj, theToUpdateViewer);
+  theObject->SetIsoOnTriangulation (theIsEnabled);
 }
 
 //=======================================================================
@@ -1701,12 +1809,8 @@ void AIS_InteractiveContext::SetDeviationCoefficient (const Handle(AIS_Interacti
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
   // 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)
   {
@@ -1735,12 +1839,8 @@ void AIS_InteractiveContext::SetHLRDeviationCoefficient (const Handle(AIS_Intera
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
   // 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)
   {
@@ -1769,12 +1869,8 @@ void AIS_InteractiveContext::SetDeviationAngle (const Handle(AIS_InteractiveObje
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
   if (theIObj->Type() != AIS_KOI_Shape)
   {
     return;
@@ -1802,12 +1898,8 @@ void AIS_InteractiveContext::SetAngleAndDeviation (const Handle(AIS_InteractiveO
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
   if (theIObj->Type() != AIS_KOI_Shape)
   {
     return;
@@ -1822,7 +1914,8 @@ void AIS_InteractiveContext::SetAngleAndDeviation (const Handle(AIS_InteractiveO
 
   if (theIObj->RecomputeEveryPrs())
   {
-    theIObj->Redisplay();
+    theIObj->Update (Standard_True);
+    theIObj->UpdateSelection();
   }
   else
   {
@@ -1843,12 +1936,8 @@ void AIS_InteractiveContext::SetHLRAngleAndDeviation (const Handle(AIS_Interacti
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
   if (theIObj->Type() != AIS_KOI_Shape)
   {
     return;
@@ -1875,12 +1964,8 @@ void AIS_InteractiveContext::SetHLRDeviationAngle (const Handle(AIS_InteractiveO
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
+  setContextToObject (theIObj);
   if (theIObj->Type() != AIS_KOI_Shape)
   {
     return;
@@ -1919,15 +2004,6 @@ Standard_Boolean AIS_InteractiveContext::HasColor (const Handle(AIS_InteractiveO
   return theIObj->HasColor();
 }
 
-//=======================================================================
-//function : Color
-//purpose  :
-//=======================================================================
-Quantity_NameOfColor AIS_InteractiveContext::Color (const Handle(AIS_InteractiveObject)& theIObj) const
-{
-  return theIObj->Color();
-}
-
 //=======================================================================
 //function : Color
 //purpose  :
@@ -1960,13 +2036,25 @@ void AIS_InteractiveContext::SetWidth (const Handle(AIS_InteractiveObject)& theI
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
+  setContextToObject (theIObj);
   theIObj->SetWidth (theWidth);
   redisplayPrsRecModes (theIObj, theToUpdateViewer);
+  if (!myLastinMain.IsNull() && myLastinMain->IsSameSelectable (theIObj))
+  {
+    if (myLastinMain->IsAutoHilight())
+    {
+      const Standard_Integer aHiMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+      myLastinMain->HilightWithColor (myMainPM,
+                                      myLastinMain->IsSelected() ? getSelStyle (theIObj, myLastinMain) : getHiStyle (theIObj, myLastinMain),
+                                      aHiMode);
+    }
+    else
+    {
+      theIObj->HilightOwnerWithColor (myMainPM,
+                                      myLastinMain->IsSelected() ? getSelStyle (theIObj, myLastinMain) : getHiStyle (theIObj, myLastinMain),
+                                      myLastinMain);
+    }
+  }
 }
 
 //=======================================================================
@@ -1990,7 +2078,7 @@ void AIS_InteractiveContext::UnsetWidth (const Handle(AIS_InteractiveObject)& th
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::SetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
-                                          const Graphic3d_NameOfMaterial       theName,
+                                          const Graphic3d_MaterialAspect&      theMaterial,
                                           const Standard_Boolean               theToUpdateViewer)
 {
   if (theIObj.IsNull())
@@ -1998,12 +2086,8 @@ void AIS_InteractiveContext::SetMaterial (const Handle(AIS_InteractiveObject)& t
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
-  theIObj->SetMaterial (theName);
+  setContextToObject (theIObj);
+  theIObj->SetMaterial (theMaterial);
   redisplayPrsRecModes (theIObj, theToUpdateViewer);
 }
 
@@ -2035,18 +2119,14 @@ void AIS_InteractiveContext::SetTransparency (const Handle(AIS_InteractiveObject
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
+  setContextToObject (theIObj);
   if (!theIObj->IsTransparent()
-    && theValue <= 0.05)
+    && theValue <= 0.005)
   {
     return;
   }
 
-  if (theValue <= 0.05)
+  if (theValue <= 0.005)
   {
     UnsetTransparency (theIObj, theToUpdateViewer);
     return;
@@ -2077,7 +2157,6 @@ void AIS_InteractiveContext::UnsetTransparency (const Handle(AIS_InteractiveObje
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::SetSelectedAspect (const Handle(Prs3d_BasicAspect)& theAspect,
-                                                const Standard_Boolean           theIsGlobalChange,
                                                 const Standard_Boolean           theToUpdateViewer)
 {
   if (HasOpenedContext())
@@ -2086,16 +2165,14 @@ void AIS_InteractiveContext::SetSelectedAspect (const Handle(Prs3d_BasicAspect)&
   }
 
   Standard_Boolean isFound = Standard_False;
-  Handle(AIS_Selection) aSelIter = AIS_Selection::Selection (myCurrentName.ToCString());
-  for (aSelIter->Init(); aSelIter->More(); aSelIter->Next())
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
   {
     isFound = Standard_True;
-    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
-    anObj->SetAspect (theAspect, theIsGlobalChange);
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
+    anObj->SetAspect (theAspect);
   }
 
-  if (isFound
-   && theToUpdateViewer)
+  if (isFound && theToUpdateViewer)
   {
     myMainVwr->Update();
   }
@@ -2106,7 +2183,7 @@ void AIS_InteractiveContext::SetSelectedAspect (const Handle(Prs3d_BasicAspect)&
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::SetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
-                                                 const Handle(AIS_Drawer)&            theDrawer,
+                                                 const Handle(Prs3d_Drawer)&          theDrawer,
                                                  const Standard_Boolean               theToUpdateViewer)
 {
   if (theIObj.IsNull())
@@ -2114,11 +2191,7 @@ void AIS_InteractiveContext::SetLocalAttributes (const Handle(AIS_InteractiveObj
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
-
+  setContextToObject (theIObj);
   theIObj->SetAttributes (theDrawer);
   Update (theIObj, theToUpdateViewer);
 }
@@ -2135,10 +2208,7 @@ void AIS_InteractiveContext::UnsetLocalAttributes (const Handle(AIS_InteractiveO
     return;
   }
 
-  if (!theIObj->HasInteractiveContext())
-  {
-    theIObj->SetContext (this);
-  }
+  setContextToObject (theIObj);
   theIObj->UnsetAttributes();
   Update (theIObj, theToUpdateViewer);
 }
@@ -2177,13 +2247,10 @@ void AIS_InteractiveContext::Status (const Handle(AIS_InteractiveObject)& theIOb
   }
 
   theStatus += "\t| Active Display Modes in the MainViewer :\n";
-  for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
-  {
-    theStatus += "\t|\t Mode ";
-    theStatus += TCollection_AsciiString (aDispModeIter.Value());
-    theStatus += "\n";
-  }
-  if (IsCurrent (theIObj))  theStatus +="\t| Current\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";
@@ -2212,11 +2279,11 @@ void AIS_InteractiveContext::GetDefModes (const Handle(AIS_InteractiveObject)& t
 
   theDispMode = theIObj->HasDisplayMode()
               ? theIObj->DisplayMode()
-              : (theIObj->AcceptDisplayMode (myDisplayMode)
-               ? myDisplayMode
+              : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
+               ? myDefaultDrawer->DisplayMode()
                : 0);
   theHiMode  = theIObj->HasHilightMode()   ? theIObj->HilightMode()   : theDispMode;
-  theSelMode = theIObj->HasSelectionMode() ? theIObj->SelectionMode() : -1;
+  theSelMode = theIObj->GlobalSelectionMode();
 }
 
 //=======================================================================
@@ -2234,35 +2301,78 @@ void AIS_InteractiveContext::EraseGlobal (const Handle(AIS_InteractiveObject)& t
 
   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
 
-  Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
-  if (aStatus->GraphicStatus() == AIS_DS_Displayed)
+  const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+  if (aStatus->GraphicStatus() == AIS_DS_Temporary
+   || aStatus->GraphicStatus() == AIS_DS_Erased)
   {
-    for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
-    {
-      if (myMainPM->IsHighlighted (theIObj, aDispModeIter.Value()))
-      {
-        myMainPM->Unhighlight (theIObj, aDispModeIter.Value());
-      }
-      myMainPM->SetVisibility (theIObj, aDispModeIter.Value(), Standard_False);
-    }
+    return;
+  }
 
-    if (IsCurrent (theIObj)
-    && !aStatus->IsDModeIn (aDispMode))
+  if (aStatus->IsHilighted())
+  {
+    Standard_DISABLE_DEPRECATION_WARNINGS
+    if (IsCurrent (theIObj))
     {
-      myMainPM->SetVisibility (theIObj, aDispMode, Standard_False);
+      AddOrRemoveCurrentObject (theIObj, Standard_False);
     }
-
-    for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
+    else if (myMainPM->IsHighlighted (theIObj, aStatus->DisplayMode()))
     {
-      mgrSelector->Deactivate (theIObj, aSelModeIter.Value(), myMainSel);
+      unhighlightGlobal (theIObj);
     }
+    Standard_ENABLE_DEPRECATION_WARNINGS
+  }
+
+  myMainPM->SetVisibility (theIObj, aStatus->DisplayMode(), Standard_False);
 
-    if (theToUpdateviewer)
+  if (aStatus->IsHilighted()
+   && theIObj->HasHilightMode())
+  {
+    unhighlightGlobal (theIObj);
+  }
+
+  if (!myLastPicked.IsNull()
+    && myLastPicked->IsSameSelectable (theIObj))
+  {
+    clearDynamicHighlight();
+  }
+
+  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(), myMainSel);
+  }
+  aStatus->ClearSelectionModes();
+  aStatus->SetGraphicStatus (AIS_DS_Erased);
+
+  if (theToUpdateviewer)
+  {
+    myMainVwr->Update();
+  }
+}
+
+//=======================================================================
+//function : unhighlightOwners
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightOwners (const Handle(AIS_InteractiveObject)& theObject)
+{
+  SelectMgr_SequenceOfOwner aSeq;
+  for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
+  {
+    if (aSelIter.Value()->IsSameSelectable (theObject))
     {
-      myMainVwr->Update();
+      aSeq.Append (aSelIter.Value());
     }
   }
-  aStatus->SetGraphicStatus (AIS_DS_Erased);
+  for (SelectMgr_SequenceOfOwner::Iterator aDelIter (aSeq); aDelIter.More(); aDelIter.Next())
+  {
+    AddOrRemoveSelected (aDelIter.Value(), Standard_False);
+  }
 }
 
 //=======================================================================
@@ -2275,60 +2385,73 @@ void AIS_InteractiveContext::ClearGlobal (const Handle(AIS_InteractiveObject)& t
   if (theIObj.IsNull()
   || !myObjects.IsBound (theIObj))
   {
+    // 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;
   }
 
   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
-  for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
+  unhighlightOwners (theIObj);
+
+  myMainPM->Erase (theIObj, -1);
+
+  // Object removes from Detected sequence
+  Standard_DISABLE_DEPRECATION_WARNINGS
+  for (Standard_Integer aDetIter = myDetectedSeq.Lower(); aDetIter <= myDetectedSeq.Upper();)
   {
-    if (aStatus->IsHilighted())
+    Handle(SelectMgr_EntityOwner) aPicked = myMainSel->Picked (myDetectedSeq (aDetIter));
+    Handle(AIS_InteractiveObject) anObj;
+    if (!aPicked.IsNull())
     {
-      if (IsCurrent (theIObj))
+      anObj = Handle(AIS_InteractiveObject)::DownCast (aPicked->Selectable());
+    }
+
+    if (!anObj.IsNull()
+      && anObj == theIObj)
+    {
+      myDetectedSeq.Remove (aDetIter);
+      if (myCurDetected == aDetIter)
       {
-        AddOrRemoveCurrentObject (theIObj, theToUpdateviewer);
+        myCurDetected = Min (myDetectedSeq.Upper(), aDetIter);
       }
-      else if (myMainPM->IsHighlighted (theIObj, aDispModeIter.Value()))
+      if (myCurHighlighted == aDetIter)
       {
-        myMainPM->Unhighlight (theIObj, aDispModeIter.Value());
+        myCurHighlighted = 0;
       }
     }
-    myMainPM->Erase (theIObj, aDispModeIter.Value());
-    myMainPM->Clear (theIObj, aDispModeIter.Value());
-    if (theIObj->HasHilightMode())
+    else
     {
-      Standard_Integer im = theIObj->HilightMode();
-      myMainPM->Unhighlight (theIObj, im);
-      myMainPM->Erase       (theIObj, im);
+      aDetIter++;
     }
   }
+  Standard_ENABLE_DEPRECATION_WARNINGS
 
-  // Object removes from Detected sequence
-  for(Standard_Integer aDetIter = 1; aDetIter < myAISDetectedSeq.Length(); ++aDetIter)
-  {
-    Handle(AIS_InteractiveObject) anObj = DetectedCurrentObject();
-    if (!anObj.IsNull()
-      && anObj != theIObj)
-    {
-      myAISDetectedSeq.Remove (aDetIter);
-    }
-  }
+  // remove IO from the selection manager to avoid memory leaks
+  const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+  mgrSelector->Remove (anObj);
+
+  myObjects.UnBind (theIObj);
+  myMainVwr->StructureManager()->UnregisterObject (theIObj);
 
-  if (myLastinMain == theIObj)
+  for (V3d_ListOfViewIterator aDefViewIter (myMainVwr->DefinedViewIterator()); aDefViewIter.More(); aDefViewIter.Next())
   {
-    myLastinMain.Nullify();
+    aDefViewIter.Value()->View()->ChangeHiddenObjects()->Remove (theIObj.get());
   }
-  if (myLastPicked == theIObj)
+
+  if (!myLastinMain.IsNull())
   {
-    myLastPicked.Nullify();
+    if (myLastinMain->IsSameSelectable (theIObj)
+     || myLastPicked->IsSameSelectable(theIObj))
+    {
+      clearDynamicHighlight();
+      myLastinMain.Nullify();
+      myLastPicked.Nullify();
+    }
   }
 
-  // remove IO from the selection manager to avoid memory leaks
-  mgrSelector->Remove (theIObj);
-
-  myObjects.UnBind (theIObj);
-
-  if (theToUpdateviewer
-   && aStatus->GraphicStatus() == AIS_DS_Displayed)
+  if (theToUpdateviewer && aStatus->GraphicStatus() == AIS_DS_Displayed)
   {
     myMainVwr->Update();
   }
@@ -2349,17 +2472,16 @@ void AIS_InteractiveContext::ClearGlobalPrs (const Handle(AIS_InteractiveObject)
   }
 
   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
-  if (aStatus->IsDModeIn (theMode))
+  if (aStatus->DisplayMode() == theMode)
   {
     const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
     if (aDispMode == theMode
      && myMainPM->IsHighlighted (theIObj, theMode))
     {
-      myMainPM->Unhighlight (theIObj, theMode);
+      unhighlightGlobal (theIObj);
     }
 
     myMainPM->Erase (theIObj, theMode);
-    myMainPM->Clear (theIObj, theMode);
   }
 
   if (aStatus->GraphicStatus() == AIS_DS_Displayed
@@ -2472,103 +2594,67 @@ Standard_Boolean AIS_InteractiveContext::IsoOnPlane() const
 }
 
 //=======================================================================
-//function : SetSelectionMode
+//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
+//function : IsoOnTriangulation
 //purpose  :
 //=======================================================================
-void AIS_InteractiveContext::UnsetSelectionMode (const Handle(AIS_InteractiveObject)& )
+Standard_Boolean AIS_InteractiveContext::IsoOnTriangulation() const
 {
-  //
+  return myDefaultDrawer->IsoOnTriangulation();
 }
 
+//function : SetPixelTolerance
+//purpose  : Disables the mechanism of adaptive tolerance calculation in
+//           SelectMgr_ViewerSelector and sets the given tolerance for ALL
+//           sensitive entities activated. For more information, see
+//           SelectMgr_ViewerSelector.hxx
 //=======================================================================
-//function : SetSensitivityMode
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::SetSensitivityMode (const StdSelect_SensitivityMode theMode)
-{
-  if (HasOpenedContext())
-  {
-    myLocalContexts (myCurLocalIndex)->SetSensitivityMode (theMode);
-  }
-  else
-  {
-    myMainSel->SetSensitivityMode (theMode);
-  }
-}
-
-//=======================================================================
-//function : SensitivityMode
-//purpose  :
-//=======================================================================
-StdSelect_SensitivityMode AIS_InteractiveContext::SensitivityMode() const
-{
-  return HasOpenedContext()
-       ? myLocalContexts (myCurLocalIndex)->SensitivityMode()
-       : myMainSel->SensitivityMode();
-}
-
-//=======================================================================
-//function : SetSensitivity
-//purpose  :
-//=======================================================================
-void AIS_InteractiveContext::SetSensitivity (const Standard_Real thePrecision)
+void AIS_InteractiveContext::SetPixelTolerance (const Standard_Integer thePrecision)
 {
   if (HasOpenedContext())
   {
-    myLocalContexts(myCurLocalIndex)->SetSensitivity (thePrecision);
+    myLocalContexts (myCurLocalIndex)->SetPixelTolerance (thePrecision);
   }
   else
   {
-    myMainSel->SetSensitivity (thePrecision);
+    myMainSel->SetPixelTolerance (thePrecision);
   }
 }
 
 //=======================================================================
-//function : Sensitivity
+//function : PixelTolerance
 //purpose  :
 //=======================================================================
-Standard_Real AIS_InteractiveContext::Sensitivity() const
+Standard_Integer AIS_InteractiveContext::PixelTolerance() const
 {
   return HasOpenedContext()
-       ? myLocalContexts(myCurLocalIndex)->Sensitivity()
-       : myMainSel->Sensitivity();
+       ? myLocalContexts (myCurLocalIndex)->PixelTolerance()
+       : myMainSel->PixelTolerance();
 }
 
 //=======================================================================
-//function : SetPixelTolerance
-//purpose  :
+//function : SetSelectionSensitivity
+//purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject
 //=======================================================================
-void AIS_InteractiveContext::SetPixelTolerance (const Standard_Integer thePrecision)
+void AIS_InteractiveContext::SetSelectionSensitivity (const Handle(AIS_InteractiveObject)& theObject,
+                                                      const Standard_Integer theMode,
+                                                      const Standard_Integer theNewSensitivity)
 {
   if (HasOpenedContext())
   {
-    myLocalContexts (myCurLocalIndex)->SetPixelTolerance (thePrecision);
-  }
-  else
-  {
-    myMainSel->SetPixelTolerance (thePrecision);
+    myLocalContexts (myCurLocalIndex)->SetSelectionSensitivity (theObject, theMode, theNewSensitivity);
+    return;
   }
-}
 
-//=======================================================================
-//function : PixelTolerance
-//purpose  :
-//=======================================================================
-Standard_Integer AIS_InteractiveContext::PixelTolerance() const
-{
-  return HasOpenedContext()
-       ? myLocalContexts (myCurLocalIndex)->PixelTolerance()
-       : myMainSel->PixelTolerance();
+  mgrSelector->SetSelectionSensitivity (theObject, theMode, theNewSensitivity);
 }
 
 //=======================================================================
@@ -2612,13 +2698,7 @@ Standard_Boolean AIS_InteractiveContext::IsInLocal (const Handle(AIS_Interactive
 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);
 
@@ -2628,17 +2708,17 @@ void AIS_InteractiveContext::InitAttributes()
   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_NameOfColor aColor = Quantity_NOC_LIGHTSTEELBLUE4;
-  aTrihAspect->FirstAxisAspect() ->SetColor (aColor);
-  aTrihAspect->SecondAxisAspect()->SetColor (aColor);
-  aTrihAspect->ThirdAxisAspect() ->SetColor (aColor);
+  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_PlaneAspect) aPlaneAspect = myDefaultDrawer->PlaneAspect();
   const Standard_Real aPlaneLength = 200.0;
@@ -2652,7 +2732,7 @@ void AIS_InteractiveContext::InitAttributes()
 //=======================================================================
 Standard_Real AIS_InteractiveContext::TrihedronSize() const
 {
-  return myDefaultDrawer->DatumAspect()->FirstAxisLength();
+  return myDefaultDrawer->DatumAspect()->AxisLength(Prs3d_DP_XAxis);
 }
 
 //=======================================================================
@@ -2673,10 +2753,10 @@ void AIS_InteractiveContext::SetTrihedronSize (const Standard_Real    theVal,
 //=======================================================================
 void AIS_InteractiveContext::SetPlaneSize(const Standard_Real    theValX,
                                           const Standard_Real    theValY,
-                                          const Standard_Boolean /*updateviewer*/)
+                                          const Standard_Boolean theToUpdateViewer)
 {
   myDefaultDrawer->PlaneAspect()->SetPlaneLength (theValX, theValY);
-  Redisplay (AIS_KOI_Datum, 7);
+  Redisplay (AIS_KOI_Datum, 7, theToUpdateViewer);
 }
 
 //=======================================================================
@@ -2729,36 +2809,140 @@ void AIS_InteractiveContext::SetZLayer (const Handle(AIS_InteractiveObject)& the
   if (theIObj.IsNull())
     return;
 
-  if (myObjects.IsBound (theIObj))
+  theIObj->SetZLayer (theLayerId);
+}
+
+//=======================================================================
+//function : GetZLayer
+//purpose  :
+//=======================================================================
+Standard_Integer AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
+{
+  return !theIObj.IsNull()
+       ?  theIObj->ZLayer()
+       :  Graphic3d_ZLayerId_UNKNOWN;
+}
+
+//=======================================================================
+//function : RebuildSelectionStructs
+//purpose  : Rebuilds 1st level of BVH selection forcibly
+//=======================================================================
+void AIS_InteractiveContext::RebuildSelectionStructs()
+{
+  myMainSel->RebuildObjectsTree (Standard_True);
+}
+
+//=======================================================================
+//function : Disconnect
+//purpose  : Disconnects selectable object from an assembly and updates selection structures
+//=======================================================================
+void AIS_InteractiveContext::Disconnect (const Handle(AIS_InteractiveObject)& theAssembly,
+                                         const Handle(AIS_InteractiveObject)& theObjToDisconnect)
+{
+  if (theAssembly->IsInstance ("AIS_MultipleConnectedInteractive"))
   {
-    if (myObjects (theIObj)->GraphicStatus() == AIS_DS_Displayed || myObjects (theIObj)->GraphicStatus() == AIS_DS_Erased)
+    Handle(AIS_MultipleConnectedInteractive) theObj (Handle(AIS_MultipleConnectedInteractive)::DownCast (theAssembly));
+    theObj->Disconnect (theObjToDisconnect);
+    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);
+}
+
+//=======================================================================
+//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)
+{
+  const Handle(AIS_Selection)& aSelection = HasOpenedContext() ?
+      myLocalContexts(myCurLocalIndex)->Selection() : mySelection;
+
+  Bnd_Box aBndSelected;
+
+  AIS_MapOfObjectOwners anObjectOwnerMap;
+  for (aSelection->Init(); aSelection->More(); aSelection->Next())
+  {
+    const Handle(SelectMgr_EntityOwner)& anOwner = aSelection->Value();
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
+    if (anObj->IsInfinite())
     {
-      theIObj->SetZLayer (myMainPM, theLayerId);
+      continue;
+    }
+
+    if (anOwner == anObj->GlobalSelOwner())
+    {
+      Bnd_Box aTmpBnd;
+      anObj->BoundingBox (aTmpBnd);
+      aBndSelected.Add (aTmpBnd);
+    }
+    else
+    {
+      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);
   }
+
+  anObjectOwnerMap.Clear();
+
+  if (aBndSelected.IsVoid())
+    return;
+
+  theView->FitAll (aBndSelected, theMargin, theToUpdate);
 }
 
 //=======================================================================
-//function : GetZLayer
+//function : SetTransformPersistence
 //purpose  :
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
+void AIS_InteractiveContext::SetTransformPersistence (const Handle(AIS_InteractiveObject)& theObject,
+                                                      const Handle(Graphic3d_TransformPers)& theTrsfPers)
 {
-  if (theIObj.IsNull())
-    return -1;
-
-  if (myObjects.IsBound (theIObj))
+  theObject->SetTransformPersistence (theTrsfPers);
+  if (!myObjects.IsBound (theObject))
   {
-    return theIObj->GetZLayer (myMainPM);
+    return;
   }
-  else if (HasOpenedContext())
+
+  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 myLocalContexts (myCurLocalIndex)->GetZLayer (theIObj);
+    anActiveViewIter.Value()->View()->InvalidateBVHData (aLayerId);
+    anActiveViewIter.Value()->View()->InvalidateZLayerBoundingBox (aLayerId);
   }
-
-  return -1;
 }