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