0024276: Memory leak due to a static variable
[occt.git] / src / Visual3d / Visual3d_ViewManager.cxx
index 8330001..c3f0bfa 100755 (executable)
@@ -1,3 +1,21 @@
+// Copyright (c) 1995-1999 Matra Datavision
+// Copyright (c) 1999-2012 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.
+//
+// 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.
+//
+// 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.
+
 
 /***********************************************************************
 
 
 /***********************************************************************
 
@@ -17,7 +35,7 @@
       24-10-97  : CAL ; Retrait de DownCast.
       20-11-97  : CAL ; Disparition de la dependance avec math
       01-12-97  : CAL ; Retrait du test IsActive sur l'Update et le Redraw
       24-10-97  : CAL ; Retrait de DownCast.
       20-11-97  : CAL ; Disparition de la dependance avec math
       01-12-97  : CAL ; Retrait du test IsActive sur l'Update et le Redraw
-      31-12-97  : CAL ; Disparition de MathGra 
+      31-12-97  : CAL ; Disparition de MathGra
       16-01-98  : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
       11-03-98  : CAL ; Visual3d_ViewManager::Remove ()
       20-05-98  : CAL ; Perfs. Connection entre structures COMPUTED.
       16-01-98  : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
       11-03-98  : CAL ; Visual3d_ViewManager::Remove ()
       20-05-98  : CAL ; Perfs. Connection entre structures COMPUTED.
@@ -48,6 +66,7 @@
 #include <Standard_ErrorHandler.hxx>
 
 #include <Aspect.hxx>
 #include <Standard_ErrorHandler.hxx>
 
 #include <Aspect.hxx>
+#include <Aspect_IdentDefinitionError.hxx>
 
 #include <Graphic3d_GraphicDriver.hxx>
 #include <Graphic3d_MapOfStructure.hxx>
 
 #include <Graphic3d_GraphicDriver.hxx>
 #include <Graphic3d_MapOfStructure.hxx>
 #include <Visual3d_PickPath.hxx>
 #include <Visual3d_SetIteratorOfSetOfView.hxx>
 
 #include <Visual3d_PickPath.hxx>
 #include <Visual3d_SetIteratorOfSetOfView.hxx>
 
-#ifndef WNT
-# include <Xw_Window.hxx>
-#else
+#if defined (_WIN32) || defined(__WIN32__)
 # include <WNT_Window.hxx>
 # include <WNT_Window.hxx>
-#endif  // WNT
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+# include <Cocoa_Window.hxx>
+#else
+# include <Xw_Window.hxx>
+#endif
 
 //-Aliases
 
 
 //-Aliases
 
 
 //-Constructors
 
 
 //-Constructors
 
-Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Aspect_GraphicDevice)& aDevice):
-Graphic3d_StructureManager (aDevice),
+Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Graphic3d_GraphicDriver)& theDriver):
+Graphic3d_StructureManager (theDriver),
 MyDefinedView (),
 MyViewGenId (View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*(Visual3d_ViewManager::CurrentId ()-1),View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*Visual3d_ViewManager::CurrentId ()-1),
 MyZBufferAuto (Standard_False),
 MyTransparency (Standard_False)
 {
 MyDefinedView (),
 MyViewGenId (View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*(Visual3d_ViewManager::CurrentId ()-1),View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*Visual3d_ViewManager::CurrentId ()-1),
 MyZBufferAuto (Standard_False),
 MyTransparency (Standard_False)
 {
+  // default layer is always presented in display layer sequence
+  // it can not be removed
+  myLayerIds.Add (0);
+  myLayerSeq.Append (0);
 
 
-Handle(Aspect_GraphicDriver) agd = aDevice->GraphicDriver ();
-
-       MyGraphicDriver = *(Handle(Graphic3d_GraphicDriver) *) &agd;
-
+  MyGraphicDriver = theDriver;
 }
 
 //-Destructors
 }
 
 //-Destructors
@@ -110,19 +132,19 @@ void Visual3d_ViewManager::Remove () {
        //
        // Destroy all defined views
        //
        //
        // Destroy all defined views
        //
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
 
 #ifdef DESTROY
        cout << "The Manager " << MyId << " have " << Length << " defined views\n";
        cout << flush;
 #endif
 
 #ifdef DESTROY
        cout << "The Manager " << MyId << " have " << Length << " defined views\n";
        cout << flush;
 #endif
-       MyDefinedView.Clear ();
 
 
+  // clear all structures whilst views are alive for correct GPU memory management
+  MyDisplayedStructure.Clear();
+  MyHighlightedStructure.Clear();
+  MyPickStructure.Clear();
+
+  // clear list of managed views
+  MyDefinedView.Clear();
 }
 
 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority) {
 }
 
 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority) {
@@ -137,13 +159,8 @@ void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structu
        //
        // Change structure priority in all defined views
        //
        //
        // Change structure priority in all defined views
        //
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->ChangeDisplayPriority
                        (AStructure, OldPriority, NewPriority);
        while (MyIterator.More ()) {
                (MyIterator.Value ())->ChangeDisplayPriority
                        (AStructure, OldPriority, NewPriority);
@@ -154,14 +171,14 @@ void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structu
 
 }
 
 
 }
 
-void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) { 
+void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
 
   //Standard_Integer LengthD   = MyDisplayedStructure.Extent() ();
 
   // Even if physically the structure cannot
   // be displayed (pb of visualisation type)
   // it has status Displayed.
 
   //Standard_Integer LengthD   = MyDisplayedStructure.Extent() ();
 
   // Even if physically the structure cannot
   // be displayed (pb of visualisation type)
   // it has status Displayed.
+
   if (!MyDisplayedStructure.Contains(AStructure))
     return;
 
   if (!MyDisplayedStructure.Contains(AStructure))
     return;
 
@@ -169,19 +186,19 @@ void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStruct
   // Recompute structure in all activated views
   //
   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
   // Recompute structure in all activated views
   //
   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
   while (MyIterator.More ()) {
     (MyIterator.Value ())->ReCompute (AStructure);
 
     // MyIterator.Next () is located on the next view
     MyIterator.Next ();
   }
   while (MyIterator.More ()) {
     (MyIterator.Value ())->ReCompute (AStructure);
 
     // MyIterator.Next () is located on the next view
     MyIterator.Next ();
   }
-  
+
 }
 
 }
 
-void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure, 
-                                     const Handle(Graphic3d_DataStructureManager)& AProjector) 
-{ 
+void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure,
+                                     const Handle(Graphic3d_DataStructureManager)& AProjector)
+{
 
   if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
 
 
   if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
 
@@ -192,19 +209,17 @@ void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStruct
 #endif
   Standard_Integer ViewId = theView->Identification ();
 
 #endif
   Standard_Integer ViewId = theView->Identification ();
 
-  Standard_Integer indexD = 0;
-
   // Even if physically the structure cannot
   // be displayed (pb of visualisation type)
   // it has status Displayed.
   if (!MyDisplayedStructure.Contains(AStructure))
     return;
   // Even if physically the structure cannot
   // be displayed (pb of visualisation type)
   // it has status Displayed.
   if (!MyDisplayedStructure.Contains(AStructure))
     return;
-       
+
   //
   // Recompute structure in all activated views
   //
   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
   //
   // Recompute structure in all activated views
   //
   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
   while (MyIterator.More ()) {
     if ((MyIterator.Value ())->Identification () == ViewId)
       theView->ReCompute (AStructure);
   while (MyIterator.More ()) {
     if ((MyIterator.Value ())->Identification () == ViewId)
       theView->ReCompute (AStructure);
@@ -217,13 +232,8 @@ void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStruct
 
 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction) {
 
 
 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction) {
 
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Clear (AStructure, WithDestruction);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Clear (AStructure, WithDestruction);
 
@@ -235,13 +245,8 @@ void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure,
 
 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
 
 
 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
 
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Connect (AMother, ADaughter);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Connect (AMother, ADaughter);
 
@@ -253,13 +258,8 @@ void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother,
 
 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
 
 
 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
 
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Disconnect (AMother, ADaughter);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Disconnect (AMother, ADaughter);
 
@@ -269,7 +269,7 @@ void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMothe
 
 }
 
 
 }
 
-void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure) { 
+void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure) {
 
 
  // Even if physically the structure cannot
 
 
  // Even if physically the structure cannot
@@ -282,7 +282,7 @@ void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructur
        // Display structure in all activated views
        //
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        // Display structure in all activated views
        //
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Display (AStructure);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Display (AStructure);
 
@@ -306,13 +306,8 @@ void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure)
        //
        // Erase structure in all defined views
        //
        //
        // Erase structure in all defined views
        //
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Erase (AStructure);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Erase (AStructure);
 
@@ -321,7 +316,6 @@ void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure)
        }
 
        MyHighlightedStructure.Remove (AStructure);
        }
 
        MyHighlightedStructure.Remove (AStructure);
-       MyVisibleStructure.Remove (AStructure);
        MyPickStructure.Remove (AStructure);
 
 }
        MyPickStructure.Remove (AStructure);
 
 }
@@ -329,7 +323,7 @@ void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure)
 void Visual3d_ViewManager::Erase () {
 
  Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
 void Visual3d_ViewManager::Erase () {
 
  Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
+
  for (; it.More(); it.Next()) {
    Handle(Graphic3d_Structure) SG = it.Key();
    SG->Erase();
  for (; it.More(); it.Next()) {
    Handle(Graphic3d_Structure) SG = it.Key();
    SG->Erase();
@@ -344,13 +338,8 @@ void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStruct
        //
        // Highlight in all activated views
        //
        //
        // Highlight in all activated views
        //
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Highlight (AStructure, AMethod);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Highlight (AStructure, AMethod);
 
@@ -362,13 +351,8 @@ void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStruct
 
 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf) {
 
 
 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf) {
 
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->SetTransform (AStructure, ATrsf);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->SetTransform (AStructure, ATrsf);
 
@@ -381,7 +365,7 @@ void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStr
 void Visual3d_ViewManager::UnHighlight () {
 
   Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
 void Visual3d_ViewManager::UnHighlight () {
 
   Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
-  
+
   for (; it.More(); it.Next()) {
     Handle(Graphic3d_Structure) SG = it.Key();
     SG->UnHighlight ();
   for (; it.More(); it.Next()) {
     Handle(Graphic3d_Structure) SG = it.Key();
     SG->UnHighlight ();
@@ -398,13 +382,8 @@ void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStru
        //
        // UnHighlight in all activated views
        //
        //
        // UnHighlight in all activated views
        //
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->UnHighlight (AStructure);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->UnHighlight (AStructure);
 
@@ -426,7 +405,7 @@ Standard_Integer Dx, Dy;
        Standard_Integer j = MyDefinedView.Extent ();
        if (j == 0) return;
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Standard_Integer j = MyDefinedView.Extent ();
        if (j == 0) return;
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ()) {
            while (MyIterator.More ()) {
                (MyIterator.Value ())->Window ()->Size (Dx, Dy);
        if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ()) {
            while (MyIterator.More ()) {
                (MyIterator.Value ())->Window ()->Size (Dx, Dy);
@@ -441,7 +420,7 @@ Standard_Integer Dx, Dy;
            if (! MyOverLayer.IsNull ())
                MyOverLayer->SetViewport (MaxDx, MaxDy);
        }
            if (! MyOverLayer.IsNull ())
                MyOverLayer->SetViewport (MaxDx, MaxDy);
        }
+
        if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ())
            MyIterator.Initialize (MyDefinedView);
        while (MyIterator.More ()) {
        if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ())
            MyIterator.Initialize (MyDefinedView);
        while (MyIterator.More ()) {
@@ -461,7 +440,7 @@ void Visual3d_ViewManager::Update () const {
        Standard_Integer j = MyDefinedView.Extent ();
        if (j == 0) return;
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Standard_Integer j = MyDefinedView.Extent ();
        if (j == 0) return;
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Update (MyUnderLayer, MyOverLayer);
 
        while (MyIterator.More ()) {
                (MyIterator.Value ())->Update (MyUnderLayer, MyOverLayer);
 
@@ -475,11 +454,6 @@ Handle(Visual3d_HSetOfView) Visual3d_ViewManager::ActivatedView () const {
 
 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
 
 
 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
 
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
 
        while (MyIterator.More ()) {
 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
 
        while (MyIterator.More ()) {
@@ -561,7 +535,7 @@ Standard_Real Dx, Dy, Ratio;
 
     AVertex.Coord (AX, AY, AZ);
 
 
     AVertex.Coord (AX, AY, AZ);
 
-    Result = MyGraphicDriver->ProjectRaster (TheCView, 
+    Result = MyGraphicDriver->ProjectRaster (TheCView,
       Standard_ShortReal (AX), Standard_ShortReal (AY), Standard_ShortReal (AZ),
       AU, AV);
 
       Standard_ShortReal (AX), Standard_ShortReal (AY), Standard_ShortReal (AZ),
       AU, AV);
 
@@ -573,11 +547,6 @@ Standard_Real Dx, Dy, Ratio;
       Standard_Real APT;
 
       Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
       Standard_Real APT;
 
       Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
-#ifdef DEB
-      Standard_Integer Length = MyDefinedView.Extent ();
-#else
-      MyDefinedView.Extent ();
-#endif
 
       Standard_Integer stop = 0;
 
 
       Standard_Integer stop = 0;
 
@@ -665,9 +634,8 @@ Graphic3d_Vertex Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)
 Graphic3d_CView TheCView;
 Graphic3d_Vertex Point;
 
 Graphic3d_CView TheCView;
 Graphic3d_Vertex Point;
 
-       if (! ViewExists (AWindow, TheCView))
-           Point.SetCoord (RealLast (), RealLast (), RealLast ());
-       else {
+       if (ViewExists (AWindow, TheCView))
+        {
 Standard_Integer Width, Height;
 Standard_ShortReal x, y, z;
 Standard_Boolean Result;
 Standard_Integer Width, Height;
 Standard_ShortReal x, y, z;
 Standard_Boolean Result;
@@ -784,8 +752,6 @@ Standard_Boolean BResult;
 
                if (WCT != 0.)
                    Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
 
                if (WCT != 0.)
                    Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
-               else
-                   Point.SetCoord (RealLast (), RealLast (), RealLast ());
            }
        }
 
            }
        }
 
@@ -799,7 +765,7 @@ void Visual3d_ViewManager::ConvertCoordWithProj (const Handle(Aspect_Window)& AW
 Graphic3d_CView TheCView;
 
         if (! ViewExists (AWindow, TheCView)) {
 Graphic3d_CView TheCView;
 
         if (! ViewExists (AWindow, TheCView)) {
-           Point.SetCoord (RealLast (), RealLast (), RealLast ());
+           Point.SetCoord (0., 0., 0.);
            Proj.SetCoord (0., 0., 0.);
         }
        else {
            Proj.SetCoord (0., 0., 0.);
         }
        else {
@@ -924,7 +890,7 @@ Standard_Boolean BResult;
                if (WCT != 0.)
                    Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
                else
                if (WCT != 0.)
                    Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
                else
-                   Point.SetCoord (RealLast (), RealLast (), RealLast ());
+                   Point.SetCoord (0., 0., 0.);
 
                 // Define projection ray
                NPCZ    = 10.0;
 
                 // Define projection ray
                NPCZ    = 10.0;
@@ -980,150 +946,6 @@ Standard_Boolean BResult;
 
 }
 
 
 }
 
-Visual3d_PickDescriptor Visual3d_ViewManager::Pick (const Visual3d_ContextPick& CTX, const Handle(Aspect_Window)& AWindow, const Standard_Integer AX, const Standard_Integer AY) {
-
-// The marking is activated only if the data is correct
-Standard_Boolean DoPick = Standard_False;
-
-CALL_DEF_PICK apick;
-Standard_Integer Width, Height;
-
-       // Parse the list of views to find a 
-       // view having this specified window
-       Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
-       int TheWindowIdOfView;
-
-#ifndef WNT
-const Handle(Xw_Window) THEWindow = *(Handle(Xw_Window) *) &AWindow;
-       int TheSpecifiedWindowId = int (THEWindow->XWindow ());
-#else
-const Handle(WNT_Window) THEWindow = *(Handle(WNT_Window) *) &AWindow;
-       int TheSpecifiedWindowId = int (THEWindow->HWindow ());
-#endif  // WNT
-
-       while ((! DoPick) && (MyIterator.More ())) {
-
-          if ( ((MyIterator.Value ())->IsDefined ()) &&
-               ((MyIterator.Value ())->IsActive ()) ) {
-
-const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
-#ifndef WNT
-const Handle(Xw_Window) theWindow = *(Handle(Xw_Window) *) &AspectWindow;
-       TheWindowIdOfView = int (theWindow->XWindow ());
-#else
-const Handle(WNT_Window) theWindow = *(Handle(WNT_Window) *) &AspectWindow;
-       TheWindowIdOfView = int (theWindow->HWindow ());
-#endif  // WNT
-               // Comparision on window IDs
-               if (TheWindowIdOfView == TheSpecifiedWindowId) {
-                       DoPick          = Standard_True;
-
-                       // Update
-                       apick.WsId      =
-                       int ((MyIterator.Value ())->Identification ());
-
-                       apick.ViewId    =
-                       int ((MyIterator.Value ())->Identification ());
-#ifndef WNT
-                       apick.DefWindow.XWindow = TheSpecifiedWindowId;
-#else
-                       apick.DefWindow.XWindow = (HWND) TheSpecifiedWindowId;
-#endif
-
-                       apick.x                 = int (AX);
-                       apick.y                 = int (AY);
-
-                       theWindow->Size (Width, Height);
-                       apick.DefWindow.dx      = float (Width);
-                       apick.DefWindow.dy      = float (Height);
-
-                       apick.Context.aperture  = (float) CTX.Aperture ();
-                       apick.Context.order     = int (CTX.Order ());
-                       apick.Context.depth     = int (CTX.Depth ());
-
-               }
-          } /* if ((MyIterator.Value ())->IsDefined ()) { */
-
-          // MyIterator.Next () is located on the next view
-          MyIterator.Next ();
-       }
-
-       if (DoPick)
-               MyGraphicDriver->Pick (apick);
-       else
-               apick.Pick.depth        = 0;
-
-       // Picking : return
-Standard_Integer i, j=0;
-Standard_Integer NbPick;
-
-Visual3d_PickDescriptor PDes (CTX);
-Visual3d_PickPath PPat;
-
-       PDes.Clear ();
-       NbPick  = 0;
-       // For i=0 it is not a graphic structure it is a view structure
-       // For i=1 it is the displayed graphic structure
-       // For i=2 to apick.Pick.depth-1 it is the connected graphic structures
-       if (apick.Pick.depth != 0) {
-           j = apick.Pick.listid[1];
-           if ((Graphic3d_StructureManager::Identification (j))->
-                                               IsSelectable ()) {
-               // Maj element number
-               PPat.SetElementNumber (apick.Pick.listelem[1]);
-               // Maj pick identifier
-               PPat.SetPickIdentifier (apick.Pick.listpickid[1]);
-               // Maj structure
-               PPat.SetStructIdentifier
-                       (Graphic3d_StructureManager::Identification (j));
-               // Maj PickPath
-               PDes.AddPickPath (PPat);
-               NbPick++;
-           }
-       }
-
-       // Not very efficient, revise (CAL 22/09/95)
-       if (apick.Pick.depth > 2) {
-Handle(Graphic3d_Structure) StructCur =
-       Graphic3d_StructureManager::Identification (j);
-Standard_Boolean found;
-Graphic3d_MapOfStructure Set;
-
-           for (i=2; i<apick.Pick.depth; i++) {
-               found = Standard_False;
-               j = apick.Pick.listid[i-1];
-               Set.Clear ();
-               StructCur->Descendants (Set);
-Graphic3d_MapIteratorOfMapOfStructure IteratorD (Set);
-
-               j = apick.Pick.listid[i];
-               while (IteratorD.More () && !found) {
-                   StructCur = IteratorD.Key ();
-                   if (StructCur->Identification () == j ) {
-                       found = Standard_True;
-                       // Maj element number
-                       PPat.SetElementNumber (apick.Pick.listelem[i]);
-                       // Maj pick identifier
-                       PPat.SetPickIdentifier (apick.Pick.listpickid[i]);
-                       // Maj structure
-                       PPat.SetStructIdentifier (StructCur);
-                       // Maj PickPath
-                       PDes.AddPickPath (PPat);
-                       NbPick++;
-                   }
-                   // IteratorD.Next () is located on the next structure
-                   IteratorD.Next ();
-               }
-           }
-       }
-
-       apick.Pick.depth        = int (NbPick);
-
-       MyGraphicDriver->InitPick ();
-
-       return (PDes);
-
-}
 
 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const {
 
 
 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const {
 
@@ -1132,15 +954,17 @@ Standard_Boolean Exist = Standard_False;
        // Parse the list of views to find
        // a view with the specified window
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        // Parse the list of views to find
        // a view with the specified window
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
-       int TheWindowIdOfView;
 
 
-#ifndef WNT
-const Handle(Xw_Window) THEWindow = *(Handle(Xw_Window) *) &AWindow;
-       int TheSpecifiedWindowId = int (THEWindow->XWindow ());
+#if defined(_WIN32) || defined(__WIN32__)
+  const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
+  int TheSpecifiedWindowId = int (THEWindow->HWindow ());
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+  const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
+  NSView* TheSpecifiedWindowId = THEWindow->HView();
 #else
 #else
-const Handle(WNT_Window) THEWindow = *(Handle(WNT_Window) *) &AWindow;
-       int TheSpecifiedWindowId = int (THEWindow->HWindow ());
-#endif  // WNT
+  const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
+  int TheSpecifiedWindowId = int (THEWindow->XWindow ());
+#endif
 
        while ((! Exist) && (MyIterator.More ())) {
 
 
        while ((! Exist) && (MyIterator.More ())) {
 
@@ -1148,17 +972,20 @@ const Handle(WNT_Window) THEWindow = *(Handle(WNT_Window) *) &AWindow;
                ((MyIterator.Value ())->IsActive ()) ) {
 
 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
                ((MyIterator.Value ())->IsActive ()) ) {
 
 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
-#ifndef WNT
-const Handle(Xw_Window) theWindow = *(Handle(Xw_Window) *) &AspectWindow;
-       TheWindowIdOfView = int (theWindow->XWindow ());
+#if defined(_WIN32) || defined(__WIN32__)
+   const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
+   int TheWindowIdOfView = int (theWindow->HWindow ());
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+   const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
+   NSView* TheWindowIdOfView = theWindow->HView();
 #else
 #else
-const Handle(WNT_Window) theWindow = *(Handle(WNT_Window) *) &AspectWindow;
-       TheWindowIdOfView = int (theWindow->HWindow ());
+   const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
+   int TheWindowIdOfView = int (theWindow->XWindow ());
 #endif  // WNT
                // Comparaison on window IDs
                if (TheWindowIdOfView == TheSpecifiedWindowId) {
                        Exist   = Standard_True;
 #endif  // WNT
                // Comparaison on window IDs
                if (TheWindowIdOfView == TheSpecifiedWindowId) {
                        Exist   = Standard_True;
-                       TheCView        = *(CALL_DEF_VIEW *)(MyIterator.Value ())->CView ();
+                       TheCView        = *(Graphic3d_CView* )(MyIterator.Value())->CView();
                }
           } /* if ((MyIterator.Value ())->IsDefined ()) */
 
                }
           } /* if ((MyIterator.Value ())->IsDefined ()) */
 
@@ -1176,12 +1003,6 @@ void Visual3d_ViewManager::Activate () {
        // Activates all deactivated views
        //
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        // Activates all deactivated views
        //
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
 
        while (MyIterator.More ()) {
                if (! (MyIterator.Value ())->IsActive ())
 
        while (MyIterator.More ()) {
                if (! (MyIterator.Value ())->IsActive ())
@@ -1198,13 +1019,8 @@ void Visual3d_ViewManager::Deactivate () {
        //
        // Deactivates all activated views
        //
        //
        // Deactivates all activated views
        //
-#ifdef DEB
-       Standard_Integer Length = MyDefinedView.Extent ();
-#else
-        MyDefinedView.Extent ();
-#endif
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
        Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+
        while (MyIterator.More ()) {
                if ((MyIterator.Value ())->IsActive ())
                        (MyIterator.Value ())->Deactivate ();
        while (MyIterator.More ()) {
                if ((MyIterator.Value ())->IsActive ())
                        (MyIterator.Value ())->Deactivate ();
@@ -1231,7 +1047,7 @@ Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard
 }
 
 Standard_Integer Visual3d_ViewManager::Identification () const {
 }
 
 Standard_Integer Visual3d_ViewManager::Identification () const {
+
        return (Graphic3d_StructureManager::Identification ());
 
 }
        return (Graphic3d_StructureManager::Identification ());
 
 }
@@ -1245,6 +1061,19 @@ Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_Vie
 
 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
 {
 
 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
 {
+  Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
+  while (MyIterator.More()) 
+  {
+    if ((MyIterator.Value())->Identification () == aViewId)
+    {
+      const Handle(Visual3d_View)& theView = MyIterator.Value();
+      //remove the view from the list
+      MyDefinedView.Remove(theView);
+      break;
+    }
+    // go to next
+    MyIterator.Next ();
+  }
   MyViewGenId.Free(aViewId);
 }
 
   MyViewGenId.Free(aViewId);
 }
 
@@ -1276,7 +1105,7 @@ void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag) {
        if (! MyZBufferAuto && ! AFlag) return;
 
        // if pass from False to True :
        if (! MyZBufferAuto && ! AFlag) return;
 
        // if pass from False to True :
-       // no problem, at the next view update, it 
+       // no problem, at the next view update, it
        // will properly ask questions to answer (SetVisualisation)
        // if pass from True to False :
        // it is necessary to modify ZBufferActivity at each view so that
        // will properly ask questions to answer (SetVisualisation)
        // if pass from True to False :
        // it is necessary to modify ZBufferActivity at each view so that
@@ -1339,3 +1168,143 @@ const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
        return (MyOverLayer);
 
 }
        return (MyOverLayer);
 
 }
+
+//=======================================================================
+//function : ChangeZLayer
+//purpose  :
+//=======================================================================
+
+void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
+                                         const Standard_Integer theLayerId)
+{
+  if (!myLayerIds.Contains (theLayerId))
+    return;
+
+  // change display layer for structure in all views
+  if (MyDisplayedStructure.Contains (theStructure))
+  {
+    Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
+    for ( ; aViewIt.More (); aViewIt.Next ())
+      (aViewIt.Value ())->ChangeZLayer (theStructure, theLayerId);
+  }
+
+  // tell graphic driver to update the structure's display layer
+  MyGraphicDriver->ChangeZLayer (
+    (*(Graphic3d_CStructure*)theStructure->CStructure ()), theLayerId);
+}
+
+//=======================================================================
+//function : GetZLayer
+//purpose  :
+//=======================================================================
+
+Standard_Integer Visual3d_ViewManager::GetZLayer (const Handle(Graphic3d_Structure)& theStructure) const
+{
+  Graphic3d_CStructure& aStructure =
+    (*(Graphic3d_CStructure*)theStructure->CStructure ());
+
+  return MyGraphicDriver->GetZLayer (aStructure);
+}
+
+//=======================================================================
+//function : AddZLayer
+//purpose  :
+//=======================================================================
+
+Standard_Boolean Visual3d_ViewManager::AddZLayer (Standard_Integer& theLayerId)
+{
+  try
+  {
+    OCC_CATCH_SIGNALS
+    theLayerId = getZLayerGenId ().Next ();
+    myLayerIds.Add (theLayerId);
+    myLayerSeq.Append (theLayerId);
+  }
+  catch (Aspect_IdentDefinitionError)
+  {
+    // new index can't be generated
+    return Standard_False;
+  }
+
+  // tell all managed views to remove display layers
+  Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
+  for ( ; aViewIt.More (); aViewIt.Next ())
+    (aViewIt.Value ())->AddZLayer (theLayerId);
+
+  return Standard_True;
+}
+
+//=======================================================================
+//function : RemoveZLayer
+//purpose  :
+//=======================================================================
+
+Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Standard_Integer theLayerId)
+{
+  if (!myLayerIds.Contains (theLayerId) || theLayerId == 0)
+    return Standard_False;
+
+  // tell all managed views to remove display layers
+  Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
+  for ( ; aViewIt.More (); aViewIt.Next ())
+    (aViewIt.Value ())->RemoveZLayer (theLayerId);
+
+  MyGraphicDriver->UnsetZLayer (theLayerId);
+
+  // remove index
+  for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
+    if (myLayerSeq(aIdx) == theLayerId)
+    {
+      myLayerSeq.Remove (aIdx);
+      break;
+    }
+
+  myLayerIds.Remove (theLayerId);
+  getZLayerGenId ().Free (theLayerId);
+
+  return Standard_True;
+}
+
+//=======================================================================
+//function : GetAllZLayers
+//purpose  :
+//=======================================================================
+
+void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
+{
+  theLayerSeq.Assign (myLayerSeq);
+}
+
+//=======================================================================
+//function : getZLayerGenId
+//purpose  :
+//=======================================================================
+
+Aspect_GenId& Visual3d_ViewManager::getZLayerGenId ()
+{
+  static Aspect_GenId aGenId (1, IntegerLast());
+  return aGenId;
+}
+
+//=======================================================================
+//function : InstallZLayers
+//purpose  :
+//=======================================================================
+
+void Visual3d_ViewManager::InstallZLayers(const Handle(Visual3d_View)& theView) const
+{
+  if (!MyDefinedView.Contains (theView))
+    return;
+
+  // erase and insert layers iteratively to provide the same layer order as
+  // in the view manager's sequence. This approach bases on the layer insertion
+  // order: the new layers are always appended to the end of the list
+  // inside of view, while layer remove operation doesn't affect the order.
+  // Starting from second layer : no need to change the default z layer.
+  for (Standard_Integer aSeqIdx = 2; aSeqIdx <= myLayerSeq.Length (); aSeqIdx++)
+  {
+    Standard_Integer aLayerID = myLayerSeq.Value (aSeqIdx);
+    theView->RemoveZLayer (aLayerID);
+    theView->AddZLayer (aLayerID);
+  }
+}