return 0;
 }
 
+namespace
+{
+  //! Checks if theMode is already turned on for theObj.
+  static Standard_Boolean InList (const Handle(AIS_InteractiveContext)& theAISContext,
+                                  const Handle(AIS_InteractiveObject)&  theObj,
+                                  const Standard_Integer                theMode)
+  {
+    TColStd_ListOfInteger anActiveModes;
+    theAISContext->ActivatedModes (theObj, anActiveModes);
+    for (TColStd_ListIteratorOfListOfInteger aModeIt (anActiveModes); aModeIt.More(); aModeIt.Next())
+    {
+      if (aModeIt.Value() == theMode)
+      {
+        return Standard_True;
+      }
+    }
+    return Standard_False;
+  }
+};
+
 //===============================================================================================
 //function : VSetSelectionMode
 //purpose  : Sets input selection mode for input object or for all displayed objects 
 //Draw arg : vselmode [object] mode On/Off (1/0)
 //===============================================================================================
-
-// function : InList 
-// purpose  : checks if theMode is already turned on for theObj
-Standard_Boolean InList(Handle(AIS_InteractiveContext) theAISContext, 
-                          Handle(AIS_InteractiveObject) theObj, 
-                          Standard_Integer theMode)
-{
-  TColStd_ListOfInteger anArray; 
-  theAISContext->ActivatedModes(theObj, anArray);
-  TColStd_ListIteratorOfListOfInteger anIt(anArray);
-  for(; anIt.More(); anIt.Next())
-  {
-    if(anIt.Value() == theMode) 
-      return Standard_True;
-  }
-  return Standard_False;
-}
-
-static Standard_Integer VSetSelectionMode(Draw_Interpretor& /*di*/, 
-                                          Standard_Integer argc, 
-                                          const char ** argv)
+static Standard_Integer VSetSelectionMode (Draw_Interpretor& /*di*/,
+                                           Standard_Integer  theArgc,
+                                           const char**      theArgv)
 {
   // Check errors
   Handle(AIS_InteractiveContext) anAISContext = ViewerTest::GetAISContext();
-  if(anAISContext.IsNull())
+  if (anAISContext.IsNull())
   {
-    std::cout << "Call vinit before!\n";
-    return 1; // TCL_ERROR
+    std::cerr << "Call vinit before!\n";
+    return 1;
   }
 
-  // Check the arguments 
-  if(argc != 3 && argc != 4)
+  // Check the arguments
+  if (theArgc != 3 && theArgc != 4)
   {
-    std::cout << "vselmode error : expects at least 2 arguments.\n"
-      << "Type help "<< argv[0] <<" for more information."; 
-    return 1; // TCL_ERROR
+    std::cerr << "vselmode error : expects at least 2 arguments.\n"
+              << "Type help "<< theArgv[0] <<" for more information.";
+    return 1;
   }
 
-  Handle(AIS_InteractiveObject) anObj;
-
-  // Set new selection mode for all objects in context
-  if(argc == 3)
+  // get objects to change selection mode
+  AIS_ListOfInteractive aTargetIOs;
+  if (theArgc == 3)
   {
-    // Get arguments 
-    Standard_Integer aMode = Draw::Atoi(argv[1]);
-    Standard_Boolean isTurnOn = Draw::Atoi(argv[2]); 
-
-    // Get all displayed objects
-    AIS_ListOfInteractive anObjList;
-    anAISContext->DisplayedObjects(anObjList);
-    AIS_ListIteratorOfListOfInteractive anObjIter;
-
-    if(aMode == 0)
-    {
-      if(anAISContext->HasOpenedContext())
-        anAISContext->CloseLocalContext();
-    }
-
-    // Turn on aMode
-    if(aMode != 0 && isTurnOn)
+    anAISContext->DisplayedObjects (aTargetIOs);
+  }
+  else
+  {
+    // Check if there is an object with given name in context
+    const TCollection_AsciiString aNameIO (theArgv[1]);
+    if (GetMapOfAIS().IsBound2 (aNameIO))
     {
-      if(!anAISContext->HasOpenedContext())
+      Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (aNameIO));
+      if (anIO.IsNull())
       {
-        anAISContext->OpenLocalContext(); 
-        for(anObjIter.Initialize(anObjList); anObjIter.More(); anObjIter.Next())
-        {
-          anAISContext->Activate(anObjIter.Value(), aMode); 
-        }
-      }
-      else
-      {
-        for(anObjIter.Initialize(anObjList); anObjIter.More(); anObjIter.Next())
-        {
-          anObj = anObjIter.Value();
-          if(!InList(anAISContext, anObj, aMode))
-            anAISContext->Activate(anObj, aMode);
-        }
+        std::cerr << "vselmode error : object name is used for non AIS viewer\n"; 
+        return 1;
       }
+      aTargetIOs.Append (anIO);
     }
+  }
 
-    // Turn off aMode
-    if(aMode != 0 && !isTurnOn)
-    {
-      if(anAISContext->HasOpenedContext())
-      {
-        for(anObjIter.Initialize(anObjList); anObjIter.More(); anObjIter.Next())
-        {
-          anObj = anObjIter.Value();
-          if(InList(anAISContext, anObj, aMode))
-            anAISContext->Deactivate(anObj, aMode);
-        }
-      }
-    }
+  const Standard_Integer aSelectionMode = Draw::Atoi (theArgc == 3 ? theArgv[1] : theArgv[2]);
+  const Standard_Boolean toTurnOn       = Draw::Atoi (theArgc == 3 ? theArgv[2] : theArgv[3]);
+  if (aSelectionMode == 0 && anAISContext->HasOpenedContext())
+  {
+    anAISContext->CloseLocalContext();
   }
 
-  // Set new selection mode for named object 
-  else
+  if (aSelectionMode != 0 && toTurnOn) // Turn on specified mode
   {
-    // Get argumnets 
-    Standard_Integer aMode = Draw::Atoi(argv[2]);
-    Standard_Boolean isTurnOn = Draw::Atoi(argv[3]);
-    TCollection_AsciiString aName(argv[1]); 
+    if (!anAISContext->HasOpenedContext())
+    {
+      anAISContext->OpenLocalContext (Standard_False);
+    }
 
-    // Check if there is an object with given name in context
-    if(GetMapOfAIS().IsBound2(aName))
+    for (AIS_ListIteratorOfListOfInteractive aTargetIt (aTargetIOs); aTargetIt.More(); aTargetIt.Next())
     {
-      anObj = Handle(AIS_InteractiveObject)::
-        DownCast(GetMapOfAIS().Find2(aName));
-      if(anObj.IsNull())
+      const Handle(AIS_InteractiveObject)& anIO = aTargetIt.Value();
+      if (!InList (anAISContext, anIO, aSelectionMode))
       {
-        std::cout << "vselmode error : object name is used for non AIS viewer\n"; 
-        return 1; // TCL_ERROR
+        anAISContext->Load (anIO, -1, Standard_True);
+        anAISContext->Activate (anIO, aSelectionMode);
       }
     }
+  }
 
-    if(aMode == 0)
-    {
-      if(anAISContext->HasOpenedContext())
-        anAISContext->CloseLocalContext();
-    }
-    // Turn on aMode
-    if(aMode != 0 && isTurnOn) 
+  if (aSelectionMode != 0 && !toTurnOn) // Turn off specified mode
+  {
+    if (!anAISContext->HasOpenedContext())
     {
-      if(!anAISContext->HasOpenedContext())
-      {
-        anAISContext->OpenLocalContext(); 
-        anAISContext->Activate(anObj, aMode);
-      }
-      else
-      {
-        if(!InList(anAISContext, anObj, aMode))
-          anAISContext->Activate(anObj, aMode);
-      }
+      return 0;
     }
 
-    // Turn off aMode
-    if(aMode != 0 && !isTurnOn)
+    for (AIS_ListIteratorOfListOfInteractive aTargetIt (aTargetIOs); aTargetIt.More(); aTargetIt.Next())
     {
-      if(anAISContext->HasOpenedContext())
+      const Handle(AIS_InteractiveObject)& anIO = aTargetIt.Value();
+      if (InList (anAISContext, anIO, aSelectionMode))
       {
-        if(InList(anAISContext, anObj, aMode))
-          anAISContext->Deactivate(anObj, aMode);
+        anAISContext->Deactivate (anIO, aSelectionMode);
       }
     }
   }
+
   return 0;
 }
 
 
--- /dev/null
+puts "============"
+puts "CR24396"
+puts "============"
+puts ""
+#######################################################################
+#  "vselmode" - disable auto loading of objects into Local Context
+#######################################################################
+
+set trihedron_axis_pick_x 29
+set trihedron_axis_pick_y 56
+
+set trihedron_axis_check_x 29
+set trihedron_axis_check_y 79
+
+set trihedron_point_pick_x 29
+set trihedron_point_pick_y 305
+
+set trihedron_point_check_x 24
+set trihedron_point_check_y 305
+
+vinit View1
+vclear
+vaxo
+vtrihedron tri
+box b 10 10 10
+vdisplay b
+vselmode b 1 1
+vfit
+
+# --------------------------------------------------- #
+# check that there is no selection of trihedron axis  #
+# --------------------------------------------------- #
+
+vmoveto $trihedron_axis_pick_x $trihedron_axis_pick_y
+
+checkcolor $trihedron_axis_pick_x $trihedron_axis_pick_y 0 0 0
+
+if { $stat != 1 } {
+   puts "Error : The trihedron should not be highlighted."
+}
+
+vselmode b 0 0
+vselmode 0 0
+verase b
+vselmode 1 1
+
+# ----------------------------------------------- #
+# check that selection of trihedron point is ok   #
+# ----------------------------------------------- #
+
+vmoveto $trihedron_point_pick_x $trihedron_point_pick_y
+
+checkcolor $trihedron_point_check_x $trihedron_point_check_y 0 1 1
+
+if { $stat != 1 } {
+   puts "Error : The trihedron point highlight is incorrect."
+}
+
+# --------------------------------------------------------- #
+# check that there is still no selection of trihedron axis  #
+# --------------------------------------------------------- #
+
+vmoveto $trihedron_axis_pick_x $trihedron_axis_pick_y
+
+checkcolor $trihedron_axis_pick_x $trihedron_axis_pick_y 0 0 0
+
+if { $stat != 1 } {
+   puts "Error : The trihedron should not be highlighted."
+}
+
+set only_screen 1