0024270: Port viewer3d mfc sample to new clipping
[occt.git] / samples / mfc / standard / 04_Viewer3d / src / ModelClippingDlg.cpp
CommitLineData
7fd59977 1// ModelClippingDlg.cpp : implementation file
2//
3
4#include "stdafx.h"
5#include "Viewer3dApp.h"
6#include "ModelClippingDlg.h"
7#include "Viewer3dDoc.h"
8#include "offsetdlg.h" // Added by ClassView
9
10#ifdef _DEBUG
11#define new DEBUG_NEW
12#undef THIS_FILE
13static char THIS_FILE[] = __FILE__;
14#endif
15
bf6acfe6 16#define EOL "\n"
17
18// =======================================================================
19// function : CModelClippingDlg
20// purpose :
21// =======================================================================
22CModelClippingDlg::CModelClippingDlg (Handle(V3d_View) theView,
23 Handle(AIS_Shape) theShape,
24 Handle(Graphic3d_ClipPlane) theClippingPlane,
25 CViewer3dDoc* theDoc,
26 CWnd* theParent)
27: CDialog(CModelClippingDlg::IDD, theParent),
28 m_ModelClipping_Z (0.0),
29 myModelClipping_Z (0.0),
30 m_ModelClippingONOFF (FALSE),
31 myView (theView),
32 myShape (theShape),
33 myClippingPlane (theClippingPlane),
34 myDoc (theDoc)
35{}
36
37// =======================================================================
38// function : DoDataExchange
39// purpose :
40// =======================================================================
7fd59977 41void CModelClippingDlg::DoDataExchange(CDataExchange* pDX)
42{
43 CDialog::DoDataExchange(pDX);
44 //{{AFX_DATA_MAP(CModelClippingDlg)
45 DDX_Control(pDX, IDC_SLIDER_MODELCLIPPING_Z, m_ModelClippingZSlidCtrl);
46 DDX_Text(pDX, IDC_EDIT_MODELCLIPPING_Z, m_ModelClipping_Z);
47 DDX_Check(pDX, IDC_CHECK_MODELCLIPPINGONOFF, m_ModelClippingONOFF);
48 //}}AFX_DATA_MAP
49}
50
51
52BEGIN_MESSAGE_MAP(CModelClippingDlg, CDialog)
53 //{{AFX_MSG_MAP(CModelClippingDlg)
54 ON_WM_HSCROLL()
55 ON_EN_CHANGE(IDC_EDIT_MODELCLIPPING_Z, OnChangeEditModelclippingZ)
56 ON_BN_CLICKED(IDC_CHECK_MODELCLIPPINGONOFF, OnCheckModelclippingonoff)
57 //}}AFX_MSG_MAP
58END_MESSAGE_MAP()
59
bf6acfe6 60// =======================================================================
61// function : OnHScroll
62// purpose :
63// =======================================================================
7fd59977 64void CModelClippingDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
65{
bf6acfe6 66 UpdateData(TRUE);
67
68 m_ModelClipping_Z = m_ModelClippingZSlidCtrl.GetPos();
69
70 UpdateData(FALSE);
71
72 // Setting the ZClipping depth at m_ZClippingDepth value
73 gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0));
74
75 myClippingPlane->SetEquation (aPlane);
76
77 gp_Trsf myTrsf;
78 myTrsf.SetTranslation (gp_Pnt (m_ModelClipping_Z, 0.0, 0.0), gp_Pnt(myModelClipping_Z, 0.0, 0.0));
79
80 // transform presentation shape
81 if (m_ModelClippingONOFF)
82 {
83 myDoc->GetAISContext()->SetLocation (myShape, TopLoc_Location (myTrsf));
84 myDoc->GetAISContext()->Redisplay (myShape);
85 myView->Update();
86 }
87 else
88 {
89 myShape->SetLocation (TopLoc_Location (myTrsf));
90 }
91
92 TCollection_AsciiString aMessage (
93 EOL "gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0));"
94 EOL
95 EOL "myClippingPlane->SetEquation (aPlane);\n" );
7fd59977 96
97 // Update The Result Message Dialog
bf6acfe6 98 myDoc->UpdateResultMessageDlg ("Change clipping plane", aMessage);
7fd59977 99
bf6acfe6 100 CDialog::OnHScroll (nSBCode, nPos, pScrollBar);
7fd59977 101}
102
bf6acfe6 103// =======================================================================
104// function : OnInitDialog
105// purpose :
106// =======================================================================
7fd59977 107BOOL CModelClippingDlg::OnInitDialog()
108{
bf6acfe6 109 CDialog::OnInitDialog();
7fd59977 110
bf6acfe6 111 const Graphic3d_ClipPlane::Equation aPlaneEquation = myClippingPlane->GetEquation();
112
113 // m_ModelClipping_Z = D (plane coefficient)
114 m_ModelClipping_Z = aPlaneEquation[3];
115 m_ModelClippingZSlidCtrl.SetRange (-750, 750, TRUE);
116 m_ModelClippingZSlidCtrl.SetPos ((int)floor (m_ModelClipping_Z));
117
118 m_ModelClippingONOFF = myClippingPlane->IsOn();
119
120 if (m_ModelClippingONOFF)
121 {
122 // register and activate clipping plane
123 if (!myView->GetClipPlanes().Contains (myClippingPlane))
124 {
125 myView->AddClipPlane (myClippingPlane);
126 }
127
128 myClippingPlane->SetOn (Standard_True);
129
130 myDoc->GetAISContext()->Display (myShape);
131 }
132
133 UpdateData (FALSE);
134
135 return TRUE;
7fd59977 136}
137
bf6acfe6 138// =======================================================================
139// function : OnChangeEditModelclippingZ
140// purpose :
141// =======================================================================
142void CModelClippingDlg::OnChangeEditModelclippingZ()
7fd59977 143{
bf6acfe6 144 UpdateData (TRUE);
145
146 m_ModelClippingZSlidCtrl.SetPos ((int)floor (m_ModelClipping_Z));
147
148 // Change clipping plane
149 gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
150
151 myClippingPlane->SetEquation (aPlane);
152
153 // transform presentation shape
154 gp_Trsf myTrsf;
155 myTrsf.SetTranslation ( gp_Pnt (m_ModelClipping_Z, 0.0, 0.0), gp_Pnt (myModelClipping_Z, 0.0, 0.0));
156
157 // transform presentation shape
158 if (m_ModelClippingONOFF)
159 {
160 myDoc->GetAISContext()->SetLocation (myShape, TopLoc_Location (myTrsf));
161 myDoc->GetAISContext()->Redisplay (myShape);
162 myView->Update();
163 }
164 else
165 {
166 myShape->SetLocation (TopLoc_Location (myTrsf));
167 }
168
169 myModelClipping_Z = m_ModelClipping_Z;
170
171 TCollection_AsciiString aMessage (
172 EOL "gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0));"
173 EOL
174 EOL "myClippingPlane->SetEquation (aPlane);\n" );
7fd59977 175
176 // Update The Result Message Dialog
bf6acfe6 177 myDoc->UpdateResultMessageDlg ("Change clipping plane", aMessage);
178}
179
180// =======================================================================
181// function : OnCheckModelclippingonoff
182// purpose :
183// =======================================================================
184void CModelClippingDlg::OnCheckModelclippingonoff()
185{
186 UpdateData(TRUE);
187
188 if (m_ModelClippingONOFF)
189 {
190 // register and activate clipping plane
191 if (!myView->GetClipPlanes().Contains (myClippingPlane))
192 {
193 myView->AddClipPlane (myClippingPlane);
194 }
195
196 myClippingPlane->SetOn (Standard_True);
197
198 myDoc->GetAISContext()->Display (myShape);
199 }
200 else
201 {
202 // deactivate clipping plane
203 myClippingPlane->SetOn (Standard_False);
204
205 myDoc->GetAISContext()->Remove (myShape);
206 }
207
208 myView->Update();
209
210 TCollection_AsciiString aMessage (
211 EOL "if (...)"
212 EOL "{"
213 EOL " // register and activate clipping plane"
214 EOL " if (!myView->GetClipPlanes().Contains (myClippingPlane))"
215 EOL " {"
216 EOL " myView->AddClipPlane (myClippingPlane);"
217 EOL " }"
218 EOL
219 EOL " myClippingPlane->SetOn (Standard_True);"
220 EOL "}"
221 EOL "else"
222 EOL "{"
223 EOL " // deactivate clipping plane"
224 EOL " myClippingPlane->SetOn (Standard_False);"
225 EOL "}" );
226
227 myDoc->UpdateResultMessageDlg ("Switch clipping on/off", aMessage);
7fd59977 228}
229
bf6acfe6 230// =======================================================================
231// function : OnCancel
232// purpose :
233// =======================================================================
7fd59977 234void CModelClippingDlg::OnCancel()
235{
bf6acfe6 236 UpdateData(TRUE);
7fd59977 237
bf6acfe6 238 if (m_ModelClippingONOFF)
239 {
240 // remove and deactivate clipping plane
241 myView->RemoveClipPlane (myClippingPlane);
7fd59977 242
bf6acfe6 243 myClippingPlane->SetOn (Standard_False);
244 }
7fd59977 245
bf6acfe6 246 m_ModelClippingONOFF=FALSE;
247
248 if (!myShape.IsNull())
249 {
250 myDoc->GetAISContext()->Remove (myShape);
251 }
252
253 myView->Update();
254
255 CDialog::OnCancel();
7fd59977 256}
257
bf6acfe6 258// =======================================================================
259// function : OnOK
260// purpose :
261// =======================================================================
7fd59977 262void CModelClippingDlg::OnOK()
263{
bf6acfe6 264 if (!myShape.IsNull())
265 {
266 myDoc->GetAISContext()->Remove (myShape);
267 }
268
269 CDialog::OnOK();
7fd59977 270}