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