16f2a674e2d703f6982043a1583eef83e1445b33
[occt.git] / src / V3d / V3d_Viewer_4.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 #include <Aspect_Background.hxx>
16 #include <Aspect_GradientBackground.hxx>
17 #include <Aspect_Grid.hxx>
18 #include <gp_Ax3.hxx>
19 #include <Graphic3d_ArrayOfPoints.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <Graphic3d_GraphicDriver.hxx>
22 #include <Graphic3d_Group.hxx>
23 #include <Graphic3d_Structure.hxx>
24 #include <Quantity_Color.hxx>
25 #include <V3d_BadValue.hxx>
26 #include <V3d_CircularGrid.hxx>
27 #include <V3d_Light.hxx>
28 #include <V3d_RectangularGrid.hxx>
29 #include <V3d_View.hxx>
30 #include <V3d_Viewer.hxx>
31 #include <Visual3d_View.hxx>
32 #include <Visual3d_ViewManager.hxx>
33
34 // =======================================================================
35 // function : Grid
36 // purpose  :
37 // =======================================================================
38 Handle(Aspect_Grid) V3d_Viewer::Grid() const
39 {
40   switch (myGridType)
41   {
42     case Aspect_GT_Circular:    return myCGrid;
43     case Aspect_GT_Rectangular: return myRGrid;
44   }
45   return myRGrid;
46 }
47
48 // =======================================================================
49 // function : GridType
50 // purpose  :
51 // =======================================================================
52 Aspect_GridType V3d_Viewer::GridType() const
53 {
54   return myGridType;
55 }
56
57 // =======================================================================
58 // function : GridDrawMode
59 // purpose  :
60 // =======================================================================
61 Aspect_GridDrawMode V3d_Viewer::GridDrawMode() const
62 {
63   return Grid()->DrawMode();
64 }
65
66 // =======================================================================
67 // function : ActivateGrid
68 // purpose  :
69 // =======================================================================
70 void V3d_Viewer::ActivateGrid (const Aspect_GridType     theType,
71                                const Aspect_GridDrawMode theMode)
72 {
73   Grid()->Erase();
74   myGridType = theType;
75   Grid()->SetDrawMode (theMode);
76   if (theMode != Aspect_GDM_None)
77   {
78     Grid()->Display();
79   }
80   Grid()->Activate();
81   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
82   {
83     ActiveView()->SetGrid (myPrivilegedPlane, Grid());
84   }
85   Update();
86 }
87
88 // =======================================================================
89 // function : DeactivateGrid
90 // purpose  :
91 // =======================================================================
92 void V3d_Viewer::DeactivateGrid()
93 {
94   Grid()->Erase();
95   myGridType = Aspect_GT_Rectangular;
96   Grid()->Deactivate();
97   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
98   {
99     ActiveView()->SetGridActivity (Standard_False);
100     if (myGridEcho
101     && !myGridEchoStructure.IsNull())
102     {
103       ActiveView()->View()->EraseImmediate (myGridEchoStructure);
104     }
105   }
106   Update();
107 }
108
109 // =======================================================================
110 // function : IsActive
111 // purpose  :
112 // =======================================================================
113 Standard_Boolean V3d_Viewer::IsActive() const
114 {
115   return Grid()->IsActive();
116 }
117
118 // =======================================================================
119 // function : RectangularGridValues
120 // purpose  :
121 // =======================================================================
122 void V3d_Viewer::RectangularGridValues (Quantity_Length&     theXOrigin,
123                                         Quantity_Length&     theYOrigin,
124                                         Quantity_Length&     theXStep,
125                                         Quantity_Length&     theYStep,
126                                         Quantity_PlaneAngle& theRotationAngle) const
127 {
128   theXOrigin       = myRGrid->XOrigin();
129   theYOrigin       = myRGrid->YOrigin();
130   theXStep         = myRGrid->XStep();
131   theYStep         = myRGrid->YStep();
132   theRotationAngle = myRGrid->RotationAngle();
133 }
134
135 // =======================================================================
136 // function : SetRectangularGridValues
137 // purpose  :
138 // =======================================================================
139 void V3d_Viewer::SetRectangularGridValues (const Quantity_Length     theXOrigin,
140                                            const Quantity_Length     theYOrigin,
141                                            const Quantity_Length     theXStep,
142                                            const Quantity_Length     theYStep,
143                                            const Quantity_PlaneAngle theRotationAngle)
144 {
145   myRGrid->SetGridValues (theXOrigin, theYOrigin, theXStep, theYStep, theRotationAngle);
146   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
147   {
148     ActiveView()->SetGrid (myPrivilegedPlane, myRGrid);
149   }
150   Update();
151 }
152
153 // =======================================================================
154 // function : CircularGridValues
155 // purpose  :
156 // =======================================================================
157 void V3d_Viewer::CircularGridValues (Quantity_Length&     theXOrigin,
158                                      Quantity_Length&     theYOrigin,
159                                      Quantity_Length&     theRadiusStep,
160                                      Standard_Integer&    theDivisionNumber,
161                                      Quantity_PlaneAngle& theRotationAngle) const
162 {
163   theXOrigin        = myCGrid->XOrigin();
164   theYOrigin        = myCGrid->YOrigin();
165   theRadiusStep     = myCGrid->RadiusStep();
166   theDivisionNumber = myCGrid->DivisionNumber();
167   theRotationAngle  = myCGrid->RotationAngle();
168 }
169
170 // =======================================================================
171 // function : SetCircularGridValues
172 // purpose  :
173 // =======================================================================
174 void V3d_Viewer::SetCircularGridValues (const Quantity_Length     theXOrigin,
175                                         const Quantity_Length     theYOrigin,
176                                         const Quantity_Length     theRadiusStep,
177                                         const Standard_Integer    theDivisionNumber,
178                                         const Quantity_PlaneAngle theRotationAngle)
179 {
180   myCGrid->SetGridValues (theXOrigin, theYOrigin, theRadiusStep,
181                           theDivisionNumber, theRotationAngle);
182   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
183   {
184     ActiveView()->SetGrid (myPrivilegedPlane, myCGrid);
185   }
186   Update();
187 }
188
189 // =======================================================================
190 // function : RectangularGridGraphicValues
191 // purpose  :
192 // =======================================================================
193 void V3d_Viewer::RectangularGridGraphicValues (Quantity_Length& theXSize,
194                                                Quantity_Length& theYSize,
195                                                Quantity_Length& theOffSet) const
196 {
197   myRGrid->GraphicValues (theXSize, theYSize, theOffSet);
198 }
199
200 // =======================================================================
201 // function : SetRectangularGridGraphicValues
202 // purpose  :
203 // =======================================================================
204 void V3d_Viewer::SetRectangularGridGraphicValues (const Quantity_Length theXSize,
205                                                   const Quantity_Length theYSize,
206                                                   const Quantity_Length theOffSet)
207 {
208   myRGrid->SetGraphicValues (theXSize, theYSize, theOffSet);
209   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
210   {
211     ActiveView()->SetGridGraphicValues (myRGrid);
212   }
213   Update();
214 }
215
216 // =======================================================================
217 // function : CircularGridGraphicValues
218 // purpose  :
219 // =======================================================================
220 void V3d_Viewer::CircularGridGraphicValues (Quantity_Length& theRadius,
221                                             Quantity_Length& theOffSet) const
222 {
223   myCGrid->GraphicValues (theRadius, theOffSet);
224 }
225
226 // =======================================================================
227 // function : SetCircularGridGraphicValues
228 // purpose  :
229 // =======================================================================
230 void V3d_Viewer::SetCircularGridGraphicValues (const Quantity_Length theRadius,
231                                                const Quantity_Length theOffSet)
232 {
233   myCGrid->SetGraphicValues (theRadius, theOffSet);
234   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
235   {
236     ActiveView()->SetGridGraphicValues (myCGrid);
237   }
238   Update();
239 }
240
241 // =======================================================================
242 // function : SetGridEcho
243 // purpose  :
244 // =======================================================================
245 void V3d_Viewer::SetGridEcho (const Standard_Boolean theToShowGrid)
246 {
247   if (myGridEcho == theToShowGrid)
248   {
249     return;
250   }
251
252   myGridEcho = theToShowGrid;
253   if (theToShowGrid
254    || myGridEchoStructure.IsNull())
255   {
256     return;
257   }
258
259   for (InitActiveViews(); MoreActiveViews(); NextActiveViews())
260   {
261     ActiveView()->View()->EraseImmediate (myGridEchoStructure);
262   }
263 }
264
265 // =======================================================================
266 // function : SetGridEcho
267 // purpose  :
268 // =======================================================================
269 void V3d_Viewer::SetGridEcho (const Handle(Graphic3d_AspectMarker3d)& theMarker)
270 {
271   if (myGridEchoStructure.IsNull())
272   {
273     myGridEchoStructure = new Graphic3d_Structure (Viewer());
274     myGridEchoGroup     = myGridEchoStructure->NewGroup();
275   }
276
277   myGridEchoAspect = theMarker;
278   myGridEchoGroup->SetPrimitivesAspect (theMarker);
279 }
280
281 // =======================================================================
282 // function : GridEcho
283 // purpose  :
284 // =======================================================================
285 Standard_Boolean V3d_Viewer::GridEcho() const
286 {
287   return myGridEcho;
288 }
289
290 // =======================================================================
291 // function : ShowGridEcho
292 // purpose  :
293 // =======================================================================
294 void V3d_Viewer::ShowGridEcho (const Handle(V3d_View)& theView,
295                                const Graphic3d_Vertex& theVertex)
296 {
297   if (!myGridEcho)
298   {
299     return;
300   }
301
302   if (myGridEchoStructure.IsNull())
303   {
304     myGridEchoStructure = new Graphic3d_Structure (Viewer());
305     myGridEchoGroup     = myGridEchoStructure->NewGroup();
306
307     myGridEchoAspect    = new Graphic3d_AspectMarker3d (Aspect_TOM_STAR, Quantity_Color (Quantity_NOC_GRAY90), 3.0);
308     myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
309   }
310
311   if (theVertex.X() == myGridEchoLastVert.X()
312    && theVertex.Y() == myGridEchoLastVert.Y()
313    && theVertex.Z() == myGridEchoLastVert.Z())
314   {
315     return;
316   }
317
318   myGridEchoLastVert = theVertex;
319   myGridEchoGroup->Clear();
320   myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
321
322   Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
323   anArrayOfPoints->AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z());
324   myGridEchoGroup->AddPrimitiveArray (anArrayOfPoints);
325
326   theView->View()->DisplayImmediate (myGridEchoStructure, Standard_True);
327 }
328
329 // =======================================================================
330 // function : HideGridEcho
331 // purpose  :
332 // =======================================================================
333 void V3d_Viewer::HideGridEcho (const Handle(V3d_View)& theView)
334 {
335   if (myGridEchoStructure.IsNull())
336   {
337     return;
338   }
339
340   myGridEchoLastVert.SetCoord (ShortRealLast(), ShortRealLast(), ShortRealLast());
341   theView->View()->EraseImmediate (myGridEchoStructure);
342 }