0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / DDataStd / DDataStd_DrawPresentation.cxx
1 // Created on: 1998-09-23
2 // Created by: Denis PASCAL
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 <DDataStd_DrawDriver.hxx>
19 #include <DDataStd_DrawPresentation.hxx>
20 #include <Draw.hxx>
21 #include <Draw_Drawable3D.hxx>
22 #include <Draw_Viewer.hxx>
23 #include <Standard_GUID.hxx>
24 #include <Standard_Type.hxx>
25 #include <TDF_Attribute.hxx>
26 #include <TDF_AttributeDelta.hxx>
27 #include <TDF_DefaultDeltaOnModification.hxx>
28 #include <TDF_DefaultDeltaOnRemoval.hxx>
29 #include <TDF_DeltaOnAddition.hxx>
30 #include <TDF_Label.hxx>
31 #include <TDF_RelocationTable.hxx>
32 #include <TDF_Tool.hxx>
33
34 IMPLEMENT_STANDARD_RTTIEXT(DDataStd_DrawPresentation,TDF_Attribute)
35
36 #ifndef _WIN32
37 extern Draw_Viewer dout;
38 #else
39 Standard_IMPORT Draw_Viewer dout;
40 #endif
41 //=======================================================================
42 //function : IsDisplayed
43 //purpose  : 
44 //=======================================================================
45
46 Standard_Boolean DDataStd_DrawPresentation::IsDisplayed (const TDF_Label& L)
47 {  
48   Handle(DDataStd_DrawPresentation) P; 
49   if (L.FindAttribute(DDataStd_DrawPresentation::GetID(),P)) {
50     return P->IsDisplayed();
51   }
52   return Standard_False;
53 }
54
55
56 //=======================================================================
57 //function : HasPresentation
58 //purpose  : 
59 //=======================================================================
60
61 Standard_Boolean DDataStd_DrawPresentation::HasPresentation (const TDF_Label& L)
62 {
63   return (L.IsAttribute(DDataStd_DrawPresentation::GetID()));
64 }
65
66 //=======================================================================
67 //function : Display
68 //purpose  : 
69 //=======================================================================
70
71 void DDataStd_DrawPresentation::Display (const TDF_Label& L)
72 {  
73   Handle(DDataStd_DrawPresentation) P;
74   // set
75   if (!L.FindAttribute(DDataStd_DrawPresentation::GetID(),P)) {
76     P = new DDataStd_DrawPresentation ();  
77     L.AddAttribute(P);
78   } 
79   // display
80   if (P->IsDisplayed()) return;
81   if (P->GetDrawable().IsNull()) P->DrawBuild();  
82   DrawDisplay (P->Label(),P);
83   P->SetDisplayed(Standard_True);
84 }
85
86 //=======================================================================
87 //function : Erase
88 //purpose  : 
89 //=======================================================================
90
91 void DDataStd_DrawPresentation::Erase(const TDF_Label& L)
92 {  
93   Handle(DDataStd_DrawPresentation) P;
94   if (L.FindAttribute(DDataStd_DrawPresentation::GetID(),P)) {
95     if (P->IsDisplayed()) {
96       DrawErase (P->Label(),P);
97       P->SetDisplayed(Standard_False);
98     }  
99   }
100 }
101
102 //=======================================================================
103 //function : Update
104 //purpose  : 
105 //=======================================================================
106
107 void  DDataStd_DrawPresentation::Update (const TDF_Label& L)
108 {
109   Handle(DDataStd_DrawPresentation) P;
110   if (L.FindAttribute(DDataStd_DrawPresentation::GetID(),P)) {   
111     DrawErase(P->Label(),P);
112     P->Backup();
113     P->DrawBuild(); 
114     DrawDisplay (P->Label(),P);
115     P->SetDisplayed(Standard_True);
116   }
117 }
118
119 //=======================================================================
120 //function : GetID
121 //purpose  : idem GUID DDataStd Display
122 //=======================================================================
123
124 const Standard_GUID& DDataStd_DrawPresentation::GetID() 
125 {
126   static Standard_GUID DDataStd_DrawPresentationID("1c0296d4-6dbc-22d4-b9c8-0070b0ee301b");
127   return DDataStd_DrawPresentationID;
128 }
129
130
131 //=======================================================================
132 //function : DDataStd_DrawPresentation
133 //purpose  : 
134 //=======================================================================
135
136 DDataStd_DrawPresentation::DDataStd_DrawPresentation () : isDisplayed(Standard_False) {}
137
138
139 //=======================================================================
140 //function : SetDisplayed
141 //purpose  : 
142 //=======================================================================
143
144 void DDataStd_DrawPresentation::SetDisplayed (const Standard_Boolean status)
145
146   isDisplayed = status;
147 }
148
149 //=======================================================================
150 //function : IsDisplayed
151 //purpose  : 
152 //=======================================================================
153
154 Standard_Boolean DDataStd_DrawPresentation::IsDisplayed () const
155
156   return isDisplayed;
157 }
158
159 //=======================================================================
160 //function : SetDrawable
161 //purpose  : 
162 //=======================================================================
163
164 void DDataStd_DrawPresentation::SetDrawable (const Handle(Draw_Drawable3D)& D)
165
166   myDrawable = D;
167 }
168
169 //=======================================================================
170 //function : GetDrawable
171 //purpose  : 
172 //=======================================================================
173
174 Handle(Draw_Drawable3D) DDataStd_DrawPresentation::GetDrawable () const
175
176   return myDrawable;
177 }
178
179 //=======================================================================
180 //function : ID
181 //purpose  : 
182 //=======================================================================
183
184 const Standard_GUID& DDataStd_DrawPresentation::ID() const { return GetID(); }
185
186
187 //=======================================================================
188 //function : NewEmpty
189 //purpose  : 
190 //=======================================================================
191
192 Handle(TDF_Attribute) DDataStd_DrawPresentation::NewEmpty() const
193
194   return new DDataStd_DrawPresentation ();
195 }
196
197 //=======================================================================
198 //function : Restore
199 //purpose  : 
200 //=======================================================================
201
202 void DDataStd_DrawPresentation::Restore (const Handle(TDF_Attribute)& With) 
203 {  
204   //std::cout<< "DDataStd_DrawPresentation::Restore" << std::endl; 
205   isDisplayed = Handle(DDataStd_DrawPresentation)::DownCast (With)->IsDisplayed();
206   myDrawable  = Handle(DDataStd_DrawPresentation)::DownCast (With)->GetDrawable();
207 }
208
209 //=======================================================================
210 //function : Paste
211 //purpose  : 
212 //=======================================================================
213
214 void DDataStd_DrawPresentation::Paste (const Handle(TDF_Attribute)& Into,
215                                        const Handle(TDF_RelocationTable)&) const
216 {   
217   Handle(DDataStd_DrawPresentation)::DownCast(Into)->SetDisplayed(isDisplayed);  
218   Handle(DDataStd_DrawPresentation)::DownCast(Into)->SetDrawable(myDrawable);
219 }
220
221 //=======================================================================
222 //function : AfterAddition
223 //purpose  : erase if displayed
224 //=======================================================================
225
226 void DDataStd_DrawPresentation::AfterAddition() 
227
228   //std::cout<< "DDataStd_DrawPresentation::AfterAddition" << std::endl; 
229   //if (isDisplayed)  DrawDisplay ();
230 }
231
232 //=======================================================================
233 //function : BeforeRemoval
234 //purpose  : erase if displayed
235 //=======================================================================
236
237 void DDataStd_DrawPresentation::BeforeRemoval() 
238
239   //std::cout<< "DDataStd_DrawPresentation::BeforeRemoval" << std::endl; 
240   //if (isDisplayed)  DrawErase ();
241 }
242
243 //=======================================================================
244 //function : BeforeForget
245 //purpose  : erase if displayed
246 //=======================================================================
247
248 void DDataStd_DrawPresentation::BeforeForget() 
249
250   //std::cout<< "DDataStd_DrawPresentation::BeforeForget" << std::endl; 
251   if (isDisplayed)  DrawErase (Label(),this);
252 }
253
254
255 //=======================================================================
256 //function : AfterResume
257 //purpose  : display if displayed
258 //=======================================================================
259
260 void DDataStd_DrawPresentation::AfterResume() 
261 {
262   //std::cout<< "DDataStd_DrawPresentation::AfterResume"<< std::endl;
263   if (isDisplayed) DrawDisplay (Label(),this);
264 }
265
266 //=======================================================================
267 //function : BeforeUndo
268 //purpose  : the associated NamedShape should be present
269 //=======================================================================
270
271 Standard_Boolean DDataStd_DrawPresentation::BeforeUndo (const Handle(TDF_AttributeDelta)& AD,
272                                                         const Standard_Boolean /*forceIt*/) 
273 {
274   Handle(DDataStd_DrawPresentation) Pme = this;
275   Handle(DDataStd_DrawPresentation) Pdt = Handle(DDataStd_DrawPresentation)::DownCast(AD->Attribute());   
276   Handle(DDataStd_DrawPresentation) Pfw;
277   if (AD->Label().FindAttribute(GetID(),Pfw)) {  
278     //std::cout<< "DDataStd_DrawPresentation::BeforeUndo : attribute in framework" << std::endl;
279   }
280   else  {
281     //std::cout<< "DDataStd_DrawPresentation::BeforeUndo : attribute not in framework" << std::endl;
282   }
283   //
284   if (AD->IsKind(STANDARD_TYPE(TDF_DeltaOnAddition))) {    
285     //std::cout<< "DDataStd_DrawPresentation::BeforeUndo : delta on addition" << std::endl;
286     if (Pfw->IsDisplayed())  DrawErase(AD->Label(),Pfw);
287     //TDF appelle BeforeRemoval effacer
288   }
289   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnRemoval))) { 
290     //std::cout<< "DDataStd_DrawPresentation::BeforeUndo : delta on removal" << std::endl; 
291   }  
292   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnModification))) {     
293     //std::cout<< "DDataStd_DrawPresentation::BeforeUndo : delta on modification" << std::endl; 
294     if (Pfw->IsDisplayed()) DrawErase(AD->Label(),Pfw);
295   } 
296   else {  
297     //std::cout<< "DDataStd_DrawPresentation::BeforeUndo : unknown delta" << std::endl;
298   }  
299   //if (Label().IsNull()) std::cout<< "DDataStd_DrawPresentation::BeforeUndo : null label" << std::endl;      
300   return Standard_True;
301 }
302
303
304 //=======================================================================
305 //function : AfterUndo
306 //purpose  : associated NamedShape should be present
307 //=======================================================================
308
309 Standard_Boolean DDataStd_DrawPresentation::AfterUndo (const Handle(TDF_AttributeDelta)& AD,
310                                                        const Standard_Boolean /*forceIt*/) 
311 {   
312   Handle(DDataStd_DrawPresentation) Pme = this;
313   Handle(DDataStd_DrawPresentation) Pdt = Handle(DDataStd_DrawPresentation)::DownCast(AD->Attribute());   
314   Handle(DDataStd_DrawPresentation) Pfw;
315   if (AD->Label().FindAttribute(GetID(),Pfw)) {  
316     //std::cout<< "DDataStd_DrawPresentation::AfterUndo : attribute in framework" << std::endl;
317   }
318   else  {
319     //std::cout<< "DDataStd_DrawPresentation::AfterUndo : attribute not in framework" << std::endl;
320   }
321   
322   //
323   if (AD->IsKind(STANDARD_TYPE(TDF_DeltaOnAddition))) {
324     //std::cout<< "DDataStd_DrawPresentation::AfterUndo : delta on addition" << std::endl;
325   }
326   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnRemoval))) {  
327     //std::cout<< "DDataStd_DrawPresentation::AfterUndo : delta on removal" << std::endl;  
328     if (Pdt->IsDisplayed()) DrawDisplay(AD->Label(),Pdt); 
329   }  
330   else if (AD->IsKind(STANDARD_TYPE(TDF_DefaultDeltaOnModification))) {     
331     //std::cout<< "DDataStd_DrawPresentation::AfterUndo : delta on modification" << std::endl;
332     if (Pdt->IsDisplayed()) DrawDisplay(AD->Label(),Pdt);
333   }
334   else {
335     //std::cout<< "DDataStd_DrawPresentation::AfterUndo : unknown delta" << std::endl;  
336   }  
337   //if (Label().IsNull()) std::cout<< "DDataStd_DrawPresentation::AfterUndo : null label" << std::endl;   
338   return Standard_True;
339 }
340
341
342
343 //=======================================================================
344 //function : DrawBuild
345 //purpose  : 
346 //=======================================================================
347
348 void DDataStd_DrawPresentation::DrawBuild ()
349 {   
350   //std::cout<< "DDataStd_DrawPresentation::DrawBuild" << std::endl;     
351   Handle(DDataStd_DrawDriver) DD = DDataStd_DrawDriver::Get();
352   if (DD.IsNull()) {
353     DD = new DDataStd_DrawDriver ();
354     DDataStd_DrawDriver::Set(DD);
355   }
356   Handle(Draw_Drawable3D) D3D = DD->Drawable(Label()); 
357   if (D3D.IsNull()) std::cout<< "DDataStd_DrawPresentation::DrawBuild : null drawable" << std::endl;
358   myDrawable = D3D;
359 }
360
361
362
363 //=======================================================================
364 //function : DrawDisplay
365 //purpose  : 
366 //=======================================================================
367
368 void DDataStd_DrawPresentation::DrawDisplay (const TDF_Label& L,
369                                              const Handle(DDataStd_DrawPresentation)& P)
370 {     
371   if (!L.IsNull()) {     
372     if (!P->GetDrawable().IsNull()) {
373       TCollection_AsciiString S;
374       TDF_Tool::Entry(L,S);
375       Standard_CString name = S.ToCString();
376       Draw::Set(name,P->GetDrawable());
377       return;
378     }
379     else {    
380       //std::cout<< "DDataStd_DrawPresentation::DrawDisplay : null Drawable" << std::endl;
381       return;
382     }
383   }
384   std::cout<< "DDataStd_DrawPresentation::DrawDisplay : null Label" << std::endl;
385 }
386
387 //=======================================================================
388 //function : DrawErase
389 //purpose  : 
390 //=======================================================================
391
392 void DDataStd_DrawPresentation::DrawErase (const TDF_Label& L,
393                                            const Handle(DDataStd_DrawPresentation)& P)
394 {    
395   if (!L.IsNull()) {   
396     if (!P->GetDrawable().IsNull()) {
397       dout.RemoveDrawable(P->GetDrawable());
398       return;
399     }    
400     else {
401       //std::cout<< "DDataStd_DrawPresentation::DrawErase : null Drawable" << std::endl;
402       return;
403     }
404   }
405   std::cout<< "DDataStd_DrawPresentation::DrawErase : null Label" << std::endl;
406 }
407
408
409
410