90236005349892f91b36373c4204775385f82a88
[occt.git] / samples / mfc / standard / 04_Viewer3d / src / ModelClippingDlg.cpp
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
13 static char THIS_FILE[] = __FILE__;
14 #endif
15
16 #define EOL "\n"
17
18 // =======================================================================
19 // function : CModelClippingDlg
20 // purpose  :
21 // =======================================================================
22 CModelClippingDlg::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 // =======================================================================
41 void 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
52 BEGIN_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
58 END_MESSAGE_MAP()
59
60 // =======================================================================
61 // function : OnHScroll
62 // purpose  :
63 // =======================================================================
64 void CModelClippingDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
65 {
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" );
96
97   // Update The Result Message Dialog
98   myDoc->UpdateResultMessageDlg ("Change clipping plane", aMessage);
99
100   CDialog::OnHScroll (nSBCode, nPos, pScrollBar);
101 }
102
103 // =======================================================================
104 // function : OnInitDialog
105 // purpose  :
106 // =======================================================================
107 BOOL CModelClippingDlg::OnInitDialog() 
108 {
109   CDialog::OnInitDialog();
110
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     Standard_Boolean toAddPlane = Standard_True;
124     Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (myView->GetClipPlanes());
125     for (; aPlaneIt.More(); aPlaneIt.Next())
126     {
127       if (aPlaneIt.Value() == myClippingPlane)
128       {
129         toAddPlane = Standard_False;
130         break;
131       }
132     }
133
134     if (toAddPlane)
135     {
136       myView->AddClipPlane (myClippingPlane);
137     }
138
139     myClippingPlane->SetOn (Standard_True);
140
141     myDoc->GetAISContext()->Display (myShape);
142   }
143
144   UpdateData (FALSE);
145
146   return TRUE;
147 }
148
149 // =======================================================================
150 // function : OnChangeEditModelclippingZ
151 // purpose  :
152 // =======================================================================
153 void CModelClippingDlg::OnChangeEditModelclippingZ()
154 {
155   UpdateData (TRUE);
156
157   m_ModelClippingZSlidCtrl.SetPos ((int)floor (m_ModelClipping_Z));
158
159   // Change clipping plane
160   gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0));
161
162   myClippingPlane->SetEquation (aPlane);
163
164   // transform presentation shape
165   gp_Trsf myTrsf;
166   myTrsf.SetTranslation ( gp_Pnt (m_ModelClipping_Z, 0.0, 0.0), gp_Pnt (myModelClipping_Z, 0.0, 0.0));
167
168   // transform presentation shape
169   if (m_ModelClippingONOFF)
170   {
171     myDoc->GetAISContext()->SetLocation (myShape, TopLoc_Location (myTrsf));
172     myDoc->GetAISContext()->Redisplay (myShape);
173     myView->Update();
174   }
175   else
176   {
177     myShape->SetLocation (TopLoc_Location (myTrsf));
178   }
179
180   myModelClipping_Z = m_ModelClipping_Z;
181
182   TCollection_AsciiString aMessage (
183     EOL "gp_Pln aPlane (gp_Pnt (-m_ModelClipping_Z, 0.0, 0.0), gp_Dir(1.0, 0.0, 0.0));"
184     EOL
185     EOL "myClippingPlane->SetEquation (aPlane);\n" );
186
187   // Update The Result Message Dialog
188   myDoc->UpdateResultMessageDlg ("Change clipping plane", aMessage);
189 }
190
191 // =======================================================================
192 // function : OnCheckModelclippingonoff
193 // purpose  :
194 // =======================================================================
195 void CModelClippingDlg::OnCheckModelclippingonoff()
196 {
197   UpdateData(TRUE);
198
199   if (m_ModelClippingONOFF)
200   {
201     // register and activate clipping plane
202     Standard_Boolean toAddPlane = Standard_True;
203     Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (myView->GetClipPlanes());
204     for (; aPlaneIt.More(); aPlaneIt.Next())
205     {
206       if (aPlaneIt.Value() == myClippingPlane)
207       {
208         toAddPlane = Standard_False;
209         break;
210       }
211     }
212
213     if (toAddPlane)
214     {
215       myView->AddClipPlane (myClippingPlane);
216     }
217
218     myClippingPlane->SetOn (Standard_True);
219
220     myDoc->GetAISContext()->Display (myShape);
221   }
222   else
223   {
224     // deactivate clipping plane
225     myClippingPlane->SetOn (Standard_False);
226
227     myDoc->GetAISContext()->Remove (myShape);
228   }
229
230   myView->Update();
231
232   TCollection_AsciiString aMessage (
233     EOL "if (...)"
234     EOL "{"
235     EOL "  // register and activate clipping plane"
236     EOL "  if (!myView->GetClipPlanes().Contains (myClippingPlane))"
237     EOL "  {"
238     EOL "    myView->AddClipPlane (myClippingPlane);"
239     EOL "  }"
240     EOL
241     EOL "  myClippingPlane->SetOn (Standard_True);"
242     EOL "}"
243     EOL "else"
244     EOL "{"
245     EOL "  // deactivate clipping plane"
246     EOL "  myClippingPlane->SetOn (Standard_False);"
247     EOL "}" );
248
249   myDoc->UpdateResultMessageDlg ("Switch clipping on/off", aMessage);
250 }
251
252 // =======================================================================
253 // function : OnCancel
254 // purpose  :
255 // =======================================================================
256 void CModelClippingDlg::OnCancel() 
257 {
258   UpdateData(TRUE);
259
260   if (m_ModelClippingONOFF)
261   {
262     // remove and deactivate clipping plane
263     myView->RemoveClipPlane (myClippingPlane);
264
265     myClippingPlane->SetOn (Standard_False);
266   }
267
268   m_ModelClippingONOFF=FALSE;
269
270   if (!myShape.IsNull())
271   {
272     myDoc->GetAISContext()->Remove (myShape);
273   }
274
275   myView->Update();
276
277   CDialog::OnCancel();
278 }
279
280 // =======================================================================
281 // function : OnOK
282 // purpose  :
283 // =======================================================================
284 void CModelClippingDlg::OnOK() 
285 {
286   if (!myShape.IsNull())
287   {
288     myDoc->GetAISContext()->Remove (myShape);
289   }
290
291   CDialog::OnOK();
292 }