bf805a9eb6524561e0dbdc18bcd9670e57c49d66
[occt.git] / samples / mfc / standard / 04_Viewer3d / src / OffsetDlg.cpp
1 // OffsetDlg.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "..\res\resource.h"
6 #include "offsetdlg.h"
7 #include "Viewer3dView.h"
8
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
14
15 #define FactorMinValue 0
16 #define FactorMaxValue 20
17
18 #define UnitsMinValue 0
19 #define UnitsMaxValue 20
20
21
22 /////////////////////////////////////////////////////////////////////////////
23 // COffsetDlg dialog
24
25
26 COffsetDlg::COffsetDlg(CViewer3dDoc* theDoc, CWnd* pParent /*=NULL*/)
27         : CDialog(COffsetDlg::IDD, pParent)
28 {
29         //{{AFX_DATA_INIT(COffsetDlg)
30         m_Factor = 1.;
31         m_Units = 0.;
32         //}}AFX_DATA_INIT
33         myDoc = theDoc;
34 }
35
36
37 void COffsetDlg::DoDataExchange(CDataExchange* pDX)
38 {
39         CDialog::DoDataExchange(pDX);
40         //{{AFX_DATA_MAP(COffsetDlg)
41         DDX_Control(pDX, IDC_SLIDER_OFFSETUNITS, m_UnitsSlidCtrl);
42         DDX_Control(pDX, IDC_SLIDER_OFFSETFACTOR, m_FactorSlidCtrl);
43         DDX_Text(pDX, IDC_EDIT_OFFSETFACTOR, m_Factor);
44         DDV_MinMaxDouble(pDX, m_Factor, FactorMinValue, FactorMaxValue);
45         DDX_Text(pDX, IDC_EDIT_OFFSETUNITS, m_Units);
46         DDV_MinMaxDouble(pDX, m_Units, UnitsMinValue, UnitsMaxValue);
47         //}}AFX_DATA_MAP
48 }
49
50
51 BEGIN_MESSAGE_MAP(COffsetDlg, CDialog)
52         //{{AFX_MSG_MAP(COffsetDlg)
53         ON_WM_HSCROLL()
54         ON_EN_CHANGE(IDC_EDIT_OFFSETFACTOR, OnChangeEditOffsetFactor)
55         ON_EN_CHANGE(IDC_EDIT_OFFSETUNITS, OnChangeEditOffsetUnits)
56         //}}AFX_MSG_MAP
57 END_MESSAGE_MAP()
58
59 /////////////////////////////////////////////////////////////////////////////
60 // COffsetDlg message handlers
61
62 BOOL COffsetDlg::OnInitDialog() 
63 {
64         CDialog::OnInitDialog();
65
66 // Initializing the ComboBox
67         SetOffsets(m_Factor,m_Units);
68
69 // Setting the m_FactorSlidCtrl position at floor(m_Factor) value (because slider position is an integer)
70         m_FactorSlidCtrl.SetRange(FactorMinValue, FactorMaxValue, TRUE);
71         m_FactorSlidCtrl.SetPos( (int) floor(m_Factor));
72
73 // Setting the m_UnitsSlidCtrl position at floor(m_Units) value (because slider position is an integer)
74         m_UnitsSlidCtrl.SetRange(UnitsMinValue, UnitsMaxValue, TRUE);
75         m_UnitsSlidCtrl.SetPos( (int) floor(m_Units));
76
77         UpdateData(FALSE);
78                         
79                 
80         return TRUE;  // return TRUE unless you set the focus to a control
81                       // EXCEPTION: OCX Property Pages should return FALSE
82 }
83
84 void COffsetDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
85 {
86
87 // Setting the m_Factor value at m_FactorSlidCtrl position
88         m_Factor = m_FactorSlidCtrl.GetPos();
89 // Setting the m_Units value at m_UnitsSlidCtrl position
90         m_Units = m_UnitsSlidCtrl.GetPos();
91
92         UpdateData(FALSE);
93
94
95         SetOffsets(m_Factor,m_Units);
96         
97         CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
98 }
99
100 void COffsetDlg::OnChangeEditOffsetFactor() 
101 {
102         UpdateData();
103
104         m_FactorSlidCtrl.SetPos(int(m_Units));
105
106         SetOffsets(m_Factor,m_Units);
107 }
108
109 void COffsetDlg::OnChangeEditOffsetUnits() 
110 {
111         UpdateData();
112
113         m_UnitsSlidCtrl.SetPos(int(m_Units));
114
115         SetOffsets(m_Factor,m_Units);
116 }
117
118 void COffsetDlg::SetOffsets(Standard_Real theFactor, Standard_Real theUnits)
119 {
120         Handle(AIS_InteractiveContext) aAISContext = myDoc->GetAISContext();
121
122         Handle(AIS_Shape) aOverlappedBox = myDoc->GetOverlappedBox();
123         Handle(AIS_Shape) aOverlappedCylinder = myDoc->GetOverlappedCylinder();
124         Handle(AIS_Shape) aOverlappedSphere = myDoc->GetOverlappedSphere();
125         Handle(AIS_Shape) aBox = myDoc->GetBox();
126         Handle(User_Cylinder) aCylinder = myDoc->GetCylinder();
127         Handle(AIS_Shape) aSphere = myDoc->GetSphere();
128
129
130         myDoc->SetDialogTitle("Set offsets");
131
132         TCollection_AsciiString Message;
133
134         Standard_Boolean OneOrMoreCurrentIsDisplayed=false;
135         Standard_ShortReal aFactor = (Standard_ShortReal)theFactor;
136         Standard_ShortReal aUnits = (Standard_ShortReal)theUnits;
137         
138         for (aAISContext->InitCurrent();aAISContext->MoreCurrent ();aAISContext->NextCurrent ())
139                 if (aAISContext->IsDisplayed(aAISContext->Current())) OneOrMoreCurrentIsDisplayed=true;
140
141         if(OneOrMoreCurrentIsDisplayed){
142                 if((aAISContext->IsCurrent(aOverlappedBox) || aAISContext->IsCurrent(aBox))
143                                 && aAISContext->IsDisplayed(aOverlappedBox)){
144                         aOverlappedBox->SetPolygonOffsets(Aspect_POM_Fill,aFactor,aUnits);
145                         Message = "Box2 -> SetPolygonOffsets(Aspect_POM_Fill, Factor, Units);\n";
146                         //myDoc -> AddTextInDialog(Message);
147                 }
148
149                 if((aAISContext->IsCurrent(aOverlappedCylinder) || aAISContext->IsCurrent(aCylinder)) 
150                                 && aAISContext->IsDisplayed(aOverlappedCylinder)){
151                         aOverlappedCylinder->SetPolygonOffsets(Aspect_POM_Fill,aFactor,aUnits);
152                         Message = Message + "Cylinder2 -> SetPolygonOffsets(Aspect_POM_Fill, Factor, Units);\n";
153                         //myDoc -> AddTextInDialog(Message);
154                 }
155
156                 if((aAISContext->IsCurrent(aOverlappedSphere) || aAISContext->IsCurrent(aSphere))
157                                 && aAISContext->IsDisplayed(aOverlappedSphere)){
158                         aOverlappedSphere->SetPolygonOffsets(Aspect_POM_Fill,aFactor,aUnits);
159                         Message = Message + "Sphere2 -> SetPolygonOffsets(Aspect_POM_Fill, Factor, Units);\n";
160                         //myDoc -> AddTextInDialog(Message);
161                 }       
162         }
163         else{
164                 if(aAISContext->IsDisplayed(aOverlappedBox)){
165                         aOverlappedBox->SetPolygonOffsets(Aspect_POM_Fill,aFactor,aUnits);
166                         Message = Message + "Box2 -> SetPolygonOffsets(Aspect_POM_Fill, Factor, Units);\n";
167                         //myDoc -> AddTextInDialog(Message);
168                 }
169
170                 if(aAISContext->IsDisplayed(aOverlappedCylinder)){
171                         aOverlappedCylinder->SetPolygonOffsets(Aspect_POM_Fill,aFactor,aUnits);
172                         Message = Message + "Cylinder2 -> SetPolygonOffsets(Aspect_POM_Fill, Factor, Units);\n";
173                         //myDoc -> AddTextInDialog(Message);
174                 }
175
176                 if(aAISContext->IsDisplayed(aOverlappedSphere)){
177                         aOverlappedSphere->SetPolygonOffsets(Aspect_POM_Fill,aFactor,aUnits);
178                         Message = Message + "Sphere2 -> SetPolygonOffsets(Aspect_POM_Fill, Factor, Units);\n";
179                         //myDoc -> AddTextInDialog(Message);
180                 }
181         }
182         
183         CString aTextInDialog = myDoc -> GetDialogText();
184   if (aTextInDialog != Message.ToCString())
185                 myDoc -> UpdateResultMessageDlg("Set offsets", Message);
186         aAISContext->UpdateCurrentViewer();
187 }
188
189 void COffsetDlg::UpdateValues()
190 {
191         Standard_Integer aOffsetMode;
192         Standard_ShortReal aBoxFactor = 0;
193         Standard_ShortReal aCylFactor = 0;
194         Standard_ShortReal aSphereFactor = 0;
195         Standard_ShortReal aCylUnits = 0;
196         Standard_ShortReal aSphereUnits = 0;
197         Standard_ShortReal aBoxUnits = 0;
198
199         Handle(AIS_InteractiveContext) aAISContext = myDoc->GetAISContext();
200
201         Handle(AIS_Shape) aOverlappedBox = myDoc->GetOverlappedBox();
202         Handle(AIS_Shape) aOverlappedCylinder = myDoc->GetOverlappedCylinder();
203         Handle(AIS_Shape) aOverlappedSphere = myDoc->GetOverlappedSphere();
204         Handle(AIS_Shape) aBox = myDoc->GetBox();
205         Handle(User_Cylinder) aCylinder = myDoc->GetCylinder();
206         Handle(AIS_Shape) aSphere = myDoc->GetSphere();
207
208         BOOL IsOverlappedCylinderDisplayed = aAISContext->IsDisplayed(aOverlappedCylinder);
209         BOOL IsOverlappedSphereDisplayed = aAISContext->IsDisplayed(aOverlappedSphere);
210         BOOL IsOverlappedBoxDisplayed = aAISContext->IsDisplayed(aOverlappedBox);
211
212         BOOL IsOverlappedCylinderCurrent = aAISContext->IsCurrent(aOverlappedCylinder);
213         BOOL IsOverlappedSphereCurrent = aAISContext->IsCurrent(aOverlappedSphere);
214         BOOL IsOverlappedBoxCurrent = aAISContext->IsCurrent(aOverlappedBox);
215
216
217         /*BOOL IsAnyOverlappedObjectCurrent = 
218                 IsOverlappedCylinderCurrent || IsOverlappedSphereCurrent || IsOverlappedBoxCurrent;*/
219
220
221         BOOL IsCylinderCurrent = aAISContext->IsCurrent(aCylinder);
222         BOOL IsSphereCurrent = aAISContext->IsCurrent(aSphere);
223         BOOL IsBoxCurrent = aAISContext->IsCurrent(aBox);
224
225         BOOL IsAnyObjectCurrent = 
226                 IsOverlappedCylinderCurrent || IsOverlappedSphereCurrent || IsOverlappedBoxCurrent
227                 || IsCylinderCurrent || IsSphereCurrent || IsBoxCurrent;
228
229         BOOL IsAnyOverlappedObjectDisplayed = 
230                 IsOverlappedCylinderDisplayed || IsOverlappedSphereDisplayed || IsOverlappedBoxDisplayed;
231
232         Standard_Boolean OneOrMoreCurrentIsDisplayed=false;
233         for (aAISContext->InitCurrent();aAISContext->MoreCurrent ();aAISContext->NextCurrent ())
234                 if (aAISContext->IsDisplayed(aAISContext->Current())) OneOrMoreCurrentIsDisplayed=true;
235
236         if(OneOrMoreCurrentIsDisplayed){
237                 if(IsOverlappedBoxDisplayed && (IsBoxCurrent || IsOverlappedBoxCurrent)){
238                         aOverlappedBox->PolygonOffsets(aOffsetMode,aBoxFactor,aBoxUnits);
239                 }
240
241                 if(IsOverlappedCylinderDisplayed && (IsCylinderCurrent || IsOverlappedCylinderCurrent)){
242                         aOverlappedCylinder->PolygonOffsets(aOffsetMode,aCylFactor,aCylUnits);
243                 }
244
245                 if(IsOverlappedSphereDisplayed && (IsSphereCurrent || IsOverlappedSphereCurrent)){
246                         aOverlappedSphere->PolygonOffsets(aOffsetMode,aSphereFactor,aSphereUnits);
247                 }
248
249         }
250         else{
251         if(IsOverlappedBoxDisplayed){
252                         aOverlappedBox->PolygonOffsets(aOffsetMode,aBoxFactor,aBoxUnits);
253                 }
254
255                 if(IsOverlappedCylinderDisplayed){
256                         aOverlappedCylinder->PolygonOffsets(aOffsetMode,aCylFactor,aCylUnits);
257                 }
258
259                 if(IsOverlappedSphereDisplayed){
260                         aOverlappedSphere->PolygonOffsets(aOffsetMode,aSphereFactor,aSphereUnits);
261                 }
262         }
263
264         Standard_Real Max = aBoxFactor;
265         if (Max < aCylFactor) Max = aCylFactor;
266         if (Max < aSphereFactor) Max = aSphereFactor;
267
268         m_Factor = Max;
269
270         Max = aBoxUnits;
271         if (Max < aCylUnits) Max = aCylUnits;
272         if (Max < aSphereUnits) Max = aSphereUnits;
273
274         m_Units = Max;
275
276         m_FactorSlidCtrl.SetPos( (int) floor(m_Factor));
277
278
279         m_UnitsSlidCtrl.SetPos( (int) floor(m_Units));
280         UpdateData(FALSE);
281
282         if(             (IsOverlappedBoxCurrent || (IsBoxCurrent && IsOverlappedBoxDisplayed)) ||
283                         (IsOverlappedCylinderCurrent || (IsCylinderCurrent && IsOverlappedCylinderDisplayed)) ||
284                         (IsOverlappedSphereCurrent || (IsSphereCurrent && IsOverlappedSphereDisplayed))  ||
285                         (!IsAnyObjectCurrent && IsAnyOverlappedObjectDisplayed)
286         )
287         {
288                 m_FactorSlidCtrl.EnableWindow();
289                 m_UnitsSlidCtrl.EnableWindow();
290                 GetDlgItem(IDC_EDIT_OFFSETFACTOR)->EnableWindow();
291                 GetDlgItem(IDC_EDIT_OFFSETUNITS)->EnableWindow();
292         }
293         else{
294                 m_FactorSlidCtrl.EnableWindow(false);
295                 m_UnitsSlidCtrl.EnableWindow(false);
296                 GetDlgItem(IDC_EDIT_OFFSETFACTOR)->EnableWindow(false);
297                 GetDlgItem(IDC_EDIT_OFFSETUNITS)->EnableWindow(false);
298         }
299
300 }