0028316: Coding Rules - Elimilate confusing aliases of Standard_Real type in V3d_View
[occt.git] / samples / mfc / occtdemo / Common / WNT / OCCDemoDoc.cpp
1 // OCCDemoDoc.cpp : implementation of the COCCDemoDoc class
2 //
3
4 #include "stdafx.h"
5 #include "OCCDemo.h"
6
7 #include "OCCDemoDoc.h"
8 #include "OCCDemo_Presentation.h"
9 #include "OCCDemoView.h"
10
11 #include <Standard_ErrorHandler.hxx>
12
13 #ifdef _DEBUG
14 #undef THIS_FILE
15 static char THIS_FILE[] = __FILE__;
16 #endif
17
18 /////////////////////////////////////////////////////////////////////////////
19 // COCCDemoDoc
20
21 IMPLEMENT_DYNCREATE(COCCDemoDoc, CDocument)
22
23 BEGIN_MESSAGE_MAP(COCCDemoDoc, CDocument)
24         //{{AFX_MSG_MAP(COCCDemoDoc)
25         ON_COMMAND(ID_BUTTONNext, OnBUTTONNext)
26         ON_COMMAND(ID_BUTTONStart, OnBUTTONStart)
27         ON_COMMAND(ID_BUTTONRepeat, OnBUTTONRepeat)
28         ON_COMMAND(ID_BUTTONPrev, OnBUTTONPrev)
29         ON_COMMAND(ID_BUTTONEnd, OnBUTTONEnd)
30         ON_UPDATE_COMMAND_UI(ID_BUTTONNext, OnUpdateBUTTONNext)
31         ON_UPDATE_COMMAND_UI(ID_BUTTONPrev, OnUpdateBUTTONPrev)
32         ON_COMMAND(ID_FILE_NEW, OnFileNew)
33         ON_COMMAND(ID_BUTTONShowResult, OnBUTTONShowResult)
34         ON_UPDATE_COMMAND_UI(ID_BUTTONShowResult, OnUpdateBUTTONShowResult)
35         ON_COMMAND(ID_DUMP_VIEW, OnDumpView)
36         //}}AFX_MSG_MAP
37 END_MESSAGE_MAP()
38
39 /////////////////////////////////////////////////////////////////////////////
40 // COCCDemoDoc construction/destruction
41
42 COCCDemoDoc::COCCDemoDoc()
43 {
44         // TODO: add one-time construction code here
45         Handle(Graphic3d_WNTGraphicDevice) theGraphicDevice = 
46                 ((COCCDemoApp*)AfxGetApp())->GetGraphicDevice();
47
48         myViewer = new V3d_Viewer(theGraphicDevice);
49         myViewer->SetDefaultLights();
50         myViewer->SetLightOn();
51   myViewer->SetDefaultBackgroundColor(Quantity_TOC_RGB, 0.,0.,0.);
52
53         myAISContext = new AIS_InteractiveContext(myViewer);
54   myShowResult = FALSE;
55   myPresentation = OCCDemo_Presentation::Current;
56   myPresentation->SetDocument(this);
57
58   CFile aFile;
59   if (!aFile.Open("Data\\points.dat", CFile::modeRead)){
60                   strcpy(myDataDir, "..\\Data");
61         }
62   else
63         strcpy(myDataDir, "Data");
64   strcpy(myLastPath, ".");
65 }
66
67 COCCDemoDoc::~COCCDemoDoc()
68 {
69 }
70
71 BOOL COCCDemoDoc::OnNewDocument()
72 {
73         if (!CDocument::OnNewDocument())
74                 return FALSE;
75
76         // TODO: add reinitialization code here
77         // (SDI documents will reuse this document)
78   
79   SetTitle(myPresentation->GetName());
80
81   myAISContext->EraseAll(Standard_False);
82   myAISContext->SetDisplayMode(AIS_Shaded);
83
84   POSITION pos = GetFirstViewPosition();
85   while (pos != NULL)
86   {
87     COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
88     pView->Reset();
89   }  
90
91         return TRUE;
92 }
93
94 void COCCDemoDoc::Start()
95 {
96   myPresentation->Init();
97   OnBUTTONStart();
98 }
99
100 /////////////////////////////////////////////////////////////////////////////
101 // COCCDemoDoc serialization
102
103 void COCCDemoDoc::Serialize(CArchive& ar)
104 {
105         if (ar.IsStoring())
106                 {}// TODO: add storing code here
107         else
108                 {}// TODO: add loading code here
109 }
110
111 /////////////////////////////////////////////////////////////////////////////
112 // COCCDemoDoc diagnostics
113
114 #ifdef _DEBUG
115 void COCCDemoDoc::AssertValid() const
116 {
117         CDocument::AssertValid();
118 }
119
120 void COCCDemoDoc::Dump(CDumpContext& dc) const
121 {
122         CDocument::Dump(dc);
123 }
124 #endif //_DEBUG
125
126 /////////////////////////////////////////////////////////////////////////////
127 // COCCDemoDoc commands
128
129 void COCCDemoDoc::OnFileNew()
130 {
131   OnNewDocument();
132   Start();
133 }
134
135 void COCCDemoDoc::InitViewButtons()
136 {
137   POSITION pos = GetFirstViewPosition();
138   while (pos != NULL)
139   {
140     COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
141     pView->InitButtons();
142   }
143 }
144
145 void COCCDemoDoc::DoSample()
146 {
147   InitViewButtons();
148
149   HCURSOR hOldCursor = ::GetCursor();
150   HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
151
152   SetCursor(hNewCursor);
153   {
154     try
155     {
156       myPresentation->DoSample();
157     }
158     catch (Standard_Failure)
159     {
160       Standard_SStream ostr;
161       ostr<<Standard_Failure::Caught()<<"\n\0";
162
163       AfxMessageBox (GetSString(ostr));
164     }
165   }
166   SetCursor(hOldCursor);
167 }
168
169 void COCCDemoDoc::OnBUTTONStart() 
170 {
171   myAISContext->EraseAll(Standard_False);
172   myPresentation->FirstSample();
173   DoSample();
174 }
175
176 void COCCDemoDoc::OnBUTTONEnd()
177 {
178   myAISContext->EraseAll(Standard_False);
179   myPresentation->LastSample();
180   DoSample();
181 }
182
183 void COCCDemoDoc::OnBUTTONRepeat() 
184 {
185   DoSample();
186 }
187
188 void COCCDemoDoc::OnBUTTONNext() 
189 {
190   if (!myPresentation->AtLastSample())
191   {
192     myPresentation->NextSample();
193     DoSample();
194   }
195 }
196
197 void COCCDemoDoc::OnBUTTONPrev() 
198 {
199   if (!myPresentation->AtFirstSample())
200   {
201     myPresentation->PrevSample();
202     DoSample();
203   }
204 }
205
206 void COCCDemoDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) 
207 {
208         pCmdUI->Enable (!myPresentation->AtLastSample());
209 }
210
211 void COCCDemoDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) 
212 {
213         pCmdUI->Enable (!myPresentation->AtFirstSample());
214 }
215
216 void COCCDemoDoc::OnBUTTONShowResult()
217 {
218   myShowResult = !myShowResult;
219   if (myShowResult)
220     myCResultDialog.ShowWindow(SW_SHOWNORMAL);
221   else
222     myCResultDialog.ShowWindow(SW_HIDE);
223 }
224
225 void COCCDemoDoc::OnUpdateBUTTONShowResult(CCmdUI* pCmdUI)
226 {
227   pCmdUI->SetCheck (myShowResult);
228 }
229
230 void COCCDemoDoc::OnDumpView() 
231 {
232   // save current directory and restore it on exit
233   char aCurPath[MAX_PATH];
234   ::GetCurrentDirectory(MAX_PATH, aCurPath);
235
236   ::SetCurrentDirectory(myLastPath);
237
238   CFileDialog *aDlg = new CFileDialog(false, "gif", "OCCView.gif", 
239     OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "GIF Files (*.gif)|*.gif||", NULL);
240
241   int result = aDlg->DoModal();
242   if ( result == IDOK) 
243   {
244     CString aFileName = aDlg->GetFileName();
245     delete aDlg;
246
247     POSITION pos = GetFirstViewPosition();
248     while (pos != NULL)
249     {
250       COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
251       pView->UpdateWindow();
252     }       
253
254     myViewer->InitActiveViews();
255     Handle(V3d_View) aView = myViewer->ActiveView();
256     char aStrFileName[MAX_PATH];
257     strcpy(aStrFileName, aFileName);
258     aView->Dump(aStrFileName);
259   }
260   else 
261     delete aDlg;
262   
263   ::GetCurrentDirectory(MAX_PATH, myLastPath);
264   ::SetCurrentDirectory(aCurPath);
265 }
266
267 void COCCDemoDoc::Fit()
268 {
269         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
270         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
271         COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
272         pView->FitAll();
273 }