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