e8aa0b9ea4fbe3ad213a3dcbe3721fc752cdf537
[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_HlrModeIsOn = FALSE;
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_HlrModeIsOn, m_HlrModeIsOn);
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_HlrModeIsOn,    OnHlrMode)
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   {
126     Handle(Graphic3d_GraphicDriver) aGraphicDriver = ((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     myActiveView->SetComputedMode (m_HlrModeIsOn);
136     myActiveView->SetWindow(aWNTWindow);
137
138     myInteractiveContext = new AIS_InteractiveContext(myActiveViewer);
139
140     // TRIHEDRON
141     Handle(Geom_Axis2Placement) aTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
142     myTrihedron=new AIS_Trihedron(aTrihedronAxis);
143
144     myInteractiveContext->Display(myTrihedron);
145         }
146         if (isFit)
147   {
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::OnHlrMode()
393 {
394   UpdateData(true);
395
396   if (!m_HlrModeIsOn)
397   {
398     myActiveView->SetComputedMode (m_HlrModeIsOn);
399   }
400   else
401   {
402     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
403     myActiveView->SetComputedMode (m_HlrModeIsOn);
404     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
405   }
406   OnDisplay(false);
407 }
408
409 void CSelectionDialog::OnLButtonDown(UINT nFlags, CPoint point) 
410 {
411   CDialog::OnLButtonDown(nFlags, point);
412
413   if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
414     (myPosMinY > point.y) ||(myPosMaxY < point.y))
415     return;
416
417   //  save the current mouse coordinate
418   myXmax=point.x;  myYmax=point.y;
419 }
420
421 void CSelectionDialog::OnLButtonUp(UINT nFlags, CPoint point) 
422 {
423   CDialog::OnLButtonUp(nFlags, point);
424
425   if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
426     (myPosMinY > point.y) ||(myPosMaxY < point.y))
427     return;
428 }
429
430 void CSelectionDialog::OnRButtonDown(UINT nFlags, CPoint point) 
431 {
432   CDialog::OnRButtonDown(nFlags, point);
433
434   if ((myPosMinX > point.x) || (myPosMaxX < point.x) ||
435       (myPosMinY > point.y) || (myPosMaxY < point.y))
436   {
437     return;
438   }
439
440   if ( nFlags & CASCADESHORTCUTKEY )
441   {
442     if (m_HlrModeIsOn)
443     {
444       myActiveView->SetComputedMode (Standard_False);
445     }
446     myActiveView->StartRotation (point.x, point.y);
447     OnDisplay (false);
448   }
449 }
450
451 void CSelectionDialog::OnRButtonUp(UINT nFlags, CPoint point) 
452 {
453   CDialog::OnRButtonUp(nFlags, point);
454
455   if ((myPosMinX > point.x) || (myPosMaxX < point.x) ||
456       (myPosMinY > point.y) || (myPosMaxY < point.y))
457   {
458     return;
459   }
460
461   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
462   // reset tyhe good HLR mode according to the strored one
463   //   --> dynamic rotation may have change it
464   myActiveView->SetComputedMode (m_HlrModeIsOn);
465   OnDisplay(false);
466   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
467 }
468
469 void CSelectionDialog::OnMouseMove(UINT nFlags, CPoint point) 
470 {
471   CDialog::OnMouseMove(nFlags, point);
472
473   if ((myPosMinX > point.x)||(myPosMaxX < point.x) ||
474     (myPosMinY > point.y) ||(myPosMaxY < point.y))
475     return;
476
477   if ( nFlags & MK_LBUTTON)
478   {//left button
479     if ( nFlags & CASCADESHORTCUTKEY ) 
480     {
481       // move with MB1 and Control : on the dynamic zooming  
482       // Do the zoom in function of mouse's coordinates  
483       myActiveView->Zoom(myXmax,myYmax,point.x,point.y); 
484       // save the current mouse coordinate 
485       myXmax = point.x;    myYmax = point.y;    
486     }// if ( nFlags & CASCADESHORTCUTKEY )  else 
487   }
488   else 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   }
496   else if ( nFlags & MK_RBUTTON)
497   {//right button
498     if ( nFlags & CASCADESHORTCUTKEY ) 
499     {
500       myActiveView->Rotation(point.x,point.y);
501     }
502   } 
503   else
504   {// No buttons 
505     myXmax = point.x; myYmax = point.y; 
506   }
507   OnDisplay(false);
508 }
509
510 void CSelectionDialog::OnOK() 
511 {
512   CDialog::OnOK();
513 }
514
515 void CSelectionDialog::OnPaint() 
516 {
517   CPaintDC dc(this); // device context for painting
518   OnDisplay(false);
519 }