0023654: Problem with displaying vertices in OCC view after closing all OCC views...
[occt.git] / samples / mfc / standard / 10_Convert / src / 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 <Standard_SStream.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_GraphicDriver) aGraphicDriver = 
47                 ((COCCDemoApp*)AfxGetApp())->GetGraphicDriver();
48
49         myViewer = new V3d_Viewer(aGraphicDriver,(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   strcpy_s(myDataDir, "Data");
60   strcpy_s(myLastPath, ".");
61 }
62
63 COCCDemoDoc::~COCCDemoDoc()
64 {
65 }
66
67 BOOL COCCDemoDoc::OnNewDocument()
68 {
69         if (!CDocument::OnNewDocument())
70                 return FALSE;
71
72         // TODO: add reinitialization code here
73         // (SDI documents will reuse this document)
74   SetTitle(myPresentation->GetName());
75
76   myAISContext->EraseAll();
77   myAISContext->SetDisplayMode(AIS_Shaded);
78
79   POSITION pos = GetFirstViewPosition();
80   while (pos != NULL)
81   {
82     COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
83     pView->Reset();
84   }  
85
86         return TRUE;
87 }
88
89 void COCCDemoDoc::Start()
90 {
91   myPresentation->Init();
92   OnBUTTONStart();
93 }
94
95 /////////////////////////////////////////////////////////////////////////////
96 // COCCDemoDoc serialization
97
98 void COCCDemoDoc::Serialize(CArchive& ar)
99 {
100         if (ar.IsStoring())
101                 {}// TODO: add storing code here
102         else
103                 {}// TODO: add loading code here
104 }
105
106 /////////////////////////////////////////////////////////////////////////////
107 // COCCDemoDoc diagnostics
108
109 #ifdef _DEBUG
110 void COCCDemoDoc::AssertValid() const
111 {
112         CDocument::AssertValid();
113 }
114
115 void COCCDemoDoc::Dump(CDumpContext& dc) const
116 {
117         CDocument::Dump(dc);
118 }
119 #endif //_DEBUG
120
121 /////////////////////////////////////////////////////////////////////////////
122 // COCCDemoDoc commands
123
124 void COCCDemoDoc::OnFileNew()
125 {
126   OnNewDocument();
127   Start();
128 }
129
130 void COCCDemoDoc::InitViewButtons()
131 {
132   POSITION pos = GetFirstViewPosition();
133   while (pos != NULL)
134   {
135     COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
136     pView->InitButtons();
137   }
138 }
139
140 void COCCDemoDoc::DoSample()
141 {
142   InitViewButtons();
143
144   HCURSOR hOldCursor = ::GetCursor();
145   HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
146
147   SetCursor(hNewCursor);
148   {
149     try
150     {
151       myPresentation->DoSample();
152     }
153     catch (Standard_Failure)
154     {
155       Standard_SStream aSStream;
156       aSStream << "An exception was caught: " << Standard_Failure::Caught() << ends;
157       Standard_CString aMsg = aSStream.str().c_str();
158 //      aSStream.rdbuf()->freeze(0);   // allow deletion of dynamic array
159       AfxMessageBox (aMsg);
160     }
161   }
162   SetCursor(hOldCursor);
163 }
164
165 void COCCDemoDoc::OnBUTTONStart() 
166 {
167   myAISContext->EraseAll();
168   myPresentation->FirstSample();
169   DoSample();
170 }
171
172 void COCCDemoDoc::OnBUTTONEnd()
173 {
174   myAISContext->EraseAll();
175   myPresentation->LastSample();
176   DoSample();
177 }
178
179 void COCCDemoDoc::OnBUTTONRepeat() 
180 {
181   DoSample();
182 }
183
184 void COCCDemoDoc::OnBUTTONNext() 
185 {
186   if (!myPresentation->AtLastSample())
187   {
188     myPresentation->NextSample();
189     DoSample();
190   }
191 }
192
193 void COCCDemoDoc::OnBUTTONPrev() 
194 {
195   if (!myPresentation->AtFirstSample())
196   {
197     myPresentation->PrevSample();
198     DoSample();
199   }
200 }
201
202 void COCCDemoDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) 
203 {
204         pCmdUI->Enable (!myPresentation->AtLastSample());
205 }
206
207 void COCCDemoDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) 
208 {
209         pCmdUI->Enable (!myPresentation->AtFirstSample());
210 }
211
212 void COCCDemoDoc::OnBUTTONShowResult()
213 {
214   myShowResult = !myShowResult;
215   if (myShowResult)
216     myCResultDialog.ShowWindow(SW_SHOWNORMAL);
217   else
218     myCResultDialog.ShowWindow(SW_HIDE);
219 }
220
221 void COCCDemoDoc::OnUpdateBUTTONShowResult(CCmdUI* pCmdUI)
222 {
223   pCmdUI->SetCheck (myShowResult);
224 }
225
226 void COCCDemoDoc::OnDumpView() 
227 {
228   CFileDialog aDlg (false, "gif", "OCCView.gif", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
229     "GIF  Files (*.GIF)|*.gif|"
230     "BMP  Files (*.BMP)|*.bmp|"
231     "PNG  Files (*.PNG)|*.png|"
232     "JPEG Files (*.JPEG)|*.jpeg|"
233     "PPM  Files (*.PPM)|*.ppm|"
234     "TIFF Files (*.TIFF)|*.tiff|"
235     "TGA  Files (*.TGA)|*.tga|"
236     "EXR  Files (*.EXR)|*.exr||", NULL);
237   if (aDlg.DoModal() != IDOK)
238   {
239     return;
240   }
241
242   for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
243   {
244     COCCDemoView* pView = (COCCDemoView* )GetNextView (aPos);
245     pView->UpdateWindow();
246   }
247   myViewer->InitActiveViews();
248   Handle(V3d_View) aView = myViewer->ActiveView();
249   aView->Dump (aDlg.GetPathName());
250 }
251
252 void COCCDemoDoc::Fit()
253 {
254         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
255         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
256         COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
257         pView->FitAll();
258 }