0024270: Port viewer3d mfc sample to new clipping
authorapl <apl@opencascade.com>
Mon, 21 Oct 2013 17:28:14 +0000 (21:28 +0400)
committerabv <abv@opencascade.com>
Thu, 24 Oct 2013 12:03:32 +0000 (16:03 +0400)
samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.cpp
samples/mfc/standard/04_Viewer3d/src/ModelClippingDlg.h
samples/mfc/standard/04_Viewer3d/src/Viewer3dView.cpp
samples/mfc/standard/04_Viewer3d/src/Viewer3dView.h

index eda0548..7aa3274 100755 (executable)
 static char THIS_FILE[] = __FILE__;
 #endif
 
 static char THIS_FILE[] = __FILE__;
 #endif
 
-
-/////////////////////////////////////////////////////////////////////////////
-// 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
-}
-
-
+#define EOL "\n"
+
+// =======================================================================
+// 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);
 void CModelClippingDlg::DoDataExchange(CDataExchange* pDX)
 {
        CDialog::DoDataExchange(pDX);
@@ -52,159 +57,214 @@ BEGIN_MESSAGE_MAP(CModelClippingDlg, CDialog)
        //}}AFX_MSG_MAP
 END_MESSAGE_MAP()
 
        //}}AFX_MSG_MAP
 END_MESSAGE_MAP()
 
-/////////////////////////////////////////////////////////////////////////////
-// CModelClippingDlg message handlers
-
+// =======================================================================
+// function : OnHScroll
+// purpose  :
+// =======================================================================
 void CModelClippingDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
 {
 void CModelClippingDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
 {
-       UpdateData(TRUE);
-
-       m_ModelClipping_Z = m_ModelClippingZSlidCtrl.GetPos();
-
-       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();
-
-  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\
-  ");
+  UpdateData(TRUE);
+
+  m_ModelClipping_Z = m_ModelClippingZSlidCtrl.GetPos();
+
+  UpdateData(FALSE);
+
+  // 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));
+
+  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
 
   // 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() 
 {
 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));
-
-       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);
-
-       UpdateData(FALSE);
-
-       return TRUE;  // return TRUE unless you set the focus to a control
-                     // EXCEPTION: OCX Property Pages should return FALSE
-} //V3d_Plane.hxx
-
-void CModelClippingDlg::OnChangeEditModelclippingZ() 
-{
-       UpdateData(TRUE);
-
-// 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();
-
-       myModelClipping_Z = m_ModelClipping_Z;
-
-  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\
-  ");
+  CDialog::OnInitDialog();
 
 
-  // Update The Result Message Dialog
-       myDoc->UpdateResultMessageDlg("SetPlaneOn",Message);
+  const Graphic3d_ClipPlane::Equation aPlaneEquation = myClippingPlane->GetEquation();
+
+  // 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));
+
+  m_ModelClippingONOFF = myClippingPlane->IsOn();
+
+  if (m_ModelClippingONOFF)
+  {
+    // register and activate clipping plane
+    if (!myView->GetClipPlanes().Contains (myClippingPlane))
+    {
+      myView->AddClipPlane (myClippingPlane);
+    }
+
+    myClippingPlane->SetOn (Standard_True);
+
+    myDoc->GetAISContext()->Display (myShape);
+  }
+
+  UpdateData (FALSE);
+
+  return TRUE;
 }
 
 }
 
-void CModelClippingDlg::OnCheckModelclippingonoff() 
+// =======================================================================
+// function : OnChangeEditModelclippingZ
+// purpose  :
+// =======================================================================
+void CModelClippingDlg::OnChangeEditModelclippingZ()
 {
 {
-       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);
-       }
-
-       myView->Update();
-
-  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\
-  ");
+  UpdateData (TRUE);
+
+  m_ModelClippingZSlidCtrl.SetPos ((int)floor (m_ModelClipping_Z));
+
+  // Change clipping plane
+  gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
+
+  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
 
   // 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() 
 {
 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() 
 {
 void CModelClippingDlg::OnOK() 
 {
-       if(!myShape.IsNull())
-               myDoc->GetAISContext()->Erase(myShape);
-       
-       CDialog::OnOK();
+  if (!myShape.IsNull())
+  {
+    myDoc->GetAISContext()->Remove (myShape);
+  }
+
+  CDialog::OnOK();
 }
 }
index c44ea1a..dc8533c 100755 (executable)
 // CModelClippingDlg dialog
 #include "Viewer3dDoc.h"
 
 // CModelClippingDlg dialog
 #include "Viewer3dDoc.h"
 
+#include <Graphic3d_ClipPlane.hxx>
+
 class CModelClippingDlg : public CDialog
 {
 class CModelClippingDlg : public CDialog
 {
-// Construction
 public:
 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)
 
 // Dialog Data
        //{{AFX_DATA(CModelClippingDlg)
@@ -49,11 +55,11 @@ protected:
        DECLARE_MESSAGE_MAP()
 
 private:
        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}}
 };
 
 //{{AFX_INSERT_LOCATION}}
index f7605cf..ef86a6e 100755 (executable)
@@ -1136,27 +1136,24 @@ GetDocument()->UpdateResultMessageDlg("SetLightOff",Message);
 
 void CViewer3dView::OnModelclipping() 
 {
 
 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() 
 }
 
 void CViewer3dView::OnOptionsTrihedronStaticTrihedron() 
index b1fce00..2cd175e 100755 (executable)
@@ -12,6 +12,8 @@
 #include <V3d_AmbientLight.hxx>
 #include <V3d_SpotLight.hxx>
 
 #include <V3d_AmbientLight.hxx>
 #include <V3d_SpotLight.hxx>
 
+#include <Graphic3d_ClipPlane.hxx>
+
 #if _MSC_VER >= 1000
 #pragma once
 #endif // _MSC_VER >= 1000
 #if _MSC_VER >= 1000
 #pragma once
 #endif // _MSC_VER >= 1000
@@ -140,12 +142,12 @@ private:
   Standard_Integer               NbActiveLights;
   Standard_Boolean               myHlrModeIsOn;
   Quantity_Factor                myCurZoom;
   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 };
 
 private:
        enum LineStyle { Solid, Dot, ShortDash, LongDash, Default };