0026272: Visualization - provide a possibility to activate selection modes without...
[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 Handle(Aspect_Grid) (myCGrid);
43   case Aspect_GT_Rectangular: return Handle(Aspect_Grid) (myRGrid);
44   }
45   return Handle(Aspect_Grid) (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       myGridEchoStructure->Erase();
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   myGridEchoStructure->Erase();
260 }
261
262 // =======================================================================
263 // function : SetGridEcho
264 // purpose  :
265 // =======================================================================
266 void V3d_Viewer::SetGridEcho (const Handle(Graphic3d_AspectMarker3d)& theMarker)
267 {
268   if (myGridEchoStructure.IsNull())
269   {
270     myGridEchoStructure = new Graphic3d_Structure (Viewer());
271     myGridEchoGroup     = myGridEchoStructure->NewGroup();
272   }
273
274   myGridEchoAspect = theMarker;
275   myGridEchoGroup->SetPrimitivesAspect (theMarker);
276 }
277
278 // =======================================================================
279 // function : GridEcho
280 // purpose  :
281 // =======================================================================
282 Standard_Boolean V3d_Viewer::GridEcho() const
283 {
284   return myGridEcho;
285 }
286
287 // =======================================================================
288 // function : ShowGridEcho
289 // purpose  :
290 // =======================================================================
291 void V3d_Viewer::ShowGridEcho (const Handle(V3d_View)& theView,
292                                const Graphic3d_Vertex& theVertex)
293 {
294   if (!myGridEcho)
295   {
296     return;
297   }
298
299   if (myGridEchoStructure.IsNull())
300   {
301     myGridEchoStructure = new Graphic3d_Structure (Viewer());
302     myGridEchoGroup     = myGridEchoStructure->NewGroup();
303
304     myGridEchoAspect    = new Graphic3d_AspectMarker3d (Aspect_TOM_STAR, Quantity_Color (Quantity_NOC_GRAY90), 3.0);
305     myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
306   }
307
308   if (theVertex.X() == myGridEchoLastVert.X()
309    && theVertex.Y() == myGridEchoLastVert.Y()
310    && theVertex.Z() == myGridEchoLastVert.Z())
311   {
312     return;
313   }
314
315   myGridEchoLastVert = theVertex;
316   myGridEchoGroup->Clear();
317   myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
318
319   Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
320   anArrayOfPoints->AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z());
321   myGridEchoGroup->AddPrimitiveArray (anArrayOfPoints);
322
323   myGridEchoStructure->SetZLayer (Graphic3d_ZLayerId_Topmost);
324   myGridEchoStructure->SetInfiniteState (Standard_True);
325   myGridEchoStructure->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
326   myGridEchoStructure->CStructure()->ViewAffinity->SetVisible (Standard_False);
327   myGridEchoStructure->CStructure()->ViewAffinity->SetVisible (theView->View()->Identification(), true);
328   myGridEchoStructure->Display();
329 }
330
331 // =======================================================================
332 // function : HideGridEcho
333 // purpose  :
334 // =======================================================================
335 void V3d_Viewer::HideGridEcho (const Handle(V3d_View)& theView)
336 {
337   if (myGridEchoStructure.IsNull())
338   {
339     return;
340   }
341
342   myGridEchoLastVert.SetCoord (ShortRealLast(), ShortRealLast(), ShortRealLast());
343   const Handle(Graphic3d_ViewAffinity)& anAffinity = myGridEchoStructure->CStructure()->ViewAffinity;
344   if (!anAffinity.IsNull() && anAffinity->IsVisible (theView->View()->Identification()))
345     myGridEchoStructure->Erase();
346 }