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