777eb8f0427493e2e6dbb4a116fc24ad45cfed39
[occt.git] / src / TPrsStd / TPrsStd_AISPresentation.cxx
1 // Created on: 1998-09-30
2 // Created by: Sergey RUIN
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_InteractiveObject.hxx>
20 #include <Standard_GUID.hxx>
21 #include <Standard_NullObject.hxx>
22 #include <Standard_NoMoreObject.hxx>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TDF_Attribute.hxx>
25 #include <TDF_AttributeDelta.hxx>
26 #include <TDF_DefaultDeltaOnModification.hxx>
27 #include <TDF_DefaultDeltaOnRemoval.hxx>
28 #include <TDF_DeltaOnAddition.hxx>
29 #include <TDF_Label.hxx>
30 #include <TDF_RelocationTable.hxx>
31 #include <TDF_Tool.hxx>
32 #include <TPrsStd_AISPresentation.hxx>
33 #include <TColStd_ListOfInteger.hxx>
34 #include <TColStd_ListIteratorOfListOfInteger.hxx>
35 #include <TDataXtd_Presentation.hxx>
36 #include <TPrsStd_AISViewer.hxx>
37 #include <TPrsStd_Driver.hxx>
38 #include <TPrsStd_DriverTable.hxx>
39
40 IMPLEMENT_STANDARD_RTTIEXT(TPrsStd_AISPresentation,TDF_Attribute)
41
42 #define NO_MORE_OBJECT "TPrsStd_AISPresentation has no associated TDataXtd_PresentationData"
43
44 //=======================================================================
45 //function : TPrsStd_AISPresentation
46 //purpose  : Constructor
47 //=======================================================================
48 TPrsStd_AISPresentation::TPrsStd_AISPresentation ()
49 {
50 }
51
52
53 //=======================================================================
54 //function : Set
55 //purpose  : 
56 //=======================================================================
57 Handle(TPrsStd_AISPresentation) TPrsStd_AISPresentation::Set
58              ( const TDF_Label& theLabel, const Standard_GUID& theDriver )
59 {
60   Handle(TPrsStd_AISPresentation) aPresentation;
61   // create associated data (unless already there)
62   Handle(TDataXtd_Presentation) aData = TDataXtd_Presentation::Set (theLabel, theDriver);
63   if(aData.IsNull())
64     throw Standard_NoMoreObject(NO_MORE_OBJECT);
65   if ( !theLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), aPresentation) )
66   {
67     aPresentation = new TPrsStd_AISPresentation();
68     theLabel.AddAttribute(aPresentation, Standard_True);
69   }
70
71   return aPresentation;
72 }
73
74 //=======================================================================
75 //function : Set
76 //purpose  : 
77 //=======================================================================
78 Handle(TPrsStd_AISPresentation) TPrsStd_AISPresentation::Set(const Handle(TDF_Attribute)& theMaster)
79 {
80   return TPrsStd_AISPresentation::Set(theMaster->Label(), theMaster->ID());
81 }
82
83 //=======================================================================
84 //function : Unset
85 //purpose  : 
86 //=======================================================================
87 void TPrsStd_AISPresentation::Unset (const TDF_Label& theLabel)
88 {
89   Handle(TPrsStd_AISPresentation) aPresentation;
90   if ( theLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), aPresentation) )
91     theLabel.ForgetAttribute(aPresentation);
92
93   // remove associated data
94   TDataXtd_Presentation::Unset (theLabel);
95 }
96
97 //=======================================================================
98 //function : getData
99 //purpose  : 
100 //=======================================================================
101 Handle(TDataXtd_Presentation) TPrsStd_AISPresentation::getData () const
102 {
103   Handle(TDataXtd_Presentation) aData;
104   if (!Label().FindAttribute(TDataXtd_Presentation::GetID(), aData))
105       throw Standard_NoMoreObject(NO_MORE_OBJECT);
106   return aData;
107 }
108
109 //=======================================================================
110 //function : GetID
111 //purpose  : 
112 //=======================================================================
113 const Standard_GUID& TPrsStd_AISPresentation::GetID()
114 {
115   static Standard_GUID TPrsStd_AISPresentationID("3680ac6c-47ae-4366-bb94-26abb6e07341");
116   return TPrsStd_AISPresentationID;
117 }
118
119 //=======================================================================
120 //function : Display
121 //purpose  : 
122 //=======================================================================
123 void TPrsStd_AISPresentation::Display(const Standard_Boolean theIsUpdate)
124 {
125   if ( theIsUpdate || myAIS.IsNull() )
126     AISUpdate();
127
128   AISDisplay();
129 }
130
131 //=======================================================================
132 //function : Erase
133 //purpose  : 
134 //=======================================================================
135 void TPrsStd_AISPresentation::Erase(const Standard_Boolean theIsRemove)
136 {
137   if ( IsDisplayed() || theIsRemove)
138   {
139     AISErase(theIsRemove);
140   }
141 }
142
143 //=======================================================================
144 //function : Update
145 //purpose  : 
146 //=======================================================================
147 void TPrsStd_AISPresentation::Update()
148 {
149   AISUpdate();
150 }
151
152 //=======================================================================
153 //function : IsDisplayed
154 //purpose  : 
155 //=======================================================================
156 Standard_Boolean TPrsStd_AISPresentation::IsDisplayed() const
157 {
158   return getData()->IsDisplayed();
159 }
160
161 //=======================================================================
162 //function : SetDisplayed
163 //purpose  : 
164 //=======================================================================
165 void TPrsStd_AISPresentation::SetDisplayed(const Standard_Boolean theIsDisplayed)
166 {
167   // this method can be called by AISUpdate() in the process of removal,
168   // while data attribute may be already removed
169   Backup();
170   getData()->SetDisplayed(theIsDisplayed);
171 }
172
173 //=======================================================================
174 //function :SetDriverGUID
175 //purpose  : 
176 //=======================================================================
177 void TPrsStd_AISPresentation::SetDriverGUID(const Standard_GUID& theGUID)
178 {
179   Backup();
180   getData()->SetDriverGUID (theGUID);
181 }
182
183 //=======================================================================
184 //function :GetDriverGUID
185 //purpose  : 
186 //=======================================================================
187 Standard_GUID TPrsStd_AISPresentation::GetDriverGUID() const
188 {
189   return getData()->GetDriverGUID();
190 }
191
192 //=======================================================================
193 //function :Material
194 //purpose  : 
195 //=======================================================================
196 Graphic3d_NameOfMaterial TPrsStd_AISPresentation::Material() const
197 {
198   return (Graphic3d_NameOfMaterial)getData()->MaterialIndex();
199 }
200
201 //=======================================================================
202 //function :HasMaterial
203 //purpose  : 
204 //=======================================================================
205 Standard_Boolean TPrsStd_AISPresentation::HasOwnMaterial() const
206 {
207   return getData()->HasOwnMaterial();
208 }
209
210 //=======================================================================
211 //function : UnsetMaterial
212 //purpose  : 
213 //=======================================================================
214 void TPrsStd_AISPresentation::UnsetMaterial()
215 {
216   if ( !getData()->HasOwnMaterial() && !myAIS.IsNull() && !myAIS->HasMaterial() )
217     return;
218
219   getData()->UnsetMaterial();
220
221   if ( myAIS.IsNull() )
222     AISUpdate();
223
224   if ( !myAIS.IsNull() && myAIS->HasMaterial() )
225   {
226     Handle(AIS_InteractiveContext) aContext = getAISContext();
227
228     if ( !aContext.IsNull() )
229       aContext->UnsetMaterial(myAIS, Standard_False);
230     else
231       myAIS->UnsetMaterial();
232   }
233 }
234
235
236 //=======================================================================
237 //function : SetMaterial
238 //purpose  : 
239 //=======================================================================
240 void TPrsStd_AISPresentation::SetMaterial(const Graphic3d_NameOfMaterial theName)
241 {
242   Backup();
243   if ( getData()->HasOwnMode() && getData()->MaterialIndex() == theName )
244     if ( !myAIS.IsNull() && myAIS->HasMaterial() && myAIS->Material() == theName )
245       return;
246
247   getData()->SetMaterialIndex(theName);
248
249   if ( myAIS.IsNull() )
250     AISUpdate();
251
252   if ( !myAIS.IsNull() )
253   {
254     if ( myAIS->HasMaterial() && myAIS->Material() == theName )
255       return;   // AIS has already had that material
256
257     Handle(AIS_InteractiveContext) aContext = getAISContext();
258
259     if ( !aContext.IsNull() )
260       aContext->SetMaterial(myAIS, theName,  Standard_False);
261     else
262       myAIS->SetMaterial(theName);
263   }
264 }
265
266 //=======================================================================
267 //function :Transparency
268 //purpose  : 
269 //=======================================================================
270 Standard_Real TPrsStd_AISPresentation::Transparency() const
271 {
272   return getData()->Transparency();
273 }
274
275 //=======================================================================
276 //function :SetTransparency
277 //purpose  : 
278 //=======================================================================
279 void TPrsStd_AISPresentation::SetTransparency(const Standard_Real theValue)
280 {
281   Backup();
282   if (getData()->HasOwnTransparency() && getData()->Transparency() == theValue)
283     if ( !myAIS.IsNull() && myAIS->Transparency() == theValue )
284       return;
285
286   getData()->SetTransparency(theValue);
287
288   if ( myAIS.IsNull() )
289     AISUpdate();
290
291   if ( !myAIS.IsNull() )
292   {
293     if ( myAIS->Transparency() == theValue )
294       return;   // AIS has already had that transparency 
295
296     Handle(AIS_InteractiveContext) aContext = getAISContext();
297
298     if ( !aContext.IsNull() )
299       aContext->SetTransparency(myAIS, theValue, Standard_False);
300     else
301       myAIS->SetTransparency(theValue);
302   }
303 }
304
305 //=======================================================================
306 //function :UnsetTransparency
307 //purpose  : 
308 //=======================================================================
309 void TPrsStd_AISPresentation::UnsetTransparency()
310 {
311   if (!getData()->HasOwnTransparency())
312     return;
313
314   getData()->UnsetTransparency();
315
316   if ( myAIS.IsNull() )
317     AISUpdate();
318
319   if ( !myAIS.IsNull() )
320   {
321     Handle(AIS_InteractiveContext) aContext = getAISContext();
322
323     if( !aContext.IsNull() )
324       aContext->UnsetTransparency(myAIS, Standard_False);
325     else
326       myAIS->UnsetTransparency();
327   }
328 }
329
330 //=======================================================================
331 //function : HasTransparency
332 //purpose  : 
333 //=======================================================================
334 Standard_Boolean TPrsStd_AISPresentation::HasOwnTransparency() const
335 {
336   return getData()->HasOwnTransparency();
337 }
338
339 //=======================================================================
340 //function : Color
341 //purpose  : 
342 //=======================================================================
343 Quantity_NameOfColor TPrsStd_AISPresentation::Color() const
344 {
345   return getData()->Color();
346 }
347
348 //=======================================================================
349 //function : HasOwnColor
350 //purpose  : 
351 //=======================================================================
352 Standard_Boolean TPrsStd_AISPresentation::HasOwnColor() const
353 {
354   return getData()->HasOwnColor();
355 }
356
357 //=======================================================================
358 //function : UnsetColor
359 //purpose  : 
360 //=======================================================================
361 void TPrsStd_AISPresentation::UnsetColor()
362 {
363   if (!getData()->HasOwnColor() && ! myAIS.IsNull() && !myAIS->HasColor())
364     return;
365   getData()->UnsetColor();
366
367   if ( myAIS.IsNull() )
368     AISUpdate();
369
370   if ( !myAIS.IsNull() && myAIS->HasColor() )
371   {
372     Handle(AIS_InteractiveContext) aContext = getAISContext();
373
374     if ( !aContext.IsNull() )
375       aContext->UnsetColor(myAIS, Standard_False);
376     else
377       myAIS->UnsetColor();
378   }
379 }
380
381 //=======================================================================
382 //function : SetColor
383 //purpose  : 
384 //=======================================================================
385 void TPrsStd_AISPresentation::SetColor(const Quantity_NameOfColor theColor)
386 {
387   Backup();
388   if ( getData()->HasOwnColor() && getData()->Color() == theColor )
389   {
390     if (!myAIS.IsNull() && myAIS->HasColor())
391     {
392       Quantity_Color aColor;
393       myAIS->Color (aColor);
394       if (aColor.Name() == theColor)
395       {
396         return;
397       }
398     }
399   }
400
401   getData()->SetColor(theColor);
402
403   if ( myAIS.IsNull() )
404     AISUpdate();
405
406   if ( !myAIS.IsNull() )
407   {
408     if (myAIS->HasColor())
409     {
410       Quantity_Color aColor;
411       myAIS->Color (aColor);
412       if (aColor.Name() == theColor)
413       {
414         return;   // AIS has already had that color
415       }
416     }
417
418     Handle(AIS_InteractiveContext) aContext = getAISContext();
419
420     if( !aContext.IsNull() )
421       aContext->SetColor(myAIS, theColor, Standard_False);
422     else 
423       myAIS->SetColor(theColor);
424   }
425 }
426
427 //=======================================================================
428 //function :Width
429 //purpose  : 
430 //=======================================================================
431 Standard_Real TPrsStd_AISPresentation::Width() const
432 {
433   return getData()->Width();
434 }
435
436 //=======================================================================
437 //function : HasWidth
438 //purpose  : 
439 //=======================================================================
440 Standard_Boolean TPrsStd_AISPresentation::HasOwnWidth() const
441 {
442   return getData()->HasOwnWidth();
443 }
444
445 //=======================================================================
446 //function : SetWidth
447 //purpose  : 
448 //=======================================================================
449 void TPrsStd_AISPresentation::SetWidth(const Standard_Real theWidth)
450 {
451   Backup();
452   if ( getData()->HasOwnWidth() && getData()->Width() == theWidth )
453     if ( !myAIS.IsNull() && myAIS->HasWidth() && myAIS->Width() == theWidth )
454       return;
455
456   getData()->SetWidth(theWidth);
457
458   if( !myAIS.IsNull() )
459   {
460     if ( myAIS->HasWidth() && myAIS->Width() == theWidth )
461       return;   // AIS has already had that width
462
463     Handle(AIS_InteractiveContext) aContext = getAISContext();
464
465     if( !aContext.IsNull() )
466       aContext->SetWidth(myAIS, theWidth, Standard_False);
467     else 
468       myAIS->SetWidth(theWidth);
469   }
470 }
471
472 //=======================================================================
473 //function : UnsetWidth
474 //purpose  : 
475 //=======================================================================
476 void TPrsStd_AISPresentation::UnsetWidth()
477 {
478   if ( !getData()->HasOwnWidth() )
479     if ( !myAIS.IsNull() && !myAIS->HasWidth() )
480       return;
481
482   getData()->UnsetWidth();
483
484   if( myAIS.IsNull() )
485     AISUpdate();
486
487   if ( !myAIS.IsNull() && myAIS->HasWidth() )
488   {
489     Handle(AIS_InteractiveContext) aContext = getAISContext();
490
491     if ( !aContext.IsNull() )
492       aContext->UnsetWidth(myAIS, Standard_False);
493     else
494       myAIS->UnsetWidth();
495   }
496 }
497
498 //=======================================================================
499 //function : Mode
500 //purpose  : 
501 //=======================================================================
502 Standard_Integer TPrsStd_AISPresentation::Mode() const
503 {
504   return getData()->Mode();
505 }
506
507 //=======================================================================
508 //function : HasOwnMode
509 //purpose  : 
510 //=======================================================================
511 Standard_Boolean TPrsStd_AISPresentation::HasOwnMode() const
512 {
513   return getData()->HasOwnMode();
514 }
515
516 //=======================================================================
517 //function : SetMode
518 //purpose  : 
519 //=======================================================================
520 void TPrsStd_AISPresentation::SetMode(const Standard_Integer theMode)
521 {
522   Backup();
523   if ( getData()->HasOwnMode() && getData()->Mode() == theMode )
524     if ( !myAIS.IsNull() && myAIS->DisplayMode() == theMode )
525       return;
526
527   getData()->SetMode(theMode);
528
529   if ( myAIS.IsNull() )
530     AISUpdate();
531
532   if ( !myAIS.IsNull() )
533   {
534     if (  myAIS->DisplayMode() == theMode )
535       return;   // AIS has already had that mode
536
537     Handle(AIS_InteractiveContext) aContext = getAISContext();
538
539     if( !aContext.IsNull() )
540       aContext->SetDisplayMode(myAIS, theMode, Standard_False);
541     else 
542       myAIS->SetDisplayMode(theMode);
543   }
544 }
545
546 //=======================================================================
547 //function : UnsetMode
548 //purpose  : 
549 //=======================================================================
550 void TPrsStd_AISPresentation::UnsetMode()
551 {
552   if ( !getData()->HasOwnMode() )
553     if ( !myAIS.IsNull() && !myAIS->HasDisplayMode() )
554       return;
555
556   getData()->UnsetMode();
557
558   if ( myAIS.IsNull() )
559     AISUpdate();
560
561   if( !myAIS.IsNull() &&  myAIS->HasDisplayMode() )
562   {
563     Handle(AIS_InteractiveContext) aContext = getAISContext();
564
565     if ( !aContext.IsNull() )
566       aContext->UnsetDisplayMode(myAIS, Standard_False);
567     else
568       myAIS->UnsetDisplayMode();
569   }
570 }
571
572 //=======================================================================
573 //function : SelectionMode
574 //purpose  : 
575 //=======================================================================
576 Standard_Integer TPrsStd_AISPresentation::SelectionMode() const
577 {
578   return getData()->SelectionMode();
579 }
580
581 //=======================================================================
582 //function : HasOwnSelectionMode
583 //purpose  : 
584 //=======================================================================
585 Standard_Boolean TPrsStd_AISPresentation::HasOwnSelectionMode() const
586 {
587   return getData()->HasOwnSelectionMode();
588 }
589
590 //=======================================================================
591 //function : SetSelectionMode
592 //purpose  : 
593 //=======================================================================
594 void TPrsStd_AISPresentation::SetSelectionMode(const Standard_Integer theSelectionMode, const Standard_Boolean theTransaction)
595 {
596   if (theTransaction)
597     Backup();
598   getData()->SetSelectionMode (theSelectionMode, theTransaction);
599   
600   if ( myAIS.IsNull() )
601     AISUpdate();
602   else
603     ActivateSelectionMode();
604 }
605
606 //=======================================================================
607 //function : UnsetSelectionMode
608 //purpose  : 
609 //=======================================================================
610 void TPrsStd_AISPresentation::UnsetSelectionMode()
611 {
612   getData()->UnsetSelectionMode ();
613   AISUpdate();
614 }
615
616 //=======================================================================
617 //function : ID
618 //purpose  : 
619 //=======================================================================
620 const Standard_GUID& TPrsStd_AISPresentation::ID() const
621 {
622   return GetID();
623 }
624
625 //=======================================================================
626 //function : BackupCopy
627 //purpose  : 
628 //=======================================================================
629 Handle(TDF_Attribute) TPrsStd_AISPresentation::BackupCopy() const 
630 {
631   return new TPrsStd_AISPresentation;
632 }
633
634
635 //=======================================================================
636 //function : NewEmpty
637 //purpose  : 
638 //=======================================================================
639 Handle(TDF_Attribute) TPrsStd_AISPresentation::NewEmpty() const
640 {   
641   return new TPrsStd_AISPresentation();
642 }
643
644 //=======================================================================
645 //function : Paste
646 //purpose  : 
647 //=======================================================================
648 void TPrsStd_AISPresentation::Restore(const Handle(TDF_Attribute)& /*theWith*/)
649 {
650   myAIS.Nullify();
651 }
652
653 //=======================================================================
654 //function : Paste
655 //purpose  : 
656 //=======================================================================
657 void TPrsStd_AISPresentation::Paste (const Handle(TDF_Attribute)& theInto,
658                                      const Handle(TDF_RelocationTable)& ) const
659 {
660   Handle(TPrsStd_AISPresentation) anInto =
661     Handle(TPrsStd_AISPresentation)::DownCast(theInto);
662
663   anInto->Backup();
664   if (!anInto->myAIS.IsNull())
665   {
666     // Save displayed flag.
667     Standard_Boolean displayed = anInto->IsDisplayed();
668     // Erase the interactive object.
669     anInto->AISErase(Standard_True);
670     // Restore the displayed flag.
671     if (displayed)
672       anInto->SetDisplayed(displayed);
673   }
674   // Nullify the interactive object.
675   // It will be restored on the next call to AISUpdate().
676   anInto->myAIS.Nullify();
677 }
678
679 //=======================================================================
680 //function : AfterAddition
681 //purpose  : erase if displayed
682 //=======================================================================
683 void TPrsStd_AISPresentation::AfterAddition() 
684 {
685   AfterResume();
686 }
687
688 //=======================================================================
689 //function : BeforeRemoval
690 //purpose  : erase if displayed
691 //=======================================================================
692 void TPrsStd_AISPresentation::BeforeRemoval() 
693 {
694   BeforeForget();
695 }
696
697 //=======================================================================
698 //function : BeforeForget
699 //purpose  : erase if displayed
700 //=======================================================================
701 void TPrsStd_AISPresentation::BeforeForget() 
702
703   if ( !myAIS.IsNull() )
704   { // Remove AISObject from context.
705     AISErase(Standard_True);
706     myAIS.Nullify();
707   }
708 }
709
710 //=======================================================================
711 //function : AfterResume
712 //purpose  : display if displayed
713 //=======================================================================
714 void TPrsStd_AISPresentation::AfterResume()
715 {
716   if ( IsDisplayed() )
717   {
718         AISUpdate();
719     AISDisplay();
720   }
721   else
722     AISErase();
723 }
724
725 //=======================================================================
726 //function : BeforeUndo
727 //purpose  : le NamedShape associe doit etre present
728 //=======================================================================
729 Standard_Boolean TPrsStd_AISPresentation::BeforeUndo (const Handle(TDF_AttributeDelta)& AD,
730                                                                               const Standard_Boolean ) 
731 {
732   Handle(TPrsStd_AISPresentation) P;
733   AD->Label().FindAttribute(TPrsStd_AISPresentation::GetID(), P);
734
735   if (AD->IsKind(STANDARD_TYPE(TDF_DeltaOnAddition)))
736   {
737     if ( !P.IsNull() )
738       P->BeforeForget();
739   }
740   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnRemoval))) {
741   }
742   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnModification)))
743   {
744     if ( !P.IsNull() )
745       P->BeforeForget();
746   }
747
748   return Standard_True;
749 }
750
751 //=======================================================================
752 //function : AfterUndo
753 //purpose  : le NamedShape associe doit etre present
754 //=======================================================================
755 Standard_Boolean TPrsStd_AISPresentation::AfterUndo (const Handle(TDF_AttributeDelta)& AD,
756                                                      const Standard_Boolean ) 
757
758   Handle(TPrsStd_AISPresentation) P;
759   AD->Label().FindAttribute(TPrsStd_AISPresentation::GetID(), P);
760
761   if (AD->IsKind(STANDARD_TYPE(TDF_DeltaOnAddition)))
762   {}
763   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnRemoval)))
764   {
765     if ( !P.IsNull() )
766       P->AfterAddition();
767   }
768   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnModification)))
769   {
770     if ( !P.IsNull() )
771       P->AfterResume();
772   }
773
774   return Standard_True;
775 }
776
777 //=======================================================================
778 //function : AISUpdate
779 //purpose  : 
780 //=======================================================================
781 void TPrsStd_AISPresentation::AISUpdate()
782 {
783   Backup();
784   getData()->Backup();
785   Handle(AIS_InteractiveContext) aContext;
786   if ( !Label().IsNull() )
787   {
788     aContext = getAISContext();
789
790     Handle(TPrsStd_Driver) aDriver;
791     if ( TPrsStd_DriverTable::Get()->FindDriver(GetDriverGUID(), aDriver) )
792     {
793       // Build a new  AIS.
794       if ( myAIS.IsNull() )
795       {
796         Handle(AIS_InteractiveObject) aNewObj;
797         if ( aDriver->Update(Label(), aNewObj) )
798         {
799                         myAIS = aNewObj;
800           aNewObj->SetOwner(this);
801         }
802       }
803       else
804       {
805         Handle(AIS_InteractiveObject) anObj = myAIS;
806         if ( aDriver->Update(Label(), anObj) )
807           if ( !(anObj ==  myAIS) )
808           {
809             if ( !aContext.IsNull() )
810               aContext->Remove (myAIS, Standard_False);
811
812             // Driver has built new AIS.
813             myAIS = anObj;
814             anObj->SetOwner(this);
815           }
816       }
817     }
818   }
819   else return;
820
821   // Apply the visualization settings.
822   if ( !myAIS.IsNull() )
823   {
824     if ( HasOwnColor() )
825     {
826       Quantity_NameOfColor aColor = Color();
827       Quantity_Color aPrsColor;
828       myAIS->Color (aPrsColor);
829       if ( !(myAIS->HasColor()) || (myAIS->HasColor() && aPrsColor.Name() != aColor) )
830       {
831         if ( !aContext.IsNull() )
832           aContext->SetColor(myAIS, aColor, Standard_False);
833         else
834           myAIS->SetColor(aColor);
835       }
836     }
837
838     if ( HasOwnMaterial() )
839     {
840       Graphic3d_NameOfMaterial aMaterial = Material();
841       if ( !(myAIS->HasMaterial()) || (myAIS->HasMaterial() && myAIS->Material() != aMaterial) )
842       {
843         if ( !aContext.IsNull() )
844           aContext->SetMaterial(myAIS, aMaterial, Standard_False );
845         else
846           myAIS->SetMaterial(aMaterial);
847       }
848     }
849
850     if ( HasOwnTransparency() )
851     {
852       const Standard_Real aTransparency = Transparency();
853       if ( myAIS->Transparency() != aTransparency )
854       {
855         if( !aContext.IsNull() )
856           aContext->SetTransparency(myAIS, aTransparency, Standard_False);
857         else
858           myAIS->SetTransparency(aTransparency);
859       }
860     }
861
862     if ( HasOwnWidth() )
863     {
864       const Standard_Real aWidth = Width();
865       if ( !(myAIS->HasWidth()) || (myAIS->HasWidth() && myAIS->Width() != aWidth) )
866       {
867         if ( !aContext.IsNull() )
868           aContext->SetWidth(myAIS, aWidth, Standard_False);
869         else
870           myAIS->SetWidth(aWidth);
871       }
872     }
873
874     if ( HasOwnMode() )
875     {
876       const Standard_Integer aMode = Mode();
877       if ( myAIS->DisplayMode() != aMode )
878         myAIS->SetDisplayMode(aMode);
879     }
880
881     ActivateSelectionMode();
882   }
883   
884   if (IsDisplayed() && !aContext.IsNull())
885     aContext->Redisplay(myAIS, Standard_False);
886 }
887
888 //=======================================================================
889 //function : AISDisplay
890 //purpose  : 
891 //=======================================================================
892 void TPrsStd_AISPresentation::AISDisplay()
893 {
894   if ( !Label().IsNull() )
895   {
896     Handle(AIS_InteractiveContext) aContext = getAISContext();
897
898     if ( aContext.IsNull() )
899       return;
900
901     if ( !myAIS.IsNull() )
902     {
903       if ( !(myAIS->GetContext()).IsNull() && (myAIS->GetContext()) != aContext )
904         myAIS->GetContext()->Remove (myAIS, Standard_False);
905
906       if ( IsDisplayed() && aContext->IsDisplayed(myAIS) )
907         return;
908
909       aContext->Display(myAIS, Standard_False);
910
911       if ( aContext->IsDisplayed(myAIS) )
912         SetDisplayed(Standard_True);
913     }
914   }
915 }
916
917 //=======================================================================
918 //function : AISErase
919 //purpose  : 
920 //=======================================================================
921 void TPrsStd_AISPresentation::AISErase(const Standard_Boolean theIsRemove)
922 {  
923   Handle(AIS_InteractiveContext) aContext, anOwnContext;
924
925   if ( !myAIS.IsNull() )
926   {
927     Backup();
928     if ( !Label().IsNull() )
929     {
930       if (IsAttribute(TDataXtd_Presentation::GetID()))
931         SetDisplayed(Standard_False); 
932
933       Handle(TPrsStd_AISViewer) viewer;
934       if( !TPrsStd_AISViewer::Find(Label(), viewer) )
935         return;
936       anOwnContext = myAIS->GetContext();
937       aContext = viewer->GetInteractiveContext();
938
939       if ( theIsRemove )
940       {
941         if ( !aContext.IsNull() )
942           aContext->Remove(myAIS, Standard_False);
943         if ( !anOwnContext.IsNull() && anOwnContext != aContext )
944           anOwnContext->Remove(myAIS, Standard_False);
945
946         myAIS.Nullify();
947       }
948       else
949       {
950         if ( !aContext.IsNull() )
951           aContext->Erase(myAIS, Standard_False);
952         if ( !anOwnContext.IsNull() && anOwnContext != aContext )
953           anOwnContext->Erase(myAIS, Standard_False);
954       }
955     }
956     else
957     {
958       if ( theIsRemove )
959       {
960         if ( !anOwnContext.IsNull() )
961         {
962           anOwnContext->Remove(myAIS, Standard_False);
963           myAIS.Nullify();
964         }
965       }
966       else
967         if( !anOwnContext.IsNull() )
968           anOwnContext->Erase(myAIS, Standard_False);
969     }
970   }
971 }
972
973 //=======================================================================
974 //function :GetAIS
975 //purpose  : 
976 //=======================================================================
977 Handle(AIS_InteractiveObject) TPrsStd_AISPresentation::GetAIS() const
978 {
979   return myAIS;
980 }
981
982 //=======================================================================
983 //function : getAISContext
984 //purpose  : 
985 //=======================================================================
986 Handle(AIS_InteractiveContext) TPrsStd_AISPresentation::getAISContext() const
987 {
988   Handle(TPrsStd_AISViewer) aViewer;
989   if ( TPrsStd_AISViewer::Find(Label(), aViewer) )
990     return aViewer->GetInteractiveContext();
991
992   return Handle_AIS_InteractiveContext();
993 }
994
995 //=======================================================================
996 //function : ActivateSelectionMode
997 //purpose  : Activates selection mode of the interactive object.
998 //           It is called internally on change of selection mode and AISUpdate().
999 //=======================================================================
1000 void TPrsStd_AISPresentation::ActivateSelectionMode()
1001 {
1002   if (!myAIS.IsNull() && HasOwnSelectionMode())
1003   {
1004     Handle(AIS_InteractiveContext) aContext = getAISContext();
1005     if (!aContext.IsNull())
1006     {
1007       TColStd_ListOfInteger anActivatedModes;
1008       aContext->ActivatedModes (myAIS, anActivatedModes);
1009       Standard_Boolean isActivated = Standard_False;
1010       Standard_Integer aSelectionMode = SelectionMode();
1011       if (aSelectionMode == -1)
1012       {
1013         aContext->Deactivate(myAIS);
1014       }
1015       else
1016       {
1017         for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
1018         {
1019           if (aModeIter.Value() == aSelectionMode)
1020           {
1021             isActivated = Standard_True;
1022             break;
1023           }
1024         }
1025         if (!isActivated)
1026           aContext->Activate (myAIS, aSelectionMode, Standard_False);
1027       }
1028     } 
1029   }
1030 }