0023831: Ghosts appears in 2D viewer of HLR MFC sample
[occt.git] / samples / mfc / standard / 08_HLR / src / SelectionDialog.cpp
CommitLineData
7fd59977 1// SelectionDialog.cpp : implementation file
2//
3
4#include "stdafx.h"
5
6#include "SelectionDialog.h"
7
8#include "HLRApp.h"
9#include "OCC_2dView.h"
10#include <ISession2D/ISession2D_Shape.h>
11#include "Prs3d_Projector.hxx"
12
13#ifdef _DEBUG
14//#define new DEBUG_NEW
15#undef THIS_FILE
16static char THIS_FILE[] = __FILE__;
17#endif
18
19// the key for shortcut ( use to activate dynamic rotation, panning )
20#define CASCADESHORTCUTKEY MK_CONTROL
21
22/////////////////////////////////////////////////////////////////////////////
23// CSelectionDialog dialog
24
25CSelectionDialog::CSelectionDialog(CHLRDoc* aDoc,CWnd* pParent /*=NULL*/)
97319109 26: CDialog(CSelectionDialog::IDD, pParent)
7fd59977 27{
97319109 28 myDoc = aDoc;
29 myDisplay = false;
30 //{{AFX_DATA_INIT(CSelectionDialog)
31 m_Algo = 0;
32 m_DisplayMode = 0;
33 m_NbIsos = 2;
34 m_DrawHiddenLine = TRUE;
35 m_DegeneratedModeOn = TRUE;
36 //}}AFX_DATA_INIT
7fd59977 37}
38
39void CSelectionDialog::DoDataExchange(CDataExchange* pDX)
40{
97319109 41 CDialog::DoDataExchange(pDX);
42 //{{AFX_DATA_MAP(CSelectionDialog)
43 DDX_Radio(pDX, IDC_PolyAlgo, m_Algo);
44 DDX_Radio(pDX, IDC_DisplayDefault, m_DisplayMode);
45 DDX_Text(pDX, IDC_EDIT_NBIsos, m_NbIsos);
46 DDX_Check(pDX, IDC_DrawHiddenLine, m_DrawHiddenLine);
47 DDX_Check(pDX, IDC_DegeneratedMode, m_DegeneratedModeOn);
48 //}}AFX_DATA_MAP
7fd59977 49}
50
51
52BEGIN_MESSAGE_MAP(CSelectionDialog, CDialog)
97319109 53 //{{AFX_MSG_MAP(CSelectionDialog)
54 ON_BN_CLICKED(ID_GetShape, OnGetShape)
55 ON_BN_CLICKED(IDC_DisplayDefault, OnDisplayDefault)
56 ON_BN_CLICKED(IDC_VIsoParametrics, OnVIsoParametrics)
57 ON_BN_CLICKED(IDC_VApparentContour, OnVApparentContour)
58 ON_BN_CLICKED(IDC_VSewingEdges, OnVSewingEdges)
59 ON_BN_CLICKED(IDC_VsharpEdges, OnVsharpEdges)
60 ON_BN_CLICKED(IDC_VsmoothEdges, OnVsmoothEdges)
61 ON_BN_CLICKED(IDC_HsharpEdges, OnHsharpEdges)
62 ON_BN_CLICKED(IDC_HsmoothEdges, OnHsmoothEdges)
63 ON_BN_CLICKED(IDC_HSewingEdges, OnHSewingEdges)
64 ON_BN_CLICKED(IDC_HIsoParametrics, OnHIsoParametrics)
65 ON_BN_CLICKED(IDC_HApparentContour, OnHApparentContour)
66 ON_EN_CHANGE(IDC_EDIT_NBIsos, OnChangeEDITNBIsos)
67 ON_BN_CLICKED(IDC_Algo, OnAlgo)
68 ON_BN_CLICKED(IDC_PolyAlgo, OnPolyAlgo)
69 ON_BN_CLICKED(ID_Update2D, OnUpdate2D)
70 ON_BN_CLICKED(IDC_TopView, OnTopView)
71 ON_BN_CLICKED(IDC_BottomView, OnBottomView)
72 ON_BN_CLICKED(IDC_LeftView, OnLeftView)
73 ON_BN_CLICKED(IDC_RightView, OnRightView)
74 ON_BN_CLICKED(IDC_FrontView, OnFrontView)
75 ON_BN_CLICKED(IDC_BackView, OnBackView)
76 ON_BN_CLICKED(IDC_AxoView, OnAxoView)
77 ON_WM_LBUTTONDOWN()
78 ON_WM_LBUTTONUP()
79 ON_WM_RBUTTONDOWN()
80 ON_WM_RBUTTONUP()
81 ON_WM_MOUSEMOVE()
82 ON_BN_CLICKED(IDC_DrawHiddenLine, OnDrawHiddenLine)
83 ON_BN_CLICKED(IDC_DegeneratedMode, OnDegeneratedMode)
84 ON_WM_DRAWITEM()
85 ON_WM_PAINT()
86 //}}AFX_MSG_MAP
7fd59977 87END_MESSAGE_MAP()
88
89/////////////////////////////////////////////////////////////////////////////
90// CSelectionDialog message handlers
91
92BOOL CSelectionDialog::OnInitDialog()
93{
97319109 94 CDialog::OnInitDialog();
95
96 VERIFY(TopView.AutoLoad(IDC_TopView, this));
97 VERIFY(BottomView.AutoLoad(IDC_BottomView, this)) ;
98 VERIFY(LeftView .AutoLoad(IDC_LeftView , this)) ;
99 VERIFY(RightView .AutoLoad(IDC_RightView , this)) ;
100 VERIFY(FrontView .AutoLoad(IDC_FrontView , this)) ;
101 VERIFY(BackView .AutoLoad(IDC_BackView , this)) ;
102 VERIFY(AxoView .AutoLoad(IDC_AxoView , this)) ;
103
104 // get the View Window position to managed mouse move
105 CRect BoxRect,ViewRect;
106 GetWindowRect(BoxRect);
107 CWnd * TheViewerWindow = GetDlgItem(IDC_DUMMYBUTTON);
108 TheViewerWindow->GetWindowRect(ViewRect);
109 myPosMinX = ViewRect.TopLeft().x - BoxRect.TopLeft().x;
110 myPosMaxX = ViewRect.Width()+myPosMinX;
111 myPosMinY = ViewRect.TopLeft().y - BoxRect.TopLeft().y;
112 myPosMaxY = myPosMinY + ViewRect.Height();
113
114 ShowHideButton(Standard_False);
115 OnDisplay(true);
116
117 return TRUE; // return TRUE unless you set the focus to a control
118 // EXCEPTION: OCX Property Pages should return FALSE
7fd59977 119}
120
121void CSelectionDialog::OnDisplay(bool isFit)
122{
97319109 123 GetDlgItem(IDC_DUMMYBUTTON)->SetRedraw(true);
124 if(!myDisplay) {
125 Handle(Graphic3d_GraphicDriver) aGraphicDriver =
126 ((CHLRApp*)AfxGetApp())->GetGraphicDriver();
7fd59977 127
de75ed09 128 myActiveViewer = new V3d_Viewer(aGraphicDriver,(short *) "Visu3D");
129 myActiveViewer->SetDefaultLights();
130 myActiveViewer->SetLightOn();
131 myActiveView = myActiveViewer->CreateView();
7fd59977 132
de75ed09 133 Handle(WNT_Window) aWNTWindow = new WNT_Window (GetDlgItem(IDC_DUMMYBUTTON)->GetSafeHwnd(),
134 Quantity_NOC_GRAY);
135 myActiveView->SetComputedMode (m_HlrModeIsOn);
136 myActiveView->SetWindow(aWNTWindow);
7fd59977 137
de75ed09 138 myInteractiveContext = new AIS_InteractiveContext(myActiveViewer);
7fd59977 139
de75ed09 140 // TRIHEDRON
141 Handle(Geom_Axis2Placement) aTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
142 myTrihedron=new AIS_Trihedron(aTrihedronAxis);
7fd59977 143
de75ed09 144 myInteractiveContext->Display(myTrihedron);
97319109 145 }
146 if(isFit) {
147 myActiveView->ZFitAll();
148 myActiveView->FitAll();
149 }
150 myActiveView->Redraw();
151 myDisplay = Standard_True;
152 GetDlgItem(IDC_DUMMYBUTTON)->SetRedraw(false);
7fd59977 153}
154
155
156void CSelectionDialog::SetTitle(CString & aTitle)
157{
158 SetWindowText(aTitle);
159}
160
161void CSelectionDialog::OnGetShape()
162{
163 UpdateData(true);
97319109 164 myDoc->GetInteractiveContext2D()->RemoveAll();
7fd59977 165 myDisplayableShape = new ISession2D_Shape( );
166 UpdateProjector();
167 myDisplayableShape->SetNbIsos(m_NbIsos);
168
169 myInteractiveContext->EraseAll(Standard_False);
170 myInteractiveContext->Display(myTrihedron);
171
172 Standard_Boolean OneOrMoreFound = Standard_False;
173 for (myDoc->GetAISContext()->InitCurrent();
174 myDoc->GetAISContext()->MoreCurrent ();
175 myDoc->GetAISContext()->NextCurrent ())
176 {
177 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(myDoc->GetAISContext()->Current());
178
179 if (!anAISShape.IsNull())
180 {
181 OneOrMoreFound = Standard_True;
182 TopoDS_Shape aShape = anAISShape->Shape();
183 myDisplayableShape->Add( aShape );
97319109 184 myInteractiveContext->Display(anAISShape);
7fd59977 185 }
97319109 186 }
7fd59977 187
188 Standard_Integer DisplayMode = m_DisplayMode;
189 if (m_Algo == 1) DisplayMode+=100;
190 if (!m_DrawHiddenLine) DisplayMode+=1000;
191
192 myDoc->GetInteractiveContext2D()->Display(myDisplayableShape, // object
193 DisplayMode, // display mode
194 DisplayMode, // selection mode
97319109 195 Standard_True); // Redraw
7fd59977 196
197 myDoc->FitAll2DViews(Standard_False); // Update Viewer
198
7fd59977 199 // check the selection :
200 // if no object : disable all possiblity!!
201 ShowHideButton(OneOrMoreFound);
202 OnDisplay(true);
203}
204
205void CSelectionDialog::Apply()
206{
207 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
97319109 208 myDoc->GetInteractiveContext2D()->RemoveAll();
7fd59977 209 UpdateData(true);
210
211 Standard_Integer DisplayMode = m_DisplayMode;
212 if (m_Algo == 1) DisplayMode+=100;
213
214 if (!m_DrawHiddenLine) DisplayMode+=1000;
215
216 myDoc->GetInteractiveContext2D()->Display(myDisplayableShape, // object
217 DisplayMode,
218 DisplayMode,
219 Standard_True); // Redraw
220
221 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
222}
223
224void CSelectionDialog::UpdateProjector()
225{
97319109 226 V3d_Coordinate DX,DY,DZ,XAt,YAt,ZAt, Vx,Vy,Vz ;
227 myActiveView->Proj(DX,DY,DZ);
228 myActiveView->At(XAt,YAt,ZAt);
229 myActiveView->Up( Vx,Vy,Vz );
230 OnDisplay(false);
231 Standard_Boolean IsPerspective = (myActiveView->Type() == V3d_PERSPECTIVE);
232 Quantity_Length aFocus = 1;
233 Prs3d_Projector aPrs3dProjector(IsPerspective,aFocus,DX,DY,DZ,XAt,YAt,ZAt,Vx,Vy,Vz);
234 HLRAlgo_Projector aProjector = aPrs3dProjector.Projector();
235
236 if (myDisplayableShape.IsNull()) return;
237 myDisplayableShape->SetProjector(aProjector);
7fd59977 238}
239
240void CSelectionDialog::ShowHideButton(Standard_Boolean EnableButton)
241{
97319109 242 UpdateData(true);
243
244 GetDlgItem(ID_Update2D )->EnableWindow(EnableButton);
245 GetDlgItem(IDC_DisplayDefault )->EnableWindow(EnableButton);
246 GetDlgItem(IDC_PolyAlgo )->EnableWindow(EnableButton);
247 GetDlgItem(IDC_Algo )->EnableWindow(EnableButton);
248
249 GetDlgItem(IDC_VsharpEdges )->EnableWindow(EnableButton);
250 GetDlgItem(IDC_VsmoothEdges )->EnableWindow(EnableButton);
251 GetDlgItem(IDC_VSewingEdges )->EnableWindow(EnableButton);
252 GetDlgItem(IDC_VApparentContour )->EnableWindow(EnableButton);
253 GetDlgItem(IDC_VIsoParametrics )->EnableWindow(EnableButton);
254
255 GetDlgItem(IDC_DrawHiddenLine )->EnableWindow(EnableButton);
256
257 GetDlgItem(IDC_HsmoothEdges )->EnableWindow(EnableButton && m_DrawHiddenLine);
258 GetDlgItem(IDC_HSewingEdges )->EnableWindow(EnableButton && m_DrawHiddenLine);
259 GetDlgItem(IDC_HApparentContour )->EnableWindow(EnableButton && m_DrawHiddenLine);
260 GetDlgItem(IDC_HsharpEdges )->EnableWindow(EnableButton && m_DrawHiddenLine);
261 GetDlgItem(IDC_HIsoParametrics )->EnableWindow(EnableButton && m_DrawHiddenLine);
262
263 GetDlgItem(IDC_EDIT_NBIsos )->EnableWindow(EnableButton);
264 GetDlgItem(IDC_STATIC_NbIsos )->EnableWindow(EnableButton);
265
266 if(m_Algo == 0)
267 {
268 if (m_DisplayMode == 5) m_DisplayMode=0;
269 if (m_DisplayMode == 10) m_DisplayMode=0;
270
271 GetDlgItem(IDC_VIsoParametrics)->EnableWindow(false);
272 GetDlgItem(IDC_HIsoParametrics)->EnableWindow(false);
273 GetDlgItem(IDC_STATIC_NbIsos) ->EnableWindow(false);
274 GetDlgItem(IDC_EDIT_NBIsos) ->EnableWindow(false);
275 }
276 else
277 {
278 GetDlgItem(IDC_VIsoParametrics)->EnableWindow(true);
279 GetDlgItem(IDC_HIsoParametrics)->EnableWindow(m_DrawHiddenLine);
280 GetDlgItem(IDC_STATIC_NbIsos) ->EnableWindow(true);
281 GetDlgItem(IDC_EDIT_NBIsos) ->EnableWindow(true);
282 }
283 UpdateData(false);
7fd59977 284}
285
286void CSelectionDialog::OnDisplayDefault()
287{ Apply(); }
288void CSelectionDialog::OnVIsoParametrics()
289{ Apply(); }
290void CSelectionDialog::OnVApparentContour()
291{ Apply(); }
292void CSelectionDialog::OnVSewingEdges()
293{ Apply(); }
294void CSelectionDialog::OnVsharpEdges()
295{ Apply(); }
296void CSelectionDialog::OnVsmoothEdges()
297{ Apply(); }
298void CSelectionDialog::OnHsharpEdges()
299{ Apply(); }
300void CSelectionDialog::OnHsmoothEdges()
301{ Apply(); }
302void CSelectionDialog::OnHSewingEdges()
303{ Apply(); }
304void CSelectionDialog::OnHIsoParametrics()
305{ Apply(); }
306void CSelectionDialog::OnHApparentContour()
307{ Apply(); }
308
309void CSelectionDialog::OnChangeEDITNBIsos()
5c1f974e 310{
311 UpdateData(true);
312 myDisplayableShape->SetNbIsos(m_NbIsos);
313 Apply();
7fd59977 314}
315void CSelectionDialog::OnAlgo()
5c1f974e 316{
317 ShowHideButton();
7fd59977 318 Apply();
319}
320
321void CSelectionDialog::OnPolyAlgo()
5c1f974e 322{
323 ShowHideButton();
7fd59977 324 Apply();
325}
326
327void CSelectionDialog::OnDrawHiddenLine()
328{
329 UpdateData(true);
5c1f974e 330 if(m_DisplayMode >=6 )
331 {
332 m_DisplayMode=0;
333 UpdateData(false);
334 }
335 ShowHideButton();
336 Apply();
7fd59977 337}
338
339void CSelectionDialog::OnUpdate2D()
340{
5c1f974e 341 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
342 myDoc->ActivateFrame(RUNTIME_CLASS(OCC_2dView),SW_NORMAL);
343 UpdateProjector();
97319109 344 Apply();
345 myDoc->FitAll2DViews(Standard_False);
5c1f974e 346 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
7fd59977 347}
348
349void CSelectionDialog::OnTopView()
350{
5c1f974e 351 myActiveView->SetProj(V3d_Zpos);
352 OnDisplay(true);
7fd59977 353}
354
355void CSelectionDialog::OnBottomView()
356{
5c1f974e 357 myActiveView->SetProj(V3d_Zneg);
358 OnDisplay(true);
7fd59977 359}
360
361void CSelectionDialog::OnLeftView()
362{
5c1f974e 363 myActiveView->SetProj(V3d_Ypos);
364 OnDisplay(true);
7fd59977 365}
366
367void CSelectionDialog::OnRightView()
368{
5c1f974e 369 myActiveView->SetProj(V3d_Yneg);
370 OnDisplay(true);
7fd59977 371}
372
373void CSelectionDialog::OnFrontView()
374{
5c1f974e 375 myActiveView->SetProj(V3d_Xpos);
376 OnDisplay(true);
7fd59977 377}
378
379void CSelectionDialog::OnBackView()
380{
5c1f974e 381 myActiveView->SetProj(V3d_Xneg);
382 OnDisplay(true);
7fd59977 383}
384
385void CSelectionDialog::OnAxoView()
386{
5c1f974e 387 myActiveView->SetProj(V3d_XposYnegZpos);
388 OnDisplay(true);
7fd59977 389}
390
de75ed09 391void CSelectionDialog::OnHlrMode()
7fd59977 392{
5c1f974e 393 UpdateData(true);
7fd59977 394
de75ed09 395 if (!m_HlrModeIsOn)
5c1f974e 396 {
de75ed09 397 myActiveView->SetComputedMode (m_HlrModeIsOn);
5c1f974e 398 }
399 else
400 {
401 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
de75ed09 402 myActiveView->SetComputedMode (m_HlrModeIsOn);
5c1f974e 403 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
404 }
405 OnDisplay(false);
7fd59977 406}
407
408void CSelectionDialog::OnLButtonDown(UINT nFlags, CPoint point)
409{
5c1f974e 410 CDialog::OnLButtonDown(nFlags, point);
7fd59977 411
5c1f974e 412 if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
413 (myPosMinY > point.y) ||(myPosMaxY < point.y))
414 return;
7fd59977 415
416 // save the current mouse coordinate
417 myXmax=point.x; myYmax=point.y;
418}
419
420void CSelectionDialog::OnLButtonUp(UINT nFlags, CPoint point)
421{
5c1f974e 422 CDialog::OnLButtonUp(nFlags, point);
7fd59977 423
5c1f974e 424 if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
425 (myPosMinY > point.y) ||(myPosMaxY < point.y))
426 return;
7fd59977 427}
428
429void CSelectionDialog::OnRButtonDown(UINT nFlags, CPoint point)
430{
5c1f974e 431 CDialog::OnRButtonDown(nFlags, point);
432
de75ed09 433 if ((myPosMinX > point.x) || (myPosMaxX < point.x) ||
434 (myPosMinY > point.y) || (myPosMaxY < point.y))
435 {
5c1f974e 436 return;
de75ed09 437 }
5c1f974e 438
de75ed09 439 if ( nFlags & CASCADESHORTCUTKEY )
5c1f974e 440 {
de75ed09 441 if (m_HlrModeIsOn)
442 {
443 myActiveView->SetComputedMode (Standard_False);
444 }
445 myActiveView->StartRotation (point.x, point.y);
446 OnDisplay (false);
5c1f974e 447 }
7fd59977 448}
449
450void CSelectionDialog::OnRButtonUp(UINT nFlags, CPoint point)
451{
5c1f974e 452 CDialog::OnRButtonUp(nFlags, point);
7fd59977 453
de75ed09 454 if ((myPosMinX > point.x) || (myPosMaxX < point.x) ||
455 (myPosMinY > point.y) || (myPosMaxY < point.y))
456 {
5c1f974e 457 return;
de75ed09 458 }
7fd59977 459
5c1f974e 460 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
de75ed09 461 // reset tyhe good HLR mode according to the strored one
462 // --> dynamic rotation may have change it
463 myActiveView->SetComputedMode (m_HlrModeIsOn);
5c1f974e 464 OnDisplay(false);
465 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
7fd59977 466}
467
468void CSelectionDialog::OnMouseMove(UINT nFlags, CPoint point)
469{
5c1f974e 470 CDialog::OnMouseMove(nFlags, point);
7fd59977 471
5c1f974e 472 if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
473 (myPosMinY > point.y) ||(myPosMaxY < point.y))
474 return;
7fd59977 475
7fd59977 476 if ( nFlags & MK_LBUTTON)
5c1f974e 477 {//left button
478 if ( nFlags & CASCADESHORTCUTKEY )
7fd59977 479 {
5c1f974e 480 // move with MB1 and Control : on the dynamic zooming
481 // Do the zoom in function of mouse's coordinates
482 myActiveView->Zoom(myXmax,myYmax,point.x,point.y);
483 // save the current mouse coordinate
484 myXmax = point.x; myYmax = point.y;
485 }// if ( nFlags & CASCADESHORTCUTKEY ) else
486 }
487 else if ( nFlags & MK_MBUTTON)
488 {
489 if ( nFlags & CASCADESHORTCUTKEY )
7fd59977 490 {
5c1f974e 491 myActiveView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
492 myXmax = point.x; myYmax = point.y;
493 }
494 }
495 else if ( nFlags & MK_RBUTTON)
496 {//right button
497 if ( nFlags & CASCADESHORTCUTKEY )
7fd59977 498 {
5c1f974e 499 myActiveView->Rotation(point.x,point.y);
500 }
501 }
502 else
503 {// No buttons
504 myXmax = point.x; myYmax = point.y;
505 }
506 OnDisplay(false);
7fd59977 507}
508
509void CSelectionDialog::OnOK()
510{
511 CDialog::OnOK();
512}
513
514void CSelectionDialog::OnPaint()
515{
5c1f974e 516 CPaintDC dc(this); // device context for painting
517 OnDisplay(false);
7fd59977 518}