4a9a3a1a23d0e908e3f6744ff1dd191b5e3c19c3
[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 #include <Visual3d_ViewManager.pxx>
60
61 #include <Standard_ErrorHandler.hxx>
62
63 #include <Aspect.hxx>
64 #include <Aspect_IdentDefinitionError.hxx>
65
66 #include <Graphic3d_GraphicDriver.hxx>
67 #include <Graphic3d_MapOfStructure.hxx>
68 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
69
70 #include <Visual3d_PickPath.hxx>
71 #include <Visual3d_SetIteratorOfSetOfView.hxx>
72
73 #if defined (_WIN32) || defined(__WIN32__)
74 # include <WNT_Window.hxx>
75 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
76 # include <Cocoa_Window.hxx>
77 #else
78 # include <Xw_Window.hxx>
79 #endif
80
81 //-Aliases
82
83 //-Global data definitions
84
85 //      -- les vues definies
86 //      MyDefinedView           :       SetOfView;
87
88 //      -- le generateur d'identificateurs de vues
89 //      MyViewGenId             :       GenId;
90
91 //-Constructors
92
93 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Graphic3d_GraphicDriver)& theDriver):
94 Graphic3d_StructureManager (theDriver),
95 MyDefinedView (),
96 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),
97 MyZBufferAuto (Standard_False),
98 MyTransparency (Standard_False)
99 {
100   // default layer is always presented in display layer sequence
101   // it can not be removed
102   myLayerIds.Add (0);
103   myLayerSeq.Append (0);
104
105   MyGraphicDriver = theDriver;
106 }
107
108 //-Destructors
109
110 void Visual3d_ViewManager::Destroy () {
111
112 #ifdef DESTROY
113         cout << "Visual3d_ViewManager::Destroy (" << MyId << ")\n" << flush;
114 #endif
115
116         Remove ();
117 }
118
119 //-Methods, in order
120
121 void Visual3d_ViewManager::Remove () {
122
123 #ifdef DESTROY
124         cout << "Visual3d_ViewManager::Remove (" << MyId << ")\n" << flush;
125 #endif
126
127         //
128         // Destroy all defined views
129         //
130
131 #ifdef DESTROY
132         cout << "The Manager " << MyId << " have " << Length << " defined views\n";
133         cout << flush;
134 #endif
135
136   // clear all structures whilst views are alive for correct GPU memory management
137   MyDisplayedStructure.Clear();
138   MyHighlightedStructure.Clear();
139   MyPickStructure.Clear();
140
141   // clear list of managed views
142   MyDefinedView.Clear();
143 }
144
145 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority) {
146
147 #ifdef TRACE
148         cout << "Visual3d_ViewManager::ChangeDisplayPriority ("
149              << AStructure->Identification ()
150              << ", " << OldPriority << ", " << NewPriority << ")\n";
151         cout << flush;
152 #endif
153
154         //
155         // Change structure priority in all defined views
156         //
157         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
158
159         while (MyIterator.More ()) {
160                 (MyIterator.Value ())->ChangeDisplayPriority
161                         (AStructure, OldPriority, NewPriority);
162
163                 // MyIterator.Next () is located on the next view
164                 MyIterator.Next ();
165         }
166
167 }
168
169 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
170
171   //Standard_Integer LengthD    = MyDisplayedStructure.Extent() ();
172
173   // Even if physically the structure cannot
174   // be displayed (pb of visualisation type)
175   // it has status Displayed.
176
177   if (!MyDisplayedStructure.Contains(AStructure))
178     return;
179
180   //
181   // Recompute structure in all activated views
182   //
183   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
184
185   while (MyIterator.More ()) {
186     (MyIterator.Value ())->ReCompute (AStructure);
187
188     // MyIterator.Next () is located on the next view
189     MyIterator.Next ();
190   }
191
192 }
193
194 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure,
195                                       const Handle(Graphic3d_DataStructureManager)& AProjector)
196 {
197
198   if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
199
200 #ifdef DOWNCAST
201   Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
202 #else
203   Handle(Visual3d_View) theView = *(Handle(Visual3d_View) *) &AProjector;
204 #endif
205   Standard_Integer ViewId = theView->Identification ();
206
207   // Even if physically the structure cannot
208   // be displayed (pb of visualisation type)
209   // it has status Displayed.
210   if (!MyDisplayedStructure.Contains(AStructure))
211     return;
212
213   //
214   // Recompute structure in all activated views
215   //
216   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
217
218   while (MyIterator.More ()) {
219     if ((MyIterator.Value ())->Identification () == ViewId)
220       theView->ReCompute (AStructure);
221
222     // MyIterator.Next () is located on the next view
223     MyIterator.Next ();
224   }
225
226 }
227
228 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction) {
229
230         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
231
232         while (MyIterator.More ()) {
233                 (MyIterator.Value ())->Clear (AStructure, WithDestruction);
234
235                 // MyIterator.Next ()  is located on the next view
236                 MyIterator.Next ();
237         }
238
239 }
240
241 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
242
243         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
244
245         while (MyIterator.More ()) {
246                 (MyIterator.Value ())->Connect (AMother, ADaughter);
247
248                 // MyIterator.Next ()  is located on the next view
249                 MyIterator.Next ();
250         }
251
252 }
253
254 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
255
256         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
257
258         while (MyIterator.More ()) {
259                 (MyIterator.Value ())->Disconnect (AMother, ADaughter);
260
261                 // MyIterator.Next ()  is located on the next view
262                 MyIterator.Next ();
263         }
264
265 }
266
267 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure) {
268
269
270  // Even if physically the structure cannot
271   // be displayed (pb of visualisation type)
272   // it has status Displayed.
273
274   MyDisplayedStructure.Add(AStructure);
275
276         //
277         // Display structure in all activated views
278         //
279         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
280
281         while (MyIterator.More ()) {
282                 (MyIterator.Value ())->Display (AStructure);
283
284                 // MyIterator.Next () is located on the next view
285                 MyIterator.Next ();
286         }
287
288 }
289
290 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure) {
291
292
293 // Even if physically the structure cannot
294   // be displayed (pb of visualisation type)
295   // it has status Displayed.
296
297  MyDisplayedStructure.Remove(AStructure);
298
299
300
301         //
302         // Erase structure in all defined views
303         //
304         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
305
306         while (MyIterator.More ()) {
307                 (MyIterator.Value ())->Erase (AStructure);
308
309                 // MyIterator.Next () is located on the next view
310                 MyIterator.Next ();
311         }
312
313         MyHighlightedStructure.Remove (AStructure);
314         MyPickStructure.Remove (AStructure);
315
316 }
317
318 void Visual3d_ViewManager::Erase () {
319
320  Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
321
322  for (; it.More(); it.Next()) {
323    Handle(Graphic3d_Structure) SG = it.Key();
324    SG->Erase();
325  }
326
327 }
328
329 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod) {
330
331   MyHighlightedStructure.Add(AStructure);
332
333         //
334         // Highlight in all activated views
335         //
336         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
337
338         while (MyIterator.More ()) {
339                 (MyIterator.Value ())->Highlight (AStructure, AMethod);
340
341                 // MyIterator.Next () is located on the next view
342                 MyIterator.Next ();
343         }
344
345 }
346
347 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf) {
348
349         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
350
351         while (MyIterator.More ()) {
352                 (MyIterator.Value ())->SetTransform (AStructure, ATrsf);
353
354                 // MyIterator.Next () is located on the next view
355                 MyIterator.Next ();
356         }
357
358 }
359
360 void Visual3d_ViewManager::UnHighlight () {
361
362   Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
363
364   for (; it.More(); it.Next()) {
365     Handle(Graphic3d_Structure) SG = it.Key();
366     SG->UnHighlight ();
367   }
368
369
370 }
371
372 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure) {
373
374   MyHighlightedStructure.Remove(AStructure);
375
376
377         //
378         // UnHighlight in all activated views
379         //
380         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
381
382         while (MyIterator.More ()) {
383                 (MyIterator.Value ())->UnHighlight (AStructure);
384
385                 // MyIterator.Next () is located on the next view
386                 MyIterator.Next ();
387         }
388
389 }
390
391 void Visual3d_ViewManager::Redraw () const {
392
393 Standard_Integer MaxDx, MaxDy;
394 Standard_Integer Dx, Dy;
395         MaxDx = MaxDy = IntegerFirst ();
396
397         //
398         // Redraw all activated views
399         //
400         Standard_Integer j = MyDefinedView.Extent ();
401         if (j == 0) return;
402         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
403
404         if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ()) {
405             while (MyIterator.More ()) {
406                 (MyIterator.Value ())->Window ()->Size (Dx, Dy);
407                 if (Dx > MaxDx) MaxDx = Dx;
408                 if (Dy > MaxDy) MaxDy = Dy;
409
410                 // MyIterator.Next () is located on the next view
411                 MyIterator.Next ();
412             }
413             if (! MyUnderLayer.IsNull ())
414                 MyUnderLayer->SetViewport (MaxDx, MaxDy);
415             if (! MyOverLayer.IsNull ())
416                 MyOverLayer->SetViewport (MaxDx, MaxDy);
417         }
418
419         if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ())
420             MyIterator.Initialize (MyDefinedView);
421         while (MyIterator.More ()) {
422             (MyIterator.Value ())->Redraw (MyUnderLayer, MyOverLayer);
423
424             // MyIterator.Next () is located on the next view
425             MyIterator.Next ();
426         }
427
428 }
429
430 void Visual3d_ViewManager::Update () const {
431
432         //
433         // Update all activated views
434         //
435         Standard_Integer j = MyDefinedView.Extent ();
436         if (j == 0) return;
437         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
438
439         while (MyIterator.More ()) {
440                 (MyIterator.Value ())->Update (MyUnderLayer, MyOverLayer);
441
442                 // MyIterator.Next () is located on the next view
443                 MyIterator.Next ();
444         }
445
446 }
447
448 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::ActivatedView () const {
449
450 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
451
452 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
453
454         while (MyIterator.More ()) {
455                 if ((MyIterator.Value ())->IsActive ())
456                         SG->Add (MyIterator.Value ());
457
458                 // MyIterator.Next () is located on the next view
459                 MyIterator.Next ();
460         }
461
462         return (SG);
463
464 }
465
466 #ifdef IMPLEMENTED
467 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const {
468
469 Standard_Boolean Result = Standard_False;
470
471         //
472         // Check all activated views
473         //
474         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
475
476         Standard_Integer i      = MyDefinedView.Extent ();
477
478         while ((! Result) && (MyIterator.More ())) {
479                 if ((MyIterator.Value ())->IsActive ())
480                         Result  =
481                         (MyIterator.Value ())->ContainsComputedStructure ();
482
483                 // MyIterator.Next () is located on the next view
484                 MyIterator.Next ();
485         }
486
487         return Result;
488 }
489 #endif
490
491 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::DefinedView () const {
492
493 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
494
495 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
496
497         while (MyIterator.More ()) {
498                 SG->Add (MyIterator.Value ());
499
500                 // MyIterator.Next () is located on the next view
501                 MyIterator.Next ();
502         }
503
504         return (SG);
505
506 }
507
508 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const {
509
510 Standard_Boolean Exist = Standard_False;
511
512         // Parse the list of views to find
513         // a view with the specified window
514         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
515
516 #if defined(_WIN32) || defined(__WIN32__)
517   const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
518   Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
519 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
520   const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
521   NSView* TheSpecifiedWindowId = THEWindow->HView();
522 #else
523   const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
524   int TheSpecifiedWindowId = int (THEWindow->XWindow ());
525 #endif
526
527         while ((! Exist) && (MyIterator.More ())) {
528
529            if ( ((MyIterator.Value ())->IsDefined ()) &&
530                 ((MyIterator.Value ())->IsActive ()) ) {
531
532 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
533 #if defined(_WIN32) || defined(__WIN32__)
534    const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
535    Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
536 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
537    const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
538    NSView* TheWindowIdOfView = theWindow->HView();
539 #else
540    const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
541    int TheWindowIdOfView = int (theWindow->XWindow ());
542 #endif  // WNT
543                 // Comparaison on window IDs
544                 if (TheWindowIdOfView == TheSpecifiedWindowId) {
545                         Exist   = Standard_True;
546                         TheCView        = *(Graphic3d_CView* )(MyIterator.Value())->CView();
547                 }
548            } /* if ((MyIterator.Value ())->IsDefined ()) */
549
550            // MyIterator.Next () is located on the next view
551            MyIterator.Next ();
552         }
553
554         return (Exist);
555
556 }
557
558 void Visual3d_ViewManager::Activate () {
559
560         //
561         // Activates all deactivated views
562         //
563         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
564
565         while (MyIterator.More ()) {
566                 if (! (MyIterator.Value ())->IsActive ())
567                         (MyIterator.Value ())->Activate ();
568
569                 // MyIterator.Next () is located on the next view
570                 MyIterator.Next ();
571         }
572
573 }
574
575 void Visual3d_ViewManager::Deactivate () {
576
577         //
578         // Deactivates all activated views
579         //
580         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
581
582         while (MyIterator.More ()) {
583                 if ((MyIterator.Value ())->IsActive ())
584                         (MyIterator.Value ())->Deactivate ();
585
586                 // MyIterator.Next () is located on the next view
587                 MyIterator.Next ();
588         }
589
590 }
591
592 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
593
594         // Retourne the planned of definable views for the current
595         // Visual3d_ViewManager.
596         return
597 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
598
599 }
600
601 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
602
603         return (Graphic3d_StructureManager::Identification (AId));
604
605 }
606
607 Standard_Integer Visual3d_ViewManager::Identification () const {
608
609         return (Graphic3d_StructureManager::Identification ());
610
611 }
612
613 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView) {
614
615         MyDefinedView.Add (AView);
616         return (MyViewGenId.Next ());
617
618 }
619
620 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
621 {
622   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
623   while (MyIterator.More()) 
624   {
625     if ((MyIterator.Value())->Identification () == aViewId)
626     {
627       const Handle(Visual3d_View)& theView = MyIterator.Value();
628       //remove the view from the list
629       MyDefinedView.Remove(theView);
630       break;
631     }
632     // go to next
633     MyIterator.Next ();
634   }
635   MyViewGenId.Free(aViewId);
636 }
637
638 void Visual3d_ViewManager::SetTransparency (const Standard_Boolean AFlag) {
639
640         if (MyTransparency && AFlag) return;
641         if (! MyTransparency && ! AFlag) return;
642
643         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
644         while (MyIterator.More ()) {
645                 (MyIterator.Value ())->SetTransparency (AFlag);
646                 // MyIterator.Next () is located on the next view
647                 MyIterator.Next ();
648         }
649
650         MyTransparency  = AFlag;
651
652 }
653
654 Standard_Boolean Visual3d_ViewManager::Transparency () const {
655
656         return (MyTransparency);
657
658 }
659
660 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag) {
661
662         if (MyZBufferAuto && AFlag) return;
663         if (! MyZBufferAuto && ! AFlag) return;
664
665         // if pass from False to True :
666         // no problem, at the next view update, it
667         // will properly ask questions to answer (SetVisualisation)
668         // if pass from True to False :
669         // it is necessary to modify ZBufferActivity at each view so that
670         // zbuffer could be active only if required by context.
671         // In this case -1 is passed so that the view ask itself the question
672         // Note : 0 forces the desactivation, 1 forces the activation
673         if (! AFlag) {
674                 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
675                 while (MyIterator.More ()) {
676                         (MyIterator.Value ())->SetZBufferActivity (-1);
677                         // MyIterator.Next () is located on the next view
678                         MyIterator.Next ();
679                 }
680         }
681         MyZBufferAuto   = AFlag;
682
683 }
684
685 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const {
686
687         return (MyZBufferAuto);
688
689 }
690
691 void Visual3d_ViewManager::SetLayer (const Handle(Visual3d_Layer)& ALayer) {
692
693 #ifdef TRACE_LAYER
694         cout << "Visual3d_ViewManager::SetLayer\n" << flush;
695 #endif
696
697         if (ALayer->Type () == Aspect_TOL_OVERLAY) {
698 #ifdef TRACE_LAYER
699                 if (MyOverLayer.IsNull ())
700                         cout << "MyOverLayer is defined" << endl;
701                 else
702                         cout << "MyOverLayer is redefined" << endl;
703 #endif
704                 MyOverLayer = ALayer;
705         }
706         else {
707 #ifdef TRACE_LAYER
708                 if (MyUnderLayer.IsNull ())
709                         cout << "MyUnderLayer is defined" << endl;
710                 else
711                         cout << "MyUnderLayer is redefined" << endl;
712 #endif
713                 MyUnderLayer = ALayer;
714         }
715
716 }
717
718 const Handle(Visual3d_Layer)& Visual3d_ViewManager::UnderLayer () const {
719
720         return (MyUnderLayer);
721
722 }
723
724 const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
725
726         return (MyOverLayer);
727
728 }
729
730 //=======================================================================
731 //function : ChangeZLayer
732 //purpose  :
733 //=======================================================================
734
735 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
736                                          const Standard_Integer theLayerId)
737 {
738   if (!myLayerIds.Contains (theLayerId))
739     return;
740
741   // change display layer for structure in all views
742   if (MyDisplayedStructure.Contains (theStructure))
743   {
744     Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
745     for ( ; aViewIt.More (); aViewIt.Next ())
746       (aViewIt.Value ())->ChangeZLayer (theStructure, theLayerId);
747   }
748
749   // tell graphic driver to update the structure's display layer
750   MyGraphicDriver->ChangeZLayer (*(theStructure->CStructure()), theLayerId);
751 }
752
753 //=======================================================================
754 //function : GetZLayer
755 //purpose  :
756 //=======================================================================
757
758 Standard_Integer Visual3d_ViewManager::GetZLayer (const Handle(Graphic3d_Structure)& theStructure) const
759 {
760   return MyGraphicDriver->GetZLayer (*theStructure->CStructure ());
761 }
762
763 //=======================================================================
764 //function : AddZLayer
765 //purpose  :
766 //=======================================================================
767
768 Standard_Boolean Visual3d_ViewManager::AddZLayer (Standard_Integer& theLayerId)
769 {
770   try
771   {
772     OCC_CATCH_SIGNALS
773     theLayerId = getZLayerGenId ().Next ();
774     myLayerIds.Add (theLayerId);
775     myLayerSeq.Append (theLayerId);
776   }
777   catch (Aspect_IdentDefinitionError)
778   {
779     // new index can't be generated
780     return Standard_False;
781   }
782
783   // tell all managed views to remove display layers
784   Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
785   for ( ; aViewIt.More (); aViewIt.Next ())
786     (aViewIt.Value ())->AddZLayer (theLayerId);
787
788   return Standard_True;
789 }
790
791 //=======================================================================
792 //function : RemoveZLayer
793 //purpose  :
794 //=======================================================================
795
796 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Standard_Integer theLayerId)
797 {
798   if (!myLayerIds.Contains (theLayerId) || theLayerId == 0)
799     return Standard_False;
800
801   // tell all managed views to remove display layers
802   Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
803   for ( ; aViewIt.More (); aViewIt.Next ())
804     (aViewIt.Value ())->RemoveZLayer (theLayerId);
805
806   MyGraphicDriver->UnsetZLayer (theLayerId);
807
808   // remove index
809   for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
810     if (myLayerSeq(aIdx) == theLayerId)
811     {
812       myLayerSeq.Remove (aIdx);
813       break;
814     }
815
816   myLayerIds.Remove (theLayerId);
817   getZLayerGenId ().Free (theLayerId);
818
819   return Standard_True;
820 }
821
822 //=======================================================================
823 //function : GetAllZLayers
824 //purpose  :
825 //=======================================================================
826
827 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
828 {
829   theLayerSeq.Assign (myLayerSeq);
830 }
831
832 //=======================================================================
833 //function : getZLayerGenId
834 //purpose  :
835 //=======================================================================
836
837 Aspect_GenId& Visual3d_ViewManager::getZLayerGenId ()
838 {
839   static Aspect_GenId aGenId (1, IntegerLast());
840   return aGenId;
841 }
842
843 //=======================================================================
844 //function : InstallZLayers
845 //purpose  :
846 //=======================================================================
847
848 void Visual3d_ViewManager::InstallZLayers(const Handle(Visual3d_View)& theView) const
849 {
850   if (!MyDefinedView.Contains (theView))
851     return;
852
853   // erase and insert layers iteratively to provide the same layer order as
854   // in the view manager's sequence. This approach bases on the layer insertion
855   // order: the new layers are always appended to the end of the list
856   // inside of view, while layer remove operation doesn't affect the order.
857   // Starting from second layer : no need to change the default z layer.
858   for (Standard_Integer aSeqIdx = 2; aSeqIdx <= myLayerSeq.Length (); aSeqIdx++)
859   {
860     Standard_Integer aLayerID = myLayerSeq.Value (aSeqIdx);
861     theView->RemoveZLayer (aLayerID);
862     theView->AddZLayer (aLayerID);
863   }
864 }