1 // OCC_3dView.cpp: implementation of the OCC_3dView class.
6 #include "OCC_3dView.h"
8 #include "OCC_3dBaseDoc.h"
9 #include <res\OCC_Resource.h>
11 #include <Graphic3d_Camera.hxx>
13 #include <OpenGl_GraphicDriver.hxx>
17 IMPLEMENT_DYNCREATE(OCC_3dView, CView)
19 BEGIN_MESSAGE_MAP(OCC_3dView, CView)
20 //{{AFX_MSG_MAP(OCC_3dView)
21 ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
22 ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
23 ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
24 ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
25 ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
26 ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
27 ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
28 ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
29 ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
30 ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
31 ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
32 ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
33 ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
34 ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
35 ON_COMMAND(ID_BUTTON_STEREOCONFIG, OnStereoConfigButton)
37 ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
38 ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
39 ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
47 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
48 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
49 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
50 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
51 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
52 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
53 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
54 ON_UPDATE_COMMAND_UI(ID_BUTTON_STEREOCONFIG, OnUpdateStereoConfigButton)
55 ON_COMMAND(ID_Modify_ChangeBackground , OnModifyChangeBackground)
59 /////////////////////////////////////////////////////////////////////////////
60 // OCC_3dView construction/destruction
62 OCC_3dView::OCC_3dView()
63 : myCurrentMode (CurAction3d_Nothing),
66 myHlrModeIsOn (Standard_False)
68 // TODO: add construction code here
71 OCC_3dView::~OCC_3dView()
81 BOOL OCC_3dView::PreCreateWindow(CREATESTRUCT& cs)
83 // TODO: Modify the Window class or styles here by modifying
84 // the CREATESTRUCT cs
85 cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC, ::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
86 return CView::PreCreateWindow(cs);
89 /////////////////////////////////////////////////////////////////////////////
91 void OCC_3dView::OnInitialUpdate()
93 CView::OnInitialUpdate();
95 myView = GetDocument()->GetViewer()->CreateView();
97 // store for restore state after rotation (which is in Degenerated mode)
98 myHlrModeIsOn = Standard_False;
99 myView->SetComputedMode (myHlrModeIsOn);
101 Handle(OpenGl_GraphicDriver) aDriver =
102 Handle(OpenGl_GraphicDriver)::DownCast (((OCC_App*)AfxGetApp())->GetGraphicDriver());
104 Handle(WNT_Window) aWNTWindow = new WNT_Window(GetSafeHwnd());
106 myView->SetWindow(aWNTWindow);
107 myView->Camera()->SetProjectionType (aDriver->Options().contextStereo
108 ? Graphic3d_Camera::Projection_Stereo
109 : Graphic3d_Camera::Projection_Orthographic);
111 if (!aWNTWindow->IsMapped())
116 // store the mode ( nothing , dynamic zooming, dynamic ... )
117 myCurrentMode = CurAction3d_Nothing;
119 m_pStereoDlg = new OCC_StereoConfigDlg (this);
120 m_pStereoDlg->SetView (myView);
121 m_pStereoDlg->Create (IDD_DIALOG_STEREO, this);
124 void OCC_3dView::OnDraw(CDC* /*pDC*/)
127 GetWindowRect(aRect);
128 if(myWidth != aRect.Width() || myHeight != aRect.Height()) {
129 myWidth = aRect.Width();
130 myHeight = aRect.Height();
131 ::PostMessage ( GetSafeHwnd() , WM_SIZE , SW_SHOW , myWidth + myHeight*65536 );
136 /////////////////////////////////////////////////////////////////////////////
137 // OCC_3dView diagnostics
140 void OCC_3dView::AssertValid() const
142 CView::AssertValid();
145 void OCC_3dView::Dump(CDumpContext& dc) const
150 OCC_3dDoc* OCC_3dView::GetDocument() // non-debug version is inline
152 // ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(OCC_3dBaseDoc)));
153 return (OCC_3dDoc*)m_pDocument;
158 /////////////////////////////////////////////////////////////////////////////
159 // OCC_3dView message handlers
160 void OCC_3dView::OnFileExportImage()
162 GetDocument()->ExportView (myView);
165 void OCC_3dView::OnSize(UINT nType, int cx, int cy)
167 OCC_BaseView::OnSize (nType, cx, cy);
168 if (!myView.IsNull())
169 myView->MustBeResized();
173 void OCC_3dView::OnBUTTONBack()
175 myView->SetProj(V3d_Ypos);
178 // See the front View
179 void OCC_3dView::OnBUTTONFront()
181 myView->SetProj(V3d_Yneg);
184 // See the bottom View
185 void OCC_3dView::OnBUTTONBottom()
187 myView->SetProj(V3d_Zneg);
191 void OCC_3dView::OnBUTTONTop()
193 myView->SetProj(V3d_Zpos);
197 void OCC_3dView::OnBUTTONLeft()
199 myView->SetProj(V3d_Xneg);
202 // See the right View
203 void OCC_3dView::OnBUTTONRight()
205 myView->SetProj(V3d_Xpos);
208 // See the axonometric View
209 void OCC_3dView::OnBUTTONAxo()
211 myView->SetProj(V3d_XposYnegZpos);
214 void OCC_3dView::OnBUTTONHlrOff()
216 myHlrModeIsOn = Standard_False;
217 myView->SetComputedMode (myHlrModeIsOn);
221 void OCC_3dView::OnBUTTONHlrOn()
223 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
224 myHlrModeIsOn = Standard_True;
225 myView->SetComputedMode (myHlrModeIsOn);
227 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
230 void OCC_3dView::OnBUTTONPan()
232 myCurrentMode = CurAction3d_DynamicPanning;
235 void OCC_3dView::OnBUTTONPanGlo()
237 // save the current zoom value
238 myCurZoom = myView->Scale();
242 myCurrentMode = CurAction3d_GlobalPanning;
245 void OCC_3dView::OnBUTTONReset()
250 void OCC_3dView::OnBUTTONRot()
252 myCurrentMode = CurAction3d_DynamicRotation;
255 void OCC_3dView::OnBUTTONZoomAll()
261 void OCC_3dView::OnBUTTONZoomProg()
262 { myCurrentMode = CurAction3d_DynamicZooming; }
264 void OCC_3dView::OnBUTTONZoomWin()
265 { myCurrentMode = CurAction3d_WindowZooming; }
267 void OCC_3dView::OnLButtonDown(UINT nFlags, CPoint point)
269 // save the current mouse coordinate in min
270 myXmin=point.x; myYmin=point.y;
271 myXmax=point.x; myYmax=point.y;
273 if ( nFlags & MK_CONTROL )
275 // Button MB1 down Control :start zomming
276 // SetCursor(AfxGetApp()->LoadStandardCursor());
280 switch (myCurrentMode)
282 case CurAction3d_Nothing : // start a drag
283 if (nFlags & MK_SHIFT)
284 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
286 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
289 case CurAction3d_DynamicZooming : // noting
291 case CurAction3d_WindowZooming : // noting
293 case CurAction3d_DynamicPanning :// noting
295 case CurAction3d_GlobalPanning :// noting
297 case CurAction3d_DynamicRotation :
300 myView->SetComputedMode (Standard_False);
303 myView->StartRotation(point.x,point.y);
306 throw Standard_Failure(" incompatible Current Mode ");
312 void OCC_3dView::OnLButtonUp(UINT nFlags, CPoint point)
314 if ( nFlags & MK_CONTROL )
320 const Handle(AIS_InteractiveContext)& aContext = GetDocument()->GetAISContext();
321 switch (myCurrentMode)
323 case CurAction3d_Nothing :
324 if (point.x == myXmin && point.y == myYmin)
325 { // no offset between down and up --> selectEvent
328 if (nFlags & MK_SHIFT )
329 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
331 GetDocument()->InputEvent (point.x,point.y,myView);
334 myXmax=point.x; myYmax=point.y;
335 drawRectangle (myXmin, myYmin, myXmax, myYmax, aContext, Standard_False);
336 if (nFlags & MK_SHIFT)
337 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
339 GetDocument()->DragEvent(point.x,point.y,1,myView);
342 case CurAction3d_DynamicZooming :
343 myCurrentMode = CurAction3d_Nothing;
345 case CurAction3d_WindowZooming :
346 myXmax=point.x; myYmax=point.y;
347 drawRectangle (myXmin, myYmin, myXmax, myYmax, aContext, Standard_False);
348 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
349 // Test if the zoom window is greater than a minimale window.
351 // Do the zoom window between Pmin and Pmax
352 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
354 myCurrentMode = CurAction3d_Nothing;
356 case CurAction3d_DynamicPanning :
357 myCurrentMode = CurAction3d_Nothing;
359 case CurAction3d_GlobalPanning :
360 myView->Place(point.x,point.y,myCurZoom);
361 myCurrentMode = CurAction3d_Nothing;
363 case CurAction3d_DynamicRotation :
364 myCurrentMode = CurAction3d_Nothing;
367 CWaitCursor aWaitCursor;
368 myView->SetComputedMode (myHlrModeIsOn);
373 myView->SetComputedMode (myHlrModeIsOn);
377 throw Standard_Failure(" incompatible Current Mode ");
379 } //switch (myCurrentMode)
380 } // else // if ( Ctrl )
383 void OCC_3dView::OnMButtonDown(UINT nFlags, CPoint /*point*/)
385 if ( nFlags & MK_CONTROL )
387 // Button MB2 down Control : panning init
388 // SetCursor(AfxGetApp()->LoadStandardCursor());
392 void OCC_3dView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
394 if ( nFlags & MK_CONTROL )
396 // Button MB2 down Control : panning init
397 // SetCursor(AfxGetApp()->LoadStandardCursor());
401 void OCC_3dView::OnRButtonDown(UINT nFlags, CPoint point)
403 if ( nFlags & MK_CONTROL )
407 myView->SetComputedMode (Standard_False);
409 myView->StartRotation(point.x,point.y);
413 GetDocument()->Popup(point.x,point.y,myView);
417 void OCC_3dView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/)
419 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
422 myView->SetComputedMode (myHlrModeIsOn);
425 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
428 void OCC_3dView::OnMouseMove(UINT nFlags, CPoint point)
430 // ============================ LEFT BUTTON =======================
431 if ( nFlags & MK_LBUTTON)
433 if ( nFlags & MK_CONTROL )
435 // move with MB1 and Control : on the dynamic zooming
436 // Do the zoom in function of mouse's coordinates
437 myView->Zoom (myXmax,myYmax,point.x,point.y);
438 // save the current mouse coordinate in min
444 const Handle(AIS_InteractiveContext)& aContext = GetDocument()->GetAISContext();
445 switch (myCurrentMode)
447 case CurAction3d_Nothing :
451 if (nFlags & MK_SHIFT)
452 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
454 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
456 drawRectangle (myXmin, myYmin, myXmax, myYmax, aContext);
459 case CurAction3d_DynamicZooming :
460 myView->Zoom(myXmax,myYmax,point.x,point.y);
461 // save the current mouse coordinate in min \n";
465 case CurAction3d_WindowZooming :
468 drawRectangle (myXmin, myYmin, myXmax, myYmax, aContext);
470 case CurAction3d_DynamicPanning :
471 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
472 myXmax = point.x; myYmax = point.y;
474 case CurAction3d_GlobalPanning : // nothing
476 case CurAction3d_DynamicRotation :
477 myView->Rotation(point.x,point.y);
481 throw Standard_Failure(" incompatible Current Mode ");
483 }// switch (myCurrentMode)
484 }// if ( nFlags & MK_CONTROL ) else
486 else if ( nFlags & MK_MBUTTON)
488 if ( nFlags & MK_CONTROL )
490 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
495 else if ( nFlags & MK_RBUTTON)
497 if ( nFlags & MK_CONTROL )
499 myView->Rotation(point.x,point.y);
506 if (nFlags & MK_SHIFT)
507 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
509 GetDocument()->MoveEvent(point.x,point.y,myView);
513 void OCC_3dView::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI)
515 pCmdUI->SetCheck (!myHlrModeIsOn);
516 pCmdUI->Enable (myHlrModeIsOn);
519 void OCC_3dView::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI)
521 pCmdUI->SetCheck (myHlrModeIsOn);
522 pCmdUI->Enable (!myHlrModeIsOn);
525 void OCC_3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
527 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
528 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
532 void OCC_3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
534 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
535 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
538 void OCC_3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
540 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
541 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
544 void OCC_3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
546 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
547 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
550 void OCC_3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
552 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
553 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
556 void OCC_3dView::OnModifyChangeBackground()
561 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
563 m_clr = RGB(R1*255,G1*255,B1*255);
565 CColorDialog dlgColor(m_clr);
566 if (dlgColor.DoModal() == IDOK)
568 m_clr = dlgColor.GetColor();
569 R1 = GetRValue(m_clr)/255.;
570 G1 = GetGValue(m_clr)/255.;
571 B1 = GetBValue(m_clr)/255.;
572 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
577 //=============================================================================
578 // function: OnStereoConfigButton
579 // purpose: Open stereographic configuration dialog
580 //=============================================================================
581 void OCC_3dView::OnStereoConfigButton()
583 m_pStereoDlg->ShowWindow (SW_SHOW);
586 //=============================================================================
587 // function: OnUpdateStereoConfigButton
588 // purpose: Enable / disable state of stereo configuration button
589 //=============================================================================
590 void OCC_3dView::OnUpdateStereoConfigButton (CCmdUI* theCmdUI)
593 Handle(Graphic3d_Camera) aCamera = myView->Camera();
595 // check that button is enabled
596 Standard_Boolean isEnabled = !aCamera.IsNull() && aCamera->IsStereo();
598 // update toggle state
599 theCmdUI->Enable (isEnabled);
603 m_pStereoDlg->ShowWindow (SW_HIDE);