0026773: Visualization - deviation angle cannot be set by AIS_InteractiveContext...
[occt.git] / src / AIS / AIS_InteractiveContext.cxx
index 8caf9ad..85b873f 100644 (file)
 
 // Modified by  XAB & Serguei Dec 97 (angle &deviation coeffts)
 
-#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_ConnectedInteractive.hxx>
-#include <AIS_MultipleConnectedInteractive.hxx>
 #include <AIS_DataMapIteratorOfDataMapOfILC.hxx>
+#include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
 #include <AIS_GlobalStatus.hxx>
+#include <AIS_InteractiveContext.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_Selection.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_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>
-
+//#include <AIS_DataMapIteratorOfDataMapOfInteractiveInteger.hxx>
 namespace
 {
   static volatile Standard_Integer THE_AIS_INDEX_SEL = 0;
@@ -66,6 +79,9 @@ namespace
     return TCollection_AsciiString ("AIS_CurContext_")
          + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_CUR));
   }
+
+  typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners;
+  typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners;
 }
 
 //=======================================================================
@@ -75,10 +91,13 @@ 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),
+myCurrentTouched(Standard_False),
+mySelectedTouched(Standard_False),
+myToHilightSelected(Standard_True),
 myFilters(new SelectMgr_OrFilter()),
 myDefaultDrawer(new Prs3d_Drawer()),
 myDefaultColor(Quantity_NOC_GOLDENROD),
@@ -88,6 +107,7 @@ myPreselectionColor(Quantity_NOC_GREEN),
 mySubIntensity(Quantity_NOC_GRAY40),
 myDisplayMode(0),
 myCurLocalIndex(0),
+myAISCurDetected(0),
 myZDetectionFlag(0),
 myIsAutoActivateSelMode( Standard_True )
 { 
@@ -113,13 +133,18 @@ void AIS_InteractiveContext::Delete() const
 
   // 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();
 }
@@ -202,7 +227,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 +291,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 +342,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 +390,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 +425,55 @@ 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 (theDispStatus == AIS_DS_Erased)
+  {
+    Erase  (theIObj, theToUpdateViewer);
+    Load (theIObj, theSelectionMode, theToAllowDecomposition);
+    return;
+  }
+
   if (!theIObj->HasInteractiveContext())
   {
     theIObj->SetContext (this);
   }
 
-  if (HasOpenedContext())
+  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,8 +483,7 @@ 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;
     }
@@ -414,23 +517,23 @@ void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIO
     }
 
     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);
+      myMainPM->Color (theIObj, aStatus->HilightColor(), aHiMod);
     }
     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);
       }
     }
@@ -467,13 +570,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);
+    }
   }
 }
 
@@ -610,11 +722,17 @@ 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())
+
+  aSelIter->Init();
+  while (aSelIter->More())
   {
-    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
+    Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aSelIter->Value());
+    Handle(AIS_InteractiveObject) anObj   = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+
     Erase (anObj, Standard_False);
     isFound = Standard_True;
+
+    aSelIter->Init();
   }
 
   if (isFound && theToUpdateViewer)
@@ -862,14 +980,14 @@ void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject
   {
     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_True);
 
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed)
+    if (aStatus->GraphicStatus() == AIS_DS_Displayed)
     {
-      Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
+      const Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
       myMainPM->Color (anIObj, aCol, aHilightMode);
-      STATUS->SetHilightColor (aCol);
+      aStatus->SetHilightColor (aCol);
     }
   }
   else
@@ -892,11 +1010,11 @@ void AIS_InteractiveContext::Unhilight(const Handle(AIS_InteractiveObject)& anIO
   {
     if(!myObjects.IsBound(anIObj)) return;
 
-    const Handle(AIS_GlobalStatus)& STATUS = myObjects(anIObj);
-    STATUS->SetHilightStatus (Standard_False);
-    STATUS->SetHilightColor(Quantity_NOC_WHITE);
+    const Handle(AIS_GlobalStatus)& aStatus = myObjects(anIObj);
+    aStatus->SetHilightStatus (Standard_False);
+    aStatus->SetHilightColor(Quantity_NOC_WHITE);
 
-    if (STATUS->GraphicStatus() == AIS_DS_Displayed)
+    if (aStatus->GraphicStatus() == AIS_DS_Displayed)
     {
       Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
       myMainPM->Unhighlight (anIObj, aHilightMode);
@@ -961,7 +1079,44 @@ Standard_Boolean AIS_InteractiveContext::IsHilighted(const Handle(AIS_Interactiv
   return Standard_False;
 }
 
+//=======================================================================
+//function : IsHilighted
+//purpose  : Returns true if the objects global status is set to highlighted.
+//           theIsCustomColor flag defines if highlight color is not equal to OCCT's
+//           default Quantity_NOC_WHITE color. If theIsCustomColor is true,
+//           custom highlight color name will be stored to theCustomColorName
+//=======================================================================
+Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(SelectMgr_EntityOwner)& theOwner,
+                                                      Standard_Boolean& theIsCustomColor,
+                                                      Quantity_NameOfColor& theCustomColorName) const
+{
+  if (theOwner.IsNull() || !theOwner->HasSelectable())
+    return Standard_False;
+
+  const Handle(AIS_InteractiveObject) anObj =
+    Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
+
+  if (!myObjects.IsBound (anObj))
+    return Standard_False;
+
+  const Handle(AIS_GlobalStatus)& anObjStatus = myObjects (anObj);
+  if (anObjStatus->IsHilighted())
+  {
+    if (anObjStatus->HilightColor() != Quantity_NOC_WHITE)
+    {
+      theIsCustomColor = Standard_True;
+      theCustomColorName = anObjStatus->HilightColor();
+    }
+    else
+    {
+      theIsCustomColor = Standard_False;
+    }
 
+    return Standard_True;
+  }
+
+  return Standard_False;
+}
 
 //=======================================================================
 //function : IsDisplayed
@@ -1325,7 +1480,7 @@ void AIS_InteractiveContext::SetDeviationCoefficient (const Standard_Real theCoe
 //=======================================================================
 void AIS_InteractiveContext::SetDeviationAngle (const Standard_Real theAngle)
 {
-  myDefaultDrawer->SetDeviationCoefficient (theAngle);
+  myDefaultDrawer->SetDeviationAngle (theAngle);
 }
 
 //=======================================================================
@@ -1432,12 +1587,18 @@ void AIS_InteractiveContext::SetDisplayMode (const AIS_DisplayMode  theMode,
     aStatus->AddDisplayMode (theMode);
     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
     {
-      myMainPM->SetVisibility (anObj, myDisplayMode, Standard_False);
       myMainPM->Display (anObj, theMode);
+      if (!myLastPicked.IsNull() && myLastPicked->Selectable() == anObj)
+      {
+        myMainPM->BeginImmediateDraw();
+        myMainPM->Unhighlight (anObj, myDisplayMode);
+        myMainPM->EndImmediateDraw (myMainVwr);
+      }
       if (aStatus->IsSubIntensityOn())
       {
         myMainPM->Color (anObj, mySubIntensity, theMode);
       }
+      myMainPM->SetVisibility (anObj, myDisplayMode, Standard_False);
     }
   }
 
@@ -1606,7 +1767,8 @@ void AIS_InteractiveContext::redisplayPrsRecModes (const Handle(AIS_InteractiveO
 {
   if (theIObj->RecomputeEveryPrs())
   {
-    theIObj->Redisplay();
+    theIObj->Update (Standard_True);
+    theIObj->UpdateSelection();
   }
   else
   {
@@ -1614,6 +1776,7 @@ void AIS_InteractiveContext::redisplayPrsRecModes (const Handle(AIS_InteractiveO
     {
       theIObj->Update (aModes.Value(), Standard_False);
     }
+    theIObj->UpdateSelection();
     theIObj->SetRecomputeOk();
   }
 
@@ -1632,7 +1795,8 @@ void AIS_InteractiveContext::redisplayPrsModes (const Handle(AIS_InteractiveObje
 {
   if (theIObj->RecomputeEveryPrs())
   {
-    theIObj->Redisplay();
+    theIObj->Update (Standard_True);
+    theIObj->UpdateSelection();
   }
   else
   {
@@ -1683,6 +1847,21 @@ void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theI
   redisplayPrsRecModes (theIObj, theToUpdateViewer);
 }
 
+//=======================================================================
+//function : SetIsoOnTriangulation
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::IsoOnTriangulation (const Standard_Boolean theIsEnabled,
+                                                 const Handle(AIS_InteractiveObject)& theObject)
+{
+  if (theObject.IsNull())
+  {
+    return;
+  }
+
+  theObject->SetIsoOnTriangulation (theIsEnabled);
+}
+
 //=======================================================================
 //function : SetDeviationCoefficient
 //purpose  :
@@ -1817,7 +1996,8 @@ void AIS_InteractiveContext::SetAngleAndDeviation (const Handle(AIS_InteractiveO
 
   if (theIObj->RecomputeEveryPrs())
   {
-    theIObj->Redisplay();
+    theIObj->Update (Standard_True);
+    theIObj->UpdateSelection();
   }
   else
   {
@@ -1962,6 +2142,19 @@ void AIS_InteractiveContext::SetWidth (const Handle(AIS_InteractiveObject)& theI
 
   theIObj->SetWidth (theWidth);
   redisplayPrsRecModes (theIObj, theToUpdateViewer);
+  if (!myLastinMain.IsNull() && myLastinMain->Selectable() == theIObj)
+  {
+    if (myLastinMain->IsAutoHilight())
+    {
+      const Standard_Integer aHiMode =
+        theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
+      myLastinMain->HilightWithColor (myMainPM, myLastinMain->IsSelected() ? mySelectionColor : myHilightColor, aHiMode);
+    }
+    else
+    {
+      theIObj->HilightOwnerWithColor (myMainPM, myLastinMain->IsSelected() ? mySelectionColor : myHilightColor, myLastinMain);
+    }
+  }
 }
 
 //=======================================================================
@@ -2101,7 +2294,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())
@@ -2178,7 +2371,6 @@ void AIS_InteractiveContext::Status (const Handle(AIS_InteractiveObject)& theIOb
     theStatus += TCollection_AsciiString (aDispModeIter.Value());
     theStatus += "\n";
   }
-  if (IsCurrent (theIObj))  theStatus +="\t| Current\n";
   if (IsSelected(theIObj)) theStatus +="\t| Selected\n";
 
   theStatus += "\t| Active Selection Modes in the MainViewer :\n";
@@ -2211,7 +2403,7 @@ void AIS_InteractiveContext::GetDefModes (const Handle(AIS_InteractiveObject)& t
                ? myDisplayMode
                : 0);
   theHiMode  = theIObj->HasHilightMode()   ? theIObj->HilightMode()   : theDispMode;
-  theSelMode = theIObj->HasSelectionMode() ? theIObj->SelectionMode() : -1;
+  theSelMode = theIObj->GlobalSelectionMode();
 }
 
 //=======================================================================
@@ -2229,35 +2421,72 @@ 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())
+    return;
+  }
+
+  for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
+  {
+    if (aStatus->IsHilighted())
     {
-      if (myMainPM->IsHighlighted (theIObj, aDispModeIter.Value()))
+      if (IsCurrent (theIObj))
+      {
+        AddOrRemoveCurrentObject (theIObj, Standard_False);
+      }
+      else if (myMainPM->IsHighlighted (theIObj, aDispModeIter.Value()))
       {
         myMainPM->Unhighlight (theIObj, aDispModeIter.Value());
       }
-      myMainPM->SetVisibility (theIObj, aDispModeIter.Value(), Standard_False);
     }
 
-    if (IsCurrent (theIObj)
-    && !aStatus->IsDModeIn (aDispMode))
-    {
-      myMainPM->SetVisibility (theIObj, aDispMode, Standard_False);
-    }
+    myMainPM->SetVisibility (theIObj, aDispModeIter.Value(), Standard_False);
+  }
 
-    for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
-    {
-      mgrSelector->Deactivate (theIObj, aSelModeIter.Value(), myMainSel);
-    }
+  if (IsSelected (theIObj)
+  && !aStatus->IsDModeIn (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)
+  if (theToUpdateviewer)
+  {
+    myMainVwr->Update();
+  }
+}
+
+//=======================================================================
+//function : unhighlightOwners
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightOwners (const Handle(AIS_InteractiveObject)& theObject)
+{
+  Handle(AIS_Selection) aSel = AIS_Selection::Selection (myCurrentName.ToCString());
+  aSel->Init();
+  while (aSel->More())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner =
+      Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
+    if (anOwner->Selectable() == theObject)
     {
-      myMainVwr->Update();
+      if (anOwner->IsSelected())
+      {
+        AddOrRemoveSelected (anOwner, Standard_False);
+        aSel->Init();
+        continue;
+      }
     }
+    aSel->Next();
   }
-  aStatus->SetGraphicStatus (AIS_DS_Erased);
 }
 
 //=======================================================================
@@ -2270,31 +2499,19 @@ 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);
+  unhighlightOwners (theIObj);
   for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
   {
-    if (aStatus->IsHilighted())
-    {
-      if (IsCurrent (theIObj))
-      {
-        AddOrRemoveCurrentObject (theIObj, theToUpdateviewer);
-      }
-      else if (myMainPM->IsHighlighted (theIObj, aDispModeIter.Value()))
-      {
-        myMainPM->Unhighlight (theIObj, aDispModeIter.Value());
-      }
-    }
     myMainPM->Erase (theIObj, aDispModeIter.Value());
     myMainPM->Clear (theIObj, aDispModeIter.Value());
-    if (theIObj->HasHilightMode())
-    {
-      Standard_Integer im = theIObj->HilightMode();
-      myMainPM->Unhighlight (theIObj, im);
-      myMainPM->Erase       (theIObj, im);
-    }
   }
 
   // Object removes from Detected sequence
@@ -2308,22 +2525,24 @@ void AIS_InteractiveContext::ClearGlobal (const Handle(AIS_InteractiveObject)& t
     }
   }
 
-  if (myLastinMain == theIObj)
-  {
-    myLastinMain.Nullify();
-  }
-  if (myLastPicked == theIObj)
-  {
-    myLastPicked.Nullify();
-  }
-
   // remove IO from the selection manager to avoid memory leaks
-  mgrSelector->Remove (theIObj);
+  const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
+  mgrSelector->Remove (anObj);
 
   myObjects.UnBind (theIObj);
+  myMainVwr->StructureManager()->UnregisterObject (theIObj);
+  for (myMainVwr->InitDefinedViews(); myMainVwr->MoreDefinedViews(); myMainVwr->NextDefinedViews())
+  {
+    myMainVwr->DefinedView()->View()->ChangeHiddenObjects()->Remove (theIObj.get());
+  }
+
+  if (!myLastinMain.IsNull() && myLastinMain->Selectable() == theIObj)
+    myLastinMain.Nullify();
+  if (!myLastPicked.IsNull() && myLastPicked->Selectable() == theIObj)
+    myLastPicked.Nullify();
+  myMainPM->ClearImmediateDraw();
 
-  if (theToUpdateviewer
-   && aStatus->GraphicStatus() == AIS_DS_Displayed)
+  if (theToUpdateviewer && aStatus->GraphicStatus() == AIS_DS_Displayed)
   {
     myMainVwr->Update();
   }
@@ -2467,103 +2686,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
 {
-  //
-}
-
-//=======================================================================
-//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();
+  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 : 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);
 }
 
 //=======================================================================
@@ -2623,8 +2806,8 @@ 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();
@@ -2724,36 +2907,110 @@ 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)
-    {
-      theIObj->SetZLayer (myMainPM, theLayerId);
-    }
+    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 (HasOpenedContext())
+  else if (theAssembly->IsInstance ("AIS_ConnectedInteractive") && theObjToDisconnect.IsNull())
   {
-    myLocalContexts (myCurLocalIndex)->SetZLayer (theIObj, theLayerId);
+    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 : GetZLayer
-//purpose  :
+//function : FitSelected
+//purpose  : Fits the view corresponding to the bounds of selected objects
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
+void AIS_InteractiveContext::FitSelected (const Handle(V3d_View)& theView,
+                                          const Standard_Real theMargin,
+                                          const Standard_Boolean theToUpdate)
 {
-  if (theIObj.IsNull())
-    return -1;
+  Standard_CString aSelName = HasOpenedContext() ?
+      myLocalContexts (myCurLocalIndex)->SelectionName().ToCString()
+    : myCurrentName.ToCString();
 
-  if (myObjects.IsBound (theIObj))
+  Bnd_Box aBndSelected;
+
+  const Handle(AIS_Selection)& aSelection = AIS_Selection::Selection (aSelName);
+  AIS_MapOfObjectOwners anObjectOwnerMap;
+  for (aSelection->Init(); aSelection->More(); aSelection->Next())
   {
-    return theIObj->GetZLayer (myMainPM);
+    Handle(AIS_InteractiveObject) anObj (Handle(AIS_InteractiveObject)::DownCast (aSelection->Value()));
+    if (!anObj.IsNull())
+    {
+      if (anObj->IsInfinite())
+        continue;
+
+      Bnd_Box aTmpBnd;
+      anObj->BoundingBox (aTmpBnd);
+      aBndSelected.Add (aTmpBnd);
+    }
+    else
+    {
+      Handle(SelectMgr_EntityOwner) anOwner (Handle(SelectMgr_EntityOwner)::DownCast (aSelection->Value()));
+      if (anOwner.IsNull())
+        continue;
+
+      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())
   {
-    return myLocalContexts (myCurLocalIndex)->GetZLayer (theIObj);
+    const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
+    Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
+    aBndSelected.Add (aTmpBox);
   }
 
-  return -1;
+  anObjectOwnerMap.Clear();
+
+  if (aBndSelected.IsVoid())
+    return;
+
+  theView->FitAll (aBndSelected, theMargin, theToUpdate);
 }