0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / AIS / AIS_InteractiveObject.cxx
1 // Created on: 1996-12-18
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1996-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 #include <AIS_InteractiveObject.hxx>
18
19 #include <AIS_InteractiveContext.hxx>
20 #include <Graphic3d_AspectFillArea3d.hxx>
21 #include <Graphic3d_AspectLine3d.hxx>
22 #include <Graphic3d_AspectMarker3d.hxx>
23 #include <Graphic3d_AspectText3d.hxx>
24 #include <Graphic3d_CStructure.hxx>
25 #include <Graphic3d_Group.hxx>
26 #include <Graphic3d_Structure.hxx>
27 #include <Prs3d_LineAspect.hxx>
28 #include <Prs3d_PointAspect.hxx>
29 #include <Prs3d_Presentation.hxx>
30 #include <Prs3d_ShadingAspect.hxx>
31 #include <Prs3d_TextAspect.hxx>
32 #include <PrsMgr_PresentationManager.hxx>
33
34 IMPLEMENT_STANDARD_RTTIEXT(AIS_InteractiveObject,SelectMgr_SelectableObject)
35
36 //=======================================================================
37 //function : AIS_InteractiveObject
38 //purpose  : 
39 //=======================================================================
40 AIS_InteractiveObject::AIS_InteractiveObject (const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
41 : SelectMgr_SelectableObject (aTypeOfPresentation3d),
42   myCTXPtr (NULL)
43 {
44   //
45 }
46
47 //=======================================================================
48 //function : Redisplay
49 //purpose  :
50 //=======================================================================
51 void AIS_InteractiveObject::Redisplay (const Standard_Boolean AllModes)
52 {
53   if (myCTXPtr == NULL)
54     return;
55
56   myCTXPtr->Redisplay (this, Standard_False, AllModes);
57 }
58
59 //=======================================================================
60 //function : 
61 //purpose  : 
62 //=======================================================================
63 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const 
64 {
65   return myCTXPtr;
66 }
67
68 //=======================================================================
69 //function : SetContext
70 //purpose  :
71 //=======================================================================
72 void AIS_InteractiveObject::SetContext (const Handle(AIS_InteractiveContext)& theCtx)
73 {
74   if (myCTXPtr == theCtx.get())
75   {
76     return;
77   }
78
79   myCTXPtr = theCtx.get();
80   if (!theCtx.IsNull())
81   {
82     myDrawer->Link (theCtx->DefaultDrawer());
83   }
84 }
85
86 //=======================================================================
87 //function : HasPresentation
88 //purpose  :
89 //=======================================================================
90 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
91 {
92   return HasInteractiveContext()
93       && myCTXPtr->MainPrsMgr()->HasPresentation (this, myDrawer->DisplayMode());
94 }
95
96 //=======================================================================
97 //function : Presentation
98 //purpose  :
99 //=======================================================================
100 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
101 {
102   if (!HasInteractiveContext())
103   {
104     return Handle(Prs3d_Presentation)();
105   }
106
107   Handle(PrsMgr_Presentation) aPrs = myCTXPtr->MainPrsMgr()->Presentation (this, myDrawer->DisplayMode(), false);
108   return aPrs;
109 }
110
111 //=======================================================================
112 //function : SetAspect 
113 //purpose  : 
114 //=======================================================================
115 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& theAspect)
116 {
117
118   if (!HasPresentation())
119   {
120     return;
121   }
122
123   Handle(Prs3d_Presentation) aPrs = Presentation();
124   if (aPrs->Groups().IsEmpty())
125   {
126     return;
127   }
128   const Handle(Graphic3d_Group)& aGroup = aPrs->Groups().Last();
129   if (Handle(Prs3d_ShadingAspect) aShadingAspect = Handle(Prs3d_ShadingAspect)::DownCast(theAspect))
130   {
131     aGroup->SetGroupPrimitivesAspect (aShadingAspect->Aspect());
132   }
133   else if (Handle(Prs3d_LineAspect) aLineAspect = Handle(Prs3d_LineAspect)::DownCast(theAspect))
134   {
135     aGroup->SetGroupPrimitivesAspect (aLineAspect->Aspect());
136   }
137   else if (Handle(Prs3d_PointAspect) aPointAspect = Handle(Prs3d_PointAspect)::DownCast(theAspect))
138   {
139     aGroup->SetGroupPrimitivesAspect (aPointAspect->Aspect());
140   }
141   else if (Handle(Prs3d_TextAspect) aTextAspect = Handle(Prs3d_TextAspect)::DownCast(theAspect))
142   {
143     aGroup->SetGroupPrimitivesAspect (aTextAspect->Aspect());
144   }
145 }
146
147 //=======================================================================
148 //function : DumpJson
149 //purpose  : 
150 //=======================================================================
151 void AIS_InteractiveObject::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
152 {
153   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
154
155   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, SelectMgr_SelectableObject)
156   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myCTXPtr)
157   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myOwner)
158 }