0028895: Visualization, V3d_View::SetComputedMode() - HLR calculation is performed...
[occt.git] / samples / mfc / occtdemo / Common / WNT / OCCDemoView.cpp
CommitLineData
7fd59977 1// OCCDemoView.cpp : implementation of the COCCDemoView class
2//
3
4#include "stdafx.h"
5#include "MainFrm.h"
6#include "OCCDemo.h"
7#include "OCCDemoDoc.h"
8#include "OCCDemoView.h"
9
10#include <Graphic3d_GraphicDriver.hxx>
7fd59977 11
12#define ValZWMin 1
13
14#ifdef _DEBUG
15#undef THIS_FILE
16static char THIS_FILE[] = __FILE__;
17#endif
18
19/////////////////////////////////////////////////////////////////////////////
20// COCCDemoView
21
22IMPLEMENT_DYNCREATE(COCCDemoView, CView)
23
24BEGIN_MESSAGE_MAP(COCCDemoView, CView)
25 //{{AFX_MSG_MAP(COCCDemoView)
26 ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
27 ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
28 ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
29 ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
30 ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
31 ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
32 ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
33 ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
34 ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
35 ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
36 ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
37 ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
38 ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
39 ON_WM_SIZE()
40 ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
41 ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
42 ON_WM_LBUTTONDOWN()
43 ON_WM_LBUTTONUP()
44 ON_WM_MOUSEMOVE()
45 ON_WM_RBUTTONDOWN()
46 ON_WM_RBUTTONUP()
47 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
48 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
49 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
50 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
51 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
52 ON_COMMAND(ID_BUTTONWire, OnBUTTONWire)
53 ON_COMMAND(ID_BUTTONShade, OnBUTTONShade)
54 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
55 ON_UPDATE_COMMAND_UI(ID_BUTTONShade, OnUpdateBUTTONShade)
56 ON_UPDATE_COMMAND_UI(ID_BUTTONWire, OnUpdateBUTTONWire)
57 //}}AFX_MSG_MAP
58END_MESSAGE_MAP()
59
60/////////////////////////////////////////////////////////////////////////////
61// COCCDemoView construction/destruction
62
63COCCDemoView::COCCDemoView()
64{
65 myXmin=0;
66 myYmin=0;
67 myXmax=0;
68 myYmax=0;
69 myCurZoom=0;
70 myCurrentMode = CurAction3d_Nothing;
71 myVisMode = VIS_SHADE;
72 m_Pen = NULL;
73 myGraphicDriver = Handle(Graphic3d_GraphicDriver)::DownCast(
74 ((COCCDemoApp*)AfxGetApp())->GetGraphicDevice()->GraphicDriver() );
75}
76
77COCCDemoView::~COCCDemoView()
78{
79 if (!myView.IsNull())
80 myView->Remove();
81 if (m_Pen)
82 delete m_Pen;
83}
84
85BOOL COCCDemoView::PreCreateWindow(CREATESTRUCT& cs)
86{
87 // TODO: Modify the Window class or styles here by modifying
88 // the CREATESTRUCT cs
89
90 return CView::PreCreateWindow(cs);
91}
92
93/////////////////////////////////////////////////////////////////////////////
94// COCCDemoView drawing
95
96void COCCDemoView::OnInitialUpdate()
97{
98 CView::OnInitialUpdate();
99
100 myView = GetDocument()->GetViewer()->CreateView();
101
102 Handle(Graphic3d_WNTGraphicDevice) theGraphicDevice =
103 ((COCCDemoApp*)AfxGetApp())->GetGraphicDevice();
104
105 Handle(WNT_Window) aWNTWindow = new WNT_Window(theGraphicDevice,GetSafeHwnd ());
106 myView->SetWindow(aWNTWindow);
107 if (!aWNTWindow->IsMapped()) aWNTWindow->Map();
108
109 myCurrentMode = CurAction3d_Nothing;
110 myVisMode = VIS_SHADE;
111 RedrawVisMode();
112}
113
114void COCCDemoView::OnDraw(CDC* pDC)
115{
116 COCCDemoDoc* pDoc = GetDocument();
117 ASSERT_VALID(pDoc);
118 // TODO: add draw code for native data here
119 myView->Redraw();
120}
121
122/////////////////////////////////////////////////////////////////////////////
123// COCCDemoView diagnostics
124
125#ifdef _DEBUG
126void COCCDemoView::AssertValid() const
127{
128 CView::AssertValid();
129}
130
131void COCCDemoView::Dump(CDumpContext& dc) const
132{
133 CView::Dump(dc);
134}
135
136COCCDemoDoc* COCCDemoView::GetDocument() // non-debug version is inline
137{
138 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COCCDemoDoc)));
139 return (COCCDemoDoc*)m_pDocument;
140}
141#endif //_DEBUG
142
143/////////////////////////////////////////////////////////////////////////////
144// COCCDemoView message handlers
145
146void COCCDemoView::OnSize(UINT nType, int cx, int cy)
147{
148 if (!myView.IsNull())
149 myView->MustBeResized();
150}
151
152void COCCDemoView::OnBUTTONBack()
153{
154 myView->SetProj(V3d_Ypos);
155}
156
157void COCCDemoView::OnBUTTONFront()
158{
159 myView->SetProj(V3d_Yneg);
160}
161
162void COCCDemoView::OnBUTTONBottom()
163{
164 myView->SetProj(V3d_Zneg);
165}
166
167void COCCDemoView::OnBUTTONTop()
168{
169 myView->SetProj(V3d_Zpos);
170}
171
172void COCCDemoView::OnBUTTONLeft()
173{
174 myView->SetProj(V3d_Xneg);
175}
176
177void COCCDemoView::OnBUTTONRight()
178{
179 myView->SetProj(V3d_Xpos);
180}
181
182void COCCDemoView::OnBUTTONAxo()
183{
184 myView->SetProj(V3d_XposYnegZpos);
185}
186
187void COCCDemoView::OnBUTTONPan()
188{
189 myCurrentMode = CurAction3d_DynamicPanning;
190}
191
192void COCCDemoView::OnBUTTONPanGlo()
193{
194 // save the current zoom value
195 myCurZoom = myView->Scale();
196 // Do a Global Zoom
197 myView->FitAll();
198 // Set the mode
199 myCurrentMode = CurAction3d_GlobalPanning;
200}
201
202void COCCDemoView::OnBUTTONReset()
203{
204 myView->Reset();
205}
206
207void COCCDemoView::OnBUTTONRot()
208{
209 myCurrentMode = CurAction3d_DynamicRotation;
210}
211
212void COCCDemoView::OnBUTTONZoomAll()
213{
214 myView->FitAll();
215 myView->ZFitAll();
216}
217
218void COCCDemoView::OnBUTTONZoomProg()
219{
220 myCurrentMode = CurAction3d_DynamicZooming;
221}
222
223void COCCDemoView::OnBUTTONZoomWin()
224{
225 myCurrentMode = CurAction3d_WindowZooming;
226}
227
228void COCCDemoView::OnLButtonDown(UINT nFlags, CPoint point)
229{
230 // save the current mouse coordinate in min
231 myXmin=point.x; myYmin=point.y;
232 myXmax=point.x; myYmax=point.y;
233
234 if ( ! (nFlags & MK_CONTROL) )
235 {
236 if (myCurrentMode == CurAction3d_DynamicRotation)
237 {
238 myView->SetComputedMode(Standard_False);
239 myView->StartRotation(point.x,point.y);
240 }
241 }
242}
243
244void COCCDemoView::OnLButtonUp(UINT nFlags, CPoint point)
245{
246 if ( !(nFlags & MK_CONTROL) )
247 {
248 switch (myCurrentMode)
249 {
250 case CurAction3d_Nothing :
251 myXmax=point.x;
252 myYmax=point.y;
253 break;
254 case CurAction3d_DynamicZooming :
255 myCurrentMode = CurAction3d_Nothing;
256 break;
257 case CurAction3d_WindowZooming :
258 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
259 myXmax=point.x;
260 myYmax=point.y;
261 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
262 // Test if the zoom window is greater than a minimale window.
263 {
264 // Do the zoom window between Pmin and Pmax
265 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
266 }
267 myCurrentMode = CurAction3d_Nothing;
268 break;
269 case CurAction3d_DynamicPanning :
270 myCurrentMode = CurAction3d_Nothing;
271 break;
272 case CurAction3d_GlobalPanning :
273 myView->Place(point.x,point.y,myCurZoom);
274 myCurrentMode = CurAction3d_Nothing;
275 break;
276 case CurAction3d_DynamicRotation :
277 if (myVisMode == VIS_HLR)
278 {
279 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
280 myView->SetComputedMode(Standard_True);
1eeef710 281 myView->Redraw();
7fd59977 282 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
283 }
284 myCurrentMode = CurAction3d_Nothing;
285 break;
286 } //switch (myCurrentMode)
287 }
288}
289
290void COCCDemoView::OnRButtonDown(UINT nFlags, CPoint point)
291{
292 if ( nFlags & MK_CONTROL )
293 {
294 myView->SetComputedMode(Standard_False);
295 myView->StartRotation(point.x,point.y);
296 }
297}
298
299void COCCDemoView::OnRButtonUp(UINT nFlags, CPoint point)
300{
301 if (myVisMode == VIS_HLR)
302 {
303 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
304 myView->SetComputedMode(Standard_True);
1eeef710 305 myView->Redraw();
7fd59977 306 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
307 }
308}
309
310void COCCDemoView::OnMouseMove(UINT nFlags, CPoint point)
311{
312 // ============================ LEFT BUTTON =======================
313 if ( nFlags & MK_LBUTTON)
314 {
315 if ( nFlags & MK_CONTROL )
316 {
317 // move with MB1 and Control : on the dynamic zooming
318 // Do the zoom in function of mouse's coordinates
319 myView->Zoom(myXmax,myYmax,point.x,point.y);
320 // save the current mouse coordinate in max
321 myXmax = point.x;
322 myYmax = point.y;
323 }
324 else // if ( Ctrl )
325 {
326 switch (myCurrentMode)
327 {
328 case CurAction3d_Nothing :
329 myXmax = point.x;
330 myYmax = point.y;
331 break;
332 case CurAction3d_DynamicZooming :
333 myView->Zoom(myXmax,myYmax,point.x,point.y);
334 // save the current mouse coordinate in max;
335 myXmax=point.x;
336 myYmax=point.y;
337 break;
338 case CurAction3d_WindowZooming :
339 myXmax = point.x; myYmax = point.y;
340 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
341 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
342 break;
343 case CurAction3d_DynamicPanning :
344 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
345 myXmax = point.x; myYmax = point.y;
346 break;
347 case CurAction3d_GlobalPanning : // nothing
348 break;
349 case CurAction3d_DynamicRotation :
350 myView->Rotation(point.x,point.y);
351 myView->Redraw();
352 break;
353 }
354 }
355 }
356 // ============================ MIDDLE BUTTON =======================
357 else if ( nFlags & MK_MBUTTON)
358 {
359 if ( nFlags & MK_CONTROL )
360 {
361 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
362 myXmax = point.x; myYmax = point.y;
363 }
364 }
365 // ============================ RIGHT BUTTON =======================
366 else if ( nFlags & MK_RBUTTON)
367 {
368 if ( nFlags & MK_CONTROL )
369 {
370 myView->Rotation(point.x,point.y);
371 }
372 }
373 // ============================ NO BUTTON =======================
374 else
375 {
376 myXmax = point.x;
377 myYmax = point.y;
378 }
379}
380
381void COCCDemoView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
382{
383 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
384 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
385}
386
387void COCCDemoView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
388{
389 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
390 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
391}
392
393void COCCDemoView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
394{
395 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
396 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
397}
398
399void COCCDemoView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
400{
401 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
402 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
403}
404
405void COCCDemoView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
406{
407 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
408 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
409}
410
411void COCCDemoView::DrawRectangle(const Standard_Integer MinX,
412 const Standard_Integer MinY,
413 const Standard_Integer MaxX,
414 const Standard_Integer MaxY,
415 const Standard_Boolean Draw,
416 const LineStyle aLineStyle)
417{
418 static int m_DrawMode;
419 if (!m_Pen && aLineStyle ==Solid )
420 {
421 m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;
422 }
423 else if (!m_Pen && aLineStyle ==Dot )
424 {
425 m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;
426 }
427 else if (!m_Pen && aLineStyle == ShortDash)
428 {
429 m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;
430 }
431 else if (!m_Pen && aLineStyle == LongDash)
432 {
433 m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;
434 }
435 else if (aLineStyle == Default)
436 {
437 m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;
438 }
439
440 CPen* aOldPen;
441 CClientDC clientDC(this);
442 if (m_Pen)
443 aOldPen = clientDC.SelectObject(m_Pen);
444 clientDC.SetROP2(m_DrawMode);
445
446 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
447 static Standard_Boolean m_IsVisible = Standard_False;
448
449 if ( m_IsVisible && !Draw) // move or up : erase at the old position
450 {
451 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
452 clientDC.LineTo(StoredMaxX,StoredMaxY);
453 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
454 m_IsVisible = false;
455 }
456
457 StoredMinX = min ( MinX, MaxX );
458 StoredMinY = min ( MinY, MaxY );
459 StoredMaxX = max ( MinX, MaxX );
460 StoredMaxY = max ( MinY, MaxY);
461
462 if (Draw) // move : draw
463 {
464 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
465 clientDC.LineTo(StoredMaxX,StoredMaxY);
466 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
467 m_IsVisible = true;
468 }
469
470 if (m_Pen)
471 clientDC.SelectObject(aOldPen);
472}
473
474void COCCDemoView::InitButtons()
475{
476 myXmin=0;
477 myYmin=0;
478 myXmax=0;
479 myYmax=0;
480 myCurZoom=0;
481 myCurrentMode = CurAction3d_Nothing;
482}
483
484void COCCDemoView::Reset()
485{
486 InitButtons();
487 myVisMode = VIS_SHADE;
488 if (!myView.IsNull())
489 {
490 RedrawVisMode();
491 myView->Reset();
492 }
493}
494
495void COCCDemoView::RedrawVisMode()
496{
497 switch (myVisMode)
498 {
499 case VIS_WIREFRAME:
500 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);
501 myView->SetComputedMode (Standard_False);
1eeef710 502 myView->Redraw();
7fd59977 503 break;
504 case VIS_SHADE:
505 GetDocument()->GetAISContext()->SetDisplayMode(AIS_Shaded);
506 myView->SetComputedMode (Standard_False);
1eeef710 507 myView->Redraw();
7fd59977 508 break;
509 case VIS_HLR:
510 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
511 myView->SetComputedMode (Standard_True);
1eeef710 512 myView->Redraw();
7fd59977 513 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
514 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);
515 break;
516 }
517}
518
519void COCCDemoView::OnBUTTONWire()
520{
521 myVisMode = VIS_WIREFRAME;
522 RedrawVisMode();
523}
524
525void COCCDemoView::OnBUTTONShade()
526{
527 myVisMode = VIS_SHADE;
528 RedrawVisMode();
529}
530
531void COCCDemoView::OnBUTTONHlrOn()
532{
533 myVisMode = VIS_HLR;
534 RedrawVisMode();
535}
536
537void COCCDemoView::OnUpdateBUTTONWire(CCmdUI* pCmdUI)
538{
539 pCmdUI->SetCheck (myVisMode == VIS_WIREFRAME);
540 pCmdUI->Enable (myVisMode != VIS_WIREFRAME);
541}
542
543void COCCDemoView::OnUpdateBUTTONShade(CCmdUI* pCmdUI)
544{
545 pCmdUI->SetCheck (myVisMode == VIS_SHADE);
546 pCmdUI->Enable (myVisMode != VIS_SHADE);
547}
548
549void COCCDemoView::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI)
550{
551 pCmdUI->SetCheck (myVisMode == VIS_HLR);
552 pCmdUI->Enable (myVisMode != VIS_HLR);
553}
554
ee2be2a8 555void COCCDemoView::GetViewCenter(Standard_Real& Xc, Standard_Real& Yc)
7fd59977 556{
557 myView->Center(Xc,Yc);
558}
559
ee2be2a8 560void COCCDemoView::SetViewCenter(Standard_Real Xc, Standard_Real Yc)
7fd59977 561{
562 myView->SetCenter(Xc,Yc);
563}
564
ee2be2a8 565void COCCDemoView::GetViewEye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)
7fd59977 566{
567 myView->Eye(X,Y,Z);
568}
569
ee2be2a8 570void COCCDemoView::SetViewEye(Standard_Real X, Standard_Real Y, Standard_Real Z)
7fd59977 571{
572 myView->SetEye(X,Y,Z);
573}
574
ee2be2a8 575Standard_Real COCCDemoView::GetViewScale()
7fd59977 576{
577 return myView->Scale();
578}
579
ee2be2a8 580void COCCDemoView::SetViewScale(Standard_Real Coef)
7fd59977 581{
582 myView->SetScale(Coef);
583}