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