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"
11 #include "ZClippingDlg.h"
12 #include "ZCueingDlg.h"
14 #include "ShadingModelDlg.h"
15 #include "ModelClippingDlg.h"
16 #include "TrihedronDlg.h"
18 #include <V3d_AmbientLight.hxx>
19 #include <V3d_DirectionalLight.hxx>
20 #include <V3d_PositionalLight.hxx>
21 #include <V3d_PositionLight.hxx>
22 #include <V3d_SpotLight.hxx>
23 #include <TopoDS_Solid.hxx>
24 #include <Precision.hxx>
25 #include <Graphic3d_GraphicDriver.hxx>
36 static char THIS_FILE[] = __FILE__;
39 //gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView);
42 Handle(AIS_Shape) spotConeShape=new AIS_Shape(TopoDS_Solid());
43 Handle(AIS_Shape) directionalEdgeShape=new AIS_Shape(TopoDS_Edge());
45 /////////////////////////////////////////////////////////////////////////////
48 IMPLEMENT_DYNCREATE(CViewer3dView, CView)
50 BEGIN_MESSAGE_MAP(CViewer3dView, CView)
51 //{{AFX_MSG_MAP(CViewer3dView)
52 ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
53 ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
54 ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
55 ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
56 ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
57 ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
58 ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
59 ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
60 ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
61 ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
62 ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
63 ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
64 ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
65 ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
67 ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
68 ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
77 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
78 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
79 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
80 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
81 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
82 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
83 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
84 ON_COMMAND(ID_Modify_ChangeBackground , OnModifyChangeBackground)
85 ON_COMMAND(ID_DIRECTIONAL_LIGHT, OnDirectionalLight)
86 ON_COMMAND(ID_SPOT_LIGHT, OnSpotLight)
87 ON_COMMAND(ID_POSITIONAL_LIGHT, OnPositionalLight)
88 ON_COMMAND(ID_AMBIENT_LIGHT, OnAmbientLight)
89 ON_COMMAND(ID_ZCUEING, OnZcueing)
90 ON_COMMAND(ID_SCALE, OnScale)
91 ON_COMMAND(ID_ZCLIPPING, OnZclipping)
92 ON_COMMAND(ID_SHADINGMODEL, OnShadingmodel)
93 ON_COMMAND(ID_ANTIALIASINGONOFF, OnAntialiasingonoff)
94 ON_COMMAND(ID_CLEAR_LIGHTS, OnClearLights)
95 ON_COMMAND(ID_MODELCLIPPING, OnModelclipping)
96 ON_COMMAND(ID_OPTIONS_TRIHEDRON_STATIC_TRIHEDRON, OnOptionsTrihedronStaticTrihedron)
100 /////////////////////////////////////////////////////////////////////////////
101 // CViewer3dView construction/destruction
103 CViewer3dView::CViewer3dView()
107 myVisMode (VIS_SHADE),
108 myCurrentMode (CurAction3d_Nothing),
116 NbActiveLights (2), // There are 2 default active lights
117 myHlrModeIsOn (Standard_False),
122 // TODO: add construction code here
123 myGraphicDriver = ((CViewer3dApp*)AfxGetApp())->GetGraphicDriver();
126 CViewer3dView::~CViewer3dView()
129 if (m_Pen) delete m_Pen;
132 BOOL CViewer3dView::PreCreateWindow(CREATESTRUCT& cs)
134 // TODO: Modify the Window class or styles here by modifying
135 // the CREATESTRUCT cs
137 return CView::PreCreateWindow(cs);
140 /////////////////////////////////////////////////////////////////////////////
141 // CViewer3dView drawing
142 void CViewer3dView::OnInitialUpdate()
144 CView::OnInitialUpdate();
146 myView = GetDocument()->GetViewer()->CreateView();
148 // store for restore state after rotation (witch is in Degenerated mode)
149 myHlrModeIsOn = Standard_False;
150 myView->SetComputedMode (myHlrModeIsOn);
152 Handle(WNT_Window) aWNTWindow = new WNT_Window (GetSafeHwnd());
153 myView->SetWindow(aWNTWindow);
154 if (!aWNTWindow->IsMapped()) aWNTWindow->Map();
156 // Standard_Integer w=100 , h=100 ; /* Debug Matrox */
157 // aWNTWindow->Size (w,h) ; /* Keeps me unsatisfied (rlb)..... */
158 /* Resize is not supposed to be done on */
160 /* I suspect another problem elsewhere */
161 // ::PostMessage ( GetSafeHwnd () , WM_SIZE , SIZE_RESTORED , w + h*65536 ) ;
163 // store the mode ( nothing , dynamic zooming, dynamic ... )
164 myCurrentMode = CurAction3d_Nothing;
165 myVisMode = VIS_SHADE;
169 void CViewer3dView::OnDraw(CDC* /*pDC*/)
171 CViewer3dDoc* pDoc = GetDocument();
174 GetWindowRect(aRect);
175 if(myWidth != aRect.Width() || myHeight != aRect.Height()) {
176 myWidth = aRect.Width();
177 myHeight = aRect.Height();
178 ::PostMessage ( GetSafeHwnd () , WM_SIZE , SW_SHOW , myWidth + myHeight*65536 );
184 /////////////////////////////////////////////////////////////////////////////
185 // CViewer3dView diagnostics
188 void CViewer3dView::AssertValid() const
190 CView::AssertValid();
193 void CViewer3dView::Dump(CDumpContext& dc) const
198 CViewer3dDoc* CViewer3dView::GetDocument() // non-debug version is inline
200 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewer3dDoc)));
201 return (CViewer3dDoc*)m_pDocument;
205 /////////////////////////////////////////////////////////////////////////////
206 // CViewer3dView message handlers
208 gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
210 V3d_Coordinate XEye,YEye,ZEye,XAt,YAt,ZAt;
211 aView->Eye(XEye,YEye,ZEye);
212 aView->At(XAt,YAt,ZAt);
213 gp_Pnt EyePoint(XEye,YEye,ZEye);
214 gp_Pnt AtPoint(XAt,YAt,ZAt);
216 gp_Vec EyeVector(EyePoint,AtPoint);
217 gp_Dir EyeDir(EyeVector);
219 gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
221 aView->Convert(int(x),int(y),X,Y,Z);
222 gp_Pnt ConvertedPoint(X,Y,Z);
223 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint);
225 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),
226 ConvertedPointOnPlane.Y(),
231 void CViewer3dView::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
233 if (!myView.IsNull())
234 myView->MustBeResized();
237 void CViewer3dView::OnBUTTONBack()
239 myView->SetProj(V3d_Xneg);
241 TCollection_AsciiString Message("\
242 myView->SetProj(V3d_Xneg);\n\
245 // Update The Result Message Dialog
246 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
247 } // See the back View
248 void CViewer3dView::OnBUTTONFront()
250 myView->SetProj(V3d_Xpos);
252 TCollection_AsciiString Message("\
253 myView->SetProj(V3d_Xpos);\n\
256 // Update The Result Message Dialog
257 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
258 } // See the front View
260 void CViewer3dView::OnBUTTONBottom()
262 myView->SetProj(V3d_Zneg);
264 TCollection_AsciiString Message("\
265 myView->SetProj(V3d_Zneg);\n\
268 // Update The Result Message Dialog
269 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
270 } // See the bottom View
271 void CViewer3dView::OnBUTTONTop()
273 myView->SetProj(V3d_Zpos);
275 TCollection_AsciiString Message("\
276 myView->SetProj(V3d_Zpos);\n\
279 // Update The Result Message Dialog
280 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
281 } // See the top View
283 void CViewer3dView::OnBUTTONLeft()
285 myView->SetProj(V3d_Ypos);
287 TCollection_AsciiString Message("\
288 myView->SetProj(V3d_Ypos);\n\
291 // Update The Result Message Dialog
292 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
293 } // See the left View
294 void CViewer3dView::OnBUTTONRight()
296 myView->SetProj(V3d_Yneg);
298 TCollection_AsciiString Message("\
299 myView->SetProj(V3d_Yneg);\n\
302 // Update The Result Message Dialog
303 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
304 } // See the right View
306 void CViewer3dView::OnBUTTONAxo()
308 myView->SetProj(V3d_XposYnegZpos);
310 TCollection_AsciiString Message("\
311 myView->SetProj(V3d_XposYnegZpos);\n\
314 // Update The Result Message Dialog
315 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
316 } // See the axonometric View
318 void CViewer3dView::OnBUTTONHlrOff()
320 myHlrModeIsOn = Standard_False;
321 myView->SetComputedMode (myHlrModeIsOn);
323 TCollection_AsciiString aMsg ("myView->SetComputedMode (Standard_False);\n"
326 // Update The Result Message Dialog
327 GetDocument()->UpdateResultMessageDlg ("SetComputedMode", aMsg);
330 void CViewer3dView::OnBUTTONHlrOn()
332 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
333 myHlrModeIsOn = Standard_True;
334 myView->SetComputedMode (myHlrModeIsOn);
335 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
337 TCollection_AsciiString aMsg ("myView->SetComputedMode (Standard_True);\n"
340 // Update The Result Message Dialog
341 GetDocument()->UpdateResultMessageDlg ("SetComputedMode", aMsg);
344 void CViewer3dView::OnBUTTONPan()
345 { myCurrentMode = CurAction3d_DynamicPanning; }
347 void CViewer3dView::OnBUTTONPanGlo()
349 // save the current zoom value
350 myCurZoom = myView->Scale();
354 myCurrentMode = CurAction3d_GlobalPanning;
357 void CViewer3dView::OnBUTTONReset()
360 void CViewer3dView::OnBUTTONRot()
361 { myCurrentMode = CurAction3d_DynamicRotation; }
364 void CViewer3dView::OnBUTTONZoomAll()
370 void CViewer3dView::OnBUTTONZoomProg()
371 { myCurrentMode = CurAction3d_DynamicZooming; }
373 void CViewer3dView::OnBUTTONZoomWin()
374 { myCurrentMode = CurAction3d_WindowZooming; }
376 void CViewer3dView::OnLButtonDown(UINT nFlags, CPoint point)
378 // save the current mouse coordinate in min
379 myXmin=point.x; myYmin=point.y;
380 myXmax=point.x; myYmax=point.y;
382 if ( nFlags & MK_CONTROL )
384 // Button MB1 down Control :start zomming
385 // SetCursor(AfxGetApp()->LoadStandardCursor());
389 switch (myCurrentMode)
391 case CurAction3d_Nothing : // start a drag
392 if (nFlags & MK_SHIFT)
393 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
395 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
398 case CurAction3d_DynamicZooming : // noting
400 case CurAction3d_WindowZooming :
402 case CurAction3d_DynamicPanning :// noting
404 case CurAction3d_GlobalPanning :// noting
406 case CurAction3d_DynamicRotation :
409 myView->SetComputedMode (Standard_False);
411 myView->StartRotation (point.x, point.y);
413 case CurAction3d_BeginPositionalLight :
415 p1 = ConvertClickToPoint(point.x,point.y,myView);
416 myCurrent_PositionalLight->SetPosition(p1.X(),p1.Y(),p1.Z()) ;
417 GetDocument()->GetAISContext()->CloseLocalContext();
418 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
419 myCurrentMode = CurAction3d_Nothing;
421 TCollection_AsciiString Message("\
422 myCurrent_PositionalLight->SetPosition(Xp, Yp, Zp) ;\n\
424 // Update The Result Message Dialog
425 GetDocument()->UpdateResultMessageDlg("SetPosition",Message);
428 case CurAction3d_BeginSpotLight :
430 p1 = ConvertClickToPoint(point.x,point.y,myView);
431 myCurrent_SpotLight = new V3d_SpotLight(myView->Viewer(),0.,0.,1., p1.X(),p1.Y(),p1.Z(),Quantity_NOC_RED);
432 myView->SetLightOn(myCurrent_SpotLight);
434 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
435 Standard_Real coneHeigth=p1.Distance(p2);
436 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
437 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
438 spotConeShape->Set(MakeCone.Solid());
439 GetDocument()->GetAISContext()->Display(spotConeShape,0,-1);
440 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
441 myCurrentMode = CurAction3d_TargetSpotLight;
443 TCollection_AsciiString Message("\
444 myCurrent_SpotLight->SetDirection(Xv, Yv, Zv);\n\
446 // Update The Result Message Dialog
447 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
450 case CurAction3d_TargetSpotLight :
452 p2 = ConvertClickToPoint(point.x,point.y,myView);
453 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a third point (to define the angle)");
454 myCurrentMode = CurAction3d_EndSpotLight;
456 TCollection_AsciiString Message("\
457 myCurrent_SpotLight->SetAngle(Angle) ;\n\
459 // Update The Result Message Dialog
460 GetDocument()->UpdateResultMessageDlg("SetAngle",Message);
463 case CurAction3d_EndSpotLight :
464 GetDocument()->GetAISContext()->Erase(spotConeShape);
465 GetDocument()->GetAISContext()->CloseLocalContext();
466 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
467 myCurrentMode = CurAction3d_Nothing;
469 case CurAction3d_BeginDirectionalLight:
471 p1 = ConvertClickToPoint(point.x,point.y,myView);
472 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
473 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
474 directionalEdgeShape->Set(MakeEdge.Edge());
475 GetDocument()->GetAISContext()->Display(directionalEdgeShape,0,-1);
476 // Create a directional light
477 myCurrent_DirectionalLight = new V3d_DirectionalLight(myView->Viewer(), p1.X(),p1.Y(),p1.Z(),0.,0.,1.);
478 myView->SetLightOn(myCurrent_DirectionalLight);
480 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
481 myCurrentMode = CurAction3d_EndDirectionalLight;
484 TCollection_AsciiString Message("\
485 myCurrent_DirectionalLight->SetDirection(Xv, Yv, Zv);\n\
487 // Update The Result Message Dialog
488 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
491 case CurAction3d_EndDirectionalLight:
492 GetDocument()->GetAISContext()->Erase(directionalEdgeShape);
493 GetDocument()->GetAISContext()->CloseLocalContext();
494 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
495 myCurrentMode = CurAction3d_Nothing;
501 void CViewer3dView::OnKeyDown(UINT nChar, UINT /*nRepCnt*/, UINT /*nFlags*/)
504 if( nChar == X_Key || nChar == Y_Key || nChar == Z_Key )
506 else if( (nChar == VK_ADD || nChar == VK_SUBTRACT) && myAxisKey )
508 myScaleDirection = nChar;
509 if( myAxisKey && myScaleDirection )
514 if( myScaleDirection == VK_ADD ) scaleX ++;
515 if( myScaleDirection == VK_SUBTRACT ) scaleX --;
518 if( myScaleDirection == VK_ADD ) scaleY ++;
519 if( myScaleDirection == VK_SUBTRACT ) scaleY --;
522 if( myScaleDirection == VK_ADD ) scaleZ ++;
523 if( myScaleDirection == VK_SUBTRACT ) scaleZ --;
528 if( scaleX < 1 ) scaleX = 1;
529 if( scaleY < 1 ) scaleY = 1;
530 if( scaleZ < 1 ) scaleZ = 1;
532 myView->SetAxialScale( scaleX, scaleY, scaleZ );
537 void CViewer3dView::OnLButtonUp(UINT nFlags, CPoint point)
539 if ( nFlags & MK_CONTROL )
545 switch (myCurrentMode)
547 case CurAction3d_Nothing :
548 if (point.x == myXmin && point.y == myYmin)
549 { // no offset between down and up --> selectEvent
552 if (nFlags & MK_SHIFT )
553 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
555 GetDocument()->InputEvent (point.x,point.y,myView);
558 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
561 if (nFlags & MK_SHIFT)
562 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
564 GetDocument()->DragEvent(point.x,point.y,1,myView);
567 case CurAction3d_DynamicZooming :
568 // SetCursor(AfxGetApp()->LoadStandardCursor());
569 myCurrentMode = CurAction3d_Nothing;
571 case CurAction3d_WindowZooming :
572 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
575 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
576 // Test if the zoom window is greater than a minimale window.
578 // Do the zoom window between Pmin and Pmax
579 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
581 myCurrentMode = CurAction3d_Nothing;
583 case CurAction3d_DynamicPanning :
584 myCurrentMode = CurAction3d_Nothing;
586 case CurAction3d_GlobalPanning :
587 myView->Place(point.x,point.y,myCurZoom);
588 myCurrentMode = CurAction3d_Nothing;
590 case CurAction3d_DynamicRotation :
591 myCurrentMode = CurAction3d_Nothing;
593 } //switch (myCurrentMode)
594 } // else // if ( Ctrl )
597 void CViewer3dView::OnMButtonDown(UINT nFlags, CPoint /*point*/)
599 if ( nFlags & MK_CONTROL )
601 // Button MB2 down Control : panning init
602 // SetCursor(AfxGetApp()->LoadStandardCursor());
606 void CViewer3dView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
608 if ( nFlags & MK_CONTROL )
610 // Button MB2 down Control : panning init
611 // SetCursor(AfxGetApp()->LoadStandardCursor());
615 void CViewer3dView::OnRButtonDown(UINT nFlags, CPoint point)
617 if ( nFlags & MK_CONTROL )
619 // SetCursor(AfxGetApp()->LoadStandardCursor());
622 myView->SetComputedMode (Standard_False);
624 myView->StartRotation (point.x, point.y);
628 GetDocument()->Popup (point.x, point.y, myView);
632 void CViewer3dView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/)
634 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
635 myView->SetComputedMode (myHlrModeIsOn);
636 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
639 void CViewer3dView::OnMouseMove(UINT nFlags, CPoint point)
641 // ============================ LEFT BUTTON =======================
642 if ( nFlags & MK_LBUTTON)
644 if ( nFlags & MK_CONTROL )
646 // move with MB1 and Control : on the dynamic zooming
647 // Do the zoom in function of mouse's coordinates
648 myView->Zoom(myXmax,myYmax,point.x,point.y);
649 // save the current mouse coordinate in min
655 switch (myCurrentMode)
657 case CurAction3d_Nothing :
658 myXmax = point.x; myYmax = point.y;
659 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
660 if (nFlags & MK_SHIFT)
661 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
663 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
664 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
666 case CurAction3d_DynamicZooming :
667 myView->Zoom(myXmax,myYmax,point.x,point.y);
668 // save the current mouse coordinate in min \n";
669 myXmax=point.x; myYmax=point.y;
671 case CurAction3d_WindowZooming :
672 myXmax = point.x; myYmax = point.y;
673 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
674 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
677 case CurAction3d_DynamicPanning :
678 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
679 myXmax = point.x; myYmax = point.y;
681 case CurAction3d_GlobalPanning : // nothing
683 case CurAction3d_DynamicRotation :
684 myView->Rotation(point.x,point.y);
687 }// switch (myCurrentMode)
688 }// if ( nFlags & MK_CONTROL ) else
689 } else // if ( nFlags & MK_LBUTTON)
690 // ============================ MIDDLE BUTTON =======================
691 if ( nFlags & MK_MBUTTON)
693 if ( nFlags & MK_CONTROL )
695 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
696 myXmax = point.x; myYmax = point.y;
699 } else // if ( nFlags & MK_MBUTTON)
700 // ============================ RIGHT BUTTON =======================
701 if ( nFlags & MK_RBUTTON)
703 if ( nFlags & MK_CONTROL )
705 myView->Rotation(point.x,point.y);
707 }else //if ( nFlags & MK_RBUTTON)
708 // ============================ NO BUTTON =======================
710 myXmax = point.x; myYmax = point.y;
711 if (myCurrentMode == CurAction3d_EndDirectionalLight)
713 p2 = ConvertClickToPoint(point.x,point.y,myView);
714 //Update the light dynamically
715 if( p1.Distance(p2)>Precision::Confusion())
717 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
718 directionalEdgeShape->Set(MakeEdge.Edge());
719 GetDocument()->GetAISContext()->Redisplay(directionalEdgeShape,0,Standard_True);
720 myCurrent_DirectionalLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
721 myView->UpdateLights();
724 else if (myCurrentMode == CurAction3d_BeginPositionalLight)
726 p2 = ConvertClickToPoint(point.x,point.y,myView);
727 //Update the light dynamically
728 myCurrent_PositionalLight->SetPosition(p2.X(),p2.Y(),p2.Z());
729 myView->UpdateLights();
731 else if (myCurrentMode == CurAction3d_TargetSpotLight)
733 p2 = ConvertClickToPoint(point.x,point.y,myView);
734 //Update the light dynamically
735 Standard_Real coneHeigth=p1.Distance(p2);
736 if( coneHeigth>Precision::Confusion())
738 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
739 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
740 spotConeShape->Set(MakeCone.Solid());
741 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,Standard_True);
742 myCurrent_SpotLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
743 myView->UpdateLights();
746 else if (myCurrentMode == CurAction3d_EndSpotLight)
748 p3 = ConvertClickToPoint(point.x,point.y,myView);
749 //Update the light dynamically
750 Standard_Real coneHeigth=p1.Distance(p2);
751 if( (p2.Distance(p3))>Precision::Confusion())
753 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
754 0, p2.Distance(p3), coneHeigth);
755 spotConeShape->Set(MakeCone.Solid());
756 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,Standard_True);
757 myCurrent_SpotLight->SetAngle(atan(p2.Distance(p3)/p1.Distance(p2))) ;
758 myView->UpdateLights();
761 if (nFlags & MK_SHIFT)
762 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
764 GetDocument()->MoveEvent(point.x,point.y,myView);
768 void CViewer3dView::OnUpdateBUTTONHlrOff (CCmdUI* pCmdUI)
770 pCmdUI->SetCheck (!myHlrModeIsOn);
771 pCmdUI->Enable (myHlrModeIsOn);
774 void CViewer3dView::OnUpdateBUTTONHlrOn (CCmdUI* pCmdUI)
776 pCmdUI->SetCheck (myHlrModeIsOn);
777 pCmdUI->Enable (!myHlrModeIsOn);
780 void CViewer3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
782 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
783 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
787 void CViewer3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
789 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
790 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
793 void CViewer3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
795 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
796 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
799 void CViewer3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
801 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
802 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
805 void CViewer3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
807 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
808 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
811 void CViewer3dView::DrawRectangle(const Standard_Integer MinX ,
812 const Standard_Integer MinY ,
813 const Standard_Integer MaxX ,
814 const Standard_Integer MaxY ,
815 const Standard_Boolean Draw ,
816 const LineStyle aLineStyle)
818 static int m_DrawMode;
819 if (!m_Pen && aLineStyle ==Solid )
820 {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
821 else if (!m_Pen && aLineStyle ==Dot )
822 {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;}
823 else if (!m_Pen && aLineStyle == ShortDash)
824 {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;}
825 else if (!m_Pen && aLineStyle == LongDash)
826 {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;}
827 else if (aLineStyle == Default)
828 { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
830 CPen* aOldPen = NULL;
831 CClientDC clientDC(this);
832 if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
833 clientDC.SetROP2(m_DrawMode);
835 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
836 static Standard_Boolean m_IsVisible;
838 if ( m_IsVisible && !Draw) // move or up : erase at the old position
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);
848 StoredMinX = min ( MinX, MaxX );
849 StoredMinY = min ( MinY, MaxY );
850 StoredMaxX = max ( MinX, MaxX );
851 StoredMaxY = max ( MinY, MaxY);
853 if (Draw) // move : draw
855 clientDC.MoveTo(StoredMinX,StoredMinY);
856 clientDC.LineTo(StoredMinX,StoredMaxY);
857 clientDC.LineTo(StoredMaxX,StoredMaxY);
858 clientDC.LineTo(StoredMaxX,StoredMinY);
859 clientDC.LineTo(StoredMinX,StoredMinY);
864 clientDC.SelectObject(aOldPen);
867 void CViewer3dView::OnModifyChangeBackground()
872 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
874 m_clr = RGB(R1*255,G1*255,B1*255);
876 CColorDialog dlgColor(m_clr);
877 if (dlgColor.DoModal() == IDOK)
879 m_clr = dlgColor.GetColor();
880 R1 = GetRValue(m_clr)/255.;
881 G1 = GetGValue(m_clr)/255.;
882 B1 = GetBValue(m_clr)/255.;
883 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
888 void CViewer3dView::OnDirectionalLight()
890 // Directional light source creation
892 // Checking if the Active lights limit number is not reached
893 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
896 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
897 myGraphicDriver->InquireLightLimit());
898 MessageBox(tmpStr,"Light creation", MB_OK);
903 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a first point");
904 GetDocument()->GetAISContext()->OpenLocalContext();
905 myCurrentMode = CurAction3d_BeginDirectionalLight;
907 TCollection_AsciiString Message("\
908 myCurrent_DirectionalLight = new V3d_DirectionalLight(myView->Viewer(), Xt, Yt, Zt, Xp, Yp, Zp);\n\
910 myView->SetLightOn(myCurrent_DirectionalLight);\n\
913 // Update The Result Message Dialog
914 GetDocument()->UpdateResultMessageDlg("V3d_DirectionalLight",Message);
917 void CViewer3dView::OnSpotLight()
919 // Spot light source creation
921 // Checking if the Active lights limit number is not reached
922 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
925 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
926 myGraphicDriver->InquireLightLimit());
927 MessageBox(tmpStr,"Light creation", MB_OK);
931 GetDocument()->GetAISContext()->OpenLocalContext();
932 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
933 myCurrentMode = CurAction3d_BeginSpotLight;
935 TCollection_AsciiString Message("\
936 myCurrent_SpotLight = new V3d_SpotLight(myView->Viewer(), Xt, Yt, Zt, Xp, Yp, Zp,Quantity_NOC_RED);\n\
938 myView->SetLightOn(myCurrent_SpotLight);\n\
941 // Update The Result Message Dialog
942 GetDocument()->UpdateResultMessageDlg("V3d_SpotLight",Message);
945 void CViewer3dView::OnPositionalLight()
947 // Positional light source creation
949 // Checking if the Active lights limit number is not reached
950 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
953 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
954 myGraphicDriver->InquireLightLimit());
955 MessageBox(tmpStr,"Light creation", MB_OK);
959 GetDocument()->GetAISContext()->OpenLocalContext();
960 myCurrent_PositionalLight=new V3d_PositionalLight(myView->Viewer(),0,0,0,Quantity_NOC_GREEN,1,0);
961 myView->SetLightOn(myCurrent_PositionalLight);
963 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
964 myCurrentMode = CurAction3d_BeginPositionalLight;
966 TCollection_AsciiString Message("\
967 myCurrent_PositionalLight=new V3d_PositionalLight(myView->Viewer(),Xp, Yp, Zp,Quantity_NOC_GREEN,1,0);\n\
969 myView->SetLightOn(myCurrent_PositionalLight) ;\n\
972 // Update The Result Message Dialog
973 GetDocument()->UpdateResultMessageDlg("V3d_PositionalLight",Message);
976 void CViewer3dView::OnAmbientLight()
978 // Ambiant light source creation
980 // Checking if the Active lights limit number is not reached
981 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
984 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
985 myGraphicDriver->InquireLightLimit());
986 MessageBox(tmpStr,"Light creation", MB_OK);
990 GetDocument()->GetAISContext()->OpenLocalContext();
991 myCurrent_AmbientLight=new V3d_AmbientLight(myView->Viewer(), Quantity_NOC_GRAY);
992 myView->SetLightOn(myCurrent_AmbientLight) ;
994 GetDocument()->GetAISContext()->CloseLocalContext();
996 myView->UpdateLights();
998 TCollection_AsciiString Message("\
999 myCurrent_AmbientLight=new V3d_AmbientLight(myView->Viewer(), Quantity_NOC_GRAY);\n\
1001 myView->SetLightOn(myCurrent_AmbientLight) ;\n\
1004 // Update The Result Message Dialog
1005 GetDocument()->UpdateResultMessageDlg("V3d_AmbientLight",Message);
1009 void CViewer3dView::OnScale()
1011 ScaleDlg Dlg(myView, this);
1016 void CViewer3dView::OnZcueing()
1018 ZCueingDlg Dlg(myView, GetDocument());
1023 void CViewer3dView::OnZclipping()
1025 ZClippingDlg Dlg(myView, GetDocument());
1032 void CViewer3dView::OnShadingmodel()
1034 TCollection_AsciiString Message("\
1035 myView->SetShadingModel(V3d_TypeOfShadingModel myTypeOfShadingModel);\n\
1037 myCurrent_V3d_View->Update();\n\
1040 // Update The Result Message Dialog
1041 GetDocument()->UpdateResultMessageDlg("SetShadingModel",Message);
1043 CShadingModelDlg Dlg(myView);
1049 void CViewer3dView::OnAntialiasingonoff()
1051 if(!myView->Antialiasing())
1052 myView->SetAntialiasingOn();
1054 myView->SetAntialiasingOff();
1058 TCollection_AsciiString Message("\
1059 if(!myView->Antialiasing())\n\
1060 myView->SetAntialiasingOn();\n\
1062 myView->SetAntialiasingOff();\n\
1065 // Update The Result Message Dialog
1066 GetDocument()->UpdateResultMessageDlg("SetAntialiasingOn/SetAntialiasingOff",Message);
1069 void CViewer3dView::OnClearLights()
1071 // Setting Off all viewer active lights
1072 TColStd_ListOfTransient lights;
1073 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())
1075 lights.Append(myView->Viewer()->ActiveLight());
1077 TColStd_ListIteratorOfListOfTransient itrLights(lights);
1078 for (; itrLights.More(); itrLights.Next())
1080 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());
1081 myView->Viewer()->SetLightOff(light);
1084 // Setting Off all view active lights
1086 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())
1088 lights.Append(myView->ActiveLight());
1090 itrLights.Initialize(lights);
1091 for (; itrLights.More(); itrLights.Next())
1093 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());
1094 myView->SetLightOff(light);
1097 myView->Viewer()->SetDefaultLights();// Setting the default lights on
1099 NbActiveLights = 2;// There are 2 default active lights
1103 TCollection_AsciiString Message("\
1104 // Setting Off all viewer active lights\n\
1105 TColStd_ListOfTransient lights;\n\
1106 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())\n\
1108 lights.Append(myView->Viewer()->ActiveLight());\n\
1110 TColStd_ListIteratorOfListOfTransient itrLights(lights);\n\
1111 for (; itrLights.More(); itrLights.Next())\n\
1113 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());\n\
1114 myView->Viewer()->SetLightOff(light);\n\
1117 // Setting Off all view active lights\n\
1119 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())\n\
1121 lights.Append(myView->ActiveLight());\n\
1123 itrLights.Initialize(lights);\n\
1124 for (; itrLights.More(); itrLights.Next())\n\
1126 Handle(V3d_Light) light = Handle(V3d_Light)::DownCast(itrLights.Value());\n\
1127 myView->SetLightOff(light);\n\
1130 myView->Viewer()->SetDefaultLights();// Setting the default lights on\n\
1133 // Update The Result Message Dialog
1134 GetDocument()->UpdateResultMessageDlg("SetLightOff",Message);
1137 void CViewer3dView::OnModelclipping()
1139 if (myClippingPlane.IsNull())
1141 gp_Pln aClipPlane (gp_Pnt (0.0, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
1142 gp_Pln aFacePlane (gp_Pnt (0.1, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
1144 // create clipping plane and add to view
1145 myClippingPlane = new Graphic3d_ClipPlane (aClipPlane);
1147 // shape to represent clipping plane
1148 BRepBuilderAPI_MakeFace aMakeFaceCommand (aFacePlane, 200.0, -200.0, 410.0, -410.0);
1149 TopoDS_Face aShape = aMakeFaceCommand.Face();
1150 myShape = new AIS_Shape (aShape);
1151 myShape->SetTransparency (0.5);
1154 CModelClippingDlg aClippingDlg (myView, myShape, myClippingPlane, GetDocument());
1156 aClippingDlg.DoModal();
1159 void CViewer3dView::OnOptionsTrihedronStaticTrihedron()
1161 CTrihedronDlg Dlg(myView, GetDocument());
1165 void CViewer3dView::InitButtons()
1172 myCurrentMode = CurAction3d_Nothing;
1175 void CViewer3dView::Reset()
1178 myVisMode = VIS_SHADE;
1179 if (!myView.IsNull())
1186 void CViewer3dView::GetViewCenter(V3d_Coordinate& Xc, V3d_Coordinate& Yc)
1188 myView->Center(Xc,Yc);
1191 void CViewer3dView::SetViewCenter(V3d_Coordinate Xc, V3d_Coordinate Yc)
1193 myView->SetCenter(Xc,Yc);
1196 void CViewer3dView::GetViewEye(V3d_Coordinate& X, V3d_Coordinate& Y, V3d_Coordinate& Z)
1201 void CViewer3dView::SetViewEye(V3d_Coordinate X, V3d_Coordinate Y, V3d_Coordinate Z)
1203 myView->SetEye(X,Y,Z);
1206 Quantity_Factor CViewer3dView::GetViewScale()
1208 return myView->Scale();
1211 void CViewer3dView::SetViewScale(Quantity_Factor Coef)
1213 myView->SetScale(Coef);
1216 void CViewer3dView::RedrawVisMode()
1221 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);
1222 myView->SetComputedMode (Standard_False);
1225 GetDocument()->GetAISContext()->SetDisplayMode(AIS_Shaded);
1226 myView->SetComputedMode (Standard_False);
1229 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1230 myView->SetComputedMode (Standard_True);
1231 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1232 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);