0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
old mode 100755 (executable)
new mode 100644 (file)
index c11509a..99928e8
 // Created on: 1997-01-29
 // Created by: Robert COUBLANC
 // Copyright (c) 1997-1999 Matra Datavision
-// Copyright (c) 1999-2012 OPEN CASCADE SAS
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 
-#define UKI60826       //GG_161199     Use the requested selection color instead of default
-
-#define        IMP280200       //GG Don't returns a NULL shape in SelectedShape()
-//                     method when the current selected is a shape !
-
-#define BUC60814       //GG_300101     Idem UKI60826
-
-#define IMP150501       //GG_150501     CADPAK_V2 Add Drag() method
-
-#define IMP160701      //ZSV  Add InitDetected(),MoreDetected(),NextDetected(),
-//                       DetectedCurrentShape(),DetectedCurrentObject()
-//                       methods
-
-#define OCC138          //VTN Avoding infinit loop in AddOrRemoveCurrentObject method.
-
-#define OCC9657
-
-#include <AIS_InteractiveContext.jxx>
-#include <SelectMgr_EntityOwner.hxx>
-#include <AIS_Selection.hxx>
-#include <AIS_StatusOfDetection.hxx>
-#include <AIS_StatusOfPick.hxx>
-
 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
-#include <AIS_LocalContext.hxx>
 #include <AIS_GlobalStatus.hxx>
+#include <AIS_InteractiveContext.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_LocalContext.hxx>
+#include <AIS_MapIteratorOfMapOfInteractive.hxx>
+#include <AIS_MapOfInteractive.hxx>
+#include <AIS_Selection.hxx>
 #include <AIS_Shape.hxx>
-
+#include <AIS_StatusOfDetection.hxx>
+#include <AIS_StatusOfPick.hxx>
+#include <Aspect_Grid.hxx>
+#include <Prs3d_BasicAspect.hxx>
+#include <Prs3d_LineAspect.hxx>
+#include <Prs3d_Presentation.hxx>
+#include <Quantity_Color.hxx>
+#include <SelectBasics_SensitiveEntity.hxx>
+#include <SelectMgr_EntityOwner.hxx>
+#include <SelectMgr_Filter.hxx>
+#include <SelectMgr_OrFilter.hxx>
+#include <SelectMgr_Selection.hxx>
+#include <SelectMgr_SelectionManager.hxx>
+#include <Standard_Transient.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TCollection_ExtendedString.hxx>
+#include <TColStd_ListIteratorOfListOfInteger.hxx>
+#include <TopLoc_Location.hxx>
+#include <TopoDS_Shape.hxx>
+#include <V3d_AmbientLight.hxx>
+#include <V3d_DirectionalLight.hxx>
 #include <V3d_Light.hxx>
 #include <V3d_PositionalLight.hxx>
 #include <V3d_SpotLight.hxx>
-#include <V3d_DirectionalLight.hxx>
-#include <V3d_AmbientLight.hxx>
-
-#include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <SelectMgr_Selection.hxx>
-#include <SelectBasics_SensitiveEntity.hxx>
-
-#ifdef IMP150501
-#include <Visual3d_TransientManager.hxx>
-#include <Prs3d_Presentation.hxx>
-#endif
-
-#ifdef OCC9657
-#include <AIS_MapOfInteractive.hxx>
-#include <AIS_MapIteratorOfMapOfInteractive.hxx>
-#endif
+#include <V3d_View.hxx>
+#include <V3d_Viewer.hxx>
+#include <Visual3d_View.hxx>
 
-//unused
-/*#ifdef DEB
-static void InfoOnLight(const Handle(V3d_View) aView)
-{
-  Standard_Real V1=0.,V2=0.,V3=0.;
-  Standard_Integer IndexOfLit(0);
-  
-  static Standard_Boolean FirstTime (Standard_True);
-  if(FirstTime)
-    {
-      FirstTime = Standard_False;
-      for (aView->InitActiveLights();aView->MoreActiveLights();aView->NextActiveLights()){
-       IndexOfLit++;
-       cout<<"lumiere no "<<IndexOfLit<<"\n************\n\t";
-       Handle(V3d_Light) Li = aView->ActiveLight();
-       Quantity_NameOfColor Col;
-       Li->Color(Col);
-       cout<<"Color :"<<Standard_Integer(Col)<<"\n\t"<<endl;
-       cout<<"type :";
-       switch(Li->Type()){
-       case V3d_AMBIENT:
-         {
-           cout<<" AMBIENT\n\t";
-//POP K4L
-//         Handle(V3d_AmbientLight)::DownCast(Li)->DisplayPosition(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           break;
-         }
-       case V3d_DIRECTIONAL:
-         {
-           cout<<" DIRECTIONAL\n\t";
-           Handle(V3d_DirectionalLight)::DownCast(Li)->DisplayPosition(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           Handle(V3d_DirectionalLight)::DownCast(Li)->Direction(V1,V2,V3);
-           cout<<"Direction : V1 = "<<V1<<"  V2 = "<<V2<<"  V3 = "<<V3<<endl; 
-
-           break;
-         }
-       case V3d_POSITIONAL:
-         {
-           cout<<" POSITIONAL\n\t";
-           Handle(V3d_PositionalLight)::DownCast(Li)->Position(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           Handle(V3d_PositionalLight)::DownCast(Li)->Target(V1,V2,V3);
-           cout<<"Target    : x1 = "<<V1<<"  x2 = "<<V2<<"  x3 = "<<V3<<endl; 
-           break;
-         }
-       case V3d_SPOT:
-         {
-           cout<<" SPOT\n\t";
-           cout<<" DIRECTIONAL\n\t";
-           Handle(V3d_SpotLight)::DownCast(Li)->Position(V1,V2,V3);
-           cout<<"Position : X1 = "<<V1<<"  X2 = "<<V2<<"  X3 = "<<V3<<endl; 
-           Handle(V3d_SpotLight)::DownCast(Li)->Direction(V1,V2,V3);
-           cout<<"Direction : V1 = "<<V1<<"  V2 = "<<V2<<"  V3 = "<<V3<<endl; 
-           Handle(V3d_PositionalLight)::DownCast(Li)->Target(V1,V2,V3);
-           cout<<"Target    : x1 = "<<V1<<"  x2 = "<<V2<<"  x3 = "<<V3<<endl; 
-           cout<<"\tAngle :"<<Handle(V3d_SpotLight)::DownCast(Li)->Angle()<<endl;
-           break;
-         }
-       }
-      }
-    }
-}
-#endif
-*/
 //=======================================================================
 //function : MoveTo
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-AIS_StatusOfDetection AIS_InteractiveContext::MoveTo(const Standard_Integer XPix, 
-                                  const Standard_Integer YPix, 
-                                  const Handle(V3d_View)& aView)
+AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  theXPix,
+                                                      const Standard_Integer  theYPix,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean  theToRedrawOnUpdate)
 {
-  if(HasOpenedContext()){
+  if (HasOpenedContext())
+  {
     myWasLastMain = Standard_True;
-    return myLocalContexts(myCurLocalIndex)->MoveTo(XPix,YPix,aView);
+    return myLocalContexts (myCurLocalIndex)->MoveTo (theXPix, theYPix, theView, theToRedrawOnUpdate);
   }
 
-#ifdef IMP160701
-    //Nullify class members storing information about detected AIS objects.
   myAISCurDetected = 0;
   myAISDetectedSeq.Clear();
-#endif
 
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle (PrsMgr_PresentationManager3d) pmgr ;
-  Handle (StdSelect_ViewerSelector3d) selector;
-  Standard_Boolean ismain = Standard_True,UpdVwr = Standard_False;
-  
-  // Preliminaires
-  if(aView->Viewer()== myMainVwr) {
-    pmgr = myMainPM;
-    selector=myMainSel;
-    myLastPicked = myLastinMain;
-    myWasLastMain = Standard_True;
-  }
-  else 
+  if (theView->Viewer() != myMainVwr)
+  {
     return AIS_SOD_Error;
-  
-  AIS_StatusOfDetection TheStat(AIS_SOD_Nothing);
-  
-  
-  // allonzy
-  selector->Pick(XPix, YPix, aView);
-
-#ifdef IMP160701
-  //filling of myAISDetectedSeq sequence storing information about detected AIS objects
-  // (the objects must be AIS_Shapes).
-  Handle(SelectMgr_EntityOwner) anEntityOwner;
-  const Standard_Integer NbDetected = selector->NbPicked();
-  for(Standard_Integer i_detect = 1;i_detect<=NbDetected;i_detect++)
+  }
+
+  // preliminaires
+  myLastPicked  = myLastinMain;
+  myWasLastMain = Standard_True;
+  AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
+  Standard_Boolean      toUpdateViewer = Standard_False;
+
+  myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
+  myMainSel->Pick (theXPix, theYPix, theView);
+
+  // filling of myAISDetectedSeq sequence storing information about detected AIS objects
+  // (the objects must be AIS_Shapes)
+  const Standard_Integer aDetectedNb = myMainSel->NbPicked();
+  Standard_Integer aNewDetected = 0;
+  for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
   {
-    anEntityOwner = selector->Picked(i_detect);
-    if(!anEntityOwner.IsNull())
-      if(myFilters->IsOk(anEntityOwner))
-      {
-        Handle(AIS_InteractiveObject) anObj = 
-               Handle(AIS_InteractiveObject)::DownCast(anEntityOwner->Selectable());
-        if(!Handle(AIS_Shape)::DownCast(anObj).IsNull())
-          myAISDetectedSeq.Append(anObj);
-      }
+    Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
+    if (anOwner.IsNull()
+     || !myFilters->IsOk (anOwner))
+    {
+      continue;
+    }
+
+    if (aNewDetected < 1)
+    {
+      aNewDetected = aDetIter;
+    }
+    Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    if (!anObj.IsNull())
+    {
+      myAISDetectedSeq.Append (anObj);
+    }
   }
-#endif
 
-  selector->Init();
-  if ( selector->More() )
+  if (aNewDetected >= 1)
   {
-    if ( HasOpenedContext() ) 
+    // does nothing if previously detected object is equal to the current one
+    Handle(SelectMgr_EntityOwner) aNewPickedOwner = myMainSel->Picked (aNewDetected);
+    if (aNewPickedOwner->Selectable() == myLastPicked)
     {
-      if ( !myFilters->IsOk( selector->OnePicked() ) ) 
-        return AIS_SOD_AllBad;
-      else
-        if ( !myLocalContexts( myCurLocalIndex )->Filter()->IsOk( selector->OnePicked() ) )
-          return AIS_SOD_AllBad;
+      return myLastPicked->State() == 1
+           ? AIS_SOD_Selected
+           : AIS_SOD_OnlyOneDetected;
     }
  
-    // Does nothing if previously detected object is equal to the current one
-    if ( selector->OnePicked()->Selectable() == myLastPicked )
-      return AIS_SOD_OnlyOneDetected;
     // Previously detected object is unhilighted if it is not selected or hilighted 
     // with selection color if it is selected. Such highlighting with selection color 
     // is needed only if myToHilightSelected flag is true. In this case previously detected
@@ -216,136 +127,137 @@ AIS_StatusOfDetection AIS_InteractiveContext::MoveTo(const Standard_Integer XPix
     // method call. As result it is necessary to rehighligt it with mySelectionColor.
     if (!myLastPicked.IsNull())
     {
-      Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
+      const Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
       if (myLastPicked->State() != 1)
       {
-        pmgr->Unhighlight (myLastPicked, aHiMod);
-        UpdVwr = Standard_True;
+        myMainPM->Unhighlight (myLastPicked, aHiMod);
+        toUpdateViewer = Standard_True;
       }
       else if (myToHilightSelected)
       {
-        pmgr->Color (myLastPicked, mySelectionColor, aHiMod);
-        UpdVwr = Standard_True;
+        myMainPM->Color (myLastPicked, mySelectionColor, aHiMod);
+        toUpdateViewer = Standard_True;
       }
     }
 
-    // Initialize myLastPicked field with currently detected object
-    myLastPicked = Handle(AIS_InteractiveObject)::DownCast (selector->OnePicked()->Selectable());
+    // initialize myLastPicked field with currently detected object
+    myLastPicked = Handle(AIS_InteractiveObject)::DownCast (aNewPickedOwner->Selectable());
+    myLastinMain = myLastPicked;
 
-    if ( ismain )
-      myLastinMain = myLastPicked;
-
-    // Highlight detected object if it is not selected or myToHilightSelected flag is true
-    if (!myLastPicked.IsNull()
-     && (myLastPicked->State()!= 1 || myToHilightSelected))
+    // highlight detected object if it is not selected or myToHilightSelected flag is true
+    if (!myLastPicked.IsNull())
     {
-      Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
-      pmgr->Color (myLastPicked, myHilightColor, aHiMod);
-      UpdVwr = Standard_True;
-    }
+      if (myLastPicked->State() != 1 || myToHilightSelected)
+      {
+        const Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
+        myMainPM->Color (myLastPicked, myHilightColor, aHiMod);
+        toUpdateViewer = Standard_True;
+      }
 
-    if (!myLastPicked.IsNull()
-      && myLastPicked->State() == 1)
-    {
-      TheStat = AIS_SOD_Selected;
+      aStatus = myLastPicked->State() == 1
+              ? AIS_SOD_Selected
+              : AIS_SOD_OnlyOneDetected;
     }
   }
   else 
   {
-    // Previously detected object is unhilighted if it is not selected or hilighted
-    // with selection color if it is selected.
-    TheStat = AIS_SOD_Nothing;
+    // previously detected object is unhilighted if it is not selected or hilighted
+    // with selection color if it is selected
+    aStatus = AIS_SOD_Nothing;
     if (!myLastPicked.IsNull())
     {
       Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
       if (myLastPicked->State() != 1)
       {
-        pmgr->Unhighlight (myLastPicked, aHiMod);
-        UpdVwr = Standard_True;
+        myMainPM->Unhighlight (myLastPicked, aHiMod);
+        toUpdateViewer = Standard_True;
       }
       else if (myToHilightSelected)
       {
-        pmgr->Color (myLastPicked, mySelectionColor, aHiMod);
-        UpdVwr = Standard_True;
+        myMainPM->Color (myLastPicked, mySelectionColor, aHiMod);
+        toUpdateViewer = Standard_True;
       }
     }
 
-    if ( ismain )
-      myLastinMain.Nullify();
+    myLastinMain.Nullify();
+    myLastPicked.Nullify();
   }
-  
-  if(UpdVwr) aView->Viewer()->Update();
-  myLastPicked.Nullify();
-  
-  mylastmoveview = aView;
-  return TheStat;
+
+  if (toUpdateViewer)
+  {
+    if (theToRedrawOnUpdate)
+    {
+      theView->Viewer()->Update();
+    }
+    else
+    {
+      theView->Viewer()->Invalidate();
+    }
+  }
+
+  mylastmoveview = theView;
+  return aStatus;
 }
 
 //=======================================================================
 //function : Select
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::Select(const Standard_Integer XPMin,
-                                               const Standard_Integer  YPMin,
-                                               const Standard_Integer  XPMax,
-                                               const Standard_Integer  YPMax, 
-                                               const Handle(V3d_View)& aView,
-                                               const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMin,
+                                                 const Standard_Integer  theYPMin,
+                                                 const Standard_Integer  theXPMax,
+                                                 const Standard_Integer  theYPMax,
+                                                 const Handle(V3d_View)& theView,
+                                                 const Standard_Boolean  toUpdateViewer)
 {
   // all objects detected by the selector are taken, previous current objects are emptied,
   // new objects are put...
 
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->Select(XPMin,YPMin,XPMax,YPMax,aView,updateviewer);
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->Select (theXPMin, theYPMin,
+                                                     theXPMax, theYPMax,
+                                                     theView, toUpdateViewer);
+  }
 
-  ClearCurrents(Standard_False);
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle(StdSelect_ViewerSelector3d) selector;
-  
-  if(aView->Viewer()== myMainVwr) {
-    selector= myMainSel;
-    myWasLastMain = Standard_True;}
+  ClearCurrents (Standard_False);
 
-  selector->Pick(XPMin,YPMin,XPMax,YPMax,aView);
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
+  Handle(StdSelect_ViewerSelector3d) aSelector;
 
-#ifdef OCC9657
-  AIS_MapOfInteractive theSelectedObj;
-  for( selector->Init(); selector->More(); selector->Next() )
+  if (theView->Viewer() == myMainVwr)
   {
-    Handle( AIS_InteractiveObject ) anObj = 
-      Handle( AIS_InteractiveObject )::DownCast( selector->Picked()->Selectable() );
-    if( !anObj.IsNull() )
-      theSelectedObj.Add( anObj );
+    aSelector = myMainSel;
+    myWasLastMain = Standard_True;
   }
-  AIS_MapIteratorOfMapOfInteractive anIt( theSelectedObj );
-  for( ; anIt.More(); anIt.Next() )
+
+  aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
+
+  AIS_MapOfInteractive anObjectsToSelect;
+  for (aSelector->Init(); aSelector->More(); aSelector->Next())
   {
-    AIS_Selection::Select( anIt.Key() );
-    anIt.Key()->State(1);
-  }
-#else
-  for(selector->Init();selector->More();selector->Next()){
-    const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-    if(!SO.IsNull()){
-#ifdef OCC138
-      AIS_Selection::Select(SO);
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-#else
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-      AIS_Selection::Select(SO);
-#endif //OCC138
+    Handle(AIS_InteractiveObject) anObj = 
+      Handle(AIS_InteractiveObject)::DownCast (aSelector->Picked()->Selectable());
+    if (!anObj.IsNull())
+    {
+      anObjectsToSelect.Add (anObj);
     }
   }
 
-#endif //OCC9657
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
+  AIS_MapIteratorOfMapOfInteractive anIt (anObjectsToSelect);
+  for ( ; anIt.More(); anIt.Next())
+  {
+    AIS_Selection::Select (anIt.Key());
+    anIt.Key()->State (1);
+  }
+
+  HilightCurrents (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbCurrents();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
   
 }
 
@@ -353,64 +265,56 @@ AIS_StatusOfPick AIS_InteractiveContext::Select(const Standard_Integer XPMin,
 //function : Select
 //purpose  : Selection by polyline
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::Select(const TColgp_Array1OfPnt2d& aPolyline,
-                                               const Handle(V3d_View)& aView,
-                                               const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
+                                                 const Handle(V3d_View)&     theView,
+                                                 const Standard_Boolean      toUpdateViewer)
 {
   // all objects detected by the selector are taken, previous current objects are emptied,
   // new objects are put...
 
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->Select(aPolyline,aView,updateviewer);
-
-  ClearCurrents(Standard_False);
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle(StdSelect_ViewerSelector3d) selector;
-  
-  if(aView->Viewer()== myMainVwr) {
-    selector= myMainSel;
-    myWasLastMain = Standard_True;}
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->Select (thePolyline, theView, toUpdateViewer);
+  }
 
-  selector->Pick(aPolyline,aView);
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
+  ClearCurrents (Standard_False);
 
+  Handle(StdSelect_ViewerSelector3d) aSelector;
 
-#ifdef OCC9657
-  AIS_MapOfInteractive theSelectedObj;
-  for( selector->Init(); selector->More(); selector->Next() )
+  if (theView->Viewer() == myMainVwr)
   {
-    Handle( AIS_InteractiveObject ) anObj = 
-      Handle( AIS_InteractiveObject )::DownCast( selector->Picked()->Selectable() );
-    if( !anObj.IsNull() )
-      theSelectedObj.Add( anObj );
+    aSelector = myMainSel;
+    myWasLastMain = Standard_True;
   }
-  AIS_MapIteratorOfMapOfInteractive anIt( theSelectedObj );
-  for( ; anIt.More(); anIt.Next() )
+
+  aSelector->Pick (thePolyline, theView);
+  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
+
+  AIS_MapOfInteractive anObjectsToSelect;
+  for (aSelector->Init(); aSelector->More(); aSelector->Next())
   {
-    AIS_Selection::Select( anIt.Key() );
-    anIt.Key()->State(1);
-  }
-#else
-  for(selector->Init();selector->More();selector->Next()){
-    const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-    if(!SO.IsNull()){
-#ifdef OCC138
-      AIS_Selection::Select(SO);
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-#else
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(1);
-      AIS_Selection::Select(SO);
-#endif //OCC138
+    Handle(AIS_InteractiveObject) anObj = 
+      Handle( AIS_InteractiveObject )::DownCast (aSelector->Picked()->Selectable());
+    if (!anObj.IsNull())
+    {
+      anObjectsToSelect.Add (anObj);
     }
   }
-#endif //OCC9657
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
+
+  AIS_MapIteratorOfMapOfInteractive anIt (anObjectsToSelect);
+  for ( ; anIt.More(); anIt.Next())
+  {
+    AIS_Selection::Select (anIt.Key());
+    anIt.Key()->State (1);
+  }
+
+  HilightCurrents (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbCurrents();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
   
 }
 
@@ -418,176 +322,204 @@ AIS_StatusOfPick AIS_InteractiveContext::Select(const TColgp_Array1OfPnt2d& aPol
 //function : Select
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::Select(const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdateViewer)
 {
-  if(HasOpenedContext()){
+  if (HasOpenedContext())
+  {
     if(myWasLastMain)
-      return myLocalContexts(myCurLocalIndex)->Select(updateviewer);
+    {
+      return myLocalContexts(myCurLocalIndex)->Select (toUpdateViewer);
+    }
     else
-      // picking was done in the collector, special processing is required...
+    {
+      myLocalContexts(myCurLocalIndex)->SetSelected (myLastPicked, toUpdateViewer);
+      return AIS_SOP_OneSelected;
+    }
+  }
+
+  if (myWasLastMain && !myLastinMain.IsNull())
+  {
+    if(myLastinMain->State() != 1)
+    {
+      SetCurrentObject (myLastinMain,Standard_False);
+      if(toUpdateViewer)
       {
-       myLocalContexts(myCurLocalIndex)->SetSelected(myLastPicked,updateviewer);
-       return AIS_SOP_OneSelected;
+        UpdateCurrentViewer();
       }
+    }
   }
-//  AIS_StatusOfPick PS(AIS_SOP_NothingSelected);
-  if(myWasLastMain && !myLastinMain.IsNull()){
-    if(myLastinMain->State()!=1){
-      SetCurrentObject(myLastinMain,Standard_False);
-      if(updateviewer)
-       UpdateCurrentViewer();}
-  }
-  else{
-    AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-    Handle(AIS_Selection) S = AIS_Selection::CurrentSelection();
-    Handle(Standard_Transient) Tr;
-    Handle(AIS_InteractiveObject) IO;
-    
-    for(S->Init();S->More();S->Next()){
-      Tr = S->Value();
-      IO = (*((Handle(AIS_InteractiveObject)*)&Tr));
-      IO->State(0);
-      Unhilight(IO,Standard_False);
-      if(myObjects.IsBound(IO)){ // anti-plantage-rob
-       if(myObjects(IO)->IsSubIntensityOn())
-         HilightWithColor(IO,mySubIntensity,Standard_False);
+  else
+  {
+    AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
+    Handle(AIS_Selection) aSelection = AIS_Selection::CurrentSelection();
+    Handle(AIS_InteractiveObject) anObj;
+    for (aSelection->Init(); aSelection->More(); aSelection->Next())
+    {
+      anObj = Handle(AIS_InteractiveObject)::DownCast (aSelection->Value());
+      anObj->State (0);
+      Unhilight (anObj,Standard_False);
+      if (myObjects.IsBound (anObj) && myObjects(anObj)->IsSubIntensityOn())
+      {
+        HilightWithColor (anObj, mySubIntensity, Standard_False);
       }
     }
-    
+
     AIS_Selection::Select();
-    if(updateviewer){
-      if(myWasLastMain)
+    if (toUpdateViewer && myWasLastMain)
+    {
         UpdateCurrentViewer();
     }
   }
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
+
+  Standard_Integer aSelNum = NbCurrents();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
 }
 
 //=======================================================================
 //function : ShiftSelect
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect(const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
 {
-  if(HasOpenedContext()){
+  if (HasOpenedContext())
+  {
     if(myWasLastMain)
-      return myLocalContexts(myCurLocalIndex)->ShiftSelect(updateviewer);
-    else{
-      myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(myLastPicked,updateviewer);
-      Standard_Integer NS =NbSelected();
-      if(NS==0) return AIS_SOP_NothingSelected;
-      if(NS==1) return AIS_SOP_OneSelected;
-      return AIS_SOP_SeveralSelected;
+    {
+      return myLocalContexts (myCurLocalIndex)->ShiftSelect (toUpdateViewer);
+    }
+    else
+    {
+      myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (myLastPicked, toUpdateViewer);
+
+      Standard_Integer aSelNum = NbSelected();
+      return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                            : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                             : AIS_SOP_SeveralSelected;
     }
   }
-  if(myWasLastMain && !myLastinMain.IsNull())
-    AddOrRemoveCurrentObject(myLastinMain,updateviewer);
 
-  Standard_Integer NS = NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-}
+  if (myWasLastMain && !myLastinMain.IsNull())
+  {
+    AddOrRemoveCurrentObject (myLastinMain, toUpdateViewer);
+  }
 
+  Standard_Integer aCurrentSelNum = NbCurrents();
+
+  return (aCurrentSelNum == 0) ? AIS_SOP_NothingSelected
+                               : (aCurrentSelNum == 1) ? AIS_SOP_OneSelected
+                                                       : AIS_SOP_SeveralSelected;
+}
 
 //=======================================================================
 //function : ShiftSelect
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect(const Standard_Integer XPMin, 
-                                                    const Standard_Integer YPMin, 
-                                                    const Standard_Integer XPMax, 
-                                                    const Standard_Integer YPMax, 
-                                                    const Handle(V3d_View)& aView,
-                                                    const Standard_Boolean updateviewer)
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer theXPMin,
+                                                      const Standard_Integer theYPMin,
+                                                      const Standard_Integer theXPMax,
+                                                      const Standard_Integer theYPMax,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean toUpdateViewer)
 {
-  
-  if(HasOpenedContext())
-    return myLocalContexts(myCurLocalIndex)->ShiftSelect(XPMin,YPMin,XPMax,YPMax,aView,updateviewer);
-  
-  UnhilightCurrents(Standard_False);
-  // OCC11904 - local variables made non-static - it looks and works better like this
-  Handle(StdSelect_ViewerSelector3d) selector;
-  
-  if(aView->Viewer()== myMainVwr) {
-    selector= myMainSel;
-    myWasLastMain = Standard_True;}
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->ShiftSelect (theXPMin, theYPMin, theXPMax, theYPMax,
+                                                          theView, toUpdateViewer);
+  }
+
+  UnhilightCurrents (Standard_False);
+
+  Handle(StdSelect_ViewerSelector3d) aSelector;
+  if (theView->Viewer() == myMainVwr)
+  {
+    aSelector = myMainSel;
+    myWasLastMain = Standard_True;
+  }
   else
+  {
     return AIS_SOP_NothingSelected;
-  
-  selector->Pick(XPMin,YPMin,XPMax,YPMax,aView);
-  AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
-  for(selector->Init();selector->More();selector->Next()){
-    const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-    if(!SO.IsNull()){
-      AIS_SelectStatus SelStat = AIS_Selection::Select(SO);
-      Standard_Integer mod = (SelStat==AIS_SS_Added)?1:0;
-      (*((Handle(AIS_InteractiveObject)*)&SO))->State(mod);
+  }
+
+  aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
+  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
+  for (aSelector->Init(); aSelector->More(); aSelector->Next())
+  {
+    Handle(AIS_InteractiveObject) anObjToSelect =  Handle(AIS_InteractiveObject)::DownCast (aSelector->Picked()->Selectable());
+    if (!anObjToSelect.IsNull())
+    {
+      AIS_SelectStatus aSelStatus = AIS_Selection::Select (anObjToSelect);
+      Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
+      anObjToSelect->State (aState);
     }
   }
-  
-  HilightCurrents(updateviewer);
-  
-  Standard_Integer NS =NbCurrents();
-  if(NS==0) return AIS_SOP_NothingSelected;
-  if(NS==1) return AIS_SOP_OneSelected;
-  return AIS_SOP_SeveralSelected;
-  
+
+  HilightCurrents (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbCurrents();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
+
 }
 
 //=======================================================================
 //function : ShiftSelect
 //purpose  : 
 //=======================================================================
-
-AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect( const TColgp_Array1OfPnt2d& aPolyline,
-                                                      const Handle(V3d_View)& aView,
-                                                      const Standard_Boolean updateviewer )
+AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
+                                                      const Handle(V3d_View)& theView,
+                                                      const Standard_Boolean toUpdateViewer)
 {
-    if( HasOpenedContext() )
-        return myLocalContexts( myCurLocalIndex )->ShiftSelect( aPolyline, aView, updateviewer );
+  if (HasOpenedContext())
+  {
+    return myLocalContexts(myCurLocalIndex)->ShiftSelect (thePolyline, theView, toUpdateViewer);
+  }
 
-    UnhilightCurrents( Standard_False );
-    // OCC11904 - local variables made non-static - it looks and works better like this
-    Handle(StdSelect_ViewerSelector3d) selector;
-  
-    if( aView->Viewer() == myMainVwr ) {
-        selector= myMainSel;
-        myWasLastMain = Standard_True;
-    }
-    else
-        return AIS_SOP_NothingSelected;
+  UnhilightCurrents (Standard_False);
 
-    selector->Pick( aPolyline, aView );
-    
-    AIS_Selection::SetCurrentSelection( myCurrentName.ToCString() );
-    for( selector->Init(); selector->More(); selector->Next() ) {
-        const Handle(SelectMgr_SelectableObject)& SO = selector->Picked()->Selectable();
-        if( !SO.IsNull() ) {
-            AIS_SelectStatus SelStat = AIS_Selection::Select( SO );
-            Standard_Integer mod = ( SelStat == AIS_SS_Added ) ? 1 : 0;
-            (*((Handle(AIS_InteractiveObject)*)&SO))->State( mod );
-        }
+  Handle(StdSelect_ViewerSelector3d) aSelector;
+
+  if (theView->Viewer() == myMainVwr)
+  {
+    aSelector= myMainSel;
+    myWasLastMain = Standard_True;
+  }
+  else
+  {
+    return AIS_SOP_NothingSelected;
+  }
+
+  aSelector->Pick (thePolyline, theView);
+
+  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
+  for (aSelector->Init(); aSelector->More(); aSelector->Next())
+  {
+    Handle(AIS_InteractiveObject) anObjToSelect = Handle(AIS_InteractiveObject)::DownCast (aSelector->Picked()->Selectable());
+    if (!anObjToSelect.IsNull())
+    {
+      AIS_SelectStatus aSelStatus = AIS_Selection::Select (anObjToSelect);
+      Standard_Integer aState = (aSelStatus == AIS_SS_Added ) ? 1 : 0;
+      anObjToSelect->State (aState);
     }
-    
-    HilightCurrents( updateviewer );
-    Standard_Integer NS = NbCurrents();
-    if( NS == 0 ) return AIS_SOP_NothingSelected;
-    if( NS == 1 ) return AIS_SOP_OneSelected;
-    return AIS_SOP_SeveralSelected;
+  }
+
+  HilightCurrents (toUpdateViewer);
+
+  Standard_Integer aSelNum = NbCurrents();
+
+  return (aSelNum == 0) ? AIS_SOP_NothingSelected
+                        : (aSelNum == 1) ? AIS_SOP_OneSelected
+                                         : AIS_SOP_SeveralSelected;
 }
 
 //=======================================================================
 //function : SetCurrentObject
 //purpose  : 
 //=======================================================================
-
 void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
                                              const Standard_Boolean updateviewer)
 {
@@ -597,11 +529,7 @@ void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject
     Standard_Boolean HasHiCol;
     if(IsHilighted(anIObj,HasHiCol,HiCol)){
       if(HasHiCol && HiCol!= mySelectionColor) {
-#ifdef UKI60826
        HilightWithColor(anIObj,mySelectionColor,updateviewer);
-#else
-       Hilight(anIObj,updateviewer);
-#endif
       }
     }
     return;
@@ -621,7 +549,7 @@ void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject
     sel->Init();
     while (sel->More()) {
       TR = sel->Value();
-      IO = *((Handle(AIS_InteractiveObject)*)&TR);
+      IO = Handle(AIS_InteractiveObject)::DownCast (TR);
       Unhilight(IO,Standard_False);
       IO->State(0);
       sel->Next();
@@ -634,25 +562,17 @@ void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject
     Standard_Boolean HasHiCol;
     if(IsHilighted(anIObj,HasHiCol,HiCol)){
       if(HasHiCol && HiCol!= mySelectionColor) {
-#ifdef UKI60826
          HilightWithColor(anIObj,mySelectionColor,Standard_False);
-#else
-         Hilight(anIObj,Standard_False);
-#endif
       }
     }
     else{
-#ifdef UKI60826
        HilightWithColor(anIObj,mySelectionColor,Standard_False);
-#else
-      Hilight(anIObj,Standard_False);
-#endif
     }
     if (updateviewer) 
       UpdateCurrentViewer();
   }
   else{
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<"Nothing Done : Opened Local Context"<<endl;
 #endif
   }
@@ -679,11 +599,7 @@ AddOrRemoveCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
     Standard_Integer mod = (SelStat==AIS_SS_Added) ? 1 : 0;
     anIObj->State(mod);
     if(mod==1)
-#ifdef BUC60814
       HilightWithColor(anIObj,mySelectionColor,Standard_False);
-#else
-      Hilight(anIObj,Standard_False);
-#endif
     else 
       Unhilight(anIObj,Standard_False);
     
@@ -692,7 +608,7 @@ AddOrRemoveCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
       UpdateCurrentViewer();
   }
   else{
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<"Nothing Done : Opened Local Context"<<endl;
 #endif
   }
@@ -767,7 +683,7 @@ void AIS_InteractiveContext::NextCurrent()
 Handle(AIS_InteractiveObject) AIS_InteractiveContext::Current() const 
 {
   Handle(Standard_Transient) TR = AIS_Selection::Selection(myCurrentName.ToCString())->Value();
-  Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&TR);
+  Handle(AIS_InteractiveObject) IO = Handle(AIS_InteractiveObject)::DownCast (TR);
   return IO;
 }
 
@@ -810,12 +726,8 @@ void AIS_InteractiveContext::HilightCurrents(const Standard_Boolean updateviewer
   sel->Init();
   while (sel->More()) {
     TR = sel->Value();
-    IO = *((Handle(AIS_InteractiveObject)*)&TR);
-#ifdef BUC60814
+    IO = Handle(AIS_InteractiveObject)::DownCast (TR);
     HilightWithColor(IO,mySelectionColor,Standard_False);
-#else
-    Hilight(IO,Standard_False);
-#endif
     sel->Next();
   }
   if(updateviewer) 
@@ -836,7 +748,7 @@ void AIS_InteractiveContext::UnhilightCurrents(const Standard_Boolean updateview
   sel->Init();
   while (sel->More()) {
     TR = sel->Value();
-    IO = *((Handle(AIS_InteractiveObject)*)&TR);
+    IO = Handle(AIS_InteractiveObject)::DownCast (TR);
     Unhilight(IO,Standard_False);
     sel->Next();
   }
@@ -858,7 +770,7 @@ void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean updateviewer)
   Handle(AIS_InteractiveObject) IO;
   for(S->Init();S->More();S->Next()){
     Tr = S->Value();
-    IO = (*((Handle(AIS_InteractiveObject)*)&Tr));
+    IO = Handle(AIS_InteractiveObject)::DownCast (Tr);
     IO->State(0);
     Unhilight(IO,Standard_False);
   }
@@ -883,12 +795,8 @@ void AIS_InteractiveContext::HilightSelected(const Standard_Boolean updateviewer
     sel->Init();
     while (sel->More()) {
       TR = sel->Value();
-      IO = *((Handle(AIS_InteractiveObject)*)&TR);
-#ifdef BUC60814
+      IO = Handle(AIS_InteractiveObject)::DownCast (TR);
       HilightWithColor(IO,mySelectionColor,Standard_False);
-#else
-      Hilight(IO,Standard_False);
-#endif
       sel->Next();
     }
   }
@@ -915,7 +823,7 @@ void AIS_InteractiveContext::UnhilightSelected(const Standard_Boolean updateview
     sel->Init();
     while (sel->More()) {
       TR = sel->Value();
-      IO = *((Handle(AIS_InteractiveObject)*)&TR);
+      IO = Handle(AIS_InteractiveObject)::DownCast (TR);
       Unhilight(IO,Standard_False);
       sel->Next();
     }
@@ -953,7 +861,7 @@ void AIS_InteractiveContext::SetSelectedCurrent()
 {
 
 
-#ifdef DEB
+#ifdef OCCT_DEBUG
   cout<<"Not Yet Implemented"<<endl;
 #endif
 }
@@ -1010,7 +918,7 @@ void AIS_InteractiveContext::AddOrRemoveSelected(const TopoDS_Shape& aShap,
                                            const Standard_Boolean updateviewer)
 { 
   if(!HasOpenedContext()) {
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
 #endif
     return;
@@ -1029,7 +937,7 @@ void AIS_InteractiveContext::AddOrRemoveSelected(const Handle(SelectMgr_EntityOw
                                                 const Standard_Boolean updateviewer)
 { 
   if(!HasOpenedContext()) {
-#ifdef DEB
+#ifdef OCCT_DEBUG
     cout<<" Attempt to remove a selected ownr with no opened local context"<<endl;
 #endif
     return;
@@ -1105,16 +1013,12 @@ void AIS_InteractiveContext::NextSelected()
 Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const 
 {
   if(!HasOpenedContext()) {
-#ifdef IMP280200
     Handle(AIS_Shape) shape = 
        Handle(AIS_Shape)::DownCast(SelectedInteractive());
     if( !shape.IsNull() ) return Standard_True;
-#endif
     return Standard_False;
   }
-  return myLocalContexts(myCurLocalIndex)->HasShape();
-  
-  
+  return myLocalContexts(myCurLocalIndex)->HasSelectedShape();
 }
 
 //=======================================================================
@@ -1124,17 +1028,23 @@ Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
 
 TopoDS_Shape AIS_InteractiveContext::SelectedShape() const 
 {
+  if (!HasOpenedContext())
+  {
+    TopoDS_Shape aResShape;
+    Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (SelectedInteractive());
+    if (!aShape.IsNull())
+    {
+      TopLoc_Location aLocTrsf = SelectedInteractive()->Transformation().Form() == gp_Identity ?
+                                 TopLoc_Location() : TopLoc_Location (SelectedInteractive()->Transformation());
+      aResShape = aShape->Shape().Located (aLocTrsf * aShape->Shape().Location());
+    }
 
-  if(!HasOpenedContext()){
-    TopoDS_Shape sh;
-#ifdef IMP280200
-    Handle(AIS_Shape) shape = 
-       Handle(AIS_Shape)::DownCast(SelectedInteractive());
-    if( !shape.IsNull() ) sh = shape->Shape();
-#endif
-    return sh;
-  } else
-    return myLocalContexts(myCurLocalIndex)->SelectedShape();
+    return aResShape;
+  } 
+  else
+  {
+    return myLocalContexts (myCurLocalIndex)->SelectedShape();
+  }
 }
 
 //=======================================================================
@@ -1150,8 +1060,10 @@ Handle(AIS_InteractiveObject) AIS_InteractiveContext::Interactive() const
 Handle(AIS_InteractiveObject) AIS_InteractiveContext::SelectedInteractive() const 
 {
   if(!HasOpenedContext()){
+    if (AIS_Selection::Selection(myCurrentName.ToCString())->Extent() == 0)
+      return NULL;
     Handle(Standard_Transient) TR  =AIS_Selection::Selection(myCurrentName.ToCString())->Value();
-    Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&TR);
+    Handle(AIS_InteractiveObject) IO = Handle(AIS_InteractiveObject)::DownCast (TR);
     return IO;}
   
   return  myLocalContexts(myCurLocalIndex)->SelectedInteractive();
@@ -1176,7 +1088,7 @@ Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const
 //function : EntityOwners
 //purpose  : 
 //=======================================================================
-void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners,
+void AIS_InteractiveContext::EntityOwners(Handle(SelectMgr_IndexedMapOfOwner)& theOwners,
                                          const Handle(AIS_InteractiveObject)& theIObj,
                                          const Standard_Integer theMode) const 
 {
@@ -1189,6 +1101,9 @@ void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners
   else
     aModes.Append( theMode );
 
+  if (theOwners.IsNull())
+    theOwners = new SelectMgr_IndexedMapOfOwner();
+
   TColStd_ListIteratorOfListOfInteger anItr( aModes );
   for (; anItr.More(); anItr.Next() )
   {
@@ -1196,18 +1111,18 @@ void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners
     if ( !theIObj->HasSelection( aMode ) )
       continue;
 
-    Handle(SelectMgr_Selection) aSel = theIObj->Selection( aMode );
+    Handle(SelectMgr_Selection) aSel = theIObj->Selection(aMode);
 
     for ( aSel->Init(); aSel->More(); aSel->Next() )
     {
-      Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive();
+      Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive()->BaseSensitive();
       if ( aEntity.IsNull() )
        continue;
 
       Handle(SelectMgr_EntityOwner) aOwner =
        Handle(SelectMgr_EntityOwner)::DownCast(aEntity->OwnerId());
       if ( !aOwner.IsNull() )
-       theOwners.Add( aOwner );
+       theOwners->Add( aOwner );
     }
   }
 }
@@ -1316,71 +1231,46 @@ Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
 
 //=======================================================================
 //function : HilightNextDetected
-//purpose  : 
+//purpose  :
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::HilightNextDetected(const Handle(V3d_View)& V)
+Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
+                                                              const Standard_Boolean  theToRedrawImmediate)
 {
-  if(!HasOpenedContext())
-    return 0;
-  return myLocalContexts(myCurLocalIndex)->HilightNextDetected(V);
+  return HasOpenedContext()
+       ? myLocalContexts (myCurLocalIndex)->HilightNextDetected (theView, theToRedrawImmediate)
+       : 0;
     
 }
 
 //=======================================================================
 //function : HilightNextDetected
-//purpose  : 
+//purpose  :
 //=======================================================================
-Standard_Integer AIS_InteractiveContext::HilightPreviousDetected(const Handle(V3d_View)& V)
+Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
+                                                                  const Standard_Boolean  theToRedrawImmediate)
 {
-  if(!HasOpenedContext())
-    return 0;
-  return myLocalContexts(myCurLocalIndex)->HilightPreviousDetected(V);
+  return HasOpenedContext()
+       ? myLocalContexts (myCurLocalIndex)->HilightPreviousDetected (theView, theToRedrawImmediate)
+       : 0;
     
 }
 
-#ifdef IMP150501
-void AIS_InteractiveContext::Drag(
-                                const Handle(V3d_View)& aView,
-                                const Handle(AIS_InteractiveObject)& anObject,
-                                const Handle(Geom_Transformation)& aTrsf,
-                                const Standard_Boolean postConcatenate,
-                                const Standard_Boolean update,
-                                const Standard_Boolean zBuffer) {
-
-  if( anObject.IsNull() || aView.IsNull() ) return;
-
-  if( update ) {
-    anObject->SetTransformation(aTrsf,postConcatenate,Standard_True);
-    aView->Update();
-  } else if( Visual3d_TransientManager::BeginDraw(aView->View(),
-                                        zBuffer,Standard_False) ) {
-    Handle(Prs3d_Presentation) P = anObject->Presentation();
-    if( !P.IsNull() ) {
-      if( postConcatenate ) P->Multiply(aTrsf);
-      else                  P->Transform(aTrsf);
-      Visual3d_TransientManager::DrawStructure(P);
-    }
-    Visual3d_TransientManager::EndDraw(Standard_True);
-  }
-}
-#endif
-
-#ifdef IMP160701
 //=======================================================================
 //function : InitDetected
 //purpose  :
 //=======================================================================
 void AIS_InteractiveContext::InitDetected()
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
   {
     myLocalContexts(myCurLocalIndex)->InitDetected();
     return;
   }
 
   if(myAISDetectedSeq.Length() != 0)
+  {
     myAISCurDetected = 1;
-
+  }
 }
 
 //=======================================================================
@@ -1389,10 +1279,12 @@ void AIS_InteractiveContext::InitDetected()
 //=======================================================================
 Standard_Boolean AIS_InteractiveContext::MoreDetected() const
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
+  {
     return myLocalContexts(myCurLocalIndex)->MoreDetected();
+  }
 
-  return (myAISCurDetected>0 &&myAISCurDetected <= myAISDetectedSeq.Length()) ?
+  return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length()) ?
           Standard_True : Standard_False;
 }
 
@@ -1415,34 +1307,35 @@ void AIS_InteractiveContext::NextDetected()
 //function : DetectedCurrentShape
 //purpose  :
 //=======================================================================
-
 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
 {
-  if(HasOpenedContext())
+  if (HasOpenedContext())
+  {
     return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
+  }
+
+  static TopoDS_Shape aDummyShape;
+
+  Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
+
+  if (aCurrentShape.IsNull())
+  {
+    return aDummyShape;
+  }
 
-  static TopoDS_Shape bidsh;
-  if(myAISCurDetected > 0 &&
-     myAISCurDetected <= myAISDetectedSeq.Length())
-    return Handle(AIS_Shape)::DownCast(myAISDetectedSeq(myAISCurDetected))->Shape();
-  return bidsh;
+  return aCurrentShape->Shape();
 }
 
 //=======================================================================
 //function : DetectedCurrentObject
 //purpose  :
 //=======================================================================
-
-Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const {
-  if(HasOpenedContext())
+Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
+{
+  if (HasOpenedContext())
+  {
     return myLocalContexts(myCurLocalIndex)->DetectedCurrentObject();
+  }
 
-  Handle(AIS_InteractiveObject) aBad;
-  if(myAISCurDetected > 0 &&
-     myAISCurDetected <= myAISDetectedSeq.Length())
-      return myAISDetectedSeq(myAISCurDetected);
-  else
-    return aBad;
+  return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
 }
-#endif