0024002: Overall code and build procedure refactoring - samples
[occt.git] / samples / mfc / standard / 04_Viewer3d / src / OCCDemo_Presentation.cpp
1 // OCCDemo_Presentation.cpp: implementation of the OCCDemo_Presentation class.
2 // This is a base class for all presentations
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "OCCDemo_Presentation.h"
7 #include "Viewer3dView.h"
8 #include "ISession_Curve.h"
9
10 #include <AIS_InteractiveObject.hxx>
11 #include <Geom_Surface.hxx>
12 #include <Geom_Curve.hxx>
13 #include <Geom2d_Curve.hxx>
14 #include <Quantity_Color.hxx>
15 #include <AIS_Shape.hxx>
16 #include <BRepBuilderAPI_MakeFace.hxx>
17 #include <Precision.hxx>
18 #include <Geom_Line.hxx>
19 #include <Geom_TrimmedCurve.hxx>
20 #include <Prs3d_Drawer.hxx>
21 #include <Prs3d_ArrowAspect.hxx>
22 #include <AIS_Point.hxx>
23 #include <Geom_CartesianPoint.hxx>
24 #include <Geom2d_OffsetCurve.hxx>
25 #include <GeomAPI.hxx>
26 #include <gp_Pln.hxx>
27 #include <Geom_OffsetCurve.hxx>
28
29 #define MAX_PARAM 1000 // if a surface parameter is infinite, it is assingned
30 // this value in order to display the "infinit" object in the viewer.
31
32
33 Standard_Boolean OCCDemo_Presentation::WaitForInput (unsigned long aMilliSeconds)
34 {
35   //::WaitForSingleObject(::CreateEvent (NULL, FALSE, FALSE, NULL), aMilliSeconds);
36   if (::MsgWaitForMultipleObjects(0, NULL, FALSE, aMilliSeconds,
37     QS_KEY | QS_MOUSEBUTTON) != WAIT_TIMEOUT)
38   {
39     MSG msg;
40     if (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
41     {
42       if (msg.message == WM_KEYUP)
43       {
44         ::PeekMessage (&msg, NULL, 0, 0, PM_REMOVE);
45         return WaitForInput (aMilliSeconds);
46       }
47       else
48         return Standard_True;
49     }
50   }
51   return Standard_False;
52 }
53
54 //================================================================
55 // Function : fixParam
56 // Purpose  : assings a finite value to theParam if it intinite
57 //            (equal to +- Precision::Infinite())
58 //================================================================
59 static Standard_Boolean fixParam(Standard_Real& theParam)
60 {
61   Standard_Boolean aResult = Standard_False;
62   if (Precision::IsNegativeInfinite(theParam))
63   {
64     theParam = -MAX_PARAM;
65     aResult = Standard_True;
66   }
67   if (Precision::IsPositiveInfinite(theParam))
68   {
69     theParam = MAX_PARAM;
70     aResult = Standard_True;
71   }
72   return aResult;
73 }
74
75 //================================================================
76 // Function : DrawSurface                           
77 // Purpose  : displays a given geometric surface in 3d viewer
78 //            (creates a finite face and displays it)
79 //================================================================
80 Handle_AIS_InteractiveObject OCCDemo_Presentation::drawSurface
81                                   (const Handle_Geom_Surface& theSurface,
82                                    const Quantity_Color& theColor,
83                                    const Standard_Boolean toDisplay)
84 {
85   Standard_Real u1, u2, v1, v2;
86   theSurface->Bounds(u1,u2,v1,v2);
87   fixParam(u1);
88   fixParam(u2);
89   fixParam(v1);
90   fixParam(v2);
91
92   Handle_AIS_Shape aGraphicSurface = 
93     new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2, Precision::Confusion()));
94
95   getAISContext()->SetMaterial(aGraphicSurface, Graphic3d_NOM_PLASTIC, toDisplay);
96   getAISContext()->SetColor(aGraphicSurface, theColor, toDisplay);
97   if (toDisplay) {
98     if (FitMode){
99                 getAISContext()->Display (aGraphicSurface, Standard_False);
100                 CViewer3dDoc::Fit();
101         }
102         else
103                 getAISContext()->Display (aGraphicSurface);
104   }
105   
106   return aGraphicSurface;
107 }
108
109 //================================================================
110 // Function : DrawCurve                                 
111 // Purpose  : displays a given curve 3d
112 //================================================================
113 Handle_AIS_InteractiveObject OCCDemo_Presentation::drawCurve
114                                   (const Handle_Geom_Curve& theCurve,
115                                    const Quantity_Color& theColor,
116                                    const Standard_Boolean toDisplay)
117 {
118   Handle(ISession_Curve) aGraphicCurve = new ISession_Curve (theCurve);
119
120   getAISContext()->SetColor (aGraphicCurve, theColor, toDisplay);
121   aGraphicCurve->Attributes()->Link()->SetLineArrowDraw(Standard_False);
122   if (toDisplay){ 
123     if (FitMode){
124                 getAISContext()->Display (aGraphicCurve, Standard_False);
125                 CViewer3dDoc::Fit();
126         }
127         else
128                 getAISContext()->Display (aGraphicCurve);
129   }
130
131   return aGraphicCurve;
132 }
133
134 //================================================================
135 // Function : DrawCurve                                 
136 // Purpose  : displays a given curve 2d
137 //================================================================
138 Handle_AIS_InteractiveObject OCCDemo_Presentation::drawCurve
139                                   (const Handle_Geom2d_Curve& theCurve,
140                                    const Quantity_Color& theColor,
141                                    const Standard_Boolean toDisplay,
142                                    const gp_Ax2& aPosition)
143 {
144   // create 3D curve in plane
145   Handle(Geom_Curve) aCurve3d;
146   if (theCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
147   {
148     Handle(Geom2d_OffsetCurve) aOffCurve =
149       Handle(Geom2d_OffsetCurve)::DownCast(theCurve);
150     Handle(Geom_Curve) aBasCurve3d =
151       GeomAPI::To3d (aOffCurve->BasisCurve(), gp_Pln(aPosition));
152     Standard_Real aDist = aOffCurve->Offset();
153     aCurve3d = new Geom_OffsetCurve (aBasCurve3d, aDist, aPosition.Direction());
154   }
155   else
156   {
157     aCurve3d = GeomAPI::To3d (theCurve, gp_Pln(aPosition));
158   }
159   return drawCurve (aCurve3d, theColor, toDisplay);
160 }
161
162 //================================================================
163 // Function : drawPoint
164 // Purpose  : displays a given point
165 //================================================================
166 Handle_AIS_Point OCCDemo_Presentation::drawPoint
167                                   (const gp_Pnt& aPnt,
168                                    const Quantity_Color& theColor,
169                                    const Standard_Boolean toDisplay)
170 {
171   Handle(AIS_Point) aGraphicPoint = new AIS_Point (new Geom_CartesianPoint(aPnt));
172
173   getAISContext()->SetColor (aGraphicPoint, theColor, toDisplay);
174   if (toDisplay) {
175     getAISContext()->Display (aGraphicPoint);
176     //COCCDemoDoc::Fit();
177   }
178
179   return aGraphicPoint;
180 }
181
182 //================================================================
183 // Function : drawVector
184 // Purpose  : displays a given vector in 3d viewer
185 //            (segment of line starting at thePnt with the arrow at the end,
186 //             the length of segment is the length of the vector)
187 //================================================================
188 Handle_AIS_InteractiveObject OCCDemo_Presentation::drawVector 
189                                   (const gp_Pnt& thePnt,
190                                    const gp_Vec& theVec,
191                                    const Quantity_Color& theColor,
192                                    const Standard_Boolean toDisplay)
193 {
194   Standard_Real aLength = theVec.Magnitude();
195   if (aLength < Precision::Confusion())
196     return Handle(AIS_InteractiveObject)();
197
198   Handle(Geom_Curve) aCurve = new Geom_Line (thePnt, theVec);
199   aCurve = new Geom_TrimmedCurve (aCurve, 0, aLength);
200
201   Handle(ISession_Curve) aGraphicCurve = new ISession_Curve (aCurve);
202
203   getAISContext()->SetColor (aGraphicCurve, theColor, toDisplay);
204   Handle(Prs3d_Drawer) aDrawer = aGraphicCurve->Attributes()->Link();
205   aDrawer->SetLineArrowDraw(Standard_True);
206   aDrawer->ArrowAspect()->SetLength(aLength/10);
207   if (toDisplay) {
208     if (FitMode){
209                 getAISContext()->Display (aGraphicCurve, Standard_False);
210                 CViewer3dDoc::Fit();
211         }
212         else
213                 getAISContext()->Display (aGraphicCurve);
214   }
215
216   return aGraphicCurve;
217 }
218
219
220 Handle_AIS_Shape OCCDemo_Presentation::drawShape 
221          (const TopoDS_Shape& theShape,const Quantity_Color& theColor,
222           const Standard_Boolean toDisplay)
223 {
224   Handle_AIS_Shape aGraphicShape = new AIS_Shape(theShape);
225
226   getAISContext()->SetMaterial(aGraphicShape, Graphic3d_NOM_PLASTIC, toDisplay);
227   getAISContext()->SetColor (aGraphicShape, theColor, toDisplay);
228   if (toDisplay){ 
229     if (FitMode){
230                 getAISContext()->Display (aGraphicShape, Standard_False);
231                 CViewer3dDoc::Fit();
232         }
233         else
234                 getAISContext()->Display (aGraphicShape);
235   }
236
237   return aGraphicShape;
238 }
239
240 Handle_AIS_Shape OCCDemo_Presentation::drawShape
241          (const TopoDS_Shape& theShape,
242           const Graphic3d_NameOfMaterial theMaterial,
243           const Standard_Boolean toDisplay)
244 {
245   Handle_AIS_Shape aGraphicShape = new AIS_Shape(theShape);
246
247   getAISContext()->SetMaterial(aGraphicShape, theMaterial, toDisplay);
248   if (toDisplay) {
249     if (FitMode){
250                 getAISContext()->Display (aGraphicShape, Standard_False);
251                 CViewer3dDoc::Fit();
252         }
253         else
254                 getAISContext()->Display (aGraphicShape);
255   }
256
257   return aGraphicShape;
258 }
259
260 void OCCDemo_Presentation::GetViewAt (V3d_Coordinate& theX, V3d_Coordinate& theY, V3d_Coordinate& theZ)
261 {
262   CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
263   CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
264   CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
265   pView->GetViewAt (theX, theY, theZ);
266 }
267
268 void OCCDemo_Presentation::SetViewAt (const V3d_Coordinate theX, const V3d_Coordinate theY, const V3d_Coordinate theZ)
269 {
270   CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
271   CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
272   CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
273   pView->SetViewAt (theX, theY, theZ);
274 }
275
276 void OCCDemo_Presentation::GetViewEye(V3d_Coordinate& X, V3d_Coordinate& Y, V3d_Coordinate& Z)
277 {
278         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
279         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
280         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
281         pView->GetViewEye(X,Y,Z);
282 }
283
284 void OCCDemo_Presentation::SetViewEye(V3d_Coordinate X, V3d_Coordinate Y, V3d_Coordinate Z)
285 {
286         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
287         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
288         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
289         pView->SetViewEye(X,Y,Z);
290 }
291
292 Quantity_Factor OCCDemo_Presentation::GetViewScale()
293 {
294         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
295         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
296         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
297         return pView->GetViewScale();
298 }
299
300 void OCCDemo_Presentation::SetViewScale(Quantity_Factor Coef)
301 {
302         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
303         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
304         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
305         pView->SetViewScale(Coef);
306 }
307
308 void OCCDemo_Presentation::ResetView()
309 {
310         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
311         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
312         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
313         pView->Reset();
314 }
315
316 Handle_AIS_InteractiveContext OCCDemo_Presentation::getAISContext() const 
317 {
318         return myDoc->GetAISContext();
319 }
320
321 Handle_V3d_Viewer OCCDemo_Presentation::getViewer() const
322 {
323         return myDoc->GetViewer();
324 }
325
326 Standard_CString OCCDemo_Presentation::GetDataDir()
327 {
328         return myDoc->GetDataDir();
329 }