0022795: Make possible to display some presentable objects in overlay of others,...
[occt.git] / src / Visual3d / Visual3d_ViewManager.cxx
1
2 /***********************************************************************
3
4      FONCTION :
5      ----------
6         Classe Visual3d_ViewManager.cxx :
7
8         Declaration of variables specific to visualisers
9
10      HISTORIQUE DES MODIFICATIONS   :
11      --------------------------------
12       Mars 1992 : NW,JPB,CAL ; Creation.
13       19-06-96  : FMN ; Suppression variables inutiles
14       04-02-97  : FMN ; Suppression de PSOutput, XWDOutput ...
15       06-05-97  : CAL ; Ajout du Clear sur les TOS_COMPUTED.
16       19-09-97  : CAL ; Remplacement de Window->Position par Window->Size;
17       24-10-97  : CAL ; Retrait de DownCast.
18       20-11-97  : CAL ; Disparition de la dependance avec math
19       01-12-97  : CAL ; Retrait du test IsActive sur l'Update et le Redraw
20       31-12-97  : CAL ; Disparition de MathGra 
21       16-01-98  : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
22       11-03-98  : CAL ; Visual3d_ViewManager::Remove ()
23       20-05-98  : CAL ; Perfs. Connection entre structures COMPUTED.
24       10-06-98  : CAL ; Modification des signatures de xxProjectRaster.
25       10-06-98  : CAL ; Modification de la signature de ViewExists.
26       01-12-98  : CAL ; S4062. Ajout des layers.
27       02-12-98  : CAL ; Remove () ne detruit plus les vues.
28
29 ************************************************************************/
30
31 /*----------------------------------------------------------------------*/
32 /*
33  * Constants
34  */
35
36 #define NO_DOWNCAST
37 #define NO_DESTROY
38
39 /*----------------------------------------------------------------------*/
40 /*
41  * Includes
42  */
43
44 // for the class
45 #include <Visual3d_ViewManager.ixx>
46 #include <Visual3d_ViewManager.pxx>
47
48 #include <Standard_ErrorHandler.hxx>
49
50 #include <Aspect.hxx>
51 #include <Aspect_IdentDefinitionError.hxx>
52
53 #include <Graphic3d_GraphicDriver.hxx>
54 #include <Graphic3d_MapOfStructure.hxx>
55 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
56
57 #include <Visual3d_PickPath.hxx>
58 #include <Visual3d_SetIteratorOfSetOfView.hxx>
59
60 #ifndef WNT
61 # include <Xw_Window.hxx>
62 #else
63 # include <WNT_Window.hxx>
64 #endif  // WNT
65
66 //-Aliases
67
68 //-Global data definitions
69
70 //      -- les vues definies
71 //      MyDefinedView           :       SetOfView;
72
73 //      -- le generateur d'identificateurs de vues
74 //      MyViewGenId             :       GenId;
75
76 //-Constructors
77
78 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Aspect_GraphicDevice)& aDevice):
79 Graphic3d_StructureManager (aDevice),
80 MyDefinedView (),
81 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),
82 MyZBufferAuto (Standard_False),
83 MyTransparency (Standard_False)
84 {
85   // default layer is always presented in display layer sequence
86   // it can not be removed
87   myLayerIds.Add (0);
88   myLayerSeq.Append (0);
89
90   Handle(Aspect_GraphicDriver) agd = aDevice->GraphicDriver ();
91
92   MyGraphicDriver = *(Handle(Graphic3d_GraphicDriver) *) &agd;
93 }
94
95 //-Destructors
96
97 void Visual3d_ViewManager::Destroy () {
98
99 #ifdef DESTROY
100         cout << "Visual3d_ViewManager::Destroy (" << MyId << ")\n" << flush;
101 #endif
102
103         Remove ();
104 }
105
106 //-Methods, in order
107
108 void Visual3d_ViewManager::Remove () {
109
110 #ifdef DESTROY
111         cout << "Visual3d_ViewManager::Remove (" << MyId << ")\n" << flush;
112 #endif
113
114         //
115         // Destroy all defined views
116         //
117 #ifdef DEB
118         Standard_Integer Length = MyDefinedView.Extent ();
119 #else
120         MyDefinedView.Extent ();
121 #endif
122
123 #ifdef DESTROY
124         cout << "The Manager " << MyId << " have " << Length << " defined views\n";
125         cout << flush;
126 #endif
127  
128         MyDefinedView.Clear ();
129
130 }
131
132 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority) {
133
134 #ifdef TRACE
135         cout << "Visual3d_ViewManager::ChangeDisplayPriority ("
136              << AStructure->Identification ()
137              << ", " << OldPriority << ", " << NewPriority << ")\n";
138         cout << flush;
139 #endif
140
141         //
142         // Change structure priority in all defined views
143         //
144 #ifdef DEB
145         Standard_Integer Length = MyDefinedView.Extent ();
146 #else
147         MyDefinedView.Extent ();
148 #endif
149         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
150  
151         while (MyIterator.More ()) {
152                 (MyIterator.Value ())->ChangeDisplayPriority
153                         (AStructure, OldPriority, NewPriority);
154
155                 // MyIterator.Next () is located on the next view
156                 MyIterator.Next ();
157         }
158
159 }
160
161 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) { 
162
163   //Standard_Integer LengthD    = MyDisplayedStructure.Extent() ();
164
165   // Even if physically the structure cannot
166   // be displayed (pb of visualisation type)
167   // it has status Displayed.
168  
169   if (!MyDisplayedStructure.Contains(AStructure))
170     return;
171
172   //
173   // Recompute structure in all activated views
174   //
175   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
176  
177   while (MyIterator.More ()) {
178     (MyIterator.Value ())->ReCompute (AStructure);
179
180     // MyIterator.Next () is located on the next view
181     MyIterator.Next ();
182   }
183   
184 }
185
186 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure, 
187                                       const Handle(Graphic3d_DataStructureManager)& AProjector) 
188
189
190   if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
191
192 #ifdef DOWNCAST
193   Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
194 #else
195   Handle(Visual3d_View) theView = *(Handle(Visual3d_View) *) &AProjector;
196 #endif
197   Standard_Integer ViewId = theView->Identification ();
198
199   Standard_Integer indexD = 0;
200
201   // Even if physically the structure cannot
202   // be displayed (pb of visualisation type)
203   // it has status Displayed.
204   if (!MyDisplayedStructure.Contains(AStructure))
205     return;
206         
207   //
208   // Recompute structure in all activated views
209   //
210   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
211  
212   while (MyIterator.More ()) {
213     if ((MyIterator.Value ())->Identification () == ViewId)
214       theView->ReCompute (AStructure);
215
216     // MyIterator.Next () is located on the next view
217     MyIterator.Next ();
218   }
219
220 }
221
222 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction) {
223
224 #ifdef DEB
225         Standard_Integer Length = MyDefinedView.Extent ();
226 #else
227         MyDefinedView.Extent ();
228 #endif
229         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
230  
231         while (MyIterator.More ()) {
232                 (MyIterator.Value ())->Clear (AStructure, WithDestruction);
233
234                 // MyIterator.Next ()  is located on the next view
235                 MyIterator.Next ();
236         }
237
238 }
239
240 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
241
242 #ifdef DEB
243         Standard_Integer Length = MyDefinedView.Extent ();
244 #else
245         MyDefinedView.Extent ();
246 #endif
247         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
248  
249         while (MyIterator.More ()) {
250                 (MyIterator.Value ())->Connect (AMother, ADaughter);
251
252                 // MyIterator.Next ()  is located on the next view
253                 MyIterator.Next ();
254         }
255
256 }
257
258 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
259
260 #ifdef DEB
261         Standard_Integer Length = MyDefinedView.Extent ();
262 #else
263         MyDefinedView.Extent ();
264 #endif
265         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
266  
267         while (MyIterator.More ()) {
268                 (MyIterator.Value ())->Disconnect (AMother, ADaughter);
269
270                 // MyIterator.Next ()  is located on the next view
271                 MyIterator.Next ();
272         }
273
274 }
275
276 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure) { 
277
278
279  // Even if physically the structure cannot
280   // be displayed (pb of visualisation type)
281   // it has status Displayed.
282
283   MyDisplayedStructure.Add(AStructure);
284
285         //
286         // Display structure in all activated views
287         //
288         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
289  
290         while (MyIterator.More ()) {
291                 (MyIterator.Value ())->Display (AStructure);
292
293                 // MyIterator.Next () is located on the next view
294                 MyIterator.Next ();
295         }
296
297 }
298
299 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure) {
300
301
302 // Even if physically the structure cannot
303   // be displayed (pb of visualisation type)
304   // it has status Displayed.
305
306  MyDisplayedStructure.Remove(AStructure);
307
308
309
310         //
311         // Erase structure in all defined views
312         //
313 #ifdef DEB
314         Standard_Integer Length = MyDefinedView.Extent ();
315 #else
316         MyDefinedView.Extent ();
317 #endif
318         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
319  
320         while (MyIterator.More ()) {
321                 (MyIterator.Value ())->Erase (AStructure);
322
323                 // MyIterator.Next () is located on the next view
324                 MyIterator.Next ();
325         }
326
327         MyHighlightedStructure.Remove (AStructure);
328         MyVisibleStructure.Remove (AStructure);
329         MyPickStructure.Remove (AStructure);
330
331 }
332
333 void Visual3d_ViewManager::Erase () {
334
335  Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
336  
337  for (; it.More(); it.Next()) {
338    Handle(Graphic3d_Structure) SG = it.Key();
339    SG->Erase();
340  }
341
342 }
343
344 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod) {
345
346   MyHighlightedStructure.Add(AStructure);
347
348         //
349         // Highlight in all activated views
350         //
351 #ifdef DEB
352         Standard_Integer Length = MyDefinedView.Extent ();
353 #else
354         MyDefinedView.Extent ();
355 #endif
356         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
357  
358         while (MyIterator.More ()) {
359                 (MyIterator.Value ())->Highlight (AStructure, AMethod);
360
361                 // MyIterator.Next () is located on the next view
362                 MyIterator.Next ();
363         }
364
365 }
366
367 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf) {
368
369 #ifdef DEB
370         Standard_Integer Length = MyDefinedView.Extent ();
371 #else
372         MyDefinedView.Extent ();
373 #endif
374         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
375  
376         while (MyIterator.More ()) {
377                 (MyIterator.Value ())->SetTransform (AStructure, ATrsf);
378
379                 // MyIterator.Next () is located on the next view
380                 MyIterator.Next ();
381         }
382
383 }
384
385 void Visual3d_ViewManager::UnHighlight () {
386
387   Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
388   
389   for (; it.More(); it.Next()) {
390     Handle(Graphic3d_Structure) SG = it.Key();
391     SG->UnHighlight ();
392   }
393
394
395 }
396
397 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure) {
398
399   MyHighlightedStructure.Remove(AStructure);
400
401
402         //
403         // UnHighlight in all activated views
404         //
405 #ifdef DEB
406         Standard_Integer Length = MyDefinedView.Extent ();
407 #else
408         MyDefinedView.Extent ();
409 #endif
410         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
411  
412         while (MyIterator.More ()) {
413                 (MyIterator.Value ())->UnHighlight (AStructure);
414
415                 // MyIterator.Next () is located on the next view
416                 MyIterator.Next ();
417         }
418
419 }
420
421 void Visual3d_ViewManager::Redraw () const {
422
423 Standard_Integer MaxDx, MaxDy;
424 Standard_Integer Dx, Dy;
425         MaxDx = MaxDy = IntegerFirst ();
426
427         //
428         // Redraw all activated views
429         //
430         Standard_Integer j = MyDefinedView.Extent ();
431         if (j == 0) return;
432         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
433  
434         if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ()) {
435             while (MyIterator.More ()) {
436                 (MyIterator.Value ())->Window ()->Size (Dx, Dy);
437                 if (Dx > MaxDx) MaxDx = Dx;
438                 if (Dy > MaxDy) MaxDy = Dy;
439
440                 // MyIterator.Next () is located on the next view
441                 MyIterator.Next ();
442             }
443             if (! MyUnderLayer.IsNull ())
444                 MyUnderLayer->SetViewport (MaxDx, MaxDy);
445             if (! MyOverLayer.IsNull ())
446                 MyOverLayer->SetViewport (MaxDx, MaxDy);
447         }
448  
449         if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ())
450             MyIterator.Initialize (MyDefinedView);
451         while (MyIterator.More ()) {
452             (MyIterator.Value ())->Redraw (MyUnderLayer, MyOverLayer);
453
454             // MyIterator.Next () is located on the next view
455             MyIterator.Next ();
456         }
457
458 }
459
460 void Visual3d_ViewManager::Update () const {
461
462         //
463         // Update all activated views
464         //
465         Standard_Integer j = MyDefinedView.Extent ();
466         if (j == 0) return;
467         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
468  
469         while (MyIterator.More ()) {
470                 (MyIterator.Value ())->Update (MyUnderLayer, MyOverLayer);
471
472                 // MyIterator.Next () is located on the next view
473                 MyIterator.Next ();
474         }
475
476 }
477
478 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::ActivatedView () const {
479
480 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
481
482 #ifdef DEB
483         Standard_Integer Length = MyDefinedView.Extent ();
484 #else
485         MyDefinedView.Extent ();
486 #endif
487 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
488
489         while (MyIterator.More ()) {
490                 if ((MyIterator.Value ())->IsActive ())
491                         SG->Add (MyIterator.Value ());
492
493                 // MyIterator.Next () is located on the next view
494                 MyIterator.Next ();
495         }
496
497         return (SG);
498
499 }
500
501 #ifdef IMPLEMENTED
502 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const {
503
504 Standard_Boolean Result = Standard_False;
505
506         //
507         // Check all activated views
508         //
509         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
510
511         Standard_Integer i      = MyDefinedView.Extent ();
512
513         while ((! Result) && (MyIterator.More ())) {
514                 if ((MyIterator.Value ())->IsActive ())
515                         Result  =
516                         (MyIterator.Value ())->ContainsComputedStructure ();
517
518                 // MyIterator.Next () is located on the next view
519                 MyIterator.Next ();
520         }
521
522         return Result;
523 }
524 #endif
525
526 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::DefinedView () const {
527
528 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
529
530 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
531
532         while (MyIterator.More ()) {
533                 SG->Add (MyIterator.Value ());
534
535                 // MyIterator.Next () is located on the next view
536                 MyIterator.Next ();
537         }
538
539         return (SG);
540
541 }
542
543 void Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)& AWindow, const Graphic3d_Vertex& AVertex, Standard_Integer& AU, Standard_Integer& AV) const {
544
545 // Convert only if the data is correct
546 Standard_Boolean Exist;
547 Graphic3d_CView  TheCView;
548 //Graphic3d_Vertex Point;
549
550 TColStd_Array2OfReal Ori_Matrix (0,3,0,3);
551 TColStd_Array2OfReal Map_Matrix (0,3,0,3);
552
553 Standard_Integer Width, Height;
554 Standard_Real AX, AY, AZ;
555 Standard_Real Dx, Dy, Ratio;
556
557         Exist   = ViewExists (AWindow, TheCView);
558
559         if (! Exist) {
560                 AU = AV = IntegerLast ();
561         }
562         else {
563     // NKV - 11.02.08 - Use graphic driver functions
564     Standard_Boolean Result;
565
566     AVertex.Coord (AX, AY, AZ);
567
568     Result = MyGraphicDriver->ProjectRaster (TheCView, 
569       Standard_ShortReal (AX), Standard_ShortReal (AY), Standard_ShortReal (AZ),
570       AU, AV);
571
572     // the old code
573     if (!Result) {
574
575       Standard_Real PtX, PtY, PtZ, PtT;
576       Standard_Real APX, APY, APZ;
577       Standard_Real APT;
578
579       Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
580 #ifdef DEB
581       Standard_Integer Length = MyDefinedView.Extent ();
582 #else
583       MyDefinedView.Extent ();
584 #endif
585
586       Standard_Integer stop = 0;
587
588       while ((! stop) && (MyIterator.More ())) {
589         if (TheCView.ViewId ==
590             (MyIterator.Value ())->Identification ()) {
591           Ori_Matrix    =
592             (MyIterator.Value ())->MatrixOfOrientation ();
593           Map_Matrix    =
594             (MyIterator.Value ())->MatrixOfMapping ();
595           stop  = 1;
596         }
597
598         // MyIterator.Next () is located on the next view
599         MyIterator.Next ();
600       }
601
602
603       // WCS -> View Reference Coordinate Space
604       PtX       = Ori_Matrix (0, 0) * AX
605                 + Ori_Matrix (0, 1) * AY
606                 + Ori_Matrix (0, 2) * AZ
607                 + Ori_Matrix (0, 3);
608       PtY       = Ori_Matrix (1, 0) * AX
609                 + Ori_Matrix (1, 1) * AY
610                 + Ori_Matrix (1, 2) * AZ
611                 + Ori_Matrix (1, 3);
612       PtZ       = Ori_Matrix (2, 0) * AX
613                 + Ori_Matrix (2, 1) * AY
614                 + Ori_Matrix (2, 2) * AZ
615                 + Ori_Matrix (2, 3);
616       PtT       = Ori_Matrix (3, 0) * AX
617                 + Ori_Matrix (3, 1) * AY
618                 + Ori_Matrix (3, 2) * AZ
619                 + Ori_Matrix (3, 3);
620
621       // VRCS -> Normalized Projection Coordinate Space
622       APX       = Map_Matrix (0, 0) * PtX
623                 + Map_Matrix (0, 1) * PtY
624                 + Map_Matrix (0, 2) * PtZ
625                 + Map_Matrix (0, 3) * PtT;
626       APY       = Map_Matrix (1, 0) * PtX
627                 + Map_Matrix (1, 1) * PtY
628                 + Map_Matrix (1, 2) * PtZ
629                 + Map_Matrix (1, 3) * PtT;
630       APZ       = Map_Matrix (2, 0) * PtX
631                 + Map_Matrix (2, 1) * PtY
632                 + Map_Matrix (2, 2) * PtZ
633                 + Map_Matrix (2, 3) * PtT;
634       APT       = Map_Matrix (3, 0) * PtX
635                 + Map_Matrix (3, 1) * PtY
636                 + Map_Matrix (3, 2) * PtZ
637                 + Map_Matrix (3, 3) * PtT;
638
639       if (APT == 0. || stop == 0) {
640         AU = AV = IntegerLast ();
641       }
642       else {
643         APX /= APT;
644         APY /= APT;
645         APZ /= APT;
646
647         // NPCS -> Device Coordinate Space
648         AWindow->Size (Width, Height);
649         Dx      = Standard_Real (Width);
650         Dy      = Standard_Real (Height);
651         Ratio   = Dx / Dy;
652         if (Ratio >= 1.) {
653           AU = Standard_Integer (APX * Dx);
654           AV = Standard_Integer (Dy - APY * Dy * Ratio);
655         }
656         else {
657           AU = Standard_Integer (APX * Dx / Ratio);
658           AV = Standard_Integer (Dy - APY * Dy);
659         }
660       }
661     }
662   }
663
664 }
665
666 Graphic3d_Vertex Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)& AWindow, const Standard_Integer AU, const Standard_Integer AV) const {
667
668 // Convert only if the data is correct
669 Graphic3d_CView TheCView;
670 Graphic3d_Vertex Point;
671
672         if (! ViewExists (AWindow, TheCView))
673             Point.SetCoord (RealLast (), RealLast (), RealLast ());
674         else {
675 Standard_Integer Width, Height;
676 Standard_ShortReal x, y, z;
677 Standard_Boolean Result;
678
679             AWindow->Size (Width, Height);
680
681             Result      = MyGraphicDriver->UnProjectRaster (TheCView,
682                                 0, 0, Width, Height,
683                                 AU, AV, x, y, z);
684
685             // unproject is done by UnProjectRaster
686             if (Result) {
687                 Point.SetCoord
688                     (Standard_Real (x), Standard_Real (y), Standard_Real (z));
689             }
690             // unproject cannot be done by UnProjectRaster
691             // Code suspended since drivers Phigs and Pex are abandoned.
692             else {
693
694 Standard_Real NPCX, NPCY, NPCZ;
695 Standard_Real VRCX, VRCY, VRCZ, VRCT;
696 Standard_Real WCX, WCY, WCZ, WCT;
697
698 TColStd_Array2OfReal TOri_Matrix (0,3,0,3);
699 TColStd_Array2OfReal TMap_Matrix (0,3,0,3);
700 TColStd_Array2OfReal TOri_Matrix_Inv (0,3,0,3);
701 TColStd_Array2OfReal TMap_Matrix_Inv (0,3,0,3);
702
703 Standard_Real Dx, Dy, Ratio;
704 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
705 Standard_Integer j;
706
707 Standard_Integer stop = 0;
708
709 Standard_Boolean BResult;
710
711                 j = MyDefinedView.Extent ();
712
713                 while ((! stop) && (MyIterator.More ())) {
714                     if (TheCView.ViewId ==
715                         (MyIterator.Value ())->Identification ()) {
716                         TOri_Matrix     =
717                                 (MyIterator.Value ())->MatrixOfOrientation ();
718                         TMap_Matrix     =
719                                 (MyIterator.Value ())->MatrixOfMapping ();
720                         stop    = 1;
721                     }
722
723                     // MyIterator.Next () is located on the next view
724                     MyIterator.Next ();
725                 }
726
727                 // View Mapping Transformation and View Clip, inversion
728                 BResult = Aspect::Inverse (TMap_Matrix, TMap_Matrix_Inv);
729
730                 // View Orientation Transformation, inversion
731                 BResult = Aspect::Inverse (TOri_Matrix, TOri_Matrix_Inv);
732
733                 // (AU, AV) : Device Coordinate Space
734                 // DCS -> NPCS Normalized Projection Coordinate Space
735                 Dx      = Standard_Real (Width);
736                 Dy      = Standard_Real (Height);
737                 Ratio   = Dx / Dy;
738
739                 if (Ratio >= 1.) {
740                         NPCX    = Standard_Real (AU) / Dx;
741                         NPCY    = (Dy - Standard_Real (AV)) / Dx;
742                 }
743                 else {
744                         NPCX    = Standard_Real (AU) / Dy;
745                         NPCY    = (Dy - Standard_Real (AV)) / Dy;
746                 }
747                 NPCZ    = 0.0;
748
749                 // NPCS -> VRCS View Reference Coordinate Space
750                 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
751
752                 VRCX    = TMap_Matrix_Inv (0, 0) * NPCX
753                         + TMap_Matrix_Inv (0, 1) * NPCY
754                         + TMap_Matrix_Inv (0, 2) * NPCZ
755                         + TMap_Matrix_Inv (0, 3);
756                 VRCY    = TMap_Matrix_Inv (1, 0) * NPCX
757                         + TMap_Matrix_Inv (1, 1) * NPCY
758                         + TMap_Matrix_Inv (1, 2) * NPCZ
759                         + TMap_Matrix_Inv (1, 3);
760                 VRCZ    = TMap_Matrix_Inv (2, 0) * NPCX
761                         + TMap_Matrix_Inv (2, 1) * NPCY
762                         + TMap_Matrix_Inv (2, 2) * NPCZ
763                         + TMap_Matrix_Inv (2, 3);
764                 VRCT    = TMap_Matrix_Inv (3, 0) * NPCX
765                         + TMap_Matrix_Inv (3, 1) * NPCY
766                         + TMap_Matrix_Inv (3, 2) * NPCZ
767                         + TMap_Matrix_Inv (3, 3);
768
769                 // VRCS -> WCS World Coordinate Space
770                 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
771
772                 WCX     = TOri_Matrix_Inv (0, 0) * VRCX
773                         + TOri_Matrix_Inv (0, 1) * VRCY
774                         + TOri_Matrix_Inv (0, 2) * VRCZ
775                         + TOri_Matrix_Inv (0, 3) * VRCT;
776                 WCY     = TOri_Matrix_Inv (1, 0) * VRCX
777                         + TOri_Matrix_Inv (1, 1) * VRCY
778                         + TOri_Matrix_Inv (1, 2) * VRCZ
779                         + TOri_Matrix_Inv (1, 3) * VRCT;
780                 WCZ     = TOri_Matrix_Inv (2, 0) * VRCX
781                         + TOri_Matrix_Inv (2, 1) * VRCY
782                         + TOri_Matrix_Inv (2, 2) * VRCZ
783                         + TOri_Matrix_Inv (2, 3) * VRCT;
784                 WCT     = TOri_Matrix_Inv (3, 0) * VRCX
785                         + TOri_Matrix_Inv (3, 1) * VRCY
786                         + TOri_Matrix_Inv (3, 2) * VRCZ
787                         + TOri_Matrix_Inv (3, 3) * VRCT;
788
789                 if (WCT != 0.)
790                     Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
791                 else
792                     Point.SetCoord (RealLast (), RealLast (), RealLast ());
793             }
794         }
795
796         return (Point);
797
798 }
799
800 void Visual3d_ViewManager::ConvertCoordWithProj (const Handle(Aspect_Window)& AWindow, const Standard_Integer AU, const Standard_Integer AV, Graphic3d_Vertex& Point, Graphic3d_Vector& Proj) const {
801
802 // Conversion only if the data is correct
803 Graphic3d_CView TheCView;
804
805         if (! ViewExists (AWindow, TheCView)) {
806             Point.SetCoord (RealLast (), RealLast (), RealLast ());
807             Proj.SetCoord (0., 0., 0.);
808         }
809         else {
810 Standard_Integer Width, Height;
811 Standard_ShortReal x, y, z;
812 Standard_ShortReal dx, dy, dz;
813 Standard_Boolean Result;
814
815             AWindow->Size (Width, Height);
816
817             Result      = MyGraphicDriver->UnProjectRasterWithRay (TheCView,
818                                 0, 0, Width, Height,
819                                 AU, AV, x, y, z, dx, dy, dz);
820
821             // unproject is done by UnProjectRaster
822             if (Result) {
823                 Point.SetCoord
824                     (Standard_Real (x), Standard_Real (y), Standard_Real (z));
825                 Proj.SetCoord
826                     (Standard_Real (dx), Standard_Real (dy), Standard_Real (dz));
827                 Proj.Normalize();
828             }
829             // unproject cannot be done by UnProjectRaster
830             // Code is suspended since drivers Phigs are Pex abandoned.
831             else {
832
833 Standard_Real NPCX, NPCY, NPCZ;
834 Standard_Real VRCX, VRCY, VRCZ, VRCT;
835 Standard_Real WCX, WCY, WCZ, WCT;
836
837 TColStd_Array2OfReal TOri_Matrix (0,3,0,3);
838 TColStd_Array2OfReal TMap_Matrix (0,3,0,3);
839 TColStd_Array2OfReal TOri_Matrix_Inv (0,3,0,3);
840 TColStd_Array2OfReal TMap_Matrix_Inv (0,3,0,3);
841
842 Standard_Real Dx, Dy, Ratio;
843 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
844 Standard_Integer j;
845
846 Standard_Integer stop = 0;
847
848 Standard_Boolean BResult;
849
850                 j = MyDefinedView.Extent ();
851
852                 while ((! stop) && (MyIterator.More ())) {
853                     if (TheCView.ViewId ==
854                         (MyIterator.Value ())->Identification ()) {
855                         TOri_Matrix     =
856                                 (MyIterator.Value ())->MatrixOfOrientation ();
857                         TMap_Matrix     =
858                                 (MyIterator.Value ())->MatrixOfMapping ();
859                         stop    = 1;
860                     }
861
862                     // MyIterator.Next () is located on the next view
863                     MyIterator.Next ();
864                 }
865
866                 // View Mapping Transformation and View Clip, inversion
867                 BResult = Aspect::Inverse (TMap_Matrix, TMap_Matrix_Inv);
868
869                 // View Orientation Transformation, inversion
870                 BResult = Aspect::Inverse (TOri_Matrix, TOri_Matrix_Inv);
871
872                 // (AU, AV) : Device Coordinate Space
873                 // DCS -> NPCS Normalized Projection Coordinate Space
874                 Dx      = Standard_Real (Width);
875                 Dy      = Standard_Real (Height);
876                 Ratio   = Dx / Dy;
877
878                 if (Ratio >= 1.) {
879                         NPCX    = Standard_Real (AU) / Dx;
880                         NPCY    = (Dy - Standard_Real (AV)) / Dx;
881                 }
882                 else {
883                         NPCX    = Standard_Real (AU) / Dy;
884                         NPCY    = (Dy - Standard_Real (AV)) / Dy;
885                 }
886                 NPCZ    = 0.0;
887
888                 // NPCS -> VRCS View Reference Coordinate Space
889                 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
890
891                 VRCX    = TMap_Matrix_Inv (0, 0) * NPCX
892                         + TMap_Matrix_Inv (0, 1) * NPCY
893                         + TMap_Matrix_Inv (0, 2) * NPCZ
894                         + TMap_Matrix_Inv (0, 3);
895                 VRCY    = TMap_Matrix_Inv (1, 0) * NPCX
896                         + TMap_Matrix_Inv (1, 1) * NPCY
897                         + TMap_Matrix_Inv (1, 2) * NPCZ
898                         + TMap_Matrix_Inv (1, 3);
899                 VRCZ    = TMap_Matrix_Inv (2, 0) * NPCX
900                         + TMap_Matrix_Inv (2, 1) * NPCY
901                         + TMap_Matrix_Inv (2, 2) * NPCZ
902                         + TMap_Matrix_Inv (2, 3);
903                 VRCT    = TMap_Matrix_Inv (3, 0) * NPCX
904                         + TMap_Matrix_Inv (3, 1) * NPCY
905                         + TMap_Matrix_Inv (3, 2) * NPCZ
906                         + TMap_Matrix_Inv (3, 3);
907
908                 // VRCS -> WCS World Coordinate Space
909                 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
910
911                 WCX     = TOri_Matrix_Inv (0, 0) * VRCX
912                         + TOri_Matrix_Inv (0, 1) * VRCY
913                         + TOri_Matrix_Inv (0, 2) * VRCZ
914                         + TOri_Matrix_Inv (0, 3) * VRCT;
915                 WCY     = TOri_Matrix_Inv (1, 0) * VRCX
916                         + TOri_Matrix_Inv (1, 1) * VRCY
917                         + TOri_Matrix_Inv (1, 2) * VRCZ
918                         + TOri_Matrix_Inv (1, 3) * VRCT;
919                 WCZ     = TOri_Matrix_Inv (2, 0) * VRCX
920                         + TOri_Matrix_Inv (2, 1) * VRCY
921                         + TOri_Matrix_Inv (2, 2) * VRCZ
922                         + TOri_Matrix_Inv (2, 3) * VRCT;
923                 WCT     = TOri_Matrix_Inv (3, 0) * VRCX
924                         + TOri_Matrix_Inv (3, 1) * VRCY
925                         + TOri_Matrix_Inv (3, 2) * VRCZ
926                         + TOri_Matrix_Inv (3, 3) * VRCT;
927
928                 if (WCT != 0.)
929                     Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
930                 else
931                     Point.SetCoord (RealLast (), RealLast (), RealLast ());
932
933                 // Define projection ray
934                 NPCZ    = 10.0;
935
936                 // NPCS -> VRCS View Reference Coordinate Space
937                 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
938
939                 VRCX    = TMap_Matrix_Inv (0, 0) * NPCX
940                         + TMap_Matrix_Inv (0, 1) * NPCY
941                         + TMap_Matrix_Inv (0, 2) * NPCZ
942                         + TMap_Matrix_Inv (0, 3);
943                 VRCY    = TMap_Matrix_Inv (1, 0) * NPCX
944                         + TMap_Matrix_Inv (1, 1) * NPCY
945                         + TMap_Matrix_Inv (1, 2) * NPCZ
946                         + TMap_Matrix_Inv (1, 3);
947                 VRCZ    = TMap_Matrix_Inv (2, 0) * NPCX
948                         + TMap_Matrix_Inv (2, 1) * NPCY
949                         + TMap_Matrix_Inv (2, 2) * NPCZ
950                         + TMap_Matrix_Inv (2, 3);
951                 VRCT    = TMap_Matrix_Inv (3, 0) * NPCX
952                         + TMap_Matrix_Inv (3, 1) * NPCY
953                         + TMap_Matrix_Inv (3, 2) * NPCZ
954                         + TMap_Matrix_Inv (3, 3);
955
956                 // VRCS -> WCS World Coordinate Space
957                 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
958
959                 WCX     = TOri_Matrix_Inv (0, 0) * VRCX
960                         + TOri_Matrix_Inv (0, 1) * VRCY
961                         + TOri_Matrix_Inv (0, 2) * VRCZ
962                         + TOri_Matrix_Inv (0, 3) * VRCT;
963                 WCY     = TOri_Matrix_Inv (1, 0) * VRCX
964                         + TOri_Matrix_Inv (1, 1) * VRCY
965                         + TOri_Matrix_Inv (1, 2) * VRCZ
966                         + TOri_Matrix_Inv (1, 3) * VRCT;
967                 WCZ     = TOri_Matrix_Inv (2, 0) * VRCX
968                         + TOri_Matrix_Inv (2, 1) * VRCY
969                         + TOri_Matrix_Inv (2, 2) * VRCZ
970                         + TOri_Matrix_Inv (2, 3) * VRCT;
971                 WCT     = TOri_Matrix_Inv (3, 0) * VRCX
972                         + TOri_Matrix_Inv (3, 1) * VRCY
973                         + TOri_Matrix_Inv (3, 2) * VRCZ
974                         + TOri_Matrix_Inv (3, 3) * VRCT;
975
976                 if (WCT != 0.) {
977                     Proj.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
978                     Proj.Normalize();
979                 }
980                 else
981                     Proj.SetCoord (0., 0., 0.);
982             }
983         }
984
985 }
986
987 Visual3d_PickDescriptor Visual3d_ViewManager::Pick (const Visual3d_ContextPick& CTX, const Handle(Aspect_Window)& AWindow, const Standard_Integer AX, const Standard_Integer AY) {
988
989 // The marking is activated only if the data is correct
990 Standard_Boolean DoPick = Standard_False;
991
992 CALL_DEF_PICK apick;
993 Standard_Integer Width, Height;
994
995         // Parse the list of views to find a 
996         // view having this specified window
997         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
998         int TheWindowIdOfView;
999
1000 #ifndef WNT
1001 const Handle(Xw_Window) THEWindow = *(Handle(Xw_Window) *) &AWindow;
1002         int TheSpecifiedWindowId = int (THEWindow->XWindow ());
1003 #else
1004 const Handle(WNT_Window) THEWindow = *(Handle(WNT_Window) *) &AWindow;
1005         int TheSpecifiedWindowId = int (THEWindow->HWindow ());
1006 #endif  // WNT
1007
1008         while ((! DoPick) && (MyIterator.More ())) {
1009
1010            if ( ((MyIterator.Value ())->IsDefined ()) &&
1011                 ((MyIterator.Value ())->IsActive ()) ) {
1012
1013 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
1014 #ifndef WNT
1015 const Handle(Xw_Window) theWindow = *(Handle(Xw_Window) *) &AspectWindow;
1016         TheWindowIdOfView = int (theWindow->XWindow ());
1017 #else
1018 const Handle(WNT_Window) theWindow = *(Handle(WNT_Window) *) &AspectWindow;
1019         TheWindowIdOfView = int (theWindow->HWindow ());
1020 #endif  // WNT
1021                 // Comparision on window IDs
1022                 if (TheWindowIdOfView == TheSpecifiedWindowId) {
1023                         DoPick          = Standard_True;
1024
1025                         // Update
1026                         apick.WsId      =
1027                         int ((MyIterator.Value ())->Identification ());
1028
1029                         apick.ViewId    =
1030                         int ((MyIterator.Value ())->Identification ());
1031 #ifndef WNT
1032                         apick.DefWindow.XWindow = TheSpecifiedWindowId;
1033 #else
1034                         apick.DefWindow.XWindow = (HWND) TheSpecifiedWindowId;
1035 #endif
1036
1037                         apick.x                 = int (AX);
1038                         apick.y                 = int (AY);
1039
1040                         theWindow->Size (Width, Height);
1041                         apick.DefWindow.dx      = float (Width);
1042                         apick.DefWindow.dy      = float (Height);
1043
1044                         apick.Context.aperture  = (float) CTX.Aperture ();
1045                         apick.Context.order     = int (CTX.Order ());
1046                         apick.Context.depth     = int (CTX.Depth ());
1047
1048                 }
1049            } /* if ((MyIterator.Value ())->IsDefined ()) { */
1050
1051            // MyIterator.Next () is located on the next view
1052            MyIterator.Next ();
1053         }
1054
1055         if (DoPick)
1056                 MyGraphicDriver->Pick (apick);
1057         else
1058                 apick.Pick.depth        = 0;
1059
1060         // Picking : return
1061 Standard_Integer i, j=0;
1062 Standard_Integer NbPick;
1063
1064 Visual3d_PickDescriptor PDes (CTX);
1065 Visual3d_PickPath PPat;
1066
1067         PDes.Clear ();
1068         NbPick  = 0;
1069         // For i=0 it is not a graphic structure it is a view structure
1070         // For i=1 it is the displayed graphic structure
1071         // For i=2 to apick.Pick.depth-1 it is the connected graphic structures
1072         if (apick.Pick.depth != 0) {
1073             j = apick.Pick.listid[1];
1074             if ((Graphic3d_StructureManager::Identification (j))->
1075                                                 IsSelectable ()) {
1076                 // Maj element number
1077                 PPat.SetElementNumber (apick.Pick.listelem[1]);
1078                 // Maj pick identifier
1079                 PPat.SetPickIdentifier (apick.Pick.listpickid[1]);
1080                 // Maj structure
1081                 PPat.SetStructIdentifier
1082                         (Graphic3d_StructureManager::Identification (j));
1083                 // Maj PickPath
1084                 PDes.AddPickPath (PPat);
1085                 NbPick++;
1086             }
1087         }
1088
1089         // Not very efficient, revise (CAL 22/09/95)
1090         if (apick.Pick.depth > 2) {
1091 Handle(Graphic3d_Structure) StructCur =
1092         Graphic3d_StructureManager::Identification (j);
1093 Standard_Boolean found;
1094 Graphic3d_MapOfStructure Set;
1095
1096             for (i=2; i<apick.Pick.depth; i++) {
1097                 found = Standard_False;
1098                 j = apick.Pick.listid[i-1];
1099                 Set.Clear ();
1100                 StructCur->Descendants (Set);
1101 Graphic3d_MapIteratorOfMapOfStructure IteratorD (Set);
1102
1103                 j = apick.Pick.listid[i];
1104                 while (IteratorD.More () && !found) {
1105                     StructCur = IteratorD.Key ();
1106                     if (StructCur->Identification () == j ) {
1107                         found = Standard_True;
1108                         // Maj element number
1109                         PPat.SetElementNumber (apick.Pick.listelem[i]);
1110                         // Maj pick identifier
1111                         PPat.SetPickIdentifier (apick.Pick.listpickid[i]);
1112                         // Maj structure
1113                         PPat.SetStructIdentifier (StructCur);
1114                         // Maj PickPath
1115                         PDes.AddPickPath (PPat);
1116                         NbPick++;
1117                     }
1118                     // IteratorD.Next () is located on the next structure
1119                     IteratorD.Next ();
1120                 }
1121             }
1122         }
1123
1124         apick.Pick.depth        = int (NbPick);
1125
1126         MyGraphicDriver->InitPick ();
1127
1128         return (PDes);
1129
1130 }
1131
1132 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const {
1133
1134 Standard_Boolean Exist = Standard_False;
1135
1136         // Parse the list of views to find
1137         // a view with the specified window
1138         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1139         int TheWindowIdOfView;
1140
1141 #ifndef WNT
1142 const Handle(Xw_Window) THEWindow = *(Handle(Xw_Window) *) &AWindow;
1143         int TheSpecifiedWindowId = int (THEWindow->XWindow ());
1144 #else
1145 const Handle(WNT_Window) THEWindow = *(Handle(WNT_Window) *) &AWindow;
1146         int TheSpecifiedWindowId = int (THEWindow->HWindow ());
1147 #endif  // WNT
1148
1149         while ((! Exist) && (MyIterator.More ())) {
1150
1151            if ( ((MyIterator.Value ())->IsDefined ()) &&
1152                 ((MyIterator.Value ())->IsActive ()) ) {
1153
1154 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
1155 #ifndef WNT
1156 const Handle(Xw_Window) theWindow = *(Handle(Xw_Window) *) &AspectWindow;
1157         TheWindowIdOfView = int (theWindow->XWindow ());
1158 #else
1159 const Handle(WNT_Window) theWindow = *(Handle(WNT_Window) *) &AspectWindow;
1160         TheWindowIdOfView = int (theWindow->HWindow ());
1161 #endif  // WNT
1162                 // Comparaison on window IDs
1163                 if (TheWindowIdOfView == TheSpecifiedWindowId) {
1164                         Exist   = Standard_True;
1165                         TheCView        = *(CALL_DEF_VIEW *)(MyIterator.Value ())->CView ();
1166                 }
1167            } /* if ((MyIterator.Value ())->IsDefined ()) */
1168
1169            // MyIterator.Next () is located on the next view
1170            MyIterator.Next ();
1171         }
1172
1173         return (Exist);
1174
1175 }
1176
1177 void Visual3d_ViewManager::Activate () {
1178
1179         //
1180         // Activates all deactivated views
1181         //
1182         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1183  
1184 #ifdef DEB
1185         Standard_Integer Length = MyDefinedView.Extent ();
1186 #else
1187         MyDefinedView.Extent ();
1188 #endif
1189
1190         while (MyIterator.More ()) {
1191                 if (! (MyIterator.Value ())->IsActive ())
1192                         (MyIterator.Value ())->Activate ();
1193
1194                 // MyIterator.Next () is located on the next view
1195                 MyIterator.Next ();
1196         }
1197
1198 }
1199
1200 void Visual3d_ViewManager::Deactivate () {
1201
1202         //
1203         // Deactivates all activated views
1204         //
1205 #ifdef DEB
1206         Standard_Integer Length = MyDefinedView.Extent ();
1207 #else
1208         MyDefinedView.Extent ();
1209 #endif
1210         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1211  
1212         while (MyIterator.More ()) {
1213                 if ((MyIterator.Value ())->IsActive ())
1214                         (MyIterator.Value ())->Deactivate ();
1215
1216                 // MyIterator.Next () is located on the next view
1217                 MyIterator.Next ();
1218         }
1219
1220 }
1221
1222 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
1223
1224         // Retourne the planned of definable views for the current
1225         // Visual3d_ViewManager.
1226         return
1227 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
1228
1229 }
1230
1231 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
1232
1233         return (Graphic3d_StructureManager::Identification (AId));
1234
1235 }
1236
1237 Standard_Integer Visual3d_ViewManager::Identification () const {
1238  
1239         return (Graphic3d_StructureManager::Identification ());
1240
1241 }
1242
1243 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView) {
1244
1245         MyDefinedView.Add (AView);
1246         return (MyViewGenId.Next ());
1247
1248 }
1249
1250 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
1251 {
1252   MyViewGenId.Free(aViewId);
1253 }
1254
1255 void Visual3d_ViewManager::SetTransparency (const Standard_Boolean AFlag) {
1256
1257         if (MyTransparency && AFlag) return;
1258         if (! MyTransparency && ! AFlag) return;
1259
1260         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1261         while (MyIterator.More ()) {
1262                 (MyIterator.Value ())->SetTransparency (AFlag);
1263                 // MyIterator.Next () is located on the next view
1264                 MyIterator.Next ();
1265         }
1266
1267         MyTransparency  = AFlag;
1268
1269 }
1270
1271 Standard_Boolean Visual3d_ViewManager::Transparency () const {
1272
1273         return (MyTransparency);
1274
1275 }
1276
1277 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag) {
1278
1279         if (MyZBufferAuto && AFlag) return;
1280         if (! MyZBufferAuto && ! AFlag) return;
1281
1282         // if pass from False to True :
1283         // no problem, at the next view update, it 
1284         // will properly ask questions to answer (SetVisualisation)
1285         // if pass from True to False :
1286         // it is necessary to modify ZBufferActivity at each view so that
1287         // zbuffer could be active only if required by context.
1288         // In this case -1 is passed so that the view ask itself the question
1289         // Note : 0 forces the desactivation, 1 forces the activation
1290         if (! AFlag) {
1291                 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1292                 while (MyIterator.More ()) {
1293                         (MyIterator.Value ())->SetZBufferActivity (-1);
1294                         // MyIterator.Next () is located on the next view
1295                         MyIterator.Next ();
1296                 }
1297         }
1298         MyZBufferAuto   = AFlag;
1299
1300 }
1301
1302 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const {
1303
1304         return (MyZBufferAuto);
1305
1306 }
1307
1308 void Visual3d_ViewManager::SetLayer (const Handle(Visual3d_Layer)& ALayer) {
1309
1310 #ifdef TRACE_LAYER
1311         cout << "Visual3d_ViewManager::SetLayer\n" << flush;
1312 #endif
1313
1314         if (ALayer->Type () == Aspect_TOL_OVERLAY) {
1315 #ifdef TRACE_LAYER
1316                 if (MyOverLayer.IsNull ())
1317                         cout << "MyOverLayer is defined" << endl;
1318                 else
1319                         cout << "MyOverLayer is redefined" << endl;
1320 #endif
1321                 MyOverLayer = ALayer;
1322         }
1323         else {
1324 #ifdef TRACE_LAYER
1325                 if (MyUnderLayer.IsNull ())
1326                         cout << "MyUnderLayer is defined" << endl;
1327                 else
1328                         cout << "MyUnderLayer is redefined" << endl;
1329 #endif
1330                 MyUnderLayer = ALayer;
1331         }
1332
1333 }
1334
1335 const Handle(Visual3d_Layer)& Visual3d_ViewManager::UnderLayer () const {
1336
1337         return (MyUnderLayer);
1338
1339 }
1340
1341 const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
1342
1343         return (MyOverLayer);
1344
1345 }
1346
1347 //=======================================================================
1348 //function : ChangeZLayer
1349 //purpose  : 
1350 //=======================================================================
1351
1352 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
1353                                          const Standard_Integer theLayerId)
1354 {
1355   if (!myLayerIds.Contains (theLayerId))
1356     return;
1357   
1358   // change display layer for structure in all views
1359   if (MyDisplayedStructure.Contains (theStructure))
1360   {
1361     Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1362     for ( ; aViewIt.More (); aViewIt.Next ())
1363       (aViewIt.Value ())->ChangeZLayer (theStructure, theLayerId);
1364   }
1365   
1366   // tell graphic driver to update the structure's display layer
1367   MyGraphicDriver->ChangeZLayer (
1368     (*(Graphic3d_CStructure*)theStructure->CStructure ()), theLayerId);
1369 }
1370
1371 //=======================================================================
1372 //function : GetZLayer
1373 //purpose  :
1374 //=======================================================================
1375
1376 Standard_Integer Visual3d_ViewManager::GetZLayer (const Handle(Graphic3d_Structure)& theStructure) const
1377 {
1378   Graphic3d_CStructure& aStructure =
1379     (*(Graphic3d_CStructure*)theStructure->CStructure ());
1380
1381   return MyGraphicDriver->GetZLayer (aStructure);
1382 }
1383
1384 //=======================================================================
1385 //function : AddZLayer
1386 //purpose  :
1387 //=======================================================================
1388
1389 Standard_Boolean Visual3d_ViewManager::AddZLayer (Standard_Integer& theLayerId)
1390 {
1391   try
1392   {
1393     OCC_CATCH_SIGNALS
1394     theLayerId = getZLayerGenId ().Next ();
1395     myLayerIds.Add (theLayerId);
1396     myLayerSeq.Append (theLayerId);
1397   }
1398   catch (Aspect_IdentDefinitionError)
1399   {
1400     // new index can't be generated
1401     return Standard_False;
1402   }
1403
1404   // tell all managed views to remove display layers
1405   Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1406   for ( ; aViewIt.More (); aViewIt.Next ())
1407     (aViewIt.Value ())->AddZLayer (theLayerId);
1408
1409   return Standard_True;
1410 }
1411
1412 //=======================================================================
1413 //function : RemoveZLayer
1414 //purpose  : 
1415 //=======================================================================
1416
1417 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Standard_Integer theLayerId)
1418 {
1419   if (!myLayerIds.Contains (theLayerId) || theLayerId == 0)
1420     return Standard_False;
1421
1422   // tell all managed views to remove display layers
1423   Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1424   for ( ; aViewIt.More (); aViewIt.Next ())
1425     (aViewIt.Value ())->RemoveZLayer (theLayerId);
1426
1427   MyGraphicDriver->UnsetZLayer (theLayerId);
1428
1429   // remove index
1430   for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
1431     if (myLayerSeq(aIdx) == theLayerId)
1432     {
1433       myLayerSeq.Remove (aIdx);
1434       break;
1435     }
1436
1437   myLayerIds.Remove (theLayerId);
1438   getZLayerGenId ().Free (theLayerId);
1439
1440   return Standard_True;
1441 }
1442
1443 //=======================================================================
1444 //function : GetAllZLayers
1445 //purpose  :
1446 //=======================================================================
1447
1448 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
1449 {
1450   theLayerSeq.Assign (myLayerSeq);
1451 }
1452
1453 //=======================================================================
1454 //function : getZLayerGenId
1455 //purpose  :
1456 //=======================================================================
1457
1458 Aspect_GenId& Visual3d_ViewManager::getZLayerGenId ()
1459 {
1460   static Aspect_GenId aGenId (1, IntegerLast());
1461   return aGenId;
1462 }