0025773: Visualization - remove AIS_Drawer class and transfer its Link() logic to...
[occt.git] / samples / mfc / standard / 10_Convert / src / OCCDemo_Presentation.cpp
CommitLineData
7fd59977 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 "OCCDemoView.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>
6262338c 20#include <Prs3d_Drawer.hxx>
7fd59977 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
33Standard_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//================================================================
59static 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//================================================================
80Handle_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 =
9181b66d 93 new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2, Precision::Confusion()));
7fd59977 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 COCCDemoDoc::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//================================================================
113Handle_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 COCCDemoDoc::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//================================================================
138Handle_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//================================================================
166Handle_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//================================================================
188Handle_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 COCCDemoDoc::Fit();
211 }
212 else
213 getAISContext()->Display (aGraphicCurve);
214 }
215
216 return aGraphicCurve;
217}
218
219
220Handle_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 COCCDemoDoc::Fit();
232 }
233 else
234 getAISContext()->Display (aGraphicShape);
235 }
236
237 return aGraphicShape;
238}
239
240Handle_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 COCCDemoDoc::Fit();
252 }
253 else
254 getAISContext()->Display (aGraphicShape);
255 }
256
257 return aGraphicShape;
258}
259
9764ccbb 260void OCCDemo_Presentation::GetViewAt (V3d_Coordinate& theX, V3d_Coordinate& theY, V3d_Coordinate& theZ)
7fd59977 261{
9764ccbb 262 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
263 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
264 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
265 pView->GetViewAt (theX, theY, theZ);
7fd59977 266}
267
9764ccbb 268void OCCDemo_Presentation::SetViewAt (const V3d_Coordinate theX, const V3d_Coordinate theY, const V3d_Coordinate theZ)
7fd59977 269{
9764ccbb 270 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
271 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
272 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
273 pView->SetViewAt (theX, theY, theZ);
7fd59977 274}
275
276void 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 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
281 pView->GetViewEye(X,Y,Z);
282}
283
284void 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 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
289 pView->SetViewEye(X,Y,Z);
290}
291
292Quantity_Factor OCCDemo_Presentation::GetViewScale()
293{
294 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
295 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
296 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
297 return pView->GetViewScale();
298}
299
300void OCCDemo_Presentation::SetViewScale(Quantity_Factor Coef)
301{
302 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
303 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
304 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
305 pView->SetViewScale(Coef);
306}
307
308void OCCDemo_Presentation::ResetView()
309{
310 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
311 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
312 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
313 pView->Reset();
9764ccbb 314}
315
316void OCCDemo_Presentation::TranslateView (const V3d_Coordinate theX, const V3d_Coordinate theY)
317{
318 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
319 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
320 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
321 pView->Translate (theX, theY);
322}