0029367: Visualization - simplify interface of V3d_View and V3d_Viewer
[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);
50         myViewer->SetDefaultLights();
51         myViewer->SetLightOn();
52   myViewer->SetDefaultBackgroundColor (Quantity_NOC_BLACK);
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 (Standard_False);
77   myAISContext->SetDisplayMode (AIS_Shaded, Standard_True);
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 const& anException)
154     {
155       Standard_SStream aSStream;
156       aSStream << "An exception was caught: " << anException << std::ends;
157       CString aMsg = aSStream.str().c_str();
158       AfxMessageBox (aMsg);
159     }
160   }
161   SetCursor(hOldCursor);
162 }
163
164 void COCCDemoDoc::OnBUTTONStart() 
165 {
166   myAISContext->EraseAll (Standard_True);
167   myPresentation->FirstSample();
168   DoSample();
169 }
170
171 void COCCDemoDoc::OnBUTTONEnd()
172 {
173   myAISContext->EraseAll (Standard_True);
174   myPresentation->LastSample();
175   DoSample();
176 }
177
178 void COCCDemoDoc::OnBUTTONRepeat() 
179 {
180   DoSample();
181 }
182
183 void COCCDemoDoc::OnBUTTONNext() 
184 {
185   if (!myPresentation->AtLastSample())
186   {
187     myPresentation->NextSample();
188     DoSample();
189   }
190 }
191
192 void COCCDemoDoc::OnBUTTONPrev() 
193 {
194   if (!myPresentation->AtFirstSample())
195   {
196     myPresentation->PrevSample();
197     DoSample();
198   }
199 }
200
201 void COCCDemoDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) 
202 {
203         pCmdUI->Enable (!myPresentation->AtLastSample());
204 }
205
206 void COCCDemoDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) 
207 {
208         pCmdUI->Enable (!myPresentation->AtFirstSample());
209 }
210
211 void COCCDemoDoc::OnBUTTONShowResult()
212 {
213   myShowResult = !myShowResult;
214   if (myShowResult)
215     myCResultDialog.ShowWindow(SW_SHOWNORMAL);
216   else
217     myCResultDialog.ShowWindow(SW_HIDE);
218 }
219
220 void COCCDemoDoc::OnUpdateBUTTONShowResult(CCmdUI* pCmdUI)
221 {
222   pCmdUI->SetCheck (myShowResult);
223 }
224
225 void COCCDemoDoc::OnDumpView() 
226 {
227   for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
228   {
229     COCCDemoView* pView = (COCCDemoView* )GetNextView (aPos);
230     pView->UpdateWindow();
231   }
232
233   Handle(V3d_View) aView = myViewer->ActiveViews().First();
234   ExportView (aView);
235 }
236
237 void COCCDemoDoc::Fit()
238 {
239         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
240         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
241         COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
242         pView->FitAll();
243 }