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_ExportFormat.hxx>
12 #include <Graphic3d_Camera.hxx>
14 #include <OpenGl_GraphicDriver.hxx>
18 IMPLEMENT_DYNCREATE(OCC_3dView, CView)
20 BEGIN_MESSAGE_MAP(OCC_3dView, CView)
21 //{{AFX_MSG_MAP(OCC_3dView)
22 ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
23 ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
24 ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
25 ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
26 ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
27 ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
28 ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
29 ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
30 ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
31 ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
32 ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
33 ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
34 ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
35 ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
36 ON_COMMAND(ID_BUTTON_STEREOCONFIG, OnStereoConfigButton)
38 ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
39 ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
40 ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
48 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
49 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
50 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
51 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
52 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
53 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
54 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
55 ON_UPDATE_COMMAND_UI(ID_BUTTON_STEREOCONFIG, OnUpdateStereoConfigButton)
56 ON_COMMAND(ID_Modify_ChangeBackground , OnModifyChangeBackground)
60 /////////////////////////////////////////////////////////////////////////////
61 // OCC_3dView construction/destruction
63 OCC_3dView::OCC_3dView()
64 : myCurrentMode (CurAction3d_Nothing),
72 myHlrModeIsOn (Standard_False),
75 // TODO: add construction code here
78 OCC_3dView::~OCC_3dView()
89 BOOL OCC_3dView::PreCreateWindow(CREATESTRUCT& cs)
91 // TODO: Modify the Window class or styles here by modifying
92 // the CREATESTRUCT cs
93 return CView::PreCreateWindow(cs);
96 /////////////////////////////////////////////////////////////////////////////
98 void OCC_3dView::OnInitialUpdate()
100 CView::OnInitialUpdate();
102 myView = GetDocument()->GetViewer()->CreateView();
104 // store for restore state after rotation (which is in Degenerated mode)
105 myHlrModeIsOn = Standard_False;
106 myView->SetComputedMode (myHlrModeIsOn);
108 Handle(OpenGl_GraphicDriver) aDriver =
109 Handle(OpenGl_GraphicDriver)::DownCast (((OCC_App*)AfxGetApp())->GetGraphicDriver());
111 Handle(WNT_Window) aWNTWindow = new WNT_Window(GetSafeHwnd());
113 myView->SetWindow(aWNTWindow);
114 myView->Camera()->SetProjectionType (aDriver->Options().contextStereo
115 ? Graphic3d_Camera::Projection_Stereo
116 : Graphic3d_Camera::Projection_Orthographic);
118 if (!aWNTWindow->IsMapped())
123 // store the mode ( nothing , dynamic zooming, dynamic ... )
124 myCurrentMode = CurAction3d_Nothing;
126 m_pStereoDlg = new OCC_StereoConfigDlg (this);
127 m_pStereoDlg->SetView (myView);
128 m_pStereoDlg->Create (IDD_DIALOG_STEREO, this);
131 void OCC_3dView::OnDraw(CDC* /*pDC*/)
134 GetWindowRect(aRect);
135 if(myWidth != aRect.Width() || myHeight != aRect.Height()) {
136 myWidth = aRect.Width();
137 myHeight = aRect.Height();
138 ::PostMessage ( GetSafeHwnd () , WM_SIZE , SW_SHOW , myWidth + myHeight*65536 );
143 /////////////////////////////////////////////////////////////////////////////
144 // OCC_3dView diagnostics
147 void OCC_3dView::AssertValid() const
149 CView::AssertValid();
152 void OCC_3dView::Dump(CDumpContext& dc) const
157 OCC_3dDoc* OCC_3dView::GetDocument() // non-debug version is inline
159 // ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(OCC_3dBaseDoc)));
160 return (OCC_3dDoc*)m_pDocument;
165 /////////////////////////////////////////////////////////////////////////////
166 // OCC_3dView message handlers
167 void OCC_3dView::OnFileExportImage()
169 GetDocument()->ExportView (myView);
172 void OCC_3dView::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
174 if (!myView.IsNull())
175 myView->MustBeResized();
179 void OCC_3dView::OnBUTTONBack()
181 myView->SetProj(V3d_Xneg);
184 // See the front View
185 void OCC_3dView::OnBUTTONFront()
187 myView->SetProj(V3d_Xpos);
190 // See the bottom View
191 void OCC_3dView::OnBUTTONBottom()
193 myView->SetProj(V3d_Zneg);
197 void OCC_3dView::OnBUTTONTop()
199 myView->SetProj(V3d_Zpos);
203 void OCC_3dView::OnBUTTONLeft()
205 myView->SetProj(V3d_Ypos);
208 // See the right View
209 void OCC_3dView::OnBUTTONRight()
211 myView->SetProj(V3d_Yneg);
214 // See the axonometric View
215 void OCC_3dView::OnBUTTONAxo()
217 myView->SetProj(V3d_XposYnegZpos);
220 void OCC_3dView::OnBUTTONHlrOff()
222 myHlrModeIsOn = Standard_False;
223 myView->SetComputedMode (myHlrModeIsOn);
226 void OCC_3dView::OnBUTTONHlrOn()
228 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
229 myHlrModeIsOn = Standard_True;
230 myView->SetComputedMode (myHlrModeIsOn);
231 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
234 void OCC_3dView::OnBUTTONPan()
236 myCurrentMode = CurAction3d_DynamicPanning;
239 void OCC_3dView::OnBUTTONPanGlo()
241 // save the current zoom value
242 myCurZoom = myView->Scale();
246 myCurrentMode = CurAction3d_GlobalPanning;
249 void OCC_3dView::OnBUTTONReset()
254 void OCC_3dView::OnBUTTONRot()
256 myCurrentMode = CurAction3d_DynamicRotation;
259 void OCC_3dView::OnBUTTONZoomAll()
265 void OCC_3dView::OnBUTTONZoomProg()
266 { myCurrentMode = CurAction3d_DynamicZooming; }
268 void OCC_3dView::OnBUTTONZoomWin()
269 { myCurrentMode = CurAction3d_WindowZooming; }
271 void OCC_3dView::OnLButtonDown(UINT nFlags, CPoint point)
273 // save the current mouse coordinate in min
274 myXmin=point.x; myYmin=point.y;
275 myXmax=point.x; myYmax=point.y;
277 if ( nFlags & MK_CONTROL )
279 // Button MB1 down Control :start zomming
280 // SetCursor(AfxGetApp()->LoadStandardCursor());
284 switch (myCurrentMode)
286 case CurAction3d_Nothing : // start a drag
287 if (nFlags & MK_SHIFT)
288 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
290 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
293 case CurAction3d_DynamicZooming : // noting
295 case CurAction3d_WindowZooming : // noting
297 case CurAction3d_DynamicPanning :// noting
299 case CurAction3d_GlobalPanning :// noting
301 case CurAction3d_DynamicRotation :
304 myView->SetComputedMode (Standard_False);
307 myView->StartRotation(point.x,point.y);
310 Standard_Failure::Raise(" incompatible Current Mode ");
316 void OCC_3dView::OnLButtonUp(UINT nFlags, CPoint point)
318 if ( nFlags & MK_CONTROL )
324 switch (myCurrentMode)
326 case CurAction3d_Nothing :
327 if (point.x == myXmin && point.y == myYmin)
328 { // no offset between down and up --> selectEvent
331 if (nFlags & MK_SHIFT )
332 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
334 GetDocument()->InputEvent (point.x,point.y,myView);
337 myXmax=point.x; myYmax=point.y;
338 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
339 if (nFlags & MK_SHIFT)
340 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
342 GetDocument()->DragEvent(point.x,point.y,1,myView);
345 case CurAction3d_DynamicZooming :
346 myCurrentMode = CurAction3d_Nothing;
348 case CurAction3d_WindowZooming :
349 myXmax=point.x; myYmax=point.y;
350 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
351 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
352 // Test if the zoom window is greater than a minimale window.
354 // Do the zoom window between Pmin and Pmax
355 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
357 myCurrentMode = CurAction3d_Nothing;
359 case CurAction3d_DynamicPanning :
360 myCurrentMode = CurAction3d_Nothing;
362 case CurAction3d_GlobalPanning :
363 myView->Place(point.x,point.y,myCurZoom);
364 myCurrentMode = CurAction3d_Nothing;
366 case CurAction3d_DynamicRotation :
367 myCurrentMode = CurAction3d_Nothing;
370 CWaitCursor aWaitCursor;
371 myView->SetComputedMode (myHlrModeIsOn);
375 myView->SetComputedMode (myHlrModeIsOn);
379 Standard_Failure::Raise(" incompatible Current Mode ");
381 } //switch (myCurrentMode)
382 } // else // if ( Ctrl )
385 void OCC_3dView::OnMButtonDown(UINT nFlags, CPoint /*point*/)
387 if ( nFlags & MK_CONTROL )
389 // Button MB2 down Control : panning init
390 // SetCursor(AfxGetApp()->LoadStandardCursor());
394 void OCC_3dView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
396 if ( nFlags & MK_CONTROL )
398 // Button MB2 down Control : panning init
399 // SetCursor(AfxGetApp()->LoadStandardCursor());
403 void OCC_3dView::OnRButtonDown(UINT nFlags, CPoint point)
405 if ( nFlags & MK_CONTROL )
409 myView->SetComputedMode (Standard_False);
411 myView->StartRotation(point.x,point.y);
415 GetDocument()->Popup(point.x,point.y,myView);
419 void OCC_3dView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/)
421 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
422 myView->SetComputedMode (myHlrModeIsOn);
423 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
426 void OCC_3dView::OnMouseMove(UINT nFlags, CPoint point)
428 // ============================ LEFT BUTTON =======================
429 if ( nFlags & MK_LBUTTON)
431 if ( nFlags & MK_CONTROL )
433 // move with MB1 and Control : on the dynamic zooming
434 // Do the zoom in function of mouse's coordinates
435 myView->Zoom(myXmax,myYmax,point.x,point.y);
436 // save the current mouse coordinate in min
442 switch (myCurrentMode)
444 case CurAction3d_Nothing :
445 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
449 if (nFlags & MK_SHIFT)
450 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
452 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
453 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
456 case CurAction3d_DynamicZooming :
457 myView->Zoom(myXmax,myYmax,point.x,point.y);
458 // save the current mouse coordinate in min \n";
459 myXmax=point.x; myYmax=point.y;
461 case CurAction3d_WindowZooming :
462 myXmax = point.x; myYmax = point.y;
463 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
464 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
466 case CurAction3d_DynamicPanning :
467 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
468 myXmax = point.x; myYmax = point.y;
470 case CurAction3d_GlobalPanning : // nothing
472 case CurAction3d_DynamicRotation :
473 myView->Rotation(point.x,point.y);
477 Standard_Failure::Raise(" incompatible Current Mode ");
479 }// switch (myCurrentMode)
480 }// if ( nFlags & MK_CONTROL ) else
482 else if ( nFlags & MK_MBUTTON)
484 if ( nFlags & MK_CONTROL )
486 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
487 myXmax = point.x; myYmax = point.y;
491 else if ( nFlags & MK_RBUTTON)
493 if ( nFlags & MK_CONTROL )
495 myView->Rotation(point.x,point.y);
500 myXmax = point.x; myYmax = point.y;
501 if (nFlags & MK_SHIFT)
502 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
504 GetDocument()->MoveEvent(point.x,point.y,myView);
508 void OCC_3dView::DrawRectangle(const Standard_Integer MinX ,
509 const Standard_Integer MinY ,
510 const Standard_Integer MaxX ,
511 const Standard_Integer MaxY ,
512 const Standard_Boolean Draw ,
513 const LineStyle aLineStyle)
515 static int m_DrawMode;
516 if (!m_Pen && aLineStyle ==Solid )
517 {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
518 else if (!m_Pen && aLineStyle ==Dot )
519 {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;}
520 else if (!m_Pen && aLineStyle == ShortDash)
521 {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;}
522 else if (!m_Pen && aLineStyle == LongDash)
523 {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;}
524 else if (aLineStyle == Default)
525 { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
527 CPen* aOldPen = NULL;
528 CClientDC clientDC(this);
529 if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
530 clientDC.SetROP2(m_DrawMode);
532 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
533 static Standard_Boolean m_IsVisible;
535 if ( m_IsVisible && !Draw) // move or up : erase at the old position
537 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
538 clientDC.LineTo(StoredMaxX,StoredMaxY);
539 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
543 StoredMinX = Min ( MinX, MaxX );
544 StoredMinY = Min ( MinY, MaxY );
545 StoredMaxX = Max ( MinX, MaxX );
546 StoredMaxY = Max ( MinY, MaxY);
548 if (Draw) // move : draw
550 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
551 clientDC.LineTo(StoredMaxX,StoredMaxY);
552 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
556 if (m_Pen) clientDC.SelectObject(aOldPen);
561 void OCC_3dView::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI)
563 pCmdUI->SetCheck (!myHlrModeIsOn);
564 pCmdUI->Enable (myHlrModeIsOn);
567 void OCC_3dView::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI)
569 pCmdUI->SetCheck (myHlrModeIsOn);
570 pCmdUI->Enable (!myHlrModeIsOn);
573 void OCC_3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
575 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
576 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
580 void OCC_3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
582 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
583 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
586 void OCC_3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
588 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
589 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
592 void OCC_3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
594 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
595 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
598 void OCC_3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
600 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
601 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
604 void OCC_3dView::OnModifyChangeBackground()
609 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
611 m_clr = RGB(R1*255,G1*255,B1*255);
613 CColorDialog dlgColor(m_clr);
614 if (dlgColor.DoModal() == IDOK)
616 m_clr = dlgColor.GetColor();
617 R1 = GetRValue(m_clr)/255.;
618 G1 = GetGValue(m_clr)/255.;
619 B1 = GetBValue(m_clr)/255.;
620 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
625 //=============================================================================
626 // function: OnStereoConfigButton
627 // purpose: Open stereographic configuration dialog
628 //=============================================================================
629 void OCC_3dView::OnStereoConfigButton()
631 m_pStereoDlg->ShowWindow (SW_SHOW);
634 //=============================================================================
635 // function: OnUpdateStereoConfigButton
636 // purpose: Enable / disable state of stereo configuration button
637 //=============================================================================
638 void OCC_3dView::OnUpdateStereoConfigButton (CCmdUI* theCmdUI)
641 Handle(Graphic3d_Camera) aCamera = myView->Camera();
643 // check that button is enabled
644 Standard_Boolean isEnabled = !aCamera.IsNull() && aCamera->IsStereo();
646 // update toggle state
647 theCmdUI->Enable (isEnabled);
651 m_pStereoDlg->ShowWindow (SW_HIDE);