From bf6acfe656b8818a3629e17fe87df79a9d74cd26 Mon Sep 17 00:00:00 2001 From: apl Date: Mon, 21 Oct 2013 21:28:14 +0400 Subject: [PATCH] 0024270: Port viewer3d mfc sample to new clipping --- .../04_Viewer3d/src/ModelClippingDlg.cpp | 326 +++++++++++------- .../04_Viewer3d/src/ModelClippingDlg.h | 22 +- .../standard/04_Viewer3d/src/Viewer3dView.cpp | 39 +-- .../standard/04_Viewer3d/src/Viewer3dView.h | 14 +- 4 files changed, 233 insertions(+), 168 deletions(-) diff --git a/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.cpp b/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.cpp index eda0548954..7aa3274f2f 100755 --- a/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.cpp +++ b/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.cpp @@ -13,26 +13,31 @@ static char THIS_FILE[] = __FILE__; #endif +#define EOL "\n" -///////////////////////////////////////////////////////////////////////////// -// CModelClippingDlg dialog - - -CModelClippingDlg::CModelClippingDlg(Handle(V3d_View) aView, Handle(V3d_Plane) aPlane, - Handle(AIS_Shape) aShape, CViewer3dDoc* pDoc, CWnd* pParent /*=NULL*/) - : CDialog(CModelClippingDlg::IDD, pParent) -{ - //{{AFX_DATA_INIT(CModelClippingDlg) - m_ModelClipping_Z = 0.0; - myView=aView; - myPlane=aPlane; - myShape=aShape; - myDoc=pDoc; - m_ModelClippingONOFF = FALSE; - //}}AFX_DATA_INIT -} - +// ======================================================================= +// function : CModelClippingDlg +// purpose : +// ======================================================================= +CModelClippingDlg::CModelClippingDlg (Handle(V3d_View) theView, + Handle(AIS_Shape) theShape, + Handle(Graphic3d_ClipPlane) theClippingPlane, + CViewer3dDoc* theDoc, + CWnd* theParent) +: CDialog(CModelClippingDlg::IDD, theParent), + m_ModelClipping_Z (0.0), + myModelClipping_Z (0.0), + m_ModelClippingONOFF (FALSE), + myView (theView), + myShape (theShape), + myClippingPlane (theClippingPlane), + myDoc (theDoc) +{} +// ======================================================================= +// function : DoDataExchange +// purpose : +// ======================================================================= void CModelClippingDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); @@ -52,159 +57,214 @@ BEGIN_MESSAGE_MAP(CModelClippingDlg, CDialog) //}}AFX_MSG_MAP END_MESSAGE_MAP() -///////////////////////////////////////////////////////////////////////////// -// CModelClippingDlg message handlers - +// ======================================================================= +// function : OnHScroll +// purpose : +// ======================================================================= void CModelClippingDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { - UpdateData(TRUE); + UpdateData(TRUE); - m_ModelClipping_Z = m_ModelClippingZSlidCtrl.GetPos(); + m_ModelClipping_Z = m_ModelClippingZSlidCtrl.GetPos(); - UpdateData(FALSE); + UpdateData(FALSE); -// Setting the ZClipping depth at m_ZClippingDepth value - gp_Pln clipPln(gp_Pnt(-m_ModelClipping_Z,0,0),gp_Dir(1,0,0)); - Standard_Real A,B,C,D; - clipPln.Coefficients(A,B,C,D); - myPlane->SetPlane(A,B,C,D); - if(m_ModelClippingONOFF) - myView->SetPlaneOn(myPlane); - gp_Trsf myTrsf; - myTrsf.SetTranslation(gp_Pnt(m_ModelClipping_Z,0,0), gp_Pnt(myModelClipping_Z,0,0)); - myDoc->GetAISContext()->SetLocation(myShape,TopLoc_Location(myTrsf)) ; - myDoc->GetAISContext()->Redisplay(myShape); - myView->Update(); + // Setting the ZClipping depth at m_ZClippingDepth value + gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0)); - TCollection_AsciiString Message("\ -gp_Pln clipPln(gp_Pnt(-m_ModelClippingZSlidCtrl.GetPos(),0,0),gp_Dir(1,0,0));\n\ -Standard_Real A,B,C,D;\n\ -clipPln.Coefficients(A,B,C,D);\n\ -myPlane->SetPlane(A,B,C,D);\n\ -myView->SetPlaneOn(myPlane); \n\ - "); + myClippingPlane->SetEquation (aPlane); + + gp_Trsf myTrsf; + myTrsf.SetTranslation (gp_Pnt (m_ModelClipping_Z, 0.0, 0.0), gp_Pnt(myModelClipping_Z, 0.0, 0.0)); + + // transform presentation shape + if (m_ModelClippingONOFF) + { + myDoc->GetAISContext()->SetLocation (myShape, TopLoc_Location (myTrsf)); + myDoc->GetAISContext()->Redisplay (myShape); + myView->Update(); + } + else + { + myShape->SetLocation (TopLoc_Location (myTrsf)); + } + + TCollection_AsciiString aMessage ( + EOL "gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0));" + EOL + EOL "myClippingPlane->SetEquation (aPlane);\n" ); // Update The Result Message Dialog - myDoc->UpdateResultMessageDlg("SetPlaneOn",Message); + myDoc->UpdateResultMessageDlg ("Change clipping plane", aMessage); - CDialog::OnHScroll(nSBCode, nPos, pScrollBar); + CDialog::OnHScroll (nSBCode, nPos, pScrollBar); } +// ======================================================================= +// function : OnInitDialog +// purpose : +// ======================================================================= BOOL CModelClippingDlg::OnInitDialog() { - CDialog::OnInitDialog(); - - Standard_Real A,B,C,D; - myPlane->Plane(A,B,C,D); - m_ModelClipping_Z = D; - m_ModelClippingZSlidCtrl.SetRange(-750, 750, TRUE); - m_ModelClippingZSlidCtrl.SetPos( (int) floor(m_ModelClipping_Z)); + CDialog::OnInitDialog(); - Handle(V3d_Plane) thePlane; - for( myView->InitActivePlanes() ; - myView->MoreActivePlanes() ; myView->NextActivePlanes() ) { - thePlane = myView->ActivePlane() ; - if( thePlane == myPlane ) m_ModelClippingONOFF = TRUE; - } - if(m_ModelClippingONOFF) - if(!myShape.IsNull()) - myDoc->GetAISContext()->Display(myShape); + const Graphic3d_ClipPlane::Equation aPlaneEquation = myClippingPlane->GetEquation(); - UpdateData(FALSE); + // m_ModelClipping_Z = D (plane coefficient) + m_ModelClipping_Z = aPlaneEquation[3]; + m_ModelClippingZSlidCtrl.SetRange (-750, 750, TRUE); + m_ModelClippingZSlidCtrl.SetPos ((int)floor (m_ModelClipping_Z)); - return TRUE; // return TRUE unless you set the focus to a control - // EXCEPTION: OCX Property Pages should return FALSE -} //V3d_Plane.hxx + m_ModelClippingONOFF = myClippingPlane->IsOn(); -void CModelClippingDlg::OnChangeEditModelclippingZ() -{ - UpdateData(TRUE); + if (m_ModelClippingONOFF) + { + // register and activate clipping plane + if (!myView->GetClipPlanes().Contains (myClippingPlane)) + { + myView->AddClipPlane (myClippingPlane); + } -// Setting the m_ZClippingDepthSlidCtrl position at floor(m_ZClippingDepth) value (because slider position is an integer) - m_ModelClippingZSlidCtrl.SetPos( (int) floor(m_ModelClipping_Z)); -// Setting the ZClipping depth at m_ZClippingDepth value - gp_Pln clipPln(gp_Pnt(-m_ModelClipping_Z,0,0),gp_Dir(1,0,0)); - Standard_Real A,B,C,D; - clipPln.Coefficients(A,B,C,D); - myPlane->SetPlane(A,B,C,D); - if(m_ModelClippingONOFF) - myView->SetPlaneOn(myPlane); - gp_Trsf myTrsf; - myTrsf.SetTranslation(gp_Pnt(m_ModelClipping_Z,0,0), gp_Pnt(myModelClipping_Z,0,0)); - myDoc->GetAISContext()->SetLocation(myShape,TopLoc_Location(myTrsf)) ; - myDoc->GetAISContext()->Redisplay(myShape); - myView->Update(); + myClippingPlane->SetOn (Standard_True); - myModelClipping_Z = m_ModelClipping_Z; + myDoc->GetAISContext()->Display (myShape); + } - TCollection_AsciiString Message("\ -gp_Pln clipPln(gp_Pnt(-m_ModelClipping_Z,0,0),gp_Dir(1,0,0));\n\ -Standard_Real A,B,C,D;\n\ -clipPln.Coefficients(A,B,C,D);\n\ -myPlane->SetPlane(A,B,C,D);\n\ -myView->SetPlaneOn(myPlane); \n\ - "); + UpdateData (FALSE); - // Update The Result Message Dialog - myDoc->UpdateResultMessageDlg("SetPlaneOn",Message); + return TRUE; } -void CModelClippingDlg::OnCheckModelclippingonoff() +// ======================================================================= +// function : OnChangeEditModelclippingZ +// purpose : +// ======================================================================= +void CModelClippingDlg::OnChangeEditModelclippingZ() { - UpdateData(TRUE); + UpdateData (TRUE); -//activate the plane - if(m_ModelClippingONOFF) - { -//activate the plane - myView->SetPlaneOn(myPlane); - myDoc->GetAISContext()->Display(myShape); - } - else -//deactivate the plane - { - myView->SetPlaneOff(myPlane); - myDoc->GetAISContext()->Erase(myShape); - } + m_ModelClippingZSlidCtrl.SetPos ((int)floor (m_ModelClipping_Z)); - myView->Update(); + // Change clipping plane + gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0)); - TCollection_AsciiString Message("\ -gp_Pln clipPln(gp_Pnt(-m_ModelClippingZSlidCtrl.GetPos(),0,0),gp_Dir(1,0,0));\n\ -Standard_Real A,B,C,D;\n\ -clipPln.Coefficients(A,B,C,D);\n\ -myPlane->SetPlane(A,B,C,D);\n\ -if(m_ModelClippingONOFF) \n\ - myView->SetPlaneOn(myPlane); \n\ -else \n\ - myView->SetPlaneOff(myPlane); \n\ - "); + myClippingPlane->SetEquation (aPlane); + + // transform presentation shape + gp_Trsf myTrsf; + myTrsf.SetTranslation ( gp_Pnt (m_ModelClipping_Z, 0.0, 0.0), gp_Pnt (myModelClipping_Z, 0.0, 0.0)); + + // transform presentation shape + if (m_ModelClippingONOFF) + { + myDoc->GetAISContext()->SetLocation (myShape, TopLoc_Location (myTrsf)); + myDoc->GetAISContext()->Redisplay (myShape); + myView->Update(); + } + else + { + myShape->SetLocation (TopLoc_Location (myTrsf)); + } + + myModelClipping_Z = m_ModelClipping_Z; + + TCollection_AsciiString aMessage ( + EOL "gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0));" + EOL + EOL "myClippingPlane->SetEquation (aPlane);\n" ); // Update The Result Message Dialog - myDoc->UpdateResultMessageDlg("SetPlaneOn",Message); + myDoc->UpdateResultMessageDlg ("Change clipping plane", aMessage); } +// ======================================================================= +// function : OnCheckModelclippingonoff +// purpose : +// ======================================================================= +void CModelClippingDlg::OnCheckModelclippingonoff() +{ + UpdateData(TRUE); + + if (m_ModelClippingONOFF) + { + // register and activate clipping plane + if (!myView->GetClipPlanes().Contains (myClippingPlane)) + { + myView->AddClipPlane (myClippingPlane); + } + + myClippingPlane->SetOn (Standard_True); + + myDoc->GetAISContext()->Display (myShape); + } + else + { + // deactivate clipping plane + myClippingPlane->SetOn (Standard_False); + + myDoc->GetAISContext()->Remove (myShape); + } + + myView->Update(); + + TCollection_AsciiString aMessage ( + EOL "if (...)" + EOL "{" + EOL " // register and activate clipping plane" + EOL " if (!myView->GetClipPlanes().Contains (myClippingPlane))" + EOL " {" + EOL " myView->AddClipPlane (myClippingPlane);" + EOL " }" + EOL + EOL " myClippingPlane->SetOn (Standard_True);" + EOL "}" + EOL "else" + EOL "{" + EOL " // deactivate clipping plane" + EOL " myClippingPlane->SetOn (Standard_False);" + EOL "}" ); + + myDoc->UpdateResultMessageDlg ("Switch clipping on/off", aMessage); +} + +// ======================================================================= +// function : OnCancel +// purpose : +// ======================================================================= void CModelClippingDlg::OnCancel() { - UpdateData(TRUE); - if(m_ModelClippingONOFF) -//deactivate the plane - myView->SetPlaneOff(myPlane); + UpdateData(TRUE); - m_ModelClippingONOFF=FALSE; + if (m_ModelClippingONOFF) + { + // remove and deactivate clipping plane + myView->RemoveClipPlane (myClippingPlane); - if(!myShape.IsNull()) - myDoc->GetAISContext()->Erase(myShape); + myClippingPlane->SetOn (Standard_False); + } - myView->Update(); - - CDialog::OnCancel(); + m_ModelClippingONOFF=FALSE; + + if (!myShape.IsNull()) + { + myDoc->GetAISContext()->Remove (myShape); + } + + myView->Update(); + + CDialog::OnCancel(); } +// ======================================================================= +// function : OnOK +// purpose : +// ======================================================================= void CModelClippingDlg::OnOK() { - if(!myShape.IsNull()) - myDoc->GetAISContext()->Erase(myShape); - - CDialog::OnOK(); + if (!myShape.IsNull()) + { + myDoc->GetAISContext()->Remove (myShape); + } + + CDialog::OnOK(); } diff --git a/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.h b/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.h index c44ea1a77d..dc8533c96f 100755 --- a/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.h +++ b/samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.h @@ -11,12 +11,18 @@ // CModelClippingDlg dialog #include "Viewer3dDoc.h" +#include + class CModelClippingDlg : public CDialog { -// Construction public: - CModelClippingDlg(Handle(V3d_View) aView, Handle(V3d_Plane) aPlane, - Handle(AIS_Shape) aShape, CViewer3dDoc* pDoc, CWnd* pParent = NULL); // standard constructor + + //! Standard constructor. + CModelClippingDlg (Handle(V3d_View) theView, + Handle(AIS_Shape) theShape, + Handle(Graphic3d_ClipPlane) theClippingPlane, + CViewer3dDoc* theDoc, + CWnd* theParent = NULL); // Dialog Data //{{AFX_DATA(CModelClippingDlg) @@ -49,11 +55,11 @@ protected: DECLARE_MESSAGE_MAP() private: - Handle_V3d_View myView; - Handle_AIS_Shape myShape; - Handle_V3d_Plane myPlane; - CViewer3dDoc* myDoc; - double myModelClipping_Z; + Handle(V3d_View) myView; + Handle(AIS_Shape) myShape; + Handle(Graphic3d_ClipPlane) myClippingPlane; + CViewer3dDoc* myDoc; + double myModelClipping_Z; }; //{{AFX_INSERT_LOCATION}} diff --git a/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.cpp b/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.cpp index f7605cfa28..ef86a6e8b6 100755 --- a/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.cpp +++ b/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.cpp @@ -1136,27 +1136,24 @@ GetDocument()->UpdateResultMessageDlg("SetLightOff",Message); void CViewer3dView::OnModelclipping() { - if( myPlane.IsNull() ) - { -//creates a plane defined : center of the box ( 50,50,50) and 1 direction - gp_Pln tmpPln(gp_Pnt(0,0,0),gp_Dir(1,0,0)); -//getting the coefficients of the gp_Pln ( ax+by+cz+d = 0 ) - Standard_Real A,B,C,D; - tmpPln.Coefficients(A,B,C,D); -//with these coefficients, creating a V3d_Plane - myPlane = new V3d_Plane(A,B,C,D); -// GetDocument()->GetViewer(),A,B,C,D); -//creates the Face -//NOTE : the face must be behind the clipping plane !! - tmpPln = gp_Pln(gp_Pnt(0.1,0,0),gp_Dir(1,0,0)); - BRepBuilderAPI_MakeFace MakeFace(tmpPln, 200, -200, 410, -410); - TopoDS_Face S = MakeFace.Face(); -//display the face - myShape = new AIS_Shape(S); - } - - CModelClippingDlg Dlg(myView, myPlane, myShape, GetDocument()); - Dlg.DoModal(); + if (myClippingPlane.IsNull()) + { + gp_Pln aClipPlane (gp_Pnt (0.0, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0)); + gp_Pln aFacePlane (gp_Pnt (0.1, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0)); + + // create clipping plane and add to view + myClippingPlane = new Graphic3d_ClipPlane (aClipPlane); + + // shape to represent clipping plane + BRepBuilderAPI_MakeFace aMakeFaceCommand (aFacePlane, 200.0, -200.0, 410.0, -410.0); + TopoDS_Face aShape = aMakeFaceCommand.Face(); + myShape = new AIS_Shape (aShape); + myShape->SetTransparency (0.5); + } + + CModelClippingDlg aClippingDlg (myView, myShape, myClippingPlane, GetDocument()); + + aClippingDlg.DoModal(); } void CViewer3dView::OnOptionsTrihedronStaticTrihedron() diff --git a/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.h b/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.h index b1fce0075c..2cd175eda6 100755 --- a/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.h +++ b/samples/mfc/standard/04_Viewer3d/src/Viewer3dView.h @@ -12,6 +12,8 @@ #include #include +#include + #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 @@ -140,12 +142,12 @@ private: Standard_Integer NbActiveLights; Standard_Boolean myHlrModeIsOn; Quantity_Factor myCurZoom; - Handle_V3d_AmbientLight myCurrent_AmbientLight; - Handle_V3d_SpotLight myCurrent_SpotLight; - Handle_V3d_PositionalLight myCurrent_PositionalLight; - Handle_V3d_DirectionalLight myCurrent_DirectionalLight; - Handle_V3d_Plane myPlane; - Handle_AIS_Shape myShape; + Handle(V3d_AmbientLight) myCurrent_AmbientLight; + Handle(V3d_SpotLight) myCurrent_SpotLight; + Handle(V3d_PositionalLight) myCurrent_PositionalLight; + Handle(V3d_DirectionalLight) myCurrent_DirectionalLight; + Handle(Graphic3d_ClipPlane) myClippingPlane; + Handle(AIS_Shape) myShape; private: enum LineStyle { Solid, Dot, ShortDash, LongDash, Default };