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()
105 // TODO: add construction code here
119 myScaleDirection = 0;
122 myVisMode = VIS_SHADE;
124 // will be set in OnInitial update, but, for more security :
125 myCurrentMode = CurAction3d_Nothing;
126 myDegenerateModeIsOn=Standard_True;
128 NbActiveLights=2; // There are 2 default active lights
129 myGraphicDriver = Handle(Graphic3d_GraphicDriver)::DownCast(
130 ((CViewer3dApp*)AfxGetApp())->GetGraphicDevice()->GraphicDriver() );
133 CViewer3dView::~CViewer3dView()
136 if (m_Pen) delete m_Pen;
139 BOOL CViewer3dView::PreCreateWindow(CREATESTRUCT& cs)
141 // TODO: Modify the Window class or styles here by modifying
142 // the CREATESTRUCT cs
144 return CView::PreCreateWindow(cs);
147 /////////////////////////////////////////////////////////////////////////////
148 // CViewer3dView drawing
149 void CViewer3dView::OnInitialUpdate()
151 CView::OnInitialUpdate();
154 myView = GetDocument()->GetViewer()->CreateView();
156 // set the default mode in wireframe ( not hidden line ! )
157 myView->SetDegenerateModeOn();
158 // store for restore state after rotation (witch is in Degenerated mode)
159 myDegenerateModeIsOn = Standard_True;
162 Handle(Graphic3d_WNTGraphicDevice) theGraphicDevice =
163 ((CViewer3dApp*)AfxGetApp())->GetGraphicDevice();
165 Handle(WNT_Window) aWNTWindow = new WNT_Window(theGraphicDevice,GetSafeHwnd ());
166 myView->SetWindow(aWNTWindow);
167 if (!aWNTWindow->IsMapped()) aWNTWindow->Map();
169 // Standard_Integer w=100 , h=100 ; /* Debug Matrox */
170 // aWNTWindow->Size (w,h) ; /* Keeps me unsatisfied (rlb)..... */
171 /* Resize is not supposed to be done on */
173 /* I suspect another problem elsewhere */
174 // ::PostMessage ( GetSafeHwnd () , WM_SIZE , SIZE_RESTORED , w + h*65536 ) ;
176 // store the mode ( nothing , dynamic zooming, dynamic ... )
177 myCurrentMode = CurAction3d_Nothing;
178 myVisMode = VIS_SHADE;
182 void CViewer3dView::OnDraw(CDC* pDC)
184 CViewer3dDoc* pDoc = GetDocument();
187 GetWindowRect(aRect);
188 if(myWidth != aRect.Width() || myHeight != aRect.Height()) {
189 myWidth = aRect.Width();
190 myHeight = aRect.Height();
191 ::PostMessage ( GetSafeHwnd () , WM_SIZE , SW_SHOW , myWidth + myHeight*65536 );
197 /////////////////////////////////////////////////////////////////////////////
198 // CViewer3dView diagnostics
201 void CViewer3dView::AssertValid() const
203 CView::AssertValid();
206 void CViewer3dView::Dump(CDumpContext& dc) const
211 CViewer3dDoc* CViewer3dView::GetDocument() // non-debug version is inline
213 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewer3dDoc)));
214 return (CViewer3dDoc*)m_pDocument;
218 /////////////////////////////////////////////////////////////////////////////
219 // CViewer3dView message handlers
221 gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
223 V3d_Coordinate XEye,YEye,ZEye,XAt,YAt,ZAt;
224 aView->Eye(XEye,YEye,ZEye);
225 aView->At(XAt,YAt,ZAt);
226 gp_Pnt EyePoint(XEye,YEye,ZEye);
227 gp_Pnt AtPoint(XAt,YAt,ZAt);
229 gp_Vec EyeVector(EyePoint,AtPoint);
230 gp_Dir EyeDir(EyeVector);
232 gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
234 aView->Convert(int(x),int(y),X,Y,Z);
235 gp_Pnt ConvertedPoint(X,Y,Z);
236 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint);
238 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),
239 ConvertedPointOnPlane.Y(),
244 void CViewer3dView::OnSize(UINT nType, int cx, int cy)
246 if (!myView.IsNull())
247 myView->MustBeResized();
250 void CViewer3dView::OnBUTTONBack()
252 myView->SetProj(V3d_Xneg);
254 TCollection_AsciiString Message("\
255 myView->SetProj(V3d_Xneg);\n\
258 // Update The Result Message Dialog
259 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
260 } // See the back View
261 void CViewer3dView::OnBUTTONFront()
263 myView->SetProj(V3d_Xpos);
265 TCollection_AsciiString Message("\
266 myView->SetProj(V3d_Xpos);\n\
269 // Update The Result Message Dialog
270 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
271 } // See the front View
273 void CViewer3dView::OnBUTTONBottom()
275 myView->SetProj(V3d_Zneg);
277 TCollection_AsciiString Message("\
278 myView->SetProj(V3d_Zneg);\n\
281 // Update The Result Message Dialog
282 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
283 } // See the bottom View
284 void CViewer3dView::OnBUTTONTop()
286 myView->SetProj(V3d_Zpos);
288 TCollection_AsciiString Message("\
289 myView->SetProj(V3d_Zpos);\n\
292 // Update The Result Message Dialog
293 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
294 } // See the top View
296 void CViewer3dView::OnBUTTONLeft()
298 myView->SetProj(V3d_Ypos);
300 TCollection_AsciiString Message("\
301 myView->SetProj(V3d_Ypos);\n\
304 // Update The Result Message Dialog
305 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
306 } // See the left View
307 void CViewer3dView::OnBUTTONRight()
309 myView->SetProj(V3d_Yneg);
311 TCollection_AsciiString Message("\
312 myView->SetProj(V3d_Yneg);\n\
315 // Update The Result Message Dialog
316 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
317 } // See the right View
319 void CViewer3dView::OnBUTTONAxo()
321 myView->SetProj(V3d_XposYnegZpos);
323 TCollection_AsciiString Message("\
324 myView->SetProj(V3d_XposYnegZpos);\n\
327 // Update The Result Message Dialog
328 GetDocument()->UpdateResultMessageDlg("SetProj",Message);
329 } // See the axonometric View
331 void CViewer3dView::OnBUTTONHlrOff()
333 myView->SetDegenerateModeOn();
334 myDegenerateModeIsOn = Standard_True;
336 TCollection_AsciiString Message("\
337 myView->SetDegenerateModeOn();\n\
340 // Update The Result Message Dialog
341 GetDocument()->UpdateResultMessageDlg("SetDegenerateModeOn",Message);
344 void CViewer3dView::OnBUTTONHlrOn()
346 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
347 myView->SetDegenerateModeOff();
348 myDegenerateModeIsOn = Standard_False;
349 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
351 TCollection_AsciiString Message("\
352 myView->SetDegenerateModeOff();\n\
355 // Update The Result Message Dialog
356 GetDocument()->UpdateResultMessageDlg("SetDegenerateModeOff",Message);
359 void CViewer3dView::OnBUTTONPan()
360 { myCurrentMode = CurAction3d_DynamicPanning; }
362 void CViewer3dView::OnBUTTONPanGlo()
364 // save the current zoom value
365 myCurZoom = myView->Scale();
369 myCurrentMode = CurAction3d_GlobalPanning;
372 void CViewer3dView::OnBUTTONReset()
375 void CViewer3dView::OnBUTTONRot()
376 { myCurrentMode = CurAction3d_DynamicRotation; }
379 void CViewer3dView::OnBUTTONZoomAll()
385 void CViewer3dView::OnBUTTONZoomProg()
386 { myCurrentMode = CurAction3d_DynamicZooming; }
388 void CViewer3dView::OnBUTTONZoomWin()
389 { myCurrentMode = CurAction3d_WindowZooming; }
391 void CViewer3dView::OnLButtonDown(UINT nFlags, CPoint point)
393 // save the current mouse coordinate in min
394 myXmin=point.x; myYmin=point.y;
395 myXmax=point.x; myYmax=point.y;
397 if ( nFlags & MK_CONTROL )
399 // Button MB1 down Control :start zomming
400 // SetCursor(AfxGetApp()->LoadStandardCursor());
404 switch (myCurrentMode)
406 case CurAction3d_Nothing : // start a drag
407 if (nFlags & MK_SHIFT)
408 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
410 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
413 case CurAction3d_DynamicZooming : // noting
415 case CurAction3d_WindowZooming :
417 case CurAction3d_DynamicPanning :// noting
419 case CurAction3d_GlobalPanning :// noting
421 case CurAction3d_DynamicRotation :
422 if (!myDegenerateModeIsOn)
423 myView->SetDegenerateModeOn();
424 myView->StartRotation(point.x,point.y);
426 case CurAction3d_BeginPositionalLight :
428 p1 = ConvertClickToPoint(point.x,point.y,myView);
429 myCurrent_PositionalLight->SetPosition(p1.X(),p1.Y(),p1.Z()) ;
430 GetDocument()->GetAISContext()->CloseLocalContext();
431 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
432 myCurrentMode = CurAction3d_Nothing;
434 TCollection_AsciiString Message("\
435 myCurrent_PositionalLight->SetPosition(Xp, Yp, Zp) ;\n\
437 // Update The Result Message Dialog
438 GetDocument()->UpdateResultMessageDlg("SetPosition",Message);
441 case CurAction3d_BeginSpotLight :
443 p1 = ConvertClickToPoint(point.x,point.y,myView);
444 myCurrent_SpotLight = new V3d_SpotLight(myView->Viewer(),0.,0.,1., p1.X(),p1.Y(),p1.Z(),Quantity_NOC_RED);
445 myView->SetLightOn(myCurrent_SpotLight);
447 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
448 Standard_Real coneHeigth=p1.Distance(p2);
449 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
450 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
451 spotConeShape->Set(MakeCone.Solid());
452 GetDocument()->GetAISContext()->Display(spotConeShape,0,-1);
453 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
454 myCurrentMode = CurAction3d_TargetSpotLight;
456 TCollection_AsciiString Message("\
457 myCurrent_SpotLight->SetDirection(Xv, Yv, Zv);\n\
459 // Update The Result Message Dialog
460 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
463 case CurAction3d_TargetSpotLight :
465 p2 = ConvertClickToPoint(point.x,point.y,myView);
466 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a third point (to define the angle)");
467 myCurrentMode = CurAction3d_EndSpotLight;
469 TCollection_AsciiString Message("\
470 myCurrent_SpotLight->SetAngle(Angle) ;\n\
472 // Update The Result Message Dialog
473 GetDocument()->UpdateResultMessageDlg("SetAngle",Message);
476 case CurAction3d_EndSpotLight :
477 GetDocument()->GetAISContext()->Erase(spotConeShape);
478 GetDocument()->GetAISContext()->CloseLocalContext();
479 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
480 myCurrentMode = CurAction3d_Nothing;
482 case CurAction3d_BeginDirectionalLight:
484 p1 = ConvertClickToPoint(point.x,point.y,myView);
485 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
486 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
487 directionalEdgeShape->Set(MakeEdge.Edge());
488 GetDocument()->GetAISContext()->Display(directionalEdgeShape,0,-1);
489 // Create a directional light
490 myCurrent_DirectionalLight = new V3d_DirectionalLight(myView->Viewer(), p1.X(),p1.Y(),p1.Z(),0.,0.,1.);
491 myView->SetLightOn(myCurrent_DirectionalLight);
493 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the target point");
494 myCurrentMode = CurAction3d_EndDirectionalLight;
497 TCollection_AsciiString Message("\
498 myCurrent_DirectionalLight->SetDirection(Xv, Yv, Zv);\n\
500 // Update The Result Message Dialog
501 GetDocument()->UpdateResultMessageDlg("SetDirection",Message);
504 case CurAction3d_EndDirectionalLight:
505 GetDocument()->GetAISContext()->Erase(directionalEdgeShape);
506 GetDocument()->GetAISContext()->CloseLocalContext();
507 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Ready");
508 myCurrentMode = CurAction3d_Nothing;
514 void CViewer3dView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
517 if( nChar == X_Key || nChar == Y_Key || nChar == Z_Key )
519 else if( (nChar == VK_ADD || nChar == VK_SUBTRACT) && myAxisKey )
521 myScaleDirection = nChar;
522 if( myAxisKey && myScaleDirection )
527 if( myScaleDirection == VK_ADD ) scaleX ++;
528 if( myScaleDirection == VK_SUBTRACT ) scaleX --;
531 if( myScaleDirection == VK_ADD ) scaleY ++;
532 if( myScaleDirection == VK_SUBTRACT ) scaleY --;
535 if( myScaleDirection == VK_ADD ) scaleZ ++;
536 if( myScaleDirection == VK_SUBTRACT ) scaleZ --;
541 if( scaleX < 1 ) scaleX = 1;
542 if( scaleY < 1 ) scaleY = 1;
543 if( scaleZ < 1 ) scaleZ = 1;
545 myView->SetAxialScale( scaleX, scaleY, scaleZ );
550 void CViewer3dView::OnLButtonUp(UINT nFlags, CPoint point)
552 if ( nFlags & MK_CONTROL )
558 switch (myCurrentMode)
560 case CurAction3d_Nothing :
561 if (point.x == myXmin && point.y == myYmin)
562 { // no offset between down and up --> selectEvent
565 if (nFlags & MK_SHIFT )
566 GetDocument()->ShiftInputEvent(point.x,point.y,myView);
568 GetDocument()->InputEvent (point.x,point.y,myView);
571 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
574 if (nFlags & MK_SHIFT)
575 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
577 GetDocument()->DragEvent(point.x,point.y,1,myView);
580 case CurAction3d_DynamicZooming :
581 // SetCursor(AfxGetApp()->LoadStandardCursor());
582 myCurrentMode = CurAction3d_Nothing;
584 case CurAction3d_WindowZooming :
585 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
588 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
589 // Test if the zoom window is greater than a minimale window.
591 // Do the zoom window between Pmin and Pmax
592 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
594 myCurrentMode = CurAction3d_Nothing;
596 case CurAction3d_DynamicPanning :
597 myCurrentMode = CurAction3d_Nothing;
599 case CurAction3d_GlobalPanning :
600 myView->Place(point.x,point.y,myCurZoom);
601 myCurrentMode = CurAction3d_Nothing;
603 case CurAction3d_DynamicRotation :
604 myCurrentMode = CurAction3d_Nothing;
606 } //switch (myCurrentMode)
607 } // else // if ( Ctrl )
610 void CViewer3dView::OnMButtonDown(UINT nFlags, CPoint point)
612 if ( nFlags & MK_CONTROL )
614 // Button MB2 down Control : panning init
615 // SetCursor(AfxGetApp()->LoadStandardCursor());
619 void CViewer3dView::OnMButtonUp(UINT nFlags, CPoint point)
621 if ( nFlags & MK_CONTROL )
623 // Button MB2 down Control : panning init
624 // SetCursor(AfxGetApp()->LoadStandardCursor());
628 void CViewer3dView::OnRButtonDown(UINT nFlags, CPoint point)
630 if ( nFlags & MK_CONTROL )
632 // SetCursor(AfxGetApp()->LoadStandardCursor());
633 if (!myDegenerateModeIsOn)
634 myView->SetDegenerateModeOn();
635 myView->StartRotation(point.x,point.y);
639 GetDocument()->Popup(point.x,point.y,myView);
643 void CViewer3dView::OnRButtonUp(UINT nFlags, CPoint point)
645 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
646 if (!myDegenerateModeIsOn)
648 myView->SetDegenerateModeOff();
649 myDegenerateModeIsOn = Standard_False;
652 myView->SetDegenerateModeOn();
653 myDegenerateModeIsOn = Standard_True;
655 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
658 void CViewer3dView::OnMouseMove(UINT nFlags, CPoint point)
660 // ============================ LEFT BUTTON =======================
661 if ( nFlags & MK_LBUTTON)
663 if ( nFlags & MK_CONTROL )
665 // move with MB1 and Control : on the dynamic zooming
666 // Do the zoom in function of mouse's coordinates
667 myView->Zoom(myXmax,myYmax,point.x,point.y);
668 // save the current mouse coordinate in min
674 switch (myCurrentMode)
676 case CurAction3d_Nothing :
677 myXmax = point.x; myYmax = point.y;
678 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
679 if (nFlags & MK_SHIFT)
680 GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
682 GetDocument()->DragEvent(myXmax,myYmax,0,myView);
683 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
685 case CurAction3d_DynamicZooming :
686 myView->Zoom(myXmax,myYmax,point.x,point.y);
687 // save the current mouse coordinate in min \n";
688 myXmax=point.x; myYmax=point.y;
690 case CurAction3d_WindowZooming :
691 myXmax = point.x; myYmax = point.y;
692 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
693 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
696 case CurAction3d_DynamicPanning :
697 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
698 myXmax = point.x; myYmax = point.y;
700 case CurAction3d_GlobalPanning : // nothing
702 case CurAction3d_DynamicRotation :
703 myView->Rotation(point.x,point.y);
706 }// switch (myCurrentMode)
707 }// if ( nFlags & MK_CONTROL ) else
708 } else // if ( nFlags & MK_LBUTTON)
709 // ============================ MIDDLE BUTTON =======================
710 if ( nFlags & MK_MBUTTON)
712 if ( nFlags & MK_CONTROL )
714 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
715 myXmax = point.x; myYmax = point.y;
718 } else // if ( nFlags & MK_MBUTTON)
719 // ============================ RIGHT BUTTON =======================
720 if ( nFlags & MK_RBUTTON)
722 if ( nFlags & MK_CONTROL )
724 myView->Rotation(point.x,point.y);
726 }else //if ( nFlags & MK_RBUTTON)
727 // ============================ NO BUTTON =======================
729 myXmax = point.x; myYmax = point.y;
730 if (myCurrentMode == CurAction3d_EndDirectionalLight)
732 p2 = ConvertClickToPoint(point.x,point.y,myView);
733 //Update the light dynamically
734 if( p1.Distance(p2)>Precision::Confusion())
736 BRepBuilderAPI_MakeEdge MakeEdge(p1, p2);
737 directionalEdgeShape->Set(MakeEdge.Edge());
738 GetDocument()->GetAISContext()->Redisplay(directionalEdgeShape,0,-1);
739 myCurrent_DirectionalLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
740 myView->UpdateLights();
743 else if (myCurrentMode == CurAction3d_BeginPositionalLight)
745 p2 = ConvertClickToPoint(point.x,point.y,myView);
746 //Update the light dynamically
747 myCurrent_PositionalLight->SetPosition(p2.X(),p2.Y(),p2.Z());
748 myView->UpdateLights();
750 else if (myCurrentMode == CurAction3d_TargetSpotLight)
752 p2 = ConvertClickToPoint(point.x,point.y,myView);
753 //Update the light dynamically
754 Standard_Real coneHeigth=p1.Distance(p2);
755 if( coneHeigth>Precision::Confusion())
757 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
758 0, (p1.Distance(p2))/tan(1.04), coneHeigth);
759 spotConeShape->Set(MakeCone.Solid());
760 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,-1);
761 myCurrent_SpotLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
762 myView->UpdateLights();
765 else if (myCurrentMode == CurAction3d_EndSpotLight)
767 p3 = ConvertClickToPoint(point.x,point.y,myView);
768 //Update the light dynamically
769 Standard_Real coneHeigth=p1.Distance(p2);
770 if( (p2.Distance(p3))>Precision::Confusion())
772 BRepPrimAPI_MakeCone MakeCone(gp_Ax2(p1, gp_Dir(gp_Vec(p1, p2))),
773 0, p2.Distance(p3), coneHeigth);
774 spotConeShape->Set(MakeCone.Solid());
775 GetDocument()->GetAISContext()->Redisplay(spotConeShape,0,-1);
776 myCurrent_SpotLight->SetAngle(atan(p2.Distance(p3)/p1.Distance(p2))) ;
777 myView->UpdateLights();
780 if (nFlags & MK_SHIFT)
781 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
783 GetDocument()->MoveEvent(point.x,point.y,myView);
787 void CViewer3dView::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI)
789 pCmdUI->SetCheck (myDegenerateModeIsOn);
790 pCmdUI->Enable (!myDegenerateModeIsOn);
793 void CViewer3dView::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI)
795 pCmdUI->SetCheck (!myDegenerateModeIsOn);
796 pCmdUI->Enable (myDegenerateModeIsOn);
799 void CViewer3dView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
801 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
802 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
806 void CViewer3dView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
808 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
809 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
812 void CViewer3dView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
814 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
815 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
818 void CViewer3dView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
820 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
821 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
824 void CViewer3dView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
826 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
827 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
830 void CViewer3dView::DrawRectangle(const Standard_Integer MinX ,
831 const Standard_Integer MinY ,
832 const Standard_Integer MaxX ,
833 const Standard_Integer MaxY ,
834 const Standard_Boolean Draw ,
835 const LineStyle aLineStyle)
837 static int m_DrawMode;
838 if (!m_Pen && aLineStyle ==Solid )
839 {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
840 else if (!m_Pen && aLineStyle ==Dot )
841 {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0)); m_DrawMode = R2_XORPEN;}
842 else if (!m_Pen && aLineStyle == ShortDash)
843 {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0)); m_DrawMode = R2_XORPEN;}
844 else if (!m_Pen && aLineStyle == LongDash)
845 {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0)); m_DrawMode = R2_NOTXORPEN;}
846 else if (aLineStyle == Default)
847 { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
850 CClientDC clientDC(this);
851 if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
852 clientDC.SetROP2(m_DrawMode);
854 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
855 static Standard_Boolean m_IsVisible;
857 if ( m_IsVisible && !Draw) // move or up : erase at the old position
859 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
860 clientDC.LineTo(StoredMaxX,StoredMaxY);
861 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
865 StoredMinX = min ( MinX, MaxX );
866 StoredMinY = min ( MinY, MaxY );
867 StoredMaxX = max ( MinX, MaxX );
868 StoredMaxY = max ( MinY, MaxY);
870 if (Draw) // move : draw
872 clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);
873 clientDC.LineTo(StoredMaxX,StoredMaxY);
874 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
878 if (m_Pen) clientDC.SelectObject(aOldPen);
881 void CViewer3dView::OnModifyChangeBackground()
886 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
888 m_clr = RGB(R1*255,G1*255,B1*255);
890 CColorDialog dlgColor(m_clr);
891 if (dlgColor.DoModal() == IDOK)
893 m_clr = dlgColor.GetColor();
894 R1 = GetRValue(m_clr)/255.;
895 G1 = GetGValue(m_clr)/255.;
896 B1 = GetBValue(m_clr)/255.;
897 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
902 void CViewer3dView::OnDirectionalLight()
904 // Directional light source creation
906 // Checking if the Active lights limit number is not reached
907 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
910 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
911 myGraphicDriver->InquireLightLimit());
912 MessageBox(tmpStr,"Light creation", MB_OK);
917 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick a first point");
918 GetDocument()->GetAISContext()->OpenLocalContext();
919 myCurrentMode = CurAction3d_BeginDirectionalLight;
921 TCollection_AsciiString Message("\
922 myCurrent_DirectionalLight = new V3d_DirectionalLight(myView->Viewer(), Xt, Yt, Zt, Xp, Yp, Zp);\n\
924 myView->SetLightOn(myCurrent_DirectionalLight);\n\
927 // Update The Result Message Dialog
928 GetDocument()->UpdateResultMessageDlg("V3d_DirectionalLight",Message);
931 void CViewer3dView::OnSpotLight()
933 // Spot light source creation
935 // Checking if the Active lights limit number is not reached
936 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
939 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
940 myGraphicDriver->InquireLightLimit());
941 MessageBox(tmpStr,"Light creation", MB_OK);
945 GetDocument()->GetAISContext()->OpenLocalContext();
946 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
947 myCurrentMode = CurAction3d_BeginSpotLight;
949 TCollection_AsciiString Message("\
950 myCurrent_SpotLight = new V3d_SpotLight(myView->Viewer(), Xt, Yt, Zt, Xp, Yp, Zp,Quantity_NOC_RED);\n\
952 myView->SetLightOn(myCurrent_SpotLight);\n\
955 // Update The Result Message Dialog
956 GetDocument()->UpdateResultMessageDlg("V3d_SpotLight",Message);
959 void CViewer3dView::OnPositionalLight()
961 // Positional light source creation
963 // Checking if the Active lights limit number is not reached
964 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
967 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
968 myGraphicDriver->InquireLightLimit());
969 MessageBox(tmpStr,"Light creation", MB_OK);
973 GetDocument()->GetAISContext()->OpenLocalContext();
974 myCurrent_PositionalLight=new V3d_PositionalLight(myView->Viewer(),0,0,0,Quantity_NOC_GREEN,1,0);
975 myView->SetLightOn(myCurrent_PositionalLight);
977 ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Pick the light position");
978 myCurrentMode = CurAction3d_BeginPositionalLight;
980 TCollection_AsciiString Message("\
981 myCurrent_PositionalLight=new V3d_PositionalLight(myView->Viewer(),Xp, Yp, Zp,Quantity_NOC_GREEN,1,0);\n\
983 myView->SetLightOn(myCurrent_PositionalLight) ;\n\
986 // Update The Result Message Dialog
987 GetDocument()->UpdateResultMessageDlg("V3d_PositionalLight",Message);
990 void CViewer3dView::OnAmbientLight()
992 // Ambiant light source creation
994 // Checking if the Active lights limit number is not reached
995 if( NbActiveLights>=myGraphicDriver->InquireLightLimit() )
998 sprintf_s(tmpStr, "You have reach the limit number of active lights (%d).\n Clear lights to create new ones.",
999 myGraphicDriver->InquireLightLimit());
1000 MessageBox(tmpStr,"Light creation", MB_OK);
1004 GetDocument()->GetAISContext()->OpenLocalContext();
1005 myCurrent_AmbientLight=new V3d_AmbientLight(myView->Viewer(), Quantity_NOC_GRAY);
1006 myView->SetLightOn(myCurrent_AmbientLight) ;
1008 GetDocument()->GetAISContext()->CloseLocalContext();
1010 myView->UpdateLights();
1012 TCollection_AsciiString Message("\
1013 myCurrent_AmbientLight=new V3d_AmbientLight(myView->Viewer(), Quantity_NOC_GRAY);\n\
1015 myView->SetLightOn(myCurrent_AmbientLight) ;\n\
1018 // Update The Result Message Dialog
1019 GetDocument()->UpdateResultMessageDlg("V3d_AmbientLight",Message);
1023 void CViewer3dView::OnScale()
1025 ScaleDlg Dlg(myView, this);
1030 void CViewer3dView::OnZcueing()
1032 ZCueingDlg Dlg(myView, GetDocument());
1037 void CViewer3dView::OnZclipping()
1039 ZClippingDlg Dlg(myView, GetDocument());
1046 void CViewer3dView::OnShadingmodel()
1048 TCollection_AsciiString Message("\
1049 myView->SetShadingModel(V3d_TypeOfShadingModel myTypeOfShadingModel);\n\
1051 myCurrent_V3d_View->Update();\n\
1054 // Update The Result Message Dialog
1055 GetDocument()->UpdateResultMessageDlg("SetShadingModel",Message);
1057 CShadingModelDlg Dlg(myView);
1063 void CViewer3dView::OnAntialiasingonoff()
1065 if(!myView->Antialiasing())
1066 myView->SetAntialiasingOn();
1068 myView->SetAntialiasingOff();
1072 TCollection_AsciiString Message("\
1073 if(!myView->Antialiasing())\n\
1074 myView->SetAntialiasingOn();\n\
1076 myView->SetAntialiasingOff();\n\
1079 // Update The Result Message Dialog
1080 GetDocument()->UpdateResultMessageDlg("SetAntialiasingOn/SetAntialiasingOff",Message);
1083 void CViewer3dView::OnClearLights()
1085 // Setting Off all viewer active lights
1086 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())
1087 myView->Viewer()->SetLightOff(myView->Viewer()->ActiveLight());
1088 // Setting Off all view active lights
1089 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())
1090 myView->SetLightOff(myView->ActiveLight());
1092 myView->Viewer()->SetDefaultLights();// Setting the default lights on
1094 NbActiveLights = 2;// There are 2 default active lights
1098 TCollection_AsciiString Message("\
1099 for(myView->Viewer()->InitActiveLights(); myView->Viewer()->MoreActiveLights(); myView->Viewer()->NextActiveLights())\n\
1100 myView->Viewer()->SetLightOff(myView->Viewer()->ActiveLight()); //Setting Off all viewer active lights\n\
1101 for(myView->InitActiveLights(); myView->MoreActiveLights(); myView->NextActiveLights())\n\
1102 myView->SetLightOff(myView->ActiveLight()); //Setting Off all view active lights\n\
1104 myView->Viewer()->SetDefaultLights();// Setting the default lights on\n\
1107 // Update The Result Message Dialog
1108 GetDocument()->UpdateResultMessageDlg("SetLightOff",Message);
1111 void CViewer3dView::OnModelclipping()
1113 if( myPlane.IsNull() )
1115 //creates a plane defined : center of the box ( 50,50,50) and 1 direction
1116 gp_Pln tmpPln(gp_Pnt(0,0,0),gp_Dir(1,0,0));
1117 //getting the coefficients of the gp_Pln ( ax+by+cz+d = 0 )
1118 Standard_Real A,B,C,D;
1119 tmpPln.Coefficients(A,B,C,D);
1120 //with these coefficients, creating a V3d_Plane
1121 myPlane = new V3d_Plane(
1122 GetDocument()->GetViewer(),A,B,C,D);
1124 //NOTE : the face must be behind the clipping plane !!
1125 tmpPln = gp_Pln(gp_Pnt(0.1,0,0),gp_Dir(1,0,0));
1126 BRepBuilderAPI_MakeFace MakeFace(tmpPln, 200, -200, 410, -410);
1127 TopoDS_Face S = MakeFace.Face();
1129 myShape = new AIS_Shape(S);
1132 CModelClippingDlg Dlg(myView, myPlane, myShape, GetDocument());
1136 void CViewer3dView::OnOptionsTrihedronStaticTrihedron()
1138 CTrihedronDlg Dlg(myView, GetDocument());
1142 void CViewer3dView::InitButtons()
1149 myCurrentMode = CurAction3d_Nothing;
1152 void CViewer3dView::Reset()
1155 myVisMode = VIS_SHADE;
1156 if (!myView.IsNull())
1163 void CViewer3dView::GetViewCenter(V3d_Coordinate& Xc, V3d_Coordinate& Yc)
1165 myView->Center(Xc,Yc);
1168 void CViewer3dView::SetViewCenter(V3d_Coordinate Xc, V3d_Coordinate Yc)
1170 myView->SetCenter(Xc,Yc);
1173 void CViewer3dView::GetViewEye(V3d_Coordinate& X, V3d_Coordinate& Y, V3d_Coordinate& Z)
1178 void CViewer3dView::SetViewEye(V3d_Coordinate X, V3d_Coordinate Y, V3d_Coordinate Z)
1180 myView->SetEye(X,Y,Z);
1183 Quantity_Factor CViewer3dView::GetViewScale()
1185 return myView->Scale();
1188 void CViewer3dView::SetViewScale(Quantity_Factor Coef)
1190 myView->SetScale(Coef);
1193 void CViewer3dView::RedrawVisMode()
1198 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);
1199 myView->SetComputedMode (Standard_False);
1202 GetDocument()->GetAISContext()->SetDisplayMode(AIS_Shaded);
1203 myView->SetComputedMode (Standard_False);
1206 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1207 myView->SetComputedMode (Standard_True);
1208 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1209 GetDocument()->GetAISContext()->SetDisplayMode(AIS_WireFrame);