0024023: Revamp the OCCT Handle -- general
[occt.git] / src / Visual3d / Visual3d_Layer.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 /***********************************************************************
15
16 FONCTION :
17 --------
18 Classe Visual3d_Layer :
19
20 HISTORIQUE DES MODIFICATIONS :
21 ----------------------------
22 Novembre 1998 : CAL : Creation.
23 ??-??-?? : ??? ; 
24
25 ************************************************************************/
26
27 // for the class
28 #include <Visual3d_Layer.ixx>
29
30 #include <Graphic3d_TypeOfPrimitive.hxx>
31 #include <Visual3d_ViewManager.hxx>
32
33 //-Aliases
34 #define MyViewManager ((Visual3d_ViewManager *) MyPtrViewManager)
35
36 //-Global data definitions
37 static Standard_Boolean theLayerState = Standard_False;
38 static Graphic3d_TypeOfPrimitive theTypeOfPrimitive = Graphic3d_TOP_UNDEFINED;
39
40 #define NO_TRACE_CALL
41 #define NO_DEBUG
42
43 //
44 //-Constructors
45 //
46
47 Visual3d_Layer::Visual3d_Layer (const Handle(Visual3d_ViewManager)& AManager, const Aspect_TypeOfLayer ALayerType, const Standard_Boolean AFlag) {
48 #ifdef TRACE_CALL
49   cout << "Visual3d_Layer::Visual3d_Layer" << endl;
50 #endif
51   MyPtrViewManager  = (void *) AManager.operator->();
52
53   MyGraphicDriver = MyViewManager->GraphicDriver();
54
55   MyCLayer.layerType  = int (ALayerType);
56   MyCLayer.ptrLayer = NULL;
57   MyCLayer.ortho[0] = -1.0;
58   MyCLayer.ortho[1] =  1.0;
59   MyCLayer.ortho[2] = -1.0;
60   MyCLayer.ortho[3] =  1.0;
61   MyCLayer.attach   = 0; // Aspect_TOC_BOTTOM_LEFT
62   MyCLayer.sizeDependent  = (AFlag ? 1 : 0);
63
64   MyViewManager->SetLayer (this);
65   MyGraphicDriver->Layer (MyCLayer);
66   MyCLayer.ptrLayer->layerData = this;
67
68 #ifdef OCCT_DEBUG
69   cout << "Visual3d_Layer::Visual3d_Layer" << endl;
70   call_def_ptrLayer ptrLayer;
71   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
72   if (ptrLayer == NULL)
73     cout << "\tptrLayer == NULL" << endl;
74   else
75     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
76 #endif
77 }
78
79 //
80 //-Destructors
81 //
82
83 void Visual3d_Layer::Destroy () {
84 #ifdef TRACE_CALL
85   cout << "Visual3d_Layer::Destroy" << endl;
86 #endif
87   MyGraphicDriver->RemoveLayer (MyCLayer);
88 }
89
90 //-Methods, in order
91
92 void Visual3d_Layer::Begin () {
93 #ifdef TRACE_CALL
94   cout << "Visual3d_Layer::Begin" << endl;
95 #endif
96   if (theLayerState)
97     Visual3d_LayerDefinitionError::Raise
98     ("Layer is already open !");
99
100   MyGraphicDriver->BeginLayer (MyCLayer);
101   theLayerState = Standard_True;
102 }
103
104 void Visual3d_Layer::End () {
105 #ifdef TRACE_CALL
106   cout << "Visual3d_Layer::End" << endl;
107 #endif
108 #ifdef OCCT_DEBUG
109   cout << "Visual3d_Layer::End" << endl;
110   call_def_ptrLayer ptrLayer;
111   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
112   if (ptrLayer == NULL)
113     cout << "\tptrLayer == NULL" << endl;
114   else
115     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
116 #endif
117   if (! theLayerState)
118     Visual3d_LayerDefinitionError::Raise
119     ("Layer is not open !");
120
121   // Flush all graphics
122   MyGraphicDriver->EndLayer ();
123   theLayerState = Standard_False;
124 #ifdef OCCT_DEBUG
125   cout << "Visual3d_Layer::End" << endl;
126   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
127   if (ptrLayer == NULL)
128     cout << "\tptrLayer == NULL" << endl;
129   else
130     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
131 #endif
132 }
133
134 void Visual3d_Layer::Clear () {
135 #ifdef TRACE_CALL
136   cout << "Visual3d_Layer::Clear" << endl;
137 #endif
138   if (theLayerState)
139     Visual3d_LayerDefinitionError::Raise
140     ("Layer is open !");
141
142   MyGraphicDriver->ClearLayer (MyCLayer);
143 }
144
145 //
146 //-Graphic definition methods
147 //
148
149 void Visual3d_Layer::BeginPolyline () {
150 #ifdef TRACE_CALL
151   cout << "Visual3d_Layer::BeginPolyline" << endl;
152 #endif
153   if (! theLayerState)
154     Visual3d_LayerDefinitionError::Raise
155     ("Layer is not open !");
156   if( theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
157     Visual3d_LayerDefinitionError::Raise
158     ("One primitive is already open !");
159
160   theTypeOfPrimitive = Graphic3d_TOP_POLYLINE;
161
162   MyGraphicDriver->BeginPolyline2d ();
163 }
164
165 void Visual3d_Layer::BeginPolygon () {
166 #ifdef TRACE_CALL
167   cout << "Visual3d_Layer::BeginPolygon" << endl;
168 #endif
169   if (! theLayerState)
170     Visual3d_LayerDefinitionError::Raise
171     ("Layer is not open !");
172   if( theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
173     Visual3d_LayerDefinitionError::Raise
174     ("One primitive is already open !");
175
176   theTypeOfPrimitive = Graphic3d_TOP_POLYGON;
177
178   MyGraphicDriver->BeginPolygon2d ();
179 }
180
181 void Visual3d_Layer::AddVertex (const Standard_Real X, const Standard_Real Y, const Standard_Boolean AFlag) {
182 #ifdef TRACE_CALL
183   cout << "Visual3d_Layer::AddVertex" << endl;
184 #endif
185   Standard_ShortReal x = Standard_ShortReal(X);
186   Standard_ShortReal y = Standard_ShortReal(Y);
187
188   switch (theTypeOfPrimitive) {
189     case Graphic3d_TOP_POLYLINE :
190       if( AFlag ) MyGraphicDriver->Draw (x, y);
191       else        MyGraphicDriver->Move (x, y);
192       break;
193     case Graphic3d_TOP_POLYGON :
194       MyGraphicDriver->Edge (x, y);
195       break;
196     default:
197       Visual3d_LayerDefinitionError::Raise
198         ("Bad Primitive type!");
199   }
200 }
201
202 void Visual3d_Layer::ClosePrimitive () {
203 #ifdef TRACE_CALL
204   cout << "Visual3d_Layer::ClosePrimitive" << endl;
205 #endif
206   switch (theTypeOfPrimitive) {
207     case Graphic3d_TOP_POLYLINE :
208       MyGraphicDriver->EndPolyline2d ();
209       break;
210     case Graphic3d_TOP_POLYGON :
211       MyGraphicDriver->EndPolygon2d ();
212       break;
213     default:
214       Visual3d_LayerDefinitionError::Raise
215         ("Bad Primitive type!");
216   }
217
218   theTypeOfPrimitive = Graphic3d_TOP_UNDEFINED;
219 }
220
221 void Visual3d_Layer::DrawRectangle (const Standard_Real X, const Standard_Real Y, const Standard_Real Width, const Standard_Real Height) {
222 #ifdef TRACE_CALL
223   cout << "Visual3d_Layer::DrawRectangle" << endl;
224 #endif
225   if (! theLayerState)
226     Visual3d_LayerDefinitionError::Raise
227     ("Layer is not open !");
228   if (theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
229     Visual3d_LayerDefinitionError::Raise
230     ("One primitive is already open !");
231
232   Standard_ShortReal x = Standard_ShortReal (X);
233   Standard_ShortReal y = Standard_ShortReal (Y);
234   Standard_ShortReal width = Standard_ShortReal (Width);
235   Standard_ShortReal height = Standard_ShortReal (Height);
236   MyGraphicDriver->Rectangle (x, y, width, height);
237 }
238
239 #ifdef WNT
240 #undef DrawText
241 #endif
242 void Visual3d_Layer::DrawText (const Standard_CString AText, const Standard_Real X, const Standard_Real Y, const Standard_Real AHeight) {
243 #ifdef TRACE_CALL
244   cout << "Visual3d_Layer::DrawText" << endl;
245 #endif
246   if (! theLayerState)
247     Visual3d_LayerDefinitionError::Raise
248     ("Layer is not open !");
249   if (theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
250     Visual3d_LayerDefinitionError::Raise
251     ("One primitive is already open !");
252
253   Standard_ShortReal x = Standard_ShortReal (X);
254   Standard_ShortReal y = Standard_ShortReal (Y);
255   Standard_ShortReal height = Standard_ShortReal (AHeight);
256   MyGraphicDriver->Text (AText, x, y, height);
257 }
258
259 void Visual3d_Layer::SetColor (const Quantity_Color& AColor) {
260 #ifdef TRACE_CALL
261   cout << "Visual3d_Layer::SetColor" << endl;
262 #endif
263   if (! theLayerState)
264     Visual3d_LayerDefinitionError::Raise
265     ("Layer is not open !");
266
267   Standard_ShortReal r = Standard_ShortReal (AColor.Red());
268   Standard_ShortReal g = Standard_ShortReal (AColor.Green());
269   Standard_ShortReal b = Standard_ShortReal (AColor.Blue());
270 #ifdef TRACE_CALL
271   cout << "\tSetColor : " << r << ", " << g << ", " << b << endl;
272 #endif
273   MyGraphicDriver->SetColor (r, g, b);
274 }
275
276 void Visual3d_Layer::SetTransparency (const Standard_ShortReal ATransparency) {
277 #ifdef TRACE_CALL
278   cout << "Visual3d_Layer::SetTransparency" << endl;
279 #endif
280   if (! theLayerState)
281     Visual3d_LayerDefinitionError::Raise
282     ("Layer is not open !");
283
284 #ifdef TRACE_CALL
285   cout << "\tSetTransparency : " << ATransparency << endl;
286 #endif
287   MyGraphicDriver->SetTransparency (ATransparency);
288 }
289
290 void Visual3d_Layer::UnsetTransparency () {
291 #ifdef TRACE_CALL
292   cout << "Visual3d_Layer::UnsetTransparency" << endl;
293 #endif
294   if (! theLayerState)
295     Visual3d_LayerDefinitionError::Raise
296     ("Layer is not open !");
297
298 #ifdef TRACE_CALL
299   cout << "\tUnsetTransparency" << endl;
300 #endif
301   MyGraphicDriver->UnsetTransparency ();
302 }
303
304 #ifdef WNT
305 #undef DrawText
306 #endif
307 void Visual3d_Layer::TextSize (const Standard_CString AText,  const Standard_Real AHeight, Standard_Real& AWidth, Standard_Real& AnAscent, Standard_Real& ADescent) const {
308   Standard_ShortReal aWidth, anAscent, aDescent;
309
310 #ifdef TRACE_CALL
311   cout << "Visual3d_Layer::TextSize" << endl;
312 #endif
313   if (! theLayerState)
314     Visual3d_LayerDefinitionError::Raise
315     ("Layer is not open !");
316   if (theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
317     Visual3d_LayerDefinitionError::Raise
318     ("One primitive is already open !");
319
320   MyGraphicDriver->TextSize (AText, (Standard_ShortReal)AHeight, aWidth, anAscent, aDescent);
321   AWidth = aWidth;
322   AnAscent = anAscent;
323   ADescent = aDescent;
324 }
325
326 void Visual3d_Layer::SetLineAttributes (const Aspect_TypeOfLine AType, const Standard_Real AWidth) {
327 #ifdef TRACE_CALL
328   cout << "Visual3d_Layer::SetLineAttributes" << endl;
329 #endif
330   if (! theLayerState)
331     Visual3d_LayerDefinitionError::Raise
332     ("Layer is not open !");
333
334   Standard_Integer type = AType;
335   Standard_ShortReal width = Standard_ShortReal (AWidth);
336 #ifdef TRACE_CALL
337   cout << "\tSetLineAttributes : " << type << ", " << width << endl;
338 #endif
339   MyGraphicDriver->SetLineAttributes (type, width);
340 }
341
342 void Visual3d_Layer::SetTextAttributes (const Standard_CString AFont, const Aspect_TypeOfDisplayText AType, const Quantity_Color& AColor) {
343 #ifdef TRACE_CALL
344   cout << "Visual3d_Layer::SetTextAttributes" << endl;
345 #endif
346   if (! theLayerState)
347     Visual3d_LayerDefinitionError::Raise
348     ("Layer is not open !");
349
350   Standard_CString font = (char*)AFont;
351   Standard_Integer type = AType;
352   Standard_ShortReal r = Standard_ShortReal (AColor.Red());
353   Standard_ShortReal g = Standard_ShortReal (AColor.Green());
354   Standard_ShortReal b = Standard_ShortReal (AColor.Blue());
355 #ifdef TRACE_CALL
356   cout << "\tSetTextAttributes : " << font << ", " << type << endl;
357   cout << "\t                    " << r << ", " << g << ", " << b << endl;
358 #endif
359   MyGraphicDriver->SetTextAttributes (font, type, r, g, b);
360 }
361
362 void Visual3d_Layer::SetOrtho (const Standard_Real Left, const Standard_Real Right, const Standard_Real Bottom, const Standard_Real Top, const Aspect_TypeOfConstraint Attach) {
363 #ifdef TRACE_CALL
364   cout << "Visual3d_Layer::SetOrtho" << endl;
365 #endif
366
367   MyCLayer.ortho[0] = Standard_ShortReal (Left);
368   MyCLayer.ortho[1] = Standard_ShortReal (Right);
369   MyCLayer.ortho[2] = Standard_ShortReal (Bottom);
370   MyCLayer.ortho[3] = Standard_ShortReal (Top);
371   MyCLayer.attach   = Standard_Integer (Attach);
372 }
373
374 void Visual3d_Layer::SetViewport (const Standard_Integer Width, const Standard_Integer Height) {
375 #ifdef TRACE_CALL
376   cout << "Visual3d_Layer::SetViewport" << endl;
377 #endif
378
379   MyCLayer.viewport[0]  = float (Width);
380   MyCLayer.viewport[1]  = float (Height);
381 }
382
383 Aspect_CLayer2d Visual3d_Layer::CLayer () const {
384 #ifdef TRACE_CALL
385   cout << "Visual3d_Layer::CLayer" << endl;
386 #endif
387 #ifdef OCCT_DEBUG
388   cout << "Visual3d_Layer::CLayer" << endl;
389   call_def_ptrLayer ptrLayer;
390   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
391   if (ptrLayer == NULL)
392     cout << "\tptrLayer == NULL" << endl;
393   else
394     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
395 #endif
396   return MyCLayer;
397 }
398
399 Aspect_TypeOfLayer Visual3d_Layer::Type () const {
400 #ifdef TRACE_CALL
401   cout << "Visual3d_Layer::Type" << endl;
402 #endif
403   return Aspect_TypeOfLayer (MyCLayer.layerType);
404 }
405
406
407 void Visual3d_Layer::AddLayerItem( const Handle(Visual3d_LayerItem)& Item )
408 {
409   MyListOfLayerItems.Append( Item );
410 }
411
412 void Visual3d_Layer::RemoveLayerItem( const Handle(Visual3d_LayerItem)& Item )
413 {
414   if ( MyListOfLayerItems.IsEmpty() )
415     return;
416
417   Visual3d_NListOfLayerItem::Iterator it( MyListOfLayerItems );
418   for( ; it.More(); it.Next() ) {
419     if ( it.Value() == Item ) {
420       MyListOfLayerItems.Remove( it );
421       break;
422     }
423   }
424 }
425
426 void Visual3d_Layer::RemoveAllLayerItems()
427 {
428   MyListOfLayerItems.Clear();
429 }
430
431 const Visual3d_NListOfLayerItem& Visual3d_Layer::GetLayerItemList() const
432 {
433   return MyListOfLayerItems;
434 }
435
436 void Visual3d_Layer::RenderLayerItems() const
437 {
438   theLayerState = Standard_True;
439
440   const Visual3d_NListOfLayerItem& items = GetLayerItemList();
441   Visual3d_NListOfLayerItem::Iterator it(items);
442   for(; it.More(); it.Next() ) {
443     it.Value()->RedrawLayerPrs();
444   }
445
446   theLayerState = Standard_False;
447 }