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 V3d_Coordinate 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);
310 TCollection_AsciiString aMsg ("myView->SetComputedMode (Standard_False);\n"
313 // Update The Result Message Dialog
314 GetDocument()->UpdateResultMessageDlg ("SetComputedMode", aMsg);
317 void CViewer3dView::OnBUTTONHlrOn()
319 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
320 myHlrModeIsOn = Standard_True;
321 myView->SetComputedMode (myHlrModeIsOn);
322 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
324 TCollection_AsciiString aMsg ("myView->SetComputedMode (Standard_True);\n"
327 // Update The Result Message Dialog
328 GetDocument()->UpdateResultMessageDlg ("SetComputedMode", aMsg);
331 void CViewer3dView::OnBUTTONPan()
332 { myCurrentMode = CurAction3d_DynamicPanning; }
334 void CViewer3dView::OnBUTTONPanGlo()
336 // save the current zoom value
337 myCurZoom = myView->Scale();
341 myCurrentMode = CurAction3d_GlobalPanning;
344 void CViewer3dView::OnBUTTONReset()
347 void CViewer3dView::OnBUTTONRot()
348 { myCurrentMode = CurAction3d_DynamicRotation; }
351 void CViewer3dView::OnBUTTONZoomAll()
357 void CViewer3dView::OnBUTTONZoomProg()
358 { myCurrentMode = CurAction3d_DynamicZooming; }
360 void CViewer3dView::OnBUTTONZoomWin()
361 { myCurrentMode = CurAction3d_WindowZooming; }
363 void CViewer3dView::OnLButtonDown(UINT nFlags, CPoint point)
365 // save the current mouse coordinate in min
366 myXmin=point.x; myYmin=point.y;
367 myXmax=point.x; myYmax=point.y;
369 if ( nFlags & MK_CONTROL )
371 // Button MB1 down Control :start zomming
372 // SetCursor(AfxGetApp()->LoadStandardCursor());
376 switch (myCurrentMode)
378 case CurAction3d_Nothing : // start a drag
379 if (nFlags & MK_SHIFT)
380 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
382 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
385 case CurAction3d_DynamicZooming : // noting
387 case CurAction3d_WindowZooming :
389 case CurAction3d_DynamicPanning :// noting
391 case CurAction3d_GlobalPanning :// noting
393 case CurAction3d_DynamicRotation :
396 myView->SetComputedMode (Standard_False);
398 myView->StartRotation (point.x, point.y);
400 case CurAction3d_BeginPositionalLight :
402 p1 = ConvertClickToPoint(point.x,point.y,myView);
403 myCurrent_PositionalLight->SetPosition(p1.X(),p1.Y(),p1.Z()) ;
404 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
405 myCurrentMode = CurAction3d_Nothing;
407 TCollection_AsciiString Message("\
408 myCurrent_PositionalLight->SetPosition(Xp, Yp, Zp) ;\n\
410 // Update The Result Message Dialog
411 GetDocument()->UpdateResultMessageDlg("SetPosition",Message);
414 case CurAction3d_BeginSpotLight :
416 p1 = ConvertClickToPoint(point.x,point.y,myView);
417 myCurrent_SpotLight = new V3d_SpotLight(myView->Viewer(),0.,0.,1., p1.X(),p1.Y(),p1.Z(),Quantity_NOC_RED);
418 myView->SetLightOn(myCurrent_SpotLight);
420 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
421 Standard_Real coneHeigth=p1.Distance(p2);
422 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
423 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
424 spotConeShape->Set(MakeCone.Solid());
425 GetDocument()->GetAISContext()->Display(spotConeShape,0,-1);
426 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
427 myCurrentMode = CurAction3d_TargetSpotLight;
429 TCollection_AsciiString Message("\
430 myCurrent_SpotLight->SetDirection(Xv, Yv, Zv);\n\
432 // Update The Result Message Dialog
433 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
436 case CurAction3d_TargetSpotLight :
438 p2 = ConvertClickToPoint(point.x,point.y,myView);
439 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a third point (to define the angle)");
440 myCurrentMode = CurAction3d_EndSpotLight;
442 TCollection_AsciiString Message("\
443 myCurrent_SpotLight->SetAngle(Angle) ;\n\
445 // Update The Result Message Dialog
446 GetDocument()->UpdateResultMessageDlg("SetAngle",Message);
449 case CurAction3d_EndSpotLight :
450 GetDocument()->GetAISContext()->Erase(spotConeShape);
451 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
452 myCurrentMode = CurAction3d_Nothing;
454 case CurAction3d_BeginDirectionalLight:
456 p1 = ConvertClickToPoint(point.x,point.y,myView);
457 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
458 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
459 directionalEdgeShape->Set(MakeEdge.Edge());
460 GetDocument()->GetAISContext()->Display(directionalEdgeShape,0,-1);
461 // Create a directional light
462 myCurrent_DirectionalLight = new V3d_DirectionalLight(myView->Viewer(), p1.X(),p1.Y(),p1.Z(),0.,0.,1.);
463 myView->SetLightOn(myCurrent_DirectionalLight);
465 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
466 myCurrentMode = CurAction3d_EndDirectionalLight;
469 TCollection_AsciiString Message("\
470 myCurrent_DirectionalLight->SetDirection(Xv, Yv, Zv);\n\
472 // Update The Result Message Dialog
473 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
476 case CurAction3d_EndDirectionalLight:
477 GetDocument()->GetAISContext()->Erase(directionalEdgeShape);
478 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
479 myCurrentMode = CurAction3d_Nothing;
485 void CViewer3dView::OnKeyDown(UINT nChar, UINT /*nRepCnt*/, UINT /*nFlags*/)
488 if( nChar == X_Key || nChar == Y_Key || nChar == Z_Key )
490 else if( (nChar == VK_ADD || nChar == VK_SUBTRACT) && myAxisKey )
492 myScaleDirection = nChar;
493 if( myAxisKey && myScaleDirection )
498 if( myScaleDirection == VK_ADD ) scaleX ++;
499 if( myScaleDirection == VK_SUBTRACT ) scaleX --;
502 if( myScaleDirection == VK_ADD ) scaleY ++;
503 if( myScaleDirection == VK_SUBTRACT ) scaleY --;
506 if( myScaleDirection == VK_ADD ) scaleZ ++;
507 if( myScaleDirection == VK_SUBTRACT ) scaleZ --;
512 if( scaleX < 1 ) scaleX = 1;
513 if( scaleY < 1 ) scaleY = 1;
514 if( scaleZ < 1 ) scaleZ = 1;
516 myView->SetAxialScale( scaleX, scaleY, scaleZ );
521 void CViewer3dView::OnLButtonUp(UINT nFlags, CPoint point)
523 if ( nFlags & MK_CONTROL )
529 switch (myCurrentMode)
531 case CurAction3d_Nothing :
532 if (point.x == myXmin && point.y == myYmin)
533 { // no offset between down and up --> selectEvent
536 if (nFlags & MK_SHIFT )
537 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
539 GetDocument()->InputEvent (point.x,point.y,myView);
542 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
545 if (nFlags & MK_SHIFT)
546 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
548 GetDocument()->DragEvent(point.x,point.y,1,myView);
551 case CurAction3d_DynamicZooming :
552 // SetCursor(AfxGetApp()->LoadStandardCursor());
553 myCurrentMode = CurAction3d_Nothing;
555 case CurAction3d_WindowZooming :
556 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
559 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
560 // Test if the zoom window is greater than a minimale window.
562 // Do the zoom window between Pmin and Pmax
563 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
565 myCurrentMode = CurAction3d_Nothing;
567 case CurAction3d_DynamicPanning :
568 myCurrentMode = CurAction3d_Nothing;
570 case CurAction3d_GlobalPanning :
571 myView->Place(point.x,point.y,myCurZoom);
572 myCurrentMode = CurAction3d_Nothing;
574 case CurAction3d_DynamicRotation :
575 myCurrentMode = CurAction3d_Nothing;
577 } //switch (myCurrentMode)
578 } // else // if ( Ctrl )
581 void CViewer3dView::OnMButtonDown(UINT nFlags, CPoint /*point*/)
583 if ( nFlags & MK_CONTROL )
585 // Button MB2 down Control : panning init
586 // SetCursor(AfxGetApp()->LoadStandardCursor());
590 void CViewer3dView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
592 if ( nFlags & MK_CONTROL )
594 // Button MB2 down Control : panning init
595 // SetCursor(AfxGetApp()->LoadStandardCursor());
599 void CViewer3dView::OnRButtonDown(UINT nFlags, CPoint point)
601 if ( nFlags & MK_CONTROL )
603 // SetCursor(AfxGetApp()->LoadStandardCursor());
606 myView->SetComputedMode (Standard_False);
608 myView->StartRotation (point.x, point.y);
612 GetDocument()->Popup (point.x, point.y, myView);
616 void CViewer3dView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/)
618 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
619 myView->SetComputedMode (myHlrModeIsOn);
620 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
623 void CViewer3dView::OnMouseMove(UINT nFlags, CPoint point)
625 // ============================ LEFT BUTTON =======================
626 if ( nFlags & MK_LBUTTON)
628 if ( nFlags & MK_CONTROL )
630 // move with MB1 and Control : on the dynamic zooming
631 // Do the zoom in function of mouse's coordinates
632 myView->Zoom(myXmax,myYmax,point.x,point.y);
633 // save the current mouse coordinate in min
639 switch (myCurrentMode)
641 case CurAction3d_Nothing :
643 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
644 myXmax = point.x; myYmax = point.y;
645 if (nFlags & MK_SHIFT)
646 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
648 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
649 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
651 case CurAction3d_DynamicZooming :
652 myView->Zoom(myXmax,myYmax,point.x,point.y);
653 // save the current mouse coordinate in min \n";
654 myXmax=point.x; myYmax=point.y;
656 case CurAction3d_WindowZooming :
657 myXmax = point.x; myYmax = point.y;
658 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
659 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
662 case CurAction3d_DynamicPanning :
663 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
664 myXmax = point.x; myYmax = point.y;
666 case CurAction3d_GlobalPanning : // nothing
668 case CurAction3d_DynamicRotation :
669 myView->Rotation(point.x,point.y);
672 }// switch (myCurrentMode)
673 }// if ( nFlags & MK_CONTROL ) else
674 } else // if ( nFlags & MK_LBUTTON)
675 // ============================ MIDDLE BUTTON =======================
676 if ( nFlags & MK_MBUTTON)
678 if ( nFlags & MK_CONTROL )
680 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
681 myXmax = point.x; myYmax = point.y;
684 } else // if ( nFlags & MK_MBUTTON)
685 // ============================ RIGHT BUTTON =======================
686 if ( nFlags & MK_RBUTTON)
688 if ( nFlags & MK_CONTROL )
690 myView->Rotation(point.x,point.y);
692 }else //if ( nFlags & MK_RBUTTON)
693 // ============================ NO BUTTON =======================
695 myXmax = point.x; myYmax = point.y;
696 if (myCurrentMode == CurAction3d_EndDirectionalLight)
698 p2 = ConvertClickToPoint(point.x,point.y,myView);
699 //Update the light dynamically
700 if( p1.Distance(p2)>Precision::Confusion())
702 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
703 directionalEdgeShape->Set(MakeEdge.Edge());
704 GetDocument()->GetAISContext()->Redisplay(directionalEdgeShape,0,Standard_True);
705 myCurrent_DirectionalLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
706 myView->UpdateLights();
709 else if (myCurrentMode == CurAction3d_BeginPositionalLight)
711 p2 = ConvertClickToPoint(point.x,point.y,myView);
712 //Update the light dynamically
713 myCurrent_PositionalLight->SetPosition(p2.X(),p2.Y(),p2.Z());
714 myView->UpdateLights();
716 else if (myCurrentMode == CurAction3d_TargetSpotLight)
718 p2 = ConvertClickToPoint(point.x,point.y,myView);
719 //Update the light dynamically
720 Standard_Real coneHeigth=p1.Distance(p2);
721 if( coneHeigth>Precision::Confusion())
723 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
724 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
725 spotConeShape->Set(MakeCone.Solid());
726 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,Standard_True);
727 myCurrent_SpotLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
728 myView->UpdateLights();
731 else if (myCurrentMode == CurAction3d_EndSpotLight)
733 p3 = ConvertClickToPoint(point.x,point.y,myView);
734 //Update the light dynamically
735 Standard_Real coneHeigth=p1.Distance(p2);
736 if( (p2.Distance(p3))>Precision::Confusion())
738 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
739 0, p2.Distance(p3), coneHeigth);
740 spotConeShape->Set(MakeCone.Solid());
741 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,Standard_True);
742 myCurrent_SpotLight->SetAngle(atan(p2.Distance(p3)/p1.Distance(p2))) ;
743 myView->UpdateLights();
746 if (nFlags & MK_SHIFT)
747 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
749 GetDocument()->MoveEvent(point.x,point.y,myView);
753 void CViewer3dView::OnUpdateBUTTONHlrOff (CCmdUI* pCmdUI)
755 pCmdUI->SetCheck (!myHlrModeIsOn);
756 pCmdUI->Enable (myHlrModeIsOn);
759 void CViewer3dView::OnUpdateBUTTONHlrOn (CCmdUI* pCmdUI)
761 pCmdUI->SetCheck (myHlrModeIsOn);
762 pCmdUI->Enable (!myHlrModeIsOn);
765 void CViewer3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
767 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
768 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
772 void CViewer3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
774 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
775 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
778 void CViewer3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
780 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
781 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
784 void CViewer3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
786 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
787 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
790 void CViewer3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
792 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
793 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
796 void CViewer3dView::DrawRectangle(const Standard_Integer MinX ,
797 const Standard_Integer MinY ,
798 const Standard_Integer MaxX ,
799 const Standard_Integer MaxY ,
800 const Standard_Boolean Draw ,
801 const LineStyle aLineStyle)
803 static int m_DrawMode;
804 if (!m_Pen && aLineStyle ==Solid )
805 {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
806 else if (!m_Pen && aLineStyle ==Dot )
807 {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;}
808 else if (!m_Pen && aLineStyle == ShortDash)
809 {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;}
810 else if (!m_Pen && aLineStyle == LongDash)
811 {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;}
812 else if (aLineStyle == Default)
813 { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
815 CPen* aOldPen = NULL;
816 CClientDC clientDC(this);
817 if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
818 clientDC.SetROP2(m_DrawMode);
820 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
821 static Standard_Boolean m_IsVisible;
823 if ( m_IsVisible && !Draw) // move or up : erase at the old position
825 clientDC.MoveTo(StoredMinX,StoredMinY);
826 clientDC.LineTo(StoredMinX,StoredMaxY);
827 clientDC.LineTo(StoredMaxX,StoredMaxY);
828 clientDC.LineTo(StoredMaxX,StoredMinY);
829 clientDC.LineTo(StoredMinX,StoredMinY);
833 StoredMinX = Min ( MinX, MaxX );
834 StoredMinY = Min ( MinY, MaxY );
835 StoredMaxX = Max ( MinX, MaxX );
836 StoredMaxY = Max ( MinY, MaxY);
838 if (Draw) // move : draw
840 clientDC.MoveTo(StoredMinX,StoredMinY);
841 clientDC.LineTo(StoredMinX,StoredMaxY);
842 clientDC.LineTo(StoredMaxX,StoredMaxY);
843 clientDC.LineTo(StoredMaxX,StoredMinY);
844 clientDC.LineTo(StoredMinX,StoredMinY);
849 clientDC.SelectObject(aOldPen);
852 void CViewer3dView::OnModifyChangeBackground()
857 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
859 m_clr = RGB(R1*255,G1*255,B1*255);
861 CColorDialog dlgColor(m_clr);
862 if (dlgColor.DoModal() == IDOK)
864 m_clr = dlgColor.GetColor();
865 R1 = GetRValue(m_clr)/255.;
866 G1 = GetGValue(m_clr)/255.;
867 B1 = GetBValue(m_clr)/255.;
868 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
873 void CViewer3dView::OnDirectionalLight()
875 // Directional light source creation
877 // Checking if the Active lights limit number is not reached
878 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
881 aMsg.Format (L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
882 MessageBox (aMsg, L"Light creation", MB_OK);
887 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a first point");
888 myCurrentMode = CurAction3d_BeginDirectionalLight;
890 TCollection_AsciiString Message("\
891 myCurrent_DirectionalLight = new V3d_DirectionalLight(myView->Viewer(), Xt, Yt, Zt, Xp, Yp, Zp);\n\
893 myView->SetLightOn(myCurrent_DirectionalLight);\n\
896 // Update The Result Message Dialog
897 GetDocument()->UpdateResultMessageDlg("V3d_DirectionalLight",Message);
900 void CViewer3dView::OnSpotLight()
902 // Spot light source creation
904 // Checking if the Active lights limit number is not reached
905 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
908 aMsg.Format(L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
909 MessageBox (aMsg, L"Light creation", MB_OK);
913 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
914 myCurrentMode = CurAction3d_BeginSpotLight;
916 TCollection_AsciiString Message("\
917 myCurrent_SpotLight = new V3d_SpotLight(myView->Viewer(), Xt, Yt, Zt, Xp, Yp, Zp,Quantity_NOC_RED);\n\
919 myView->SetLightOn(myCurrent_SpotLight);\n\
922 // Update The Result Message Dialog
923 GetDocument()->UpdateResultMessageDlg("V3d_SpotLight",Message);
926 void CViewer3dView::OnPositionalLight()
928 // Positional light source creation
930 // Checking if the Active lights limit number is not reached
931 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
934 aMsg.Format(L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
935 MessageBox(aMsg, L"Light creation", MB_OK);
939 myCurrent_PositionalLight=new V3d_PositionalLight(myView->Viewer(),0,0,0,Quantity_NOC_GREEN,1,0);
940 myView->SetLightOn(myCurrent_PositionalLight);
942 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
943 myCurrentMode = CurAction3d_BeginPositionalLight;
945 TCollection_AsciiString Message("\
946 myCurrent_PositionalLight=new V3d_PositionalLight(myView->Viewer(),Xp, Yp, Zp,Quantity_NOC_GREEN,1,0);\n\
948 myView->SetLightOn(myCurrent_PositionalLight) ;\n\
951 // Update The Result Message Dialog
952 GetDocument()->UpdateResultMessageDlg("V3d_PositionalLight",Message);
955 void CViewer3dView::OnAmbientLight()
957 // Ambiant light source creation
959 // Checking if the Active lights limit number is not reached
960 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
963 aMsg.Format(L"You have reach the limit number of active lights (%d).\n Clear lights to create new ones.", myGraphicDriver->InquireLightLimit());
964 MessageBox(aMsg, L"Light creation", MB_OK);
968 myCurrent_AmbientLight=new V3d_AmbientLight(myView->Viewer(), Quantity_NOC_GRAY);
969 myView->SetLightOn(myCurrent_AmbientLight) ;
972 myView->UpdateLights();
974 TCollection_AsciiString Message("\
975 myCurrent_AmbientLight=new V3d_AmbientLight(myView->Viewer(), Quantity_NOC_GRAY);\n\
977 myView->SetLightOn(myCurrent_AmbientLight) ;\n\
980 // Update The Result Message Dialog
981 GetDocument()->UpdateResultMessageDlg("V3d_AmbientLight",Message);
985 void CViewer3dView::OnScale()
987 ScaleDlg Dlg(myView, this);
994 void CViewer3dView::OnShadingmodel()
996 TCollection_AsciiString Message("\
997 myView->SetShadingModel(V3d_TypeOfShadingModel myTypeOfShadingModel);\n\
999 myCurrent_V3d_View->Update();\n\
1002 // Update The Result Message Dialog
1003 GetDocument()->UpdateResultMessageDlg("SetShadingModel",Message);
1005 CShadingModelDlg Dlg(myView);
1011 void CViewer3dView::OnAntialiasingonoff()
1013 Graphic3d_RenderingParams& aParams = myView->ChangeRenderingParams();
1014 aParams.NbMsaaSamples = aParams.NbMsaaSamples == 0 ? 8 : 0;
1017 TCollection_AsciiString Message("\
1018 Graphic3d_RenderingParams& aParams = myView->ChangeRenderingParams();\n\
1019 aParams.NbMsaaSamples = aParams.NbMsaaSamples == 0 ? 8 : 0;\n\
1022 // Update The Result Message Dialog
1023 GetDocument()->UpdateResultMessageDlg("SetAntialiasingOn/SetAntialiasingOff",Message);
1026 void CViewer3dView::OnClearLights()
1028 // Setting Off all viewer active lights
1029 TColStd_ListOfTransient lights;
1030 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())
1032 lights.Append(myView->Viewer()->ActiveLight());
1034 TColStd_ListIteratorOfListOfTransient itrLights(lights);
1035 for (; itrLights.More(); itrLights.Next())
1037 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());
1038 myView->Viewer()->SetLightOff(light);
1041 // Setting Off all view active lights
1043 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())
1045 lights.Append(myView->ActiveLight());
1047 itrLights.Initialize(lights);
1048 for (; itrLights.More(); itrLights.Next())
1050 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());
1051 myView->SetLightOff(light);
1054 myView->Viewer()->SetDefaultLights();// Setting the default lights on
1056 NbActiveLights = 2;// There are 2 default active lights
1060 TCollection_AsciiString Message("\
1061 // Setting Off all viewer active lights\n\
1062 TColStd_ListOfTransient lights;\n\
1063 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())\n\
1065 lights.Append(myView->Viewer()->ActiveLight());\n\
1067 TColStd_ListIteratorOfListOfTransient itrLights(lights);\n\
1068 for (; itrLights.More(); itrLights.Next())\n\
1070 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());\n\
1071 myView->Viewer()->SetLightOff(light);\n\
1074 // Setting Off all view active lights\n\
1076 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())\n\
1078 lights.Append(myView->ActiveLight());\n\
1080 itrLights.Initialize(lights);\n\
1081 for (; itrLights.More(); itrLights.Next())\n\
1083 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());\n\
1084 myView->SetLightOff(light);\n\
1087 myView->Viewer()->SetDefaultLights();// Setting the default lights on\n\
1090 // Update The Result Message Dialog
1091 GetDocument()->UpdateResultMessageDlg("SetLightOff",Message);
1094 void CViewer3dView::OnModelclipping()
1096 if (myClippingPlane.IsNull())
1098 gp_Pln aClipPlane (gp_Pnt (0.0, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
1099 gp_Pln aFacePlane (gp_Pnt (0.1, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
1101 // create clipping plane and add to view
1102 myClippingPlane = new Graphic3d_ClipPlane (aClipPlane);
1104 // shape to represent clipping plane
1105 BRepBuilderAPI_MakeFace aMakeFaceCommand (aFacePlane, 200.0, -200.0, 410.0, -410.0);
1106 TopoDS_Face aShape = aMakeFaceCommand.Face();
1107 myShape = new AIS_Shape (aShape);
1108 myShape->SetTransparency (0.5);
1111 CModelClippingDlg aClippingDlg (myView, myShape, myClippingPlane, GetDocument());
1113 aClippingDlg.DoModal();
1116 void CViewer3dView::OnOptionsTrihedronStaticTrihedron()
1118 CTrihedronDlg Dlg(myView, GetDocument());
1122 void CViewer3dView::InitButtons()
1129 myCurrentMode = CurAction3d_Nothing;
1132 void CViewer3dView::Reset()
1135 myVisMode = VIS_SHADE;
1136 if (!myView.IsNull())
1143 void CViewer3dView::GetViewAt (V3d_Coordinate& theX, V3d_Coordinate& theY, V3d_Coordinate& theZ) const
1145 myView->At (theX, theY, theZ);
1148 void CViewer3dView::SetViewAt (const V3d_Coordinate theX, const V3d_Coordinate theY, const V3d_Coordinate theZ)
1150 myView->SetAt (theX, theY, theZ);
1153 void CViewer3dView::GetViewEye(V3d_Coordinate& X, V3d_Coordinate& Y, V3d_Coordinate& Z)
1158 void CViewer3dView::SetViewEye(V3d_Coordinate X, V3d_Coordinate Y, V3d_Coordinate Z)
1160 myView->SetEye(X,Y,Z);
1163 Quantity_Factor CViewer3dView::GetViewScale()
1165 return myView->Scale();
1168 void CViewer3dView::SetViewScale(Quantity_Factor Coef)
1170 myView->SetScale(Coef);
1173 void CViewer3dView::RedrawVisMode()
1178 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);
1179 myView->SetComputedMode (Standard_False);
1182 GetDocument()->GetAISContext()->SetDisplayMode(AIS_Shaded);
1183 myView->SetComputedMode (Standard_False);
1186 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1187 myView->SetComputedMode (Standard_True);
1188 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1189 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);