Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / standard / 08_HLR / src / SelectionDialog.cpp
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
16 static 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
25 CSelectionDialog::CSelectionDialog(CHLRDoc* aDoc,CWnd* pParent /*=NULL*/)
26         : CDialog(CSelectionDialog::IDD, pParent)
27 {
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
37 }
38
39 void CSelectionDialog::DoDataExchange(CDataExchange* pDX)
40 {
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
49 }
50
51
52 BEGIN_MESSAGE_MAP(CSelectionDialog, CDialog)
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
87 END_MESSAGE_MAP()
88
89 /////////////////////////////////////////////////////////////////////////////
90 // CSelectionDialog message handlers
91
92 BOOL CSelectionDialog::OnInitDialog() 
93 {
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
119 }
120
121 void CSelectionDialog::OnDisplay(bool isFit) 
122 {
123         GetDlgItem(IDC_DUMMYBUTTON)->SetRedraw(true);
124         if(!myDisplay) {
125             Handle(Graphic3d_WNTGraphicDevice) theGraphicDevice = 
126                         ((CHLRApp*)AfxGetApp())->GetGraphicDevice();
127
128                 myActiveViewer = new V3d_Viewer(theGraphicDevice,(short *) "Visu3D");
129                 myActiveViewer->SetDefaultLights();
130                 myActiveViewer->SetLightOn();
131                 myActiveView = myActiveViewer->CreateView();
132
133                 Handle(WNT_Window) aWNTWindow = new WNT_Window(theGraphicDevice,
134                                                                        GetDlgItem(IDC_DUMMYBUTTON)->GetSafeHwnd(),
135                                                        Quantity_NOC_GRAY);
136
137             aWNTWindow->SetDoubleBuffer(Standard_False);
138
139             if (m_DegeneratedModeOn) myActiveView->SetDegenerateModeOn();
140                 myActiveView->SetWindow(aWNTWindow);
141
142                 myInteractiveContext = new AIS_InteractiveContext(myActiveViewer);
143
144                 // TRIHEDRON
145                 Handle(Geom_Axis2Placement) aTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
146                 myTrihedron=new AIS_Trihedron(aTrihedronAxis);
147
148                 myInteractiveContext->Display(myTrihedron);
149         }
150         if(isFit) {
151                 myActiveView->ZFitAll();
152                 myActiveView->FitAll();
153         }
154         myActiveView->Redraw();
155         myDisplay = Standard_True;
156         GetDlgItem(IDC_DUMMYBUTTON)->SetRedraw(false);
157 }
158
159
160 void CSelectionDialog::SetTitle(CString & aTitle)
161 {
162   SetWindowText(aTitle);
163 }
164
165 void CSelectionDialog::OnGetShape() 
166 {
167   UpdateData(true);
168   myDoc->GetInteractiveContext2D()->EraseAll();
169   myDisplayableShape = new ISession2D_Shape( );
170   UpdateProjector();
171   myDisplayableShape->SetNbIsos(m_NbIsos);
172
173   myInteractiveContext->EraseAll(Standard_False);
174   myInteractiveContext->Display(myTrihedron);
175
176   Standard_Boolean OneOrMoreFound = Standard_False;
177   for (myDoc->GetAISContext()->InitCurrent();
178        myDoc->GetAISContext()->MoreCurrent ();
179        myDoc->GetAISContext()->NextCurrent ())
180   {
181     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(myDoc->GetAISContext()->Current());
182
183     if (!anAISShape.IsNull())
184       {
185         OneOrMoreFound = Standard_True;
186         TopoDS_Shape aShape = anAISShape->Shape();
187         myDisplayableShape->Add( aShape  );
188                 myInteractiveContext->Display(anAISShape);
189       }
190    }
191
192   Standard_Integer DisplayMode = m_DisplayMode;
193   if (m_Algo == 1) DisplayMode+=100;
194   if (!m_DrawHiddenLine) DisplayMode+=1000;
195
196    myDoc->GetInteractiveContext2D()->Display(myDisplayableShape,  // object
197                                              DisplayMode,  // display mode
198                                              DisplayMode,   // selection mode 
199                                               Standard_True);   // Redraw
200
201   myDoc->FitAll2DViews(Standard_False); // Update Viewer
202
203
204   // check the selection :
205   // if no object : disable all possiblity!!
206   ShowHideButton(OneOrMoreFound);
207   OnDisplay(true);
208 }
209
210 void CSelectionDialog::Apply() 
211 {
212   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
213   myDoc->GetInteractiveContext2D()->EraseAll(Standard_False);
214   UpdateData(true);
215
216   Standard_Integer DisplayMode = m_DisplayMode;
217   if (m_Algo == 1) DisplayMode+=100;
218
219   if (!m_DrawHiddenLine) DisplayMode+=1000;
220
221   myDoc->GetInteractiveContext2D()->Display(myDisplayableShape,  // object
222                                             DisplayMode,  
223                                             DisplayMode,
224                                             Standard_True); // Redraw
225
226   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
227 }
228
229 void CSelectionDialog::UpdateProjector()
230 {
231     V3d_Coordinate DX,DY,DZ,XAt,YAt,ZAt, Vx,Vy,Vz ; 
232     myActiveView->Proj(DX,DY,DZ); 
233     myActiveView->At(XAt,YAt,ZAt); 
234     myActiveView->Up( Vx,Vy,Vz );
235         OnDisplay(false);
236     Standard_Boolean IsPerspective = (myActiveView->Type() == V3d_PERSPECTIVE);
237     Quantity_Length aFocus = 1;
238     Prs3d_Projector aPrs3dProjector(IsPerspective,aFocus,DX,DY,DZ,XAt,YAt,ZAt,Vx,Vy,Vz);
239     HLRAlgo_Projector aProjector = aPrs3dProjector.Projector();
240  
241         if (myDisplayableShape.IsNull()) return;
242     myDisplayableShape->SetProjector(aProjector);
243 }
244
245 void CSelectionDialog::ShowHideButton(Standard_Boolean EnableButton)
246 {
247         UpdateData(true);
248
249     GetDlgItem(ID_Update2D          )->EnableWindow(EnableButton);
250     GetDlgItem(IDC_DisplayDefault   )->EnableWindow(EnableButton);
251     GetDlgItem(IDC_PolyAlgo         )->EnableWindow(EnableButton);
252     GetDlgItem(IDC_Algo             )->EnableWindow(EnableButton);
253
254     GetDlgItem(IDC_VsharpEdges      )->EnableWindow(EnableButton);
255     GetDlgItem(IDC_VsmoothEdges     )->EnableWindow(EnableButton);
256     GetDlgItem(IDC_VSewingEdges     )->EnableWindow(EnableButton);
257     GetDlgItem(IDC_VApparentContour )->EnableWindow(EnableButton);
258     GetDlgItem(IDC_VIsoParametrics  )->EnableWindow(EnableButton);
259
260     GetDlgItem(IDC_DrawHiddenLine   )->EnableWindow(EnableButton);
261
262     GetDlgItem(IDC_HsmoothEdges     )->EnableWindow(EnableButton && m_DrawHiddenLine);
263     GetDlgItem(IDC_HSewingEdges     )->EnableWindow(EnableButton && m_DrawHiddenLine);
264     GetDlgItem(IDC_HApparentContour )->EnableWindow(EnableButton && m_DrawHiddenLine);
265     GetDlgItem(IDC_HsharpEdges      )->EnableWindow(EnableButton && m_DrawHiddenLine);
266     GetDlgItem(IDC_HIsoParametrics  )->EnableWindow(EnableButton && m_DrawHiddenLine);
267
268     GetDlgItem(IDC_EDIT_NBIsos      )->EnableWindow(EnableButton);
269     GetDlgItem(IDC_STATIC_NbIsos    )->EnableWindow(EnableButton);
270
271     if(m_Algo == 0)
272     {
273        if (m_DisplayMode == 5) m_DisplayMode=0;
274        if (m_DisplayMode == 10) m_DisplayMode=0;
275
276        GetDlgItem(IDC_VIsoParametrics)->EnableWindow(false);
277        GetDlgItem(IDC_HIsoParametrics)->EnableWindow(false);
278        GetDlgItem(IDC_STATIC_NbIsos)  ->EnableWindow(false);
279        GetDlgItem(IDC_EDIT_NBIsos)    ->EnableWindow(false);
280     }
281     else
282     {
283        GetDlgItem(IDC_VIsoParametrics)->EnableWindow(true);
284        GetDlgItem(IDC_HIsoParametrics)->EnableWindow(m_DrawHiddenLine);
285        GetDlgItem(IDC_STATIC_NbIsos)  ->EnableWindow(true);
286        GetDlgItem(IDC_EDIT_NBIsos)    ->EnableWindow(true);
287     }
288         UpdateData(false);
289
290
291 void CSelectionDialog::OnDisplayDefault() 
292 { Apply(); }
293 void CSelectionDialog::OnVIsoParametrics() 
294 { Apply(); }
295 void CSelectionDialog::OnVApparentContour() 
296 { Apply(); }
297 void CSelectionDialog::OnVSewingEdges() 
298 { Apply(); }
299 void CSelectionDialog::OnVsharpEdges() 
300 { Apply(); }
301 void CSelectionDialog::OnVsmoothEdges() 
302 { Apply(); }
303 void CSelectionDialog::OnHsharpEdges() 
304 { Apply(); }
305 void CSelectionDialog::OnHsmoothEdges() 
306 { Apply(); }
307 void CSelectionDialog::OnHSewingEdges() 
308 { Apply(); }
309 void CSelectionDialog::OnHIsoParametrics() 
310 { Apply(); }
311 void CSelectionDialog::OnHApparentContour() 
312 { Apply(); }
313
314 void CSelectionDialog::OnChangeEDITNBIsos() 
315
316         UpdateData(true);
317         myDisplayableShape->SetNbIsos(m_NbIsos);
318         Apply(); 
319 }
320 void CSelectionDialog::OnAlgo() 
321 { ShowHideButton();
322   Apply();
323 }
324
325 void CSelectionDialog::OnPolyAlgo() 
326 { ShowHideButton();     
327   Apply();
328 }
329
330 void CSelectionDialog::OnDrawHiddenLine() 
331 {
332   UpdateData(true);
333   if (m_DisplayMode >=6 ) 
334   { m_DisplayMode=0;
335     UpdateData(false); }
336   ShowHideButton();     
337   Apply();      
338 }
339
340 void CSelectionDialog::OnUpdate2D() 
341 {
342     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
343     myDoc->ActivateFrame(RUNTIME_CLASS(OCC_2dView),SW_NORMAL);
344     UpdateProjector();
345         Apply();                                        
346     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
347 }
348
349 void CSelectionDialog::OnTopView() 
350 {
351     myActiveView->SetProj(V3d_Zpos);
352         OnDisplay(true);
353 }
354
355 void CSelectionDialog::OnBottomView() 
356 {
357     myActiveView->SetProj(V3d_Zneg);
358         OnDisplay(true);
359 }
360
361 void CSelectionDialog::OnLeftView() 
362 {
363     myActiveView->SetProj(V3d_Ypos);
364         OnDisplay(true);
365 }
366
367 void CSelectionDialog::OnRightView() 
368 {
369     myActiveView->SetProj(V3d_Yneg);
370         OnDisplay(true);
371 }
372
373 void CSelectionDialog::OnFrontView() 
374 {
375     myActiveView->SetProj(V3d_Xpos);
376         OnDisplay(true);
377 }
378
379 void CSelectionDialog::OnBackView() 
380 {
381     myActiveView->SetProj(V3d_Xneg);
382         OnDisplay(true);
383 }
384
385 void CSelectionDialog::OnAxoView() 
386 {
387     myActiveView->SetProj(V3d_XposYnegZpos);
388         OnDisplay(true);
389 }
390
391 void CSelectionDialog::OnDegeneratedMode() 
392 {
393         UpdateData(true);
394
395     if(m_DegeneratedModeOn)
396     {
397           myActiveView->SetDegenerateModeOn();
398           m_DegeneratedModeOn = Standard_True;
399     }
400     else
401     {
402       SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
403           myActiveView->SetDegenerateModeOff();
404           m_DegeneratedModeOn = Standard_False;
405       SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
406     }
407         OnDisplay(false);
408 }
409
410 void CSelectionDialog::OnLButtonDown(UINT nFlags, CPoint point) 
411 {
412         CDialog::OnLButtonDown(nFlags, point);
413
414         if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
415                 (myPosMinY > point.y) ||(myPosMaxY < point.y))
416                 return;
417
418   //  save the current mouse coordinate
419   myXmax=point.x;  myYmax=point.y;
420 }
421
422 void CSelectionDialog::OnLButtonUp(UINT nFlags, CPoint point) 
423 {
424         CDialog::OnLButtonUp(nFlags, point);
425
426         if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
427                 (myPosMinY > point.y) ||(myPosMaxY < point.y))
428                 return;
429 }
430
431 void CSelectionDialog::OnRButtonDown(UINT nFlags, CPoint point) 
432 {
433         CDialog::OnRButtonDown(nFlags, point);
434
435         if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
436                 (myPosMinY > point.y) ||(myPosMaxY < point.y))
437                 return;
438
439    if ( nFlags & CASCADESHORTCUTKEY ) 
440           {
441             if (!m_DegeneratedModeOn)
442               myActiveView->SetDegenerateModeOn();
443             myActiveView->StartRotation(point.x,point.y);  
444                 OnDisplay(false);
445           }
446 }
447
448 void CSelectionDialog::OnRButtonUp(UINT nFlags, CPoint point) 
449 {
450         CDialog::OnRButtonUp(nFlags, point);
451
452         if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
453                 (myPosMinY > point.y) ||(myPosMaxY < point.y))
454                 return;
455
456     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
457     // reset tyhe good Degenerated mode according to the strored one
458     //   --> dynamic rotation may have change it 
459     if (!m_DegeneratedModeOn)  
460       myActiveView->SetDegenerateModeOff();
461      else
462       myActiveView->SetDegenerateModeOn();
463     OnDisplay(false);
464     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
465 }
466
467 void CSelectionDialog::OnMouseMove(UINT nFlags, CPoint point) 
468 {
469         CDialog::OnMouseMove(nFlags, point);
470
471         if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
472                 (myPosMinY > point.y) ||(myPosMaxY < point.y))
473                 return;
474
475     //   ============================  LEFT BUTTON =======================
476   if ( nFlags & MK_LBUTTON)
477     {
478      if ( nFlags & CASCADESHORTCUTKEY ) 
479           {
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     } else //   if ( nFlags & MK_LBUTTON) 
487     //   ============================  MIDDLE BUTTON =======================
488     if ( nFlags & MK_MBUTTON)
489     {
490      if ( nFlags & CASCADESHORTCUTKEY ) 
491           {
492                 myActiveView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
493                 myXmax = point.x; myYmax = point.y;     
494           }
495     } else //  if ( nFlags & MK_MBUTTON)
496     //   ============================  RIGHT BUTTON =======================
497     if ( nFlags & MK_RBUTTON)
498     {
499      if ( nFlags & CASCADESHORTCUTKEY ) 
500           {
501          myActiveView->Rotation(point.x,point.y);
502           }
503     } else //if ( nFlags & MK_RBUTTON)
504     //   ============================  NO BUTTON =======================
505     {  // No buttons 
506           myXmax = point.x; myYmax = point.y;   
507    }
508         OnDisplay(false);
509 }
510
511 void CSelectionDialog::OnOK() 
512 {
513   CDialog::OnOK();
514 }
515
516 void CSelectionDialog::OnPaint() 
517 {
518         CPaintDC dc(this); // device context for painting
519         
520         OnDisplay(false);
521 }