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