From b3c433fea3dceee4990d3697f004da874eb5718d Mon Sep 17 00:00:00 2001
From: vpa <vpa@opencascade.com>
Date: Fri, 17 Apr 2015 20:12:29 +0300
Subject: [PATCH] 0026076: Visualization - empty bounding box of a shape after
 closing local context

Fixed handling of invalid bounding boxes;
Links related to temporary objects are now removed from selection structures;
Test case for issue #26076
---
 src/AIS/AIS_InteractiveObject.cxx             | 14 ++++++++
 src/AIS/AIS_LocalContext.cxx                  | 11 ++++++-
 .../SelectMgr_SelectableObjectSet.cxx         |  3 ++
 src/SelectMgr/SelectMgr_SelectionManager.cxx  |  3 --
 src/ViewerTest/ViewerTest.cxx                 |  3 +-
 tests/bugs/vis/bug26076                       | 33 +++++++++++++++++++
 6 files changed, 62 insertions(+), 5 deletions(-)
 create mode 100644 tests/bugs/vis/bug26076

diff --git a/src/AIS/AIS_InteractiveObject.cxx b/src/AIS/AIS_InteractiveObject.cxx
index 0256b6ec47..cc4dad7bee 100644
--- a/src/AIS/AIS_InteractiveObject.cxx
+++ b/src/AIS/AIS_InteractiveObject.cxx
@@ -603,6 +603,13 @@ void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
       const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations.First().Presentation();
       const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
       const Graphic3d_BndBox4f& aBndBox = aStruct->CStructure()->BoundingBox();
+
+      if (!aBndBox.IsValid())
+      {
+        theBndBox.SetVoid();
+        return;
+      }
+
       theBndBox.Update (static_cast<Standard_Real> (aBndBox.CornerMin().x()),
                         static_cast<Standard_Real> (aBndBox.CornerMin().y()),
                         static_cast<Standard_Real> (aBndBox.CornerMin().z()),
@@ -636,6 +643,13 @@ void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
         const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
         const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
         const Graphic3d_BndBox4f& aBndBox = aStruct->CStructure()->BoundingBox();
+
+        if (!aBndBox.IsValid())
+        {
+          theBndBox.SetVoid();
+          return;
+        }
+
         theBndBox.Update (static_cast<Standard_Real> (aBndBox.CornerMin().x()),
                           static_cast<Standard_Real> (aBndBox.CornerMin().y()),
                           static_cast<Standard_Real> (aBndBox.CornerMin().z()),
diff --git a/src/AIS/AIS_LocalContext.cxx b/src/AIS/AIS_LocalContext.cxx
index 6549e9a388..259cf016f6 100644
--- a/src/AIS/AIS_LocalContext.cxx
+++ b/src/AIS/AIS_LocalContext.cxx
@@ -1017,8 +1017,17 @@ void AIS_LocalContext::ClearObjects()
 
     }
   ClearSelected( Standard_False );
+
+  // Clear selection structures for temporary objects, created in local context
+  for (AIS_DataMapIteratorOfDataMapOfSelStat anIter (myActiveObjects); anIter.More(); anIter.Next())
+  {
+    if (anIter.Value()->IsTemporary())
+    {
+      mySM->Remove (anIter.Key(), myMainVS);
+    }
+  }
+
   myActiveObjects.Clear();
-//  myMainVS->ClearAreas();myMainVS->ClearSensitive();
 }
 
 
diff --git a/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx b/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx
index 9148657f04..014fdbde95 100644
--- a/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx
+++ b/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx
@@ -72,6 +72,9 @@ Select3D_BndBox3d SelectMgr_SelectableObjectSet::Box (const Standard_Integer the
   const Handle(SelectMgr_SelectableObject)& anObject = GetObjectById (theIndex);
   Bnd_Box aBox;
   anObject->BoundingBox (aBox);
+  if (aBox.IsVoid())
+    return Select3D_BndBox3d();
+
   return Select3D_BndBox3d (SelectMgr_Vec3 (aBox.CornerMin().X(), aBox.CornerMin().Y(), aBox.CornerMin().Z()),
                             SelectMgr_Vec3 (aBox.CornerMax().X(), aBox.CornerMax().Y(), aBox.CornerMax().Z()));
 }
diff --git a/src/SelectMgr/SelectMgr_SelectionManager.cxx b/src/SelectMgr/SelectMgr_SelectionManager.cxx
index 5e8513baec..3ded691d2c 100644
--- a/src/SelectMgr/SelectMgr_SelectionManager.cxx
+++ b/src/SelectMgr/SelectMgr_SelectionManager.cxx
@@ -273,9 +273,6 @@ void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject
         break;
       }
     }
-    theSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
-    theSelector->RemoveSelectableObject (theObject);
-    theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
 
     if (aSelectors.IsEmpty())
     {
diff --git a/src/ViewerTest/ViewerTest.cxx b/src/ViewerTest/ViewerTest.cxx
index e8d03fd097..5f046013b9 100644
--- a/src/ViewerTest/ViewerTest.cxx
+++ b/src/ViewerTest/ViewerTest.cxx
@@ -3478,7 +3478,8 @@ static int VDisplay2 (Draw_Interpretor& theDI,
         {
           aShape->SetHilightMode (anObjHighMode);
         }
-        GetMapOfAIS().Bind (aShape, aName);
+        if (!toDisplayLocal)
+          GetMapOfAIS().Bind (aShape, aName);
 
         Standard_Integer aDispMode = aShape->HasDisplayMode()
                                    ? aShape->DisplayMode()
diff --git a/tests/bugs/vis/bug26076 b/tests/bugs/vis/bug26076
new file mode 100644
index 0000000000..90f83f441c
--- /dev/null
+++ b/tests/bugs/vis/bug26076
@@ -0,0 +1,33 @@
+puts "============"
+puts "CR26076"
+puts "============"
+puts ""
+
+##########################################################################################
+puts "Visualization - empty bounding box of a shape after closing local context"
+##########################################################################################
+
+pload VISUALIZATION MODELING
+
+vinit
+
+box b1 1 1 1
+box b2 4 5 6
+
+# display box b1 in global context
+vdisplay b1
+# display box b2 in local context only
+vdisplay -local b2
+
+vfit
+
+# display only box b1 by closing local context and erasing box b2
+vdonly b1
+
+vfit
+
+# check that selection works properly
+vmoveto 100 100
+checkcolor 205 205 0 1 1
+
+vdump ${imagedir}/${casename}.png