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