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 <Visual3d_View.hxx>
13 #include <Graphic3d_ExportFormat.hxx>
14 #include <Graphic3d_Camera.hxx>
16 #include <OpenGl_GraphicDriver.hxx>
20 IMPLEMENT_DYNCREATE(OCC_3dView, CView)
22 BEGIN_MESSAGE_MAP(OCC_3dView, CView)
23 //{{AFX_MSG_MAP(OCC_3dView)
24 ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
25 ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
26 ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
27 ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
28 ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
29 ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
30 ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
31 ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
32 ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
33 ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
34 ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
35 ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
36 ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
37 ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
38 ON_COMMAND(ID_BUTTON_STEREOCONFIG, OnStereoConfigButton)
40 ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
41 ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
42 ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
50 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
51 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
52 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
53 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
54 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
55 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
56 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
57 ON_UPDATE_COMMAND_UI(ID_BUTTON_STEREOCONFIG, OnUpdateStereoConfigButton)
58 ON_COMMAND(ID_Modify_ChangeBackground , OnModifyChangeBackground)
62 /////////////////////////////////////////////////////////////////////////////
63 // OCC_3dView construction/destruction
65 OCC_3dView::OCC_3dView()
66 : myCurrentMode (CurAction3d_Nothing),
74 myHlrModeIsOn (Standard_False),
77 // TODO: add construction code here
80 OCC_3dView::~OCC_3dView()
91 BOOL OCC_3dView::PreCreateWindow(CREATESTRUCT& cs)
93 // TODO: Modify the Window class or styles here by modifying
94 // the CREATESTRUCT cs
95 return CView::PreCreateWindow(cs);
98 /////////////////////////////////////////////////////////////////////////////
100 void OCC_3dView::OnInitialUpdate()
102 CView::OnInitialUpdate();
104 myView = GetDocument()->GetViewer()->CreateView();
106 // store for restore state after rotation (which is in Degenerated mode)
107 myHlrModeIsOn = Standard_False;
108 myView->SetComputedMode (myHlrModeIsOn);
110 Handle(OpenGl_GraphicDriver) aDriver =
111 Handle(OpenGl_GraphicDriver)::DownCast (((OCC_App*)AfxGetApp())->GetGraphicDriver());
113 Handle(WNT_Window) aWNTWindow = new WNT_Window(GetSafeHwnd());
115 myView->SetWindow(aWNTWindow);
116 myView->Camera()->SetProjectionType (aDriver->Options().contextStereo
117 ? Graphic3d_Camera::Projection_Stereo
118 : Graphic3d_Camera::Projection_Orthographic);
120 if (!aWNTWindow->IsMapped())
125 // store the mode ( nothing , dynamic zooming, dynamic ... )
126 myCurrentMode = CurAction3d_Nothing;
128 m_pStereoDlg = new OCC_StereoConfigDlg (this);
129 m_pStereoDlg->SetView (myView);
130 m_pStereoDlg->Create (IDD_DIALOG_STEREO, this);
133 void OCC_3dView::OnDraw(CDC* /*pDC*/)
136 GetWindowRect(aRect);
137 if(myWidth != aRect.Width() || myHeight != aRect.Height()) {
138 myWidth = aRect.Width();
139 myHeight = aRect.Height();
140 ::PostMessage ( GetSafeHwnd () , WM_SIZE , SW_SHOW , myWidth + myHeight*65536 );
145 /////////////////////////////////////////////////////////////////////////////
146 // OCC_3dView diagnostics
149 void OCC_3dView::AssertValid() const
151 CView::AssertValid();
154 void OCC_3dView::Dump(CDumpContext& dc) const
159 OCC_3dDoc* OCC_3dView::GetDocument() // non-debug version is inline
161 // ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(OCC_3dBaseDoc)));
162 return (OCC_3dDoc*)m_pDocument;
167 /////////////////////////////////////////////////////////////////////////////
168 // OCC_3dView message handlers
169 void OCC_3dView::OnFileExportImage()
171 GetDocument()->ExportView (myView);
174 void OCC_3dView::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
176 if (!myView.IsNull())
177 myView->MustBeResized();
181 void OCC_3dView::OnBUTTONBack()
183 myView->SetProj(V3d_Xneg);
186 // See the front View
187 void OCC_3dView::OnBUTTONFront()
189 myView->SetProj(V3d_Xpos);
192 // See the bottom View
193 void OCC_3dView::OnBUTTONBottom()
195 myView->SetProj(V3d_Zneg);
199 void OCC_3dView::OnBUTTONTop()
201 myView->SetProj(V3d_Zpos);
205 void OCC_3dView::OnBUTTONLeft()
207 myView->SetProj(V3d_Ypos);
210 // See the right View
211 void OCC_3dView::OnBUTTONRight()
213 myView->SetProj(V3d_Yneg);
216 // See the axonometric View
217 void OCC_3dView::OnBUTTONAxo()
219 myView->SetProj(V3d_XposYnegZpos);
222 void OCC_3dView::OnBUTTONHlrOff()
224 myHlrModeIsOn = Standard_False;
225 myView->SetComputedMode (myHlrModeIsOn);
228 void OCC_3dView::OnBUTTONHlrOn()
230 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
231 myHlrModeIsOn = Standard_True;
232 myView->SetComputedMode (myHlrModeIsOn);
233 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
236 void OCC_3dView::OnBUTTONPan()
238 myCurrentMode = CurAction3d_DynamicPanning;
241 void OCC_3dView::OnBUTTONPanGlo()
243 // save the current zoom value
244 myCurZoom = myView->Scale();
248 myCurrentMode = CurAction3d_GlobalPanning;
251 void OCC_3dView::OnBUTTONReset()
256 void OCC_3dView::OnBUTTONRot()
258 myCurrentMode = CurAction3d_DynamicRotation;
261 void OCC_3dView::OnBUTTONZoomAll()
267 void OCC_3dView::OnBUTTONZoomProg()
268 { myCurrentMode = CurAction3d_DynamicZooming; }
270 void OCC_3dView::OnBUTTONZoomWin()
271 { myCurrentMode = CurAction3d_WindowZooming; }
273 void OCC_3dView::OnLButtonDown(UINT nFlags, CPoint point)
275 // save the current mouse coordinate in min
276 myXmin=point.x; myYmin=point.y;
277 myXmax=point.x; myYmax=point.y;
279 if ( nFlags & MK_CONTROL )
281 // Button MB1 down Control :start zomming
282 // SetCursor(AfxGetApp()->LoadStandardCursor());
286 switch (myCurrentMode)
288 case CurAction3d_Nothing : // start a drag
289 if (nFlags & MK_SHIFT)
290 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
292 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
295 case CurAction3d_DynamicZooming : // noting
297 case CurAction3d_WindowZooming : // noting
299 case CurAction3d_DynamicPanning :// noting
301 case CurAction3d_GlobalPanning :// noting
303 case CurAction3d_DynamicRotation :
306 myView->SetComputedMode (Standard_False);
309 myView->StartRotation(point.x,point.y);
312 Standard_Failure::Raise(" incompatible Current Mode ");
318 void OCC_3dView::OnLButtonUp(UINT nFlags, CPoint point)
320 if ( nFlags & MK_CONTROL )
326 switch (myCurrentMode)
328 case CurAction3d_Nothing :
329 if (point.x == myXmin && point.y == myYmin)
330 { // no offset between down and up --> selectEvent
333 if (nFlags & MK_SHIFT )
334 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
336 GetDocument()->InputEvent (point.x,point.y,myView);
339 myXmax=point.x; myYmax=point.y;
340 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
341 if (nFlags & MK_SHIFT)
342 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
344 GetDocument()->DragEvent(point.x,point.y,1,myView);
347 case CurAction3d_DynamicZooming :
348 // SetCursor(AfxGetApp()->LoadStandardCursor());
349 myCurrentMode = CurAction3d_Nothing;
351 case CurAction3d_WindowZooming :
352 myXmax=point.x; myYmax=point.y;
353 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
354 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
355 // Test if the zoom window is greater than a minimale window.
357 // Do the zoom window between Pmin and Pmax
358 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
360 myCurrentMode = CurAction3d_Nothing;
362 case CurAction3d_DynamicPanning :
363 myCurrentMode = CurAction3d_Nothing;
365 case CurAction3d_GlobalPanning :
366 myView->Place(point.x,point.y,myCurZoom);
367 myCurrentMode = CurAction3d_Nothing;
369 case CurAction3d_DynamicRotation :
370 myCurrentMode = CurAction3d_Nothing;
373 CWaitCursor aWaitCursor;
374 myView->SetComputedMode (myHlrModeIsOn);
378 myView->SetComputedMode (myHlrModeIsOn);
382 Standard_Failure::Raise(" incompatible Current Mode ");
384 } //switch (myCurrentMode)
385 } // else // if ( Ctrl )
388 void OCC_3dView::OnMButtonDown(UINT nFlags, CPoint /*point*/)
390 if ( nFlags & MK_CONTROL )
392 // Button MB2 down Control : panning init
393 // SetCursor(AfxGetApp()->LoadStandardCursor());
397 void OCC_3dView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
399 if ( nFlags & MK_CONTROL )
401 // Button MB2 down Control : panning init
402 // SetCursor(AfxGetApp()->LoadStandardCursor());
406 void OCC_3dView::OnRButtonDown(UINT nFlags, CPoint point)
408 if ( nFlags & MK_CONTROL )
412 myView->SetComputedMode (Standard_False);
414 myView->StartRotation(point.x,point.y);
418 GetDocument()->Popup(point.x,point.y,myView);
422 void OCC_3dView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/)
424 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
425 myView->SetComputedMode (myHlrModeIsOn);
426 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
429 void OCC_3dView::OnMouseMove(UINT nFlags, CPoint point)
431 // ============================ LEFT BUTTON =======================
432 if ( nFlags & MK_LBUTTON)
434 if ( nFlags & MK_CONTROL )
436 // move with MB1 and Control : on the dynamic zooming
437 // Do the zoom in function of mouse's coordinates
438 myView->Zoom(myXmax,myYmax,point.x,point.y);
439 // save the current mouse coordinate in min
445 switch (myCurrentMode)
447 case CurAction3d_Nothing :
448 myXmax = point.x; myYmax = point.y;
449 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
450 if (nFlags & MK_SHIFT)
451 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
453 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
454 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
457 case CurAction3d_DynamicZooming :
458 myView->Zoom(myXmax,myYmax,point.x,point.y);
459 // save the current mouse coordinate in min \n";
460 myXmax=point.x; myYmax=point.y;
462 case CurAction3d_WindowZooming :
463 myXmax = point.x; myYmax = point.y;
464 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
465 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
467 case CurAction3d_DynamicPanning :
468 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
469 myXmax = point.x; myYmax = point.y;
471 case CurAction3d_GlobalPanning : // nothing
473 case CurAction3d_DynamicRotation :
474 myView->Rotation(point.x,point.y);
478 Standard_Failure::Raise(" incompatible Current Mode ");
480 }// switch (myCurrentMode)
481 }// if ( nFlags & MK_CONTROL ) else
483 else if ( nFlags & MK_MBUTTON)
485 if ( nFlags & MK_CONTROL )
487 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
488 myXmax = point.x; myYmax = point.y;
492 else if ( nFlags & MK_RBUTTON)
494 if ( nFlags & MK_CONTROL )
496 myView->Rotation(point.x,point.y);
501 myXmax = point.x; myYmax = point.y;
502 if (nFlags & MK_SHIFT)
503 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
505 GetDocument()->MoveEvent(point.x,point.y,myView);
509 void OCC_3dView::DrawRectangle(const Standard_Integer MinX ,
510 const Standard_Integer MinY ,
511 const Standard_Integer MaxX ,
512 const Standard_Integer MaxY ,
513 const Standard_Boolean Draw ,
514 const LineStyle aLineStyle)
516 static int m_DrawMode;
517 if (!m_Pen && aLineStyle ==Solid )
518 {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
519 else if (!m_Pen && aLineStyle ==Dot )
520 {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;}
521 else if (!m_Pen && aLineStyle == ShortDash)
522 {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;}
523 else if (!m_Pen && aLineStyle == LongDash)
524 {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;}
525 else if (aLineStyle == Default)
526 { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
528 CPen* aOldPen = NULL;
529 CClientDC clientDC(this);
530 if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
531 clientDC.SetROP2(m_DrawMode);
533 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
534 static Standard_Boolean m_IsVisible;
536 if ( m_IsVisible && !Draw) // move or up : erase at the old position
538 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
539 clientDC.LineTo(StoredMaxX,StoredMaxY);
540 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
544 StoredMinX = min ( MinX, MaxX );
545 StoredMinY = min ( MinY, MaxY );
546 StoredMaxX = max ( MinX, MaxX );
547 StoredMaxY = max ( MinY, MaxY);
549 if (Draw) // move : draw
551 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
552 clientDC.LineTo(StoredMaxX,StoredMaxY);
553 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
557 if (m_Pen) clientDC.SelectObject(aOldPen);
562 void OCC_3dView::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI)
564 pCmdUI->SetCheck (!myHlrModeIsOn);
565 pCmdUI->Enable (myHlrModeIsOn);
568 void OCC_3dView::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI)
570 pCmdUI->SetCheck (myHlrModeIsOn);
571 pCmdUI->Enable (!myHlrModeIsOn);
574 void OCC_3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
576 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
577 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
581 void OCC_3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
583 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
584 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
587 void OCC_3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
589 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
590 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
593 void OCC_3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
595 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
596 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
599 void OCC_3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
601 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
602 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
605 void OCC_3dView::OnModifyChangeBackground()
610 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
612 m_clr = RGB(R1*255,G1*255,B1*255);
614 CColorDialog dlgColor(m_clr);
615 if (dlgColor.DoModal() == IDOK)
617 m_clr = dlgColor.GetColor();
618 R1 = GetRValue(m_clr)/255.;
619 G1 = GetGValue(m_clr)/255.;
620 B1 = GetBValue(m_clr)/255.;
621 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
626 //=============================================================================
627 // function: OnStereoConfigButton
628 // purpose: Open stereographic configuration dialog
629 //=============================================================================
630 void OCC_3dView::OnStereoConfigButton()
632 m_pStereoDlg->ShowWindow (SW_SHOW);
635 //=============================================================================
636 // function: OnUpdateStereoConfigButton
637 // purpose: Enable / disable state of stereo configuration button
638 //=============================================================================
639 void OCC_3dView::OnUpdateStereoConfigButton (CCmdUI* theCmdUI)
642 Handle(Graphic3d_Camera) aCamera = myView->Camera();
644 // check that button is enabled
645 Standard_Boolean isEnabled = !aCamera.IsNull() && aCamera->IsStereo();
647 // update toggle state
648 theCmdUI->Enable (isEnabled);
652 m_pStereoDlg->ShowWindow (SW_HIDE);