0028110: Configuration - specify Unicode charset instead of multibyte in project...
[occt.git] / src / Draw / Draw_Grid.cxx
1 // Created on: 1994-02-03
2 // Created by: Jean Marc LACHAUME
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Draw_Appli.hxx>
19 #include <Draw_Color.hxx>
20 #include <Draw_ColorKind.hxx>
21 #include <Draw_Display.hxx>
22 #include <Draw_Grid.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Trsf.hxx>
25 #include <Standard_Type.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(Draw_Grid,Draw_Drawable3D)
28
29 static Standard_Real MinimumStep = 1.e-3 ;
30 static Standard_Real Ratio       = 200.0 ;
31
32 extern Draw_Viewer dout;
33
34 //=======================================================================
35 // Function : Draw_Grid
36 // Purpose  : Constructor.
37 //=======================================================================
38
39 Draw_Grid::Draw_Grid () :
40        myStepX    (0.0) ,
41        myStepY    (0.0) ,
42        myStepZ    (0.0) ,
43        myIsActive (Standard_False)
44 {
45 }
46
47 //=======================================================================
48 // Function : Steps
49 // Purpose  : Sets the steps along the X, Y & Z axis.
50 //=======================================================================
51
52 void Draw_Grid::Steps (const Standard_Real StepX,
53                                 const Standard_Real StepY,
54                                 const Standard_Real StepZ)
55 {
56   myStepX = Abs (StepX) ;
57   myStepY = Abs (StepY) ;
58   myStepZ = Abs (StepZ) ;
59   myIsActive =    myStepX > MinimumStep
60                && myStepY > MinimumStep
61                && myStepZ > MinimumStep ;
62 }
63
64 //=======================================================================
65 // Function : DrawOn
66 // Purpose  : Displays the grid.
67 //=======================================================================
68
69 void Draw_Grid::DrawOn (Draw_Display& Out) const
70 {
71   if (!myIsActive) return ;
72   
73   Standard_Integer xmin, xmax, ymin, ymax ;
74   Standard_Integer IndexX, IndexY ;
75   Standard_Real StepX, StepY ;
76   Standard_Integer MinIndexX, MaxIndexX, MinIndexY, MaxIndexY ;
77   Standard_Real Offset ;
78   Standard_Real zoom, Xmin, Xmax, Ymin, Ymax ;
79   gp_Trsf T ;
80   gp_Pnt Pnt1, Pnt2 ;
81
82   Standard_Integer IdtView ;
83   char *Type ;
84
85   IdtView = Out.ViewId () ;
86   if (!dout.HasView (IdtView)) return ;
87   Type = dout.GetType (IdtView) ;
88   switch (*(Type+1)) {
89     case 'X' : StepX = myStepX ; break ;
90     case 'Y' : StepX = myStepY ; break ;
91     case 'Z' : StepX = myStepZ ; break ;
92     default  : StepX = 0.0 ; break ;
93   }
94   switch (*(Type+3)) {
95     case 'X' : StepY = myStepX ; break ;
96     case 'Y' : StepY = myStepY ; break ;
97     case 'Z' : StepY = myStepZ ; break ;
98     default  : StepY = 0.0 ; break ;
99   }
100   
101   if (StepX > MinimumStep && StepY > MinimumStep) {
102
103     dout.GetFrame (IdtView, xmin, ymin, xmax, ymax) ;
104     dout.GetTrsf  (IdtView, T) ; T.Invert () ;
105     zoom = dout.Zoom (IdtView) ;
106
107     Xmin = ((Standard_Real) xmin) / zoom ;
108     Xmax = ((Standard_Real) xmax) / zoom ;
109     Ymin = ((Standard_Real) ymin) / zoom ;
110     Ymax = ((Standard_Real) ymax) / zoom ;
111
112     Offset = Min (Xmax - Xmin, Ymax - Ymin) / Ratio ;
113
114     MinIndexX = (Standard_Integer) (Xmin / StepX) ;
115     MaxIndexX = (Standard_Integer) (Xmax / StepX) ;
116     MinIndexY = (Standard_Integer) (Ymin / StepY) ;
117     MaxIndexY = (Standard_Integer) (Ymax / StepY) ;
118
119     for (IndexX = MinIndexX ; IndexX <= MaxIndexX ; IndexX++) {
120       for (IndexY = MinIndexY ; IndexY <= MaxIndexY ; IndexY++) {
121         Standard_Real X = ((Standard_Real) IndexX) * StepX ;
122         Standard_Real Y = ((Standard_Real) IndexY) * StepY ;
123         
124         Pnt1.SetCoord (X - Offset, Y, 0.0) ; Pnt1.Transform (T) ;
125         Pnt2.SetCoord (X + Offset, Y, 0.0) ; Pnt2.Transform (T) ;
126         Out.SetColor (Draw_Color (Draw_bleu)) ;
127         Out.Draw (Pnt1, Pnt2) ;
128
129         Pnt1.SetCoord (X, Y - Offset, 0.0) ; Pnt1.Transform (T) ;
130         Pnt2.SetCoord (X, Y + Offset, 0.0) ; Pnt2.Transform (T) ;
131         Out.SetColor (Draw_Color (Draw_bleu)) ;
132         Out.Draw (Pnt1, Pnt2) ;
133       }
134     }
135
136   }
137 }