daa6b793d9d20d0dd323d2b6804831e47593f473
[occt.git] / src / Visual3d / Visual3d_ViewManager.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 /***********************************************************************
16
17      FONCTION :
18      ----------
19         Classe Visual3d_ViewManager.cxx :
20
21         Declaration of variables specific to visualisers
22
23      HISTORIQUE DES MODIFICATIONS   :
24      --------------------------------
25       Mars 1992 : NW,JPB,CAL ; Creation.
26       19-06-96  : FMN ; Suppression variables inutiles
27       04-02-97  : FMN ; Suppression de PSOutput, XWDOutput ...
28       06-05-97  : CAL ; Ajout du Clear sur les TOS_COMPUTED.
29       19-09-97  : CAL ; Remplacement de Window->Position par Window->Size;
30       24-10-97  : CAL ; Retrait de DownCast.
31       20-11-97  : CAL ; Disparition de la dependance avec math
32       01-12-97  : CAL ; Retrait du test IsActive sur l'Update et le Redraw
33       31-12-97  : CAL ; Disparition de MathGra
34       16-01-98  : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
35       11-03-98  : CAL ; Visual3d_ViewManager::Remove ()
36       20-05-98  : CAL ; Perfs. Connection entre structures COMPUTED.
37       10-06-98  : CAL ; Modification des signatures de xxProjectRaster.
38       10-06-98  : CAL ; Modification de la signature de ViewExists.
39       01-12-98  : CAL ; S4062. Ajout des layers.
40       02-12-98  : CAL ; Remove () ne detruit plus les vues.
41
42 ************************************************************************/
43
44 /*----------------------------------------------------------------------*/
45 /*
46  * Constants
47  */
48
49 #define NO_DOWNCAST
50 #define NO_DESTROY
51
52 /*----------------------------------------------------------------------*/
53 /*
54  * Includes
55  */
56
57 // for the class
58 #include <Visual3d_ViewManager.ixx>
59
60 #include <Standard_ErrorHandler.hxx>
61
62 #include <Aspect_IdentDefinitionError.hxx>
63
64 #include <Graphic3d_GraphicDriver.hxx>
65 #include <Graphic3d_MapOfStructure.hxx>
66 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
67
68
69 #if defined (_WIN32) || defined(__WIN32__)
70 # include <WNT_Window.hxx>
71 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
72 # include <Cocoa_Window.hxx>
73 #else
74 # include <Xw_Window.hxx>
75 #endif
76
77 // views identifiers : possible range
78 #define View_IDMIN      1
79 #define View_IDMAX      10000
80
81 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Graphic3d_GraphicDriver)& theDriver):
82 Graphic3d_StructureManager (theDriver),
83 MyDefinedView (),
84 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),
85 MyZBufferAuto (Standard_False),
86 myZLayerGenId (1, IntegerLast())
87 {
88   MyGraphicDriver = theDriver;
89
90   // default layers are always presented in display layer sequence it can not be removed
91   Graphic3d_ZLayerSettings aDefSettings;
92   aDefSettings.Flags = Graphic3d_ZLayerDepthTest
93                      | Graphic3d_ZLayerDepthWrite;
94   aDefSettings.IsImmediate = false;
95   myLayerIds.Add             (Graphic3d_ZLayerId_Default);
96   myLayerSeq.Append          (Graphic3d_ZLayerId_Default);
97   myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Default, aDefSettings);
98
99   Graphic3d_ZLayerSettings aTopSettings;
100   aTopSettings.Flags = Graphic3d_ZLayerDepthTest
101                      | Graphic3d_ZLayerDepthWrite;
102   aTopSettings.IsImmediate = true;
103   myLayerIds.Add             (Graphic3d_ZLayerId_Top);
104   myLayerSeq.Append          (Graphic3d_ZLayerId_Top);
105   myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Top, aTopSettings);
106
107   Graphic3d_ZLayerSettings aTopmostSettings;
108   aTopmostSettings.Flags = Graphic3d_ZLayerDepthTest
109                          | Graphic3d_ZLayerDepthWrite
110                          | Graphic3d_ZLayerDepthClear;
111   aTopmostSettings.IsImmediate = true;
112   myLayerIds.Add             (Graphic3d_ZLayerId_Topmost);
113   myLayerSeq.Append          (Graphic3d_ZLayerId_Topmost);
114   myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Topmost, aTopmostSettings);
115
116   Graphic3d_ZLayerSettings anOsdSettings;
117   anOsdSettings.Flags = 0;
118   anOsdSettings.IsImmediate = true;
119   myLayerIds.Add             (Graphic3d_ZLayerId_TopOSD);
120   myLayerSeq.Append          (Graphic3d_ZLayerId_TopOSD);
121   myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_TopOSD, anOsdSettings);
122 }
123
124 //-Destructors
125
126 void Visual3d_ViewManager::Destroy () {
127
128 #ifdef DESTROY
129         cout << "Visual3d_ViewManager::Destroy (" << MyId << ")\n" << flush;
130 #endif
131
132         Remove ();
133 }
134
135 //-Methods, in order
136
137 void Visual3d_ViewManager::Remove () {
138
139 #ifdef DESTROY
140         cout << "Visual3d_ViewManager::Remove (" << MyId << ")\n" << flush;
141 #endif
142
143         //
144         // Destroy all defined views
145         //
146
147 #ifdef DESTROY
148         cout << "The Manager " << MyId << " have " << Length << " defined views\n";
149         cout << flush;
150 #endif
151
152   // clear all structures whilst views are alive for correct GPU memory management
153   MyDisplayedStructure.Clear();
154   MyHighlightedStructure.Clear();
155
156   // clear list of managed views
157   MyDefinedView.Clear();
158 }
159
160 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority)
161 {
162
163 #ifdef TRACE
164   cout << "Visual3d_ViewManager::ChangeDisplayPriority ("
165     << AStructure->Identification ()
166     << ", " << OldPriority << ", " << NewPriority << ")\n";
167   cout << flush;
168 #endif
169
170   //
171   // Change structure priority in all defined views
172   //
173   for(int i=1; i<=MyDefinedView.Length(); i++)
174   {
175     (MyDefinedView.Value(i))->ChangeDisplayPriority(AStructure, OldPriority, NewPriority);
176   }
177 }
178
179 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
180
181   //Standard_Integer LengthD    = MyDisplayedStructure.Extent() ();
182
183   // Even if physically the structure cannot
184   // be displayed (pb of visualisation type)
185   // it has status Displayed.
186
187   if (!MyDisplayedStructure.Contains(AStructure))
188     return;
189
190   //
191   // Recompute structure in all activated views
192   //
193   for(int i=1; i<=MyDefinedView.Length(); i++)
194   {
195     (MyDefinedView.Value(i))->ReCompute(AStructure);
196   }
197 }
198
199 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure,
200                                       const Handle(Graphic3d_DataStructureManager)& AProjector)
201 {
202
203   if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
204
205 #ifdef DOWNCAST
206   Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
207 #else
208   Handle(Visual3d_View) theView = *(Handle(Visual3d_View) *) &AProjector;
209 #endif
210   Standard_Integer ViewId = theView->Identification ();
211
212   // Even if physically the structure cannot
213   // be displayed (pb of visualisation type)
214   // it has status Displayed.
215   if (!MyDisplayedStructure.Contains(AStructure))
216     return;
217
218   //
219   // Recompute structure in all activated views
220   //
221   for(int i=1; i<=MyDefinedView.Length(); i++)
222   {
223     if ((MyDefinedView.Value(i))->Identification () == ViewId)
224     {
225       theView->ReCompute (AStructure);
226     }
227   }
228 }
229
230 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction)
231 {
232   for(int i=1; i<=MyDefinedView.Length(); i++)
233   {
234     (MyDefinedView.Value(i))->Clear(AStructure, WithDestruction);
235   }
236 }
237
238 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter)
239 {
240   for(int i=1; i<=MyDefinedView.Length(); i++)
241   {
242     (MyDefinedView.Value(i))->Connect (AMother, ADaughter);
243   }
244 }
245
246 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter)
247 {
248   for(int i=1; i<=MyDefinedView.Length(); i++)
249   {
250     (MyDefinedView.Value(i))->Disconnect (AMother, ADaughter);
251   }
252 }
253
254 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure)
255 {
256   // Even if physically the structure cannot
257   // be displayed (pb of visualisation type)
258   // it has status Displayed.
259
260   MyDisplayedStructure.Add(AStructure);
261   
262   for(int i=1; i<=MyDefinedView.Length(); i++)
263   {
264     (MyDefinedView.Value(i))->Display(AStructure);
265   }
266 }
267
268 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure)
269 {
270   // Even if physically the structure cannot
271   // be displayed (pb of visualisation type)
272   // it has status Displayed.
273
274   MyDisplayedStructure.Remove(AStructure);
275
276   //
277   // Erase structure in all defined views
278   //
279   for(int i=1; i<=MyDefinedView.Length(); i++)
280   {
281     (MyDefinedView.Value(i))->Erase (AStructure);
282   }
283
284   MyHighlightedStructure.Remove (AStructure);
285 }
286
287 void Visual3d_ViewManager::Erase () {
288
289  Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
290
291  for (; it.More(); it.Next()) {
292    Handle(Graphic3d_Structure) SG = it.Key();
293    SG->Erase();
294  }
295
296 }
297
298 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod)
299 {
300   MyHighlightedStructure.Add(AStructure);
301   
302   //
303   // Highlight in all activated views
304   //
305   
306   for(int i=1; i<=MyDefinedView.Length(); i++)
307   {
308     (MyDefinedView.Value(i))->Highlight (AStructure, AMethod);
309   }
310 }
311
312 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf)
313 {
314   for(int i=1; i<=MyDefinedView.Length(); i++)
315   {
316     (MyDefinedView.Value(i))->SetTransform (AStructure, ATrsf);
317   }
318 }
319
320 void Visual3d_ViewManager::UnHighlight () {
321
322   Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
323
324   for (; it.More(); it.Next()) {
325     Handle(Graphic3d_Structure) SG = it.Key();
326     SG->UnHighlight ();
327   }
328
329
330 }
331
332 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure)
333 {
334   MyHighlightedStructure.Remove(AStructure);
335
336   //
337   // UnHighlight in all activated views
338   //
339
340   for(int i=1; i<=MyDefinedView.Length(); i++)
341   {
342     (MyDefinedView.Value(i))->UnHighlight (AStructure);
343   }
344 }
345
346 void Visual3d_ViewManager::Redraw() const
347 {
348   // redraw all activated views
349   if (MyDefinedView.Length() == 0)
350   {
351     return;
352   }
353
354   if (!MyUnderLayer.IsNull() || !MyOverLayer.IsNull())
355   {
356     Standard_Integer aWidth = 0, aHeight = 0;
357     Standard_Integer aWidthMax  = 0;
358     Standard_Integer aHeightMax = 0;
359
360     for(int i=1; i<=MyDefinedView.Length(); i++)
361     {
362       MyDefinedView.Value(i)->Window()->Size (aWidth, aHeight);
363       aWidthMax  = Max (aWidthMax,  aWidth);
364       aHeightMax = Max (aHeightMax, aHeight);
365     }
366
367     if (!MyUnderLayer.IsNull())
368     {
369       MyUnderLayer->SetViewport (aWidthMax, aHeightMax);
370     }
371     if (!MyOverLayer.IsNull())
372     {
373       MyOverLayer->SetViewport (aWidthMax, aHeightMax);
374     }
375   }
376
377   for(int i=1; i<=MyDefinedView.Length(); i++)
378   {
379     MyDefinedView.Value(i)->Redraw (MyUnderLayer, MyOverLayer);
380   }
381 }
382
383 void Visual3d_ViewManager::Update() const
384 {
385   Redraw();
386 }
387
388 void Visual3d_ViewManager::RedrawImmediate() const
389 {
390   if (MyDefinedView.Length() == 0)
391   {
392     return;
393   }
394
395   // update all activated views
396   for(int i=1; i<=MyDefinedView.Length(); i++)
397   {
398     MyDefinedView.Value(i)->RedrawImmediate (MyUnderLayer, MyOverLayer);
399   }
400 }
401
402 void Visual3d_ViewManager::Invalidate() const
403 {
404   if (MyDefinedView.Length() == 0)
405   {
406     return;
407   }
408
409   // update all activated views
410   for(int i=1; i<=MyDefinedView.Length(); i++)
411   {
412     MyDefinedView.Value(i)->Invalidate();
413   }
414 }
415
416 Handle(Visual3d_HSequenceOfView) Visual3d_ViewManager::ActivatedView () const
417 {
418
419   Handle(Visual3d_HSequenceOfView) SG = new Visual3d_HSequenceOfView();
420
421   for(int i=1; i<=MyDefinedView.Length(); i++)
422   {
423     if ((MyDefinedView.Value(i))->IsActive ())
424     {
425       SG->Append(MyDefinedView.Value(i));
426     }
427   }
428
429   return (SG);
430 }
431
432 #ifdef IMPLEMENTED
433 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const
434 {
435   Standard_Boolean Result = Standard_False;
436
437   //
438   // Check all activated views
439   //
440   for(int i=1; (!Result) && i<=MyDefinedView.Length(); i++)
441   {
442     if ((MyDefinedView.Value(i))->IsActive())
443     {
444       Result = (MyDefinedView.Value(i))->ContainsComputedStructure();
445     }
446   }
447
448   return Result;
449 }
450 #endif
451
452 const Visual3d_SequenceOfView& Visual3d_ViewManager::DefinedViews() const
453 {
454   return MyDefinedView;
455 }
456
457 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const
458 {
459   Standard_Boolean Exist = Standard_False;
460
461   // Parse the list of views to find
462   // a view with the specified window
463
464 #if defined(_WIN32)
465   const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
466   Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
467 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
468   const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
469   NSView* TheSpecifiedWindowId = THEWindow->HView();
470 #elif defined(__ANDROID__)
471   int TheSpecifiedWindowId = -1;
472 #else
473   const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
474   int TheSpecifiedWindowId = int (THEWindow->XWindow ());
475 #endif
476
477   for(int i=1; (!Exist) && i<=MyDefinedView.Length(); i++)
478   {
479     if ( ((MyDefinedView.Value(i))->IsDefined ()) && ((MyDefinedView.Value(i))->IsActive ()) )
480     {
481       const Handle(Aspect_Window) AspectWindow = (MyDefinedView.Value(i))->Window();
482
483 #if defined(_WIN32)
484       const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
485       Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
486 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
487       const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
488       NSView* TheWindowIdOfView = theWindow->HView();
489 #elif defined(__ANDROID__)
490       int TheWindowIdOfView = 0;
491 #else
492       const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
493       int TheWindowIdOfView = int (theWindow->XWindow ());
494 #endif  // WNT
495       // Comparaison on window IDs
496       if (TheWindowIdOfView == TheSpecifiedWindowId)
497       {
498         Exist = Standard_True;
499         TheCView = *(Graphic3d_CView* )(MyDefinedView.Value(i))->CView();
500       }
501     }
502   }
503
504   return (Exist);
505 }
506
507 void Visual3d_ViewManager::Activate ()
508 {
509   //
510   // Activates all deactivated views
511   //
512   for(int i=1; i<=MyDefinedView.Length(); i++)
513   {
514     if (! (MyDefinedView.Value(i))->IsActive())
515     {
516       (MyDefinedView.Value(i))->Activate();
517     }
518   }
519 }
520
521 void Visual3d_ViewManager::Deactivate ()
522 {
523   //
524   // Deactivates all activated views
525   //
526   for(int i=1; i<=MyDefinedView.Length(); i++)
527   {
528     if ((MyDefinedView.Value(i))->IsActive())
529     {
530       (MyDefinedView.Value(i))->Deactivate();
531     }
532   }
533
534 }
535
536 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
537
538         // Retourne the planned of definable views for the current
539         // Visual3d_ViewManager.
540         return
541 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
542
543 }
544
545 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
546
547         return (Graphic3d_StructureManager::Identification (AId));
548
549 }
550
551 Standard_Integer Visual3d_ViewManager::Identification () const {
552
553         return (Graphic3d_StructureManager::Identification ());
554
555 }
556
557 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView)
558 {
559   MyDefinedView.Append(AView);
560   return (MyViewGenId.Next ());
561 }
562
563 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
564 {
565   for(int i=1; i<=MyDefinedView.Length(); i++)
566   {
567     if ((MyDefinedView.Value(i))->Identification() == aViewId)
568     {
569       //remove the view from the list
570       MyDefinedView.Remove(i);
571       break;
572     }
573   }
574
575   MyViewGenId.Free(aViewId);
576 }
577
578 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag)
579 {
580   if (MyZBufferAuto && AFlag) return;
581   if (! MyZBufferAuto && ! AFlag) return;
582
583   // if pass from False to True :
584   // no problem, at the next view update, it
585   // will properly ask questions to answer (SetVisualisation)
586   // if pass from True to False :
587   // it is necessary to modify ZBufferActivity at each view so that
588   // zbuffer could be active only if required by context.
589   // In this case -1 is passed so that the view ask itself the question
590   // Note : 0 forces the desactivation, 1 forces the activation
591   if (! AFlag)
592   {
593     for(int i=1; i<=MyDefinedView.Length(); i++)
594     {
595       (MyDefinedView.Value(i))->SetZBufferActivity(-1);
596     }
597   }
598   MyZBufferAuto = AFlag;
599 }
600
601 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const
602 {
603   return (MyZBufferAuto);
604 }
605
606 void Visual3d_ViewManager::SetLayer (const Handle(Visual3d_Layer)& ALayer) {
607
608 #ifdef TRACE_LAYER
609         cout << "Visual3d_ViewManager::SetLayer\n" << flush;
610 #endif
611
612         if (ALayer->Type () == Aspect_TOL_OVERLAY) {
613 #ifdef TRACE_LAYER
614                 if (MyOverLayer.IsNull ())
615                         cout << "MyOverLayer is defined" << endl;
616                 else
617                         cout << "MyOverLayer is redefined" << endl;
618 #endif
619                 MyOverLayer = ALayer;
620         }
621         else {
622 #ifdef TRACE_LAYER
623                 if (MyUnderLayer.IsNull ())
624                         cout << "MyUnderLayer is defined" << endl;
625                 else
626                         cout << "MyUnderLayer is redefined" << endl;
627 #endif
628                 MyUnderLayer = ALayer;
629         }
630
631 }
632
633 const Handle(Visual3d_Layer)& Visual3d_ViewManager::UnderLayer () const {
634
635         return (MyUnderLayer);
636
637 }
638
639 const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
640
641         return (MyOverLayer);
642
643 }
644
645 //=======================================================================
646 //function : ChangeZLayer
647 //purpose  :
648 //=======================================================================
649
650 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
651                                          const Graphic3d_ZLayerId           theLayerId)
652 {
653   if (!myLayerIds.Contains (theLayerId)
654    || !MyDisplayedStructure.Contains (theStructure))
655   {
656     return;
657   }
658
659   // change display layer for structure in all views
660   for (int aViewIter = 1; aViewIter <= MyDefinedView.Length(); ++aViewIter)
661   {
662     MyDefinedView.Value (aViewIter)->ChangeZLayer (theStructure, theLayerId);
663   }
664 }
665
666 //=======================================================================
667 //function : SetZLayerSettings
668 //purpose  :
669 //=======================================================================
670 void Visual3d_ViewManager::SetZLayerSettings (const Graphic3d_ZLayerId        theLayerId,
671                                               const Graphic3d_ZLayerSettings& theSettings)
672 {
673   // tell all managed views to set zlayer settings
674   for(int i=1; i<=MyDefinedView.Length(); i++)
675   {
676     (MyDefinedView.Value(i))->SetZLayerSettings (theLayerId, theSettings);
677   }
678
679   if (myMapOfZLayerSettings.IsBound (theLayerId))
680   {
681     myMapOfZLayerSettings.ChangeFind (theLayerId) = theSettings;
682   }
683   else
684   {
685     myMapOfZLayerSettings.Bind (theLayerId, theSettings);
686   }
687   
688 }
689
690 //=======================================================================
691 //function : ZLayerSettings
692 //purpose  :
693 //=======================================================================
694 Graphic3d_ZLayerSettings Visual3d_ViewManager::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
695 {
696   if (!myLayerIds.Contains (theLayerId))
697   {
698     return Graphic3d_ZLayerSettings();
699   }
700
701   return myMapOfZLayerSettings.Find (theLayerId);
702 }
703
704 //=======================================================================
705 //function : AddZLayer
706 //purpose  :
707 //=======================================================================
708
709 Standard_Boolean Visual3d_ViewManager::AddZLayer (Graphic3d_ZLayerId& theLayerId)
710 {
711   try
712   {
713     OCC_CATCH_SIGNALS
714     theLayerId = myZLayerGenId.Next();
715     myLayerIds.Add    (theLayerId);
716     myLayerSeq.Append (theLayerId);
717   }
718   catch (Aspect_IdentDefinitionError)
719   {
720     // new index can't be generated
721     return Standard_False;
722   }
723
724   // default z-layer settings
725   myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
726
727   // tell all managed views to remove display layers
728   for(int i=1; i<=MyDefinedView.Length(); i++)
729   {
730     (MyDefinedView.Value(i))->AddZLayer(theLayerId);
731   }
732
733   return Standard_True;
734 }
735
736 //=======================================================================
737 //function : RemoveZLayer
738 //purpose  :
739 //=======================================================================
740
741 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
742 {
743   if (!myLayerIds.Contains (theLayerId)
744     || theLayerId < myZLayerGenId.Lower()
745     || theLayerId > myZLayerGenId.Upper())
746   {
747     return Standard_False;
748   }
749
750   // tell all managed views to remove display layers
751   for(int i=1; i<=MyDefinedView.Length(); i++)
752   {
753     (MyDefinedView.Value(i))->RemoveZLayer (theLayerId);
754   }
755
756   MyGraphicDriver->UnsetZLayer (theLayerId);
757
758   // remove index
759   for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
760   {
761     if (myLayerSeq (aIdx) == theLayerId)
762     {
763       myLayerSeq.Remove (aIdx);
764       break;
765     }
766   }
767
768   myMapOfZLayerSettings.UnBind (theLayerId);
769
770   myLayerIds.Remove  (theLayerId);
771   myZLayerGenId.Free (theLayerId);
772
773   return Standard_True;
774 }
775
776 //=======================================================================
777 //function : GetAllZLayers
778 //purpose  :
779 //=======================================================================
780
781 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
782 {
783   theLayerSeq.Assign (myLayerSeq);
784 }
785
786 //=======================================================================
787 //function : InstallZLayers
788 //purpose  :
789 //=======================================================================
790
791 void Visual3d_ViewManager::InstallZLayers(const Handle(Visual3d_View)& theView) const
792 {
793   Standard_Boolean isContainsView = Standard_False;
794   for(int i=1; i<=MyDefinedView.Length(); i++)
795   {
796     if(MyDefinedView.Value(i) == theView)
797     {
798       isContainsView = Standard_True;
799       break;
800     }
801   }
802   if (!isContainsView)
803     return;
804
805   // erase and insert layers iteratively to provide the same layer order as
806   // in the view manager's sequence. This approach bases on the layer insertion
807   // order: the new layers are always appended to the end of the list
808   // inside of view, while layer remove operation doesn't affect the order.
809   // Starting from second layer : no need to change the default z layer.
810   for (Standard_Integer aSeqIdx = 1; aSeqIdx <= myLayerSeq.Length(); ++aSeqIdx)
811   {
812     const Graphic3d_ZLayerId        aLayerID  = myLayerSeq.Value (aSeqIdx);
813     const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
814     if (aLayerID < myZLayerGenId.Lower()
815      || aLayerID > myZLayerGenId.Upper())
816     {
817       theView->SetZLayerSettings (aLayerID, aSettings);
818       continue;
819     }
820
821     theView->RemoveZLayer      (aLayerID);
822     theView->AddZLayer         (aLayerID);
823     theView->SetZLayerSettings (aLayerID, aSettings);
824   }
825 }