1 // Viewer3dView.cpp : implementation of the CViewer3dView class
6 #include "Viewer3dView.h"
8 #include "OCC_MainFrame.h"
9 #include "Viewer3dApp.h"
10 #include "Viewer3dDoc.h"
12 #include "ShadingModelDlg.h"
13 #include "ModelClippingDlg.h"
14 #include "TrihedronDlg.h"
16 #include <V3d_AmbientLight.hxx>
17 #include <V3d_DirectionalLight.hxx>
18 #include <V3d_PositionalLight.hxx>
19 #include <V3d_PositionLight.hxx>
20 #include <V3d_SpotLight.hxx>
21 #include <TopoDS_Solid.hxx>
22 #include <Precision.hxx>
23 #include <Graphic3d_GraphicDriver.hxx>
34 static char THIS_FILE[] = __FILE__;
37 //gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView);
40 Handle(AIS_Shape) spotConeShape=new AIS_Shape(TopoDS_Solid());
41 Handle(AIS_Shape) directionalEdgeShape=new AIS_Shape(TopoDS_Edge());
43 /////////////////////////////////////////////////////////////////////////////
46 IMPLEMENT_DYNCREATE(CViewer3dView, CView)
48 BEGIN_MESSAGE_MAP(CViewer3dView, CView)
49 //{{AFX_MSG_MAP(CViewer3dView)
50 ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
51 ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
52 ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
53 ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
54 ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
55 ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
56 ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
57 ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
58 ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
59 ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
60 ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
61 ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
62 ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
63 ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
65 ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
66 ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
75 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
76 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
77 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
78 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
79 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
80 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
81 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
82 ON_COMMAND(ID_Modify_ChangeBackground , OnModifyChangeBackground)
83 ON_COMMAND(ID_DIRECTIONAL_LIGHT, OnDirectionalLight)
84 ON_COMMAND(ID_SPOT_LIGHT, OnSpotLight)
85 ON_COMMAND(ID_POSITIONAL_LIGHT, OnPositionalLight)
86 ON_COMMAND(ID_AMBIENT_LIGHT, OnAmbientLight)
87 ON_COMMAND(ID_SCALE, OnScale)
88 ON_COMMAND(ID_SHADINGMODEL, OnShadingmodel)
89 ON_COMMAND(ID_ANTIALIASINGONOFF, OnAntialiasingonoff)
90 ON_COMMAND(ID_CLEAR_LIGHTS, OnClearLights)
91 ON_COMMAND(ID_MODELCLIPPING, OnModelclipping)
92 ON_COMMAND(ID_OPTIONS_TRIHEDRON_STATIC_TRIHEDRON, OnOptionsTrihedronStaticTrihedron)
96 /////////////////////////////////////////////////////////////////////////////
97 // CViewer3dView construction/destruction
99 CViewer3dView::CViewer3dView()
103 myVisMode (VIS_SHADE),
104 myCurrentMode (CurAction3d_Nothing),
110 NbActiveLights (2), // There are 2 default active lights
111 myHlrModeIsOn (Standard_False),
116 // TODO: add construction code here
117 myGraphicDriver = ((CViewer3dApp*)AfxGetApp())->GetGraphicDriver();
120 CViewer3dView::~CViewer3dView()
123 if (m_Pen) delete m_Pen;
126 BOOL CViewer3dView::PreCreateWindow(CREATESTRUCT& cs)
128 // TODO: Modify the Window class or styles here by modifying
129 // the CREATESTRUCT cs
130 cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC, ::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
131 return CView::PreCreateWindow(cs);
134 /////////////////////////////////////////////////////////////////////////////
135 // CViewer3dView drawing
136 void CViewer3dView::OnInitialUpdate()
138 CView::OnInitialUpdate();
140 myView = GetDocument()->GetViewer()->CreateView();
142 // store for restore state after rotation (witch is in Degenerated mode)
143 myHlrModeIsOn = Standard_False;
144 myView->SetComputedMode (myHlrModeIsOn);
146 Handle(WNT_Window) aWNTWindow = new WNT_Window (GetSafeHwnd());
147 myView->SetWindow(aWNTWindow);
148 if (!aWNTWindow->IsMapped()) aWNTWindow->Map();
150 // Standard_Integer w=100 , h=100 ; /* Debug Matrox */
151 // aWNTWindow->Size (w,h) ; /* Keeps me unsatisfied (rlb)..... */
152 /* Resize is not supposed to be done on */
154 /* I suspect another problem elsewhere */
155 // ::PostMessage ( GetSafeHwnd () , WM_SIZE , SIZE_RESTORED , w + h*65536 ) ;
157 // store the mode ( nothing , dynamic zooming, dynamic ... )
158 myCurrentMode = CurAction3d_Nothing;
159 myVisMode = VIS_SHADE;
163 void CViewer3dView::OnDraw(CDC* /*pDC*/)
165 CViewer3dDoc* pDoc = GetDocument();
170 /////////////////////////////////////////////////////////////////////////////
171 // CViewer3dView diagnostics
174 void CViewer3dView::AssertValid() const
176 CView::AssertValid();
179 void CViewer3dView::Dump(CDumpContext& dc) const
184 CViewer3dDoc* CViewer3dView::GetDocument() // non-debug version is inline
186 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewer3dDoc)));
187 return (CViewer3dDoc*)m_pDocument;
191 /////////////////////////////////////////////////////////////////////////////
192 // CViewer3dView message handlers
194 gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
196 Standard_Real XEye,YEye,ZEye,XAt,YAt,ZAt;
197 aView->Eye(XEye,YEye,ZEye);
198 aView->At(XAt,YAt,ZAt);
199 gp_Pnt EyePoint(XEye,YEye,ZEye);
200 gp_Pnt AtPoint(XAt,YAt,ZAt);
202 gp_Vec EyeVector(EyePoint,AtPoint);
203 gp_Dir EyeDir(EyeVector);
205 gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
207 aView->Convert(int(x),int(y),X,Y,Z);
208 gp_Pnt ConvertedPoint(X,Y,Z);
209 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint);
211 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),
212 ConvertedPointOnPlane.Y(),
217 void CViewer3dView::OnSize(UINT nType, int cx, int cy)
219 CView::OnSize (nType, cx, cy);
220 if (!myView.IsNull())
221 myView->MustBeResized();
224 void CViewer3dView::OnBUTTONBack()
226 myView->SetProj(V3d_Ypos);
228 TCollection_AsciiString Message("\
229 myView->SetProj(V3d_Ypos);\n\
232 // Update The Result Message Dialog
233 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
234 } // See the back View
235 void CViewer3dView::OnBUTTONFront()
237 myView->SetProj(V3d_Yneg);
239 TCollection_AsciiString Message("\
240 myView->SetProj(V3d_Yneg);\n\
243 // Update The Result Message Dialog
244 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
245 } // See the front View
247 void CViewer3dView::OnBUTTONBottom()
249 myView->SetProj(V3d_Zneg);
251 TCollection_AsciiString Message("\
252 myView->SetProj(V3d_Zneg);\n\
255 // Update The Result Message Dialog
256 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
257 } // See the bottom View
258 void CViewer3dView::OnBUTTONTop()
260 myView->SetProj(V3d_Zpos);
262 TCollection_AsciiString Message("\
263 myView->SetProj(V3d_Zpos);\n\
266 // Update The Result Message Dialog
267 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
268 } // See the top View
270 void CViewer3dView::OnBUTTONLeft()
272 myView->SetProj(V3d_Xneg);
274 TCollection_AsciiString Message("\
275 myView->SetProj(V3d_Xneg);\n\
278 // Update The Result Message Dialog
279 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
280 } // See the left View
281 void CViewer3dView::OnBUTTONRight()
283 myView->SetProj(V3d_Xpos);
285 TCollection_AsciiString Message("\
286 myView->SetProj(V3d_Xpos);\n\
289 // Update The Result Message Dialog
290 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
291 } // See the right View
293 void CViewer3dView::OnBUTTONAxo()
295 myView->SetProj(V3d_XposYnegZpos);
297 TCollection_AsciiString Message("\
298 myView->SetProj(V3d_XposYnegZpos);\n\
301 // Update The Result Message Dialog
302 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
303 } // See the axonometric View
305 void CViewer3dView::OnBUTTONHlrOff()
307 myHlrModeIsOn = Standard_False;
308 myView->SetComputedMode (myHlrModeIsOn);
311 TCollection_AsciiString aMsg ("myView->SetComputedMode (Standard_False);\n"
314 // Update The Result Message Dialog
315 GetDocument()->UpdateResultMessageDlg ("SetComputedMode", aMsg);
318 void CViewer3dView::OnBUTTONHlrOn()
320 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
321 myHlrModeIsOn = Standard_True;
322 myView->SetComputedMode (myHlrModeIsOn);
324 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
326 TCollection_AsciiString aMsg ("myView->SetComputedMode (Standard_True);\n"
329 // Update The Result Message Dialog
330 GetDocument()->UpdateResultMessageDlg ("SetComputedMode", aMsg);
333 void CViewer3dView::OnBUTTONPan()
334 { myCurrentMode = CurAction3d_DynamicPanning; }
336 void CViewer3dView::OnBUTTONPanGlo()
338 // save the current zoom value
339 myCurZoom = myView->Scale();
343 myCurrentMode = CurAction3d_GlobalPanning;
346 void CViewer3dView::OnBUTTONReset()
349 void CViewer3dView::OnBUTTONRot()
350 { myCurrentMode = CurAction3d_DynamicRotation; }
353 void CViewer3dView::OnBUTTONZoomAll()
359 void CViewer3dView::OnBUTTONZoomProg()
360 { myCurrentMode = CurAction3d_DynamicZooming; }
362 void CViewer3dView::OnBUTTONZoomWin()
363 { myCurrentMode = CurAction3d_WindowZooming; }
365 void CViewer3dView::OnLButtonDown(UINT nFlags, CPoint point)
367 // save the current mouse coordinate in min
368 myXmin=point.x; myYmin=point.y;
369 myXmax=point.x; myYmax=point.y;
371 if ( nFlags & MK_CONTROL )
373 // Button MB1 down Control :start zomming
374 // SetCursor(AfxGetApp()->LoadStandardCursor());
378 switch (myCurrentMode)
380 case CurAction3d_Nothing : // start a drag
381 if (nFlags & MK_SHIFT)
382 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
384 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
387 case CurAction3d_DynamicZooming : // noting
389 case CurAction3d_WindowZooming :
391 case CurAction3d_DynamicPanning :// noting
393 case CurAction3d_GlobalPanning :// noting
395 case CurAction3d_DynamicRotation :
398 myView->SetComputedMode (Standard_False);
400 myView->StartRotation (point.x, point.y);
402 case CurAction3d_BeginPositionalLight :
404 p1 = ConvertClickToPoint(point.x,point.y,myView);
405 myCurrent_PositionalLight->SetPosition(p1.X(),p1.Y(),p1.Z()) ;
406 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
407 myCurrentMode = CurAction3d_Nothing;
409 TCollection_AsciiString Message("\
410 myCurrent_PositionalLight->SetPosition(Xp, Yp, Zp) ;\n\
412 // Update The Result Message Dialog
413 GetDocument()->UpdateResultMessageDlg("SetPosition",Message);
416 case CurAction3d_BeginSpotLight :
418 p1 = ConvertClickToPoint(point.x,point.y,myView);
419 myCurrent_SpotLight = new V3d_SpotLight (p1, gp_Dir (gp_XYZ (0.0, 0.0, 1.0) - p1.XYZ()), Quantity_NOC_RED);
420 myView->SetLightOn(myCurrent_SpotLight);
422 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
423 Standard_Real coneHeigth=p1.Distance(p2);
424 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
425 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
426 spotConeShape->Set(MakeCone.Solid());
427 GetDocument()->GetAISContext()->Display (spotConeShape, 0, -1, Standard_True);
428 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
429 myCurrentMode = CurAction3d_TargetSpotLight;
431 TCollection_AsciiString Message("\
432 myCurrent_SpotLight->SetDirection(Xv, Yv, Zv);\n\
434 // Update The Result Message Dialog
435 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
438 case CurAction3d_TargetSpotLight :
440 p2 = ConvertClickToPoint(point.x,point.y,myView);
441 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a third point (to define the angle)");
442 myCurrentMode = CurAction3d_EndSpotLight;
444 TCollection_AsciiString Message("\
445 myCurrent_SpotLight->SetAngle(Angle) ;\n\
447 // Update The Result Message Dialog
448 GetDocument()->UpdateResultMessageDlg("SetAngle",Message);
451 case CurAction3d_EndSpotLight :
452 GetDocument()->GetAISContext()->Erase (spotConeShape, Standard_True);
453 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
454 myCurrentMode = CurAction3d_Nothing;
456 case CurAction3d_BeginDirectionalLight:
458 p1 = ConvertClickToPoint(point.x,point.y,myView);
459 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
460 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
461 directionalEdgeShape->Set(MakeEdge.Edge());
462 GetDocument()->GetAISContext()->Display (directionalEdgeShape, 0, -1, Standard_True);
463 // Create a directional light
464 myCurrent_DirectionalLight = new V3d_DirectionalLight (gp_Dir (p1.XYZ() - gp_XYZ (0.,0.,1.)));
465 myView->SetLightOn(myCurrent_DirectionalLight);
467 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
468 myCurrentMode = CurAction3d_EndDirectionalLight;
471 TCollection_AsciiString Message("\
472 myCurrent_DirectionalLight->SetDirection(Xv, Yv, Zv);\n\
474 // Update The Result Message Dialog
475 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
478 case CurAction3d_EndDirectionalLight:
479 GetDocument()->GetAISContext()->Erase (directionalEdgeShape, Standard_True);
480 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
481 myCurrentMode = CurAction3d_Nothing;
487 void CViewer3dView::OnKeyDown(UINT nChar, UINT /*nRepCnt*/, UINT /*nFlags*/)
490 if( nChar == X_Key || nChar == Y_Key || nChar == Z_Key )
492 else if( (nChar == VK_ADD || nChar == VK_SUBTRACT) && myAxisKey )
494 myScaleDirection = nChar;
495 if( myAxisKey && myScaleDirection )
500 if( myScaleDirection == VK_ADD ) scaleX ++;
501 if( myScaleDirection == VK_SUBTRACT ) scaleX --;
504 if( myScaleDirection == VK_ADD ) scaleY ++;
505 if( myScaleDirection == VK_SUBTRACT ) scaleY --;
508 if( myScaleDirection == VK_ADD ) scaleZ ++;
509 if( myScaleDirection == VK_SUBTRACT ) scaleZ --;
514 if( scaleX < 1 ) scaleX = 1;
515 if( scaleY < 1 ) scaleY = 1;
516 if( scaleZ < 1 ) scaleZ = 1;
518 myView->SetAxialScale( scaleX, scaleY, scaleZ );
523 void CViewer3dView::OnLButtonUp(UINT nFlags, CPoint point)
525 if ( nFlags & MK_CONTROL )
531 switch (myCurrentMode)
533 case CurAction3d_Nothing :
534 if (point.x == myXmin && point.y == myYmin)
535 { // no offset between down and up --> selectEvent
538 if (nFlags & MK_SHIFT )
539 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
541 GetDocument()->InputEvent (point.x,point.y,myView);
544 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
547 if (nFlags & MK_SHIFT)
548 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
550 GetDocument()->DragEvent(point.x,point.y,1,myView);
553 case CurAction3d_DynamicZooming :
554 // SetCursor(AfxGetApp()->LoadStandardCursor());
555 myCurrentMode = CurAction3d_Nothing;
557 case CurAction3d_WindowZooming :
558 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
561 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
562 // Test if the zoom window is greater than a minimale window.
564 // Do the zoom window between Pmin and Pmax
565 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
567 myCurrentMode = CurAction3d_Nothing;
569 case CurAction3d_DynamicPanning :
570 myCurrentMode = CurAction3d_Nothing;
572 case CurAction3d_GlobalPanning :
573 myView->Place(point.x,point.y,myCurZoom);
574 myCurrentMode = CurAction3d_Nothing;
576 case CurAction3d_DynamicRotation :
577 myCurrentMode = CurAction3d_Nothing;
579 } //switch (myCurrentMode)
580 } // else // if ( Ctrl )
583 void CViewer3dView::OnMButtonDown(UINT nFlags, CPoint /*point*/)
585 if ( nFlags & MK_CONTROL )
587 // Button MB2 down Control : panning init
588 // SetCursor(AfxGetApp()->LoadStandardCursor());
592 void CViewer3dView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
594 if ( nFlags & MK_CONTROL )
596 // Button MB2 down Control : panning init
597 // SetCursor(AfxGetApp()->LoadStandardCursor());
601 void CViewer3dView::OnRButtonDown(UINT nFlags, CPoint point)
603 if ( nFlags & MK_CONTROL )
605 // SetCursor(AfxGetApp()->LoadStandardCursor());
608 myView->SetComputedMode (Standard_False);
610 myView->StartRotation (point.x, point.y);
614 GetDocument()->Popup (point.x, point.y, myView);
618 void CViewer3dView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/)
620 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
623 myView->SetComputedMode (myHlrModeIsOn);
626 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
629 void CViewer3dView::OnMouseMove(UINT nFlags, CPoint point)
631 // ============================ LEFT BUTTON =======================
632 if ( nFlags & MK_LBUTTON)
634 if ( nFlags & MK_CONTROL )
636 // move with MB1 and Control : on the dynamic zooming
637 // Do the zoom in function of mouse's coordinates
638 myView->Zoom(myXmax,myYmax,point.x,point.y);
639 // save the current mouse coordinate in min
645 switch (myCurrentMode)
647 case CurAction3d_Nothing :
649 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
650 myXmax = point.x; myYmax = point.y;
651 if (nFlags & MK_SHIFT)
652 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
654 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
655 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
657 case CurAction3d_DynamicZooming :
658 myView->Zoom(myXmax,myYmax,point.x,point.y);
659 // save the current mouse coordinate in min \n";
660 myXmax=point.x; myYmax=point.y;
662 case CurAction3d_WindowZooming :
663 myXmax = point.x; myYmax = point.y;
664 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
665 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
668 case CurAction3d_DynamicPanning :
669 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
670 myXmax = point.x; myYmax = point.y;
672 case CurAction3d_GlobalPanning : // nothing
674 case CurAction3d_DynamicRotation :
675 myView->Rotation(point.x,point.y);
678 }// switch (myCurrentMode)
679 }// if ( nFlags & MK_CONTROL ) else
680 } else // if ( nFlags & MK_LBUTTON)
681 // ============================ MIDDLE BUTTON =======================
682 if ( nFlags & MK_MBUTTON)
684 if ( nFlags & MK_CONTROL )
686 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
687 myXmax = point.x; myYmax = point.y;
690 } else // if ( nFlags & MK_MBUTTON)
691 // ============================ RIGHT BUTTON =======================
692 if ( nFlags & MK_RBUTTON)
694 if ( nFlags & MK_CONTROL )
696 myView->Rotation(point.x,point.y);
698 }else //if ( nFlags & MK_RBUTTON)
699 // ============================ NO BUTTON =======================
701 myXmax = point.x; myYmax = point.y;
702 if (myCurrentMode == CurAction3d_EndDirectionalLight)
704 p2 = ConvertClickToPoint(point.x,point.y,myView);
705 //Update the light dynamically
706 if( p1.Distance(p2)>Precision::Confusion())
708 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
709 directionalEdgeShape->Set(MakeEdge.Edge());
710 GetDocument()->GetAISContext()->Redisplay(directionalEdgeShape,0,Standard_True);
711 myCurrent_DirectionalLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
712 myView->UpdateLights();
715 else if (myCurrentMode == CurAction3d_BeginPositionalLight)
717 p2 = ConvertClickToPoint(point.x,point.y,myView);
718 //Update the light dynamically
719 myCurrent_PositionalLight->SetPosition(p2.X(),p2.Y(),p2.Z());
720 myView->UpdateLights();
722 else if (myCurrentMode == CurAction3d_TargetSpotLight)
724 p2 = ConvertClickToPoint(point.x,point.y,myView);
725 //Update the light dynamically
726 Standard_Real coneHeigth=p1.Distance(p2);
727 if( coneHeigth>Precision::Confusion())
729 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
730 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
731 spotConeShape->Set(MakeCone.Solid());
732 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,Standard_True);
733 myCurrent_SpotLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
734 myView->UpdateLights();
737 else if (myCurrentMode == CurAction3d_EndSpotLight)
739 p3 = ConvertClickToPoint(point.x,point.y,myView);
740 //Update the light dynamically
741 Standard_Real coneHeigth=p1.Distance(p2);
742 if( (p2.Distance(p3))>Precision::Confusion())
744 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
745 0, p2.Distance(p3), coneHeigth);
746 spotConeShape->Set(MakeCone.Solid());
747 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,Standard_True);
748 myCurrent_SpotLight->SetAngle((float )atan(p2.Distance(p3)/p1.Distance(p2))) ;
749 myView->UpdateLights();
752 if (nFlags & MK_SHIFT)
753 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
755 GetDocument()->MoveEvent(point.x,point.y,myView);
759 void CViewer3dView::OnUpdateBUTTONHlrOff (CCmdUI* pCmdUI)
761 pCmdUI->SetCheck (!myHlrModeIsOn);
762 pCmdUI->Enable (myHlrModeIsOn);
765 void CViewer3dView::OnUpdateBUTTONHlrOn (CCmdUI* pCmdUI)
767 pCmdUI->SetCheck (myHlrModeIsOn);
768 pCmdUI->Enable (!myHlrModeIsOn);
771 void CViewer3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
773 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
774 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
778 void CViewer3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
780 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
781 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
784 void CViewer3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
786 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
787 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
790 void CViewer3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
792 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
793 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
796 void CViewer3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
798 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
799 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
802 void CViewer3dView::DrawRectangle(const Standard_Integer MinX ,
803 const Standard_Integer MinY ,
804 const Standard_Integer MaxX ,
805 const Standard_Integer MaxY ,
806 const Standard_Boolean Draw ,
807 const LineStyle aLineStyle)
809 static int m_DrawMode;
810 if (!m_Pen && aLineStyle ==Solid )
811 {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
812 else if (!m_Pen && aLineStyle ==Dot )
813 {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;}
814 else if (!m_Pen && aLineStyle == ShortDash)
815 {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;}
816 else if (!m_Pen && aLineStyle == LongDash)
817 {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;}
818 else if (aLineStyle == Default)
819 { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
821 CPen* aOldPen = NULL;
822 CClientDC clientDC(this);
823 if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
824 clientDC.SetROP2(m_DrawMode);
826 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
827 static Standard_Boolean m_IsVisible;
829 if ( m_IsVisible && !Draw) // move or up : erase at the old position
831 clientDC.MoveTo(StoredMinX,StoredMinY);
832 clientDC.LineTo(StoredMinX,StoredMaxY);
833 clientDC.LineTo(StoredMaxX,StoredMaxY);
834 clientDC.LineTo(StoredMaxX,StoredMinY);
835 clientDC.LineTo(StoredMinX,StoredMinY);
839 StoredMinX = Min ( MinX, MaxX );
840 StoredMinY = Min ( MinY, MaxY );
841 StoredMaxX = Max ( MinX, MaxX );
842 StoredMaxY = Max ( MinY, MaxY);
844 if (Draw) // move : draw
846 clientDC.MoveTo(StoredMinX,StoredMinY);
847 clientDC.LineTo(StoredMinX,StoredMaxY);
848 clientDC.LineTo(StoredMaxX,StoredMaxY);
849 clientDC.LineTo(StoredMaxX,StoredMinY);
850 clientDC.LineTo(StoredMinX,StoredMinY);
855 clientDC.SelectObject(aOldPen);
858 void CViewer3dView::OnModifyChangeBackground()
863 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
865 m_clr = RGB(R1*255,G1*255,B1*255);
867 CColorDialog dlgColor(m_clr);
868 if (dlgColor.DoModal() == IDOK)
870 m_clr = dlgColor.GetColor();
871 R1 = GetRValue(m_clr)/255.;
872 G1 = GetGValue(m_clr)/255.;
873 B1 = GetBValue(m_clr)/255.;
874 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
879 void CViewer3dView::OnDirectionalLight()
881 // Directional light source creation
883 // Checking if the Active lights limit number is not reached
884 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
887 aMsg.Format (L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
888 MessageBox (aMsg, L"Light creation", MB_OK);
893 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a first point");
894 myCurrentMode = CurAction3d_BeginDirectionalLight;
896 TCollection_AsciiString Message("\
897 myCurrent_DirectionalLight = new V3d_DirectionalLight (gp_Dir (theDirection));\n\
899 myView->SetLightOn(myCurrent_DirectionalLight);\n\
902 // Update The Result Message Dialog
903 GetDocument()->UpdateResultMessageDlg("V3d_DirectionalLight",Message);
906 void CViewer3dView::OnSpotLight()
908 // Spot light source creation
910 // Checking if the Active lights limit number is not reached
911 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
914 aMsg.Format(L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
915 MessageBox (aMsg, L"Light creation", MB_OK);
919 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
920 myCurrentMode = CurAction3d_BeginSpotLight;
922 TCollection_AsciiString Message("\
923 myCurrent_SpotLight = new V3d_SpotLight (gp_Pnt (thePosition), gp_Dir (theDirection), Quantity_NOC_RED);\n\
925 myView->SetLightOn(myCurrent_SpotLight);\n\
928 // Update The Result Message Dialog
929 GetDocument()->UpdateResultMessageDlg("V3d_SpotLight",Message);
932 void CViewer3dView::OnPositionalLight()
934 // Positional light source creation
936 // Checking if the Active lights limit number is not reached
937 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
940 aMsg.Format(L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
941 MessageBox(aMsg, L"Light creation", MB_OK);
945 myCurrent_PositionalLight=new V3d_PositionalLight (gp_Pnt (0,0,0), Quantity_NOC_GREEN);
946 myCurrent_PositionalLight->SetAttenuation (1, 0);
947 myView->SetLightOn(myCurrent_PositionalLight);
949 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
950 myCurrentMode = CurAction3d_BeginPositionalLight;
952 TCollection_AsciiString Message("\
953 myCurrent_PositionalLight=new V3d_PositionalLight (gp_Pnt(thePosition),Quantity_NOC_GREEN);\n\
954 myCurrent_PositionalLight->SetAttenuation (1, 0);\n\
956 myView->SetLightOn(myCurrent_PositionalLight) ;\n\
959 // Update The Result Message Dialog
960 GetDocument()->UpdateResultMessageDlg("V3d_PositionalLight",Message);
963 void CViewer3dView::OnAmbientLight()
965 // Ambiant light source creation
967 // Checking if the Active lights limit number is not reached
968 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
971 aMsg.Format(L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
972 MessageBox(aMsg, L"Light creation", MB_OK);
976 myCurrent_AmbientLight=new V3d_AmbientLight (Quantity_NOC_GRAY);
977 myView->SetLightOn(myCurrent_AmbientLight) ;
980 myView->UpdateLights();
982 TCollection_AsciiString Message("\
983 myCurrent_AmbientLight=new V3d_AmbientLight(Quantity_NOC_GRAY);\n\
985 myView->SetLightOn(myCurrent_AmbientLight) ;\n\
988 // Update The Result Message Dialog
989 GetDocument()->UpdateResultMessageDlg("V3d_AmbientLight",Message);
993 void CViewer3dView::OnScale()
995 ScaleDlg Dlg(myView, this);
1002 void CViewer3dView::OnShadingmodel()
1004 TCollection_AsciiString Message("\
1005 myView->SetShadingModel(V3d_TypeOfShadingModel myTypeOfShadingModel);\n\
1007 myCurrent_V3d_View->Update();\n\
1010 // Update The Result Message Dialog
1011 GetDocument()->UpdateResultMessageDlg("SetShadingModel",Message);
1013 CShadingModelDlg Dlg(myView);
1019 void CViewer3dView::OnAntialiasingonoff()
1021 Graphic3d_RenderingParams& aParams = myView->ChangeRenderingParams();
1022 aParams.NbMsaaSamples = aParams.NbMsaaSamples == 0 ? 8 : 0;
1025 TCollection_AsciiString Message("\
1026 Graphic3d_RenderingParams& aParams = myView->ChangeRenderingParams();\n\
1027 aParams.NbMsaaSamples = aParams.NbMsaaSamples == 0 ? 8 : 0;\n\
1030 // Update The Result Message Dialog
1031 GetDocument()->UpdateResultMessageDlg("SetAntialiasingOn/SetAntialiasingOff",Message);
1034 void CViewer3dView::OnClearLights()
1036 // Setting Off all viewer active lights
1037 TColStd_ListOfTransient lights;
1038 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())
1040 lights.Append(myView->Viewer()->ActiveLight());
1042 TColStd_ListIteratorOfListOfTransient itrLights(lights);
1043 for (; itrLights.More(); itrLights.Next())
1045 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());
1046 myView->Viewer()->SetLightOff(light);
1049 // Setting Off all view active lights
1051 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())
1053 lights.Append(myView->ActiveLight());
1055 itrLights.Initialize(lights);
1056 for (; itrLights.More(); itrLights.Next())
1058 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());
1059 myView->SetLightOff(light);
1062 myView->Viewer()->SetDefaultLights();// Setting the default lights on
1064 NbActiveLights = 2;// There are 2 default active lights
1068 TCollection_AsciiString Message("\
1069 // Setting Off all viewer active lights\n\
1070 TColStd_ListOfTransient lights;\n\
1071 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())\n\
1073 lights.Append(myView->Viewer()->ActiveLight());\n\
1075 TColStd_ListIteratorOfListOfTransient itrLights(lights);\n\
1076 for (; itrLights.More(); itrLights.Next())\n\
1078 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());\n\
1079 myView->Viewer()->SetLightOff(light);\n\
1082 // Setting Off all view active lights\n\
1084 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())\n\
1086 lights.Append(myView->ActiveLight());\n\
1088 itrLights.Initialize(lights);\n\
1089 for (; itrLights.More(); itrLights.Next())\n\
1091 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());\n\
1092 myView->SetLightOff(light);\n\
1095 myView->Viewer()->SetDefaultLights();// Setting the default lights on\n\
1098 // Update The Result Message Dialog
1099 GetDocument()->UpdateResultMessageDlg("SetLightOff",Message);
1102 void CViewer3dView::OnModelclipping()
1104 if (myClippingPlane.IsNull())
1106 gp_Pln aClipPlane (gp_Pnt (0.0, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
1107 gp_Pln aFacePlane (gp_Pnt (0.1, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
1109 // create clipping plane and add to view
1110 myClippingPlane = new Graphic3d_ClipPlane (aClipPlane);
1112 // shape to represent clipping plane
1113 BRepBuilderAPI_MakeFace aMakeFaceCommand (aFacePlane, 200.0, -200.0, 410.0, -410.0);
1114 TopoDS_Face aShape = aMakeFaceCommand.Face();
1115 myShape = new AIS_Shape (aShape);
1116 myShape->SetTransparency (0.5);
1119 CModelClippingDlg aClippingDlg (myView, myShape, myClippingPlane, GetDocument());
1121 aClippingDlg.DoModal();
1124 void CViewer3dView::OnOptionsTrihedronStaticTrihedron()
1126 CTrihedronDlg Dlg(myView, GetDocument());
1130 void CViewer3dView::InitButtons()
1137 myCurrentMode = CurAction3d_Nothing;
1140 void CViewer3dView::Reset()
1143 myVisMode = VIS_SHADE;
1144 if (!myView.IsNull())
1151 void CViewer3dView::GetViewAt (Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const
1153 myView->At (theX, theY, theZ);
1156 void CViewer3dView::SetViewAt (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ)
1158 myView->SetAt (theX, theY, theZ);
1161 void CViewer3dView::GetViewEye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)
1166 void CViewer3dView::SetViewEye(Standard_Real X, Standard_Real Y, Standard_Real Z)
1168 myView->SetEye(X,Y,Z);
1171 Standard_Real CViewer3dView::GetViewScale()
1173 return myView->Scale();
1176 void CViewer3dView::SetViewScale(Standard_Real Coef)
1178 myView->SetScale(Coef);
1181 void CViewer3dView::RedrawVisMode()
1186 GetDocument()->GetAISContext()->SetDisplayMode (AIS_WireFrame, Standard_True);
1187 myView->SetComputedMode (Standard_False);
1191 GetDocument()->GetAISContext()->SetDisplayMode (AIS_Shaded, Standard_True);
1192 myView->SetComputedMode (Standard_False);
1196 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1197 myView->SetComputedMode (Standard_True);
1199 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1200 GetDocument()->GetAISContext()->SetDisplayMode (AIS_WireFrame, Standard_True);