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