0031521: Samples - update MFC ImportExport sample to use AIS_ViewController
[occt.git] / samples / mfc / standard / Common / OCC_3dBaseDoc.cpp
1 // OCC_3dBaseDoc.cpp: implementation of the OCC_3dBaseDoc class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include <stdafx.h>
6
7 #include "OCC_3dBaseDoc.h"
8
9 #include "OCC_3dView.h"
10 #include "OCC_App.h"
11 #include <res\OCC_Resource.h>
12 #include "ImportExport/ImportExport.h"
13 #include "AISDialogs.h"
14 #include <AIS_ListOfInteractive.hxx>
15 #include <AIS_ListIteratorOfListOfInteractive.hxx>
16 #include <TColStd_ListIteratorOfListOfInteger.hxx>
17 #include <TColStd_ListOfInteger.hxx>
18 #include <TopoDS_Shape.hxx>
19
20 BEGIN_MESSAGE_MAP(OCC_3dBaseDoc, OCC_BaseDoc)
21   //{{AFX_MSG_MAP(OCC_3dBaseDoc)
22   ON_COMMAND(ID_FILE_IMPORT_BREP, OnFileImportBrep)
23   ON_COMMAND(ID_FILE_EXPORT_BREP, OnFileExportBrep)
24   ON_COMMAND(ID_OBJECT_ERASE, OnObjectErase)
25   ON_UPDATE_COMMAND_UI(ID_OBJECT_ERASE, OnUpdateObjectErase)
26   ON_COMMAND(ID_OBJECT_COLOR, OnObjectColor)
27   ON_UPDATE_COMMAND_UI(ID_OBJECT_COLOR, OnUpdateObjectColor)
28   ON_COMMAND(ID_OBJECT_SHADING, OnObjectShading)
29   ON_UPDATE_COMMAND_UI(ID_OBJECT_SHADING, OnUpdateObjectShading)
30   ON_COMMAND(ID_OBJECT_WIREFRAME, OnObjectWireframe)
31   ON_UPDATE_COMMAND_UI(ID_OBJECT_WIREFRAME, OnUpdateObjectWireframe)
32   ON_COMMAND(ID_OBJECT_TRANSPARENCY, OnObjectTransparency)
33   ON_UPDATE_COMMAND_UI(ID_OBJECT_TRANSPARENCY, OnUpdateObjectTransparency)
34   ON_COMMAND(ID_OBJECT_MATERIAL, OnObjectMaterial)
35   ON_UPDATE_COMMAND_UI(ID_OBJECT_MATERIAL, OnUpdateObjectMaterial)
36   ON_COMMAND(ID_OBJECT_DISPLAYALL, OnObjectDisplayall)
37   ON_UPDATE_COMMAND_UI(ID_OBJECT_DISPLAYALL, OnUpdateObjectDisplayall)
38   ON_COMMAND(ID_OBJECT_REMOVE, OnObjectRemove)
39   ON_UPDATE_COMMAND_UI(ID_OBJECT_REMOVE, OnUpdateObjectRemove)
40
41   //}}AFX_MSG_MAP
42   ON_COMMAND_EX_RANGE(ID_OBJECT_MATERIAL_BRASS,ID_OBJECT_MATERIAL_DEFAULT, OnObjectMaterialRange)
43   ON_UPDATE_COMMAND_UI_RANGE(ID_OBJECT_MATERIAL_BRASS,ID_OBJECT_MATERIAL_DEFAULT, OnUpdateObjectMaterialRange)
44
45   //RayTracing
46   ON_COMMAND(ID_OBJECT_RAY_TRACING,OnObjectRayTracing)
47   ON_COMMAND(ID_OBJECT_SHADOWS,OnObjectShadows)
48   ON_COMMAND(ID_OBJECT_REFLECTIONS,OnObjectReflections)
49   ON_COMMAND(ID_OBJECT_ANTI_ALIASING,OnObjectAntiAliasing)
50
51   ON_UPDATE_COMMAND_UI(ID_OBJECT_RAY_TRACING, OnUpdateV3dButtons)
52   ON_UPDATE_COMMAND_UI(ID_OBJECT_SHADOWS, OnUpdateV3dButtons)
53   ON_UPDATE_COMMAND_UI(ID_OBJECT_REFLECTIONS, OnUpdateV3dButtons)
54   ON_UPDATE_COMMAND_UI(ID_OBJECT_ANTI_ALIASING, OnUpdateV3dButtons)
55 END_MESSAGE_MAP()
56
57
58 //////////////////////////////////////////////////////////////////////
59 // Construction/Destruction
60 //////////////////////////////////////////////////////////////////////
61
62 OCC_3dBaseDoc::OCC_3dBaseDoc()
63 :myPopupMenuNumber(0)
64 {
65   AfxInitRichEdit();
66
67   Handle(Graphic3d_GraphicDriver) aGraphicDriver = ((OCC_App*)AfxGetApp())->GetGraphicDriver();
68
69   myViewer = new V3d_Viewer (aGraphicDriver);
70   myViewer->SetDefaultLights();
71   myViewer->SetLightOn();
72   myAISContext = new AIS_InteractiveContext (myViewer);
73
74   myRayTracingIsOn            = false;
75   myRaytracedShadowsIsOn      = true;
76   myRaytracedReflectionsIsOn  = false;
77   myRaytracedAntialiasingIsOn = false;
78 }
79
80 OCC_3dBaseDoc::~OCC_3dBaseDoc()
81 {
82 }
83
84 //-----------------------------------------------------------------------------------------
85 //
86 //-----------------------------------------------------------------------------------------
87 void OCC_3dBaseDoc::DragEvent (const Standard_Integer theMouseX,
88                                const Standard_Integer theMouseY,
89                                const Standard_Integer theState,
90                                const Handle(V3d_View)& theView)
91 {
92   // TheState == -1  button down
93   // TheState ==  0  move
94   // TheState ==  1  button up
95
96   static Standard_Integer aStartDragX = 0;
97   static Standard_Integer aStartDragY = 0;
98
99   switch (theState)
100   {
101   case -1:
102     {
103       aStartDragX = theMouseX;
104       aStartDragY = theMouseY;
105       break;
106     }
107   case 0:
108     {
109       myAISContext->UpdateCurrentViewer();
110       break;
111     }
112   case 1:
113     {
114       myAISContext->Select (aStartDragX, aStartDragY,
115                             theMouseX, theMouseY,
116                             theView, Standard_True);
117       break;
118     }
119   };
120 }
121
122 //-----------------------------------------------------------------------------------------
123 //
124 //-----------------------------------------------------------------------------------------
125 void OCC_3dBaseDoc::InputEvent (const Standard_Integer theMouseX,
126                                 const Standard_Integer theMouseY,
127                                 const Handle(V3d_View)& theView)
128 {
129   myAISContext->MoveTo (theMouseX, theMouseY, theView, Standard_False);
130   myAISContext->Select (Standard_True);
131 }
132
133 //-----------------------------------------------------------------------------------------
134 //
135 //-----------------------------------------------------------------------------------------
136 void OCC_3dBaseDoc::MoveEvent (const Standard_Integer theMouseX,
137                                const Standard_Integer theMouseY,
138                                const Handle(V3d_View)& theView)
139 {
140   myAISContext->MoveTo (theMouseX, theMouseY, theView, Standard_True);
141 }
142
143 //-----------------------------------------------------------------------------------------
144 //
145 //-----------------------------------------------------------------------------------------
146 void OCC_3dBaseDoc::ShiftMoveEvent (const Standard_Integer theMouseX,
147                                     const Standard_Integer theMouseY,
148                                     const Handle(V3d_View)& theView)
149 {
150   myAISContext->MoveTo (theMouseX, theMouseY, theView, Standard_True);
151 }
152
153 //-----------------------------------------------------------------------------------------
154 //
155 //-----------------------------------------------------------------------------------------
156 void OCC_3dBaseDoc::ShiftDragEvent (const Standard_Integer theMouseX,
157                                     const Standard_Integer theMouseY,
158                                     const Standard_Integer theState,
159                                     const Handle(V3d_View)& theView)
160 {
161   // TheState == -1  button down
162   // TheState ==  0  move
163   // TheState ==  1  button up
164
165   static Standard_Integer aStartDragX = 0;
166   static Standard_Integer aStartDragY = 0;
167
168   if (theState == -1)
169   {
170     // button down
171     aStartDragX = theMouseX;
172     aStartDragY = theMouseY;
173   }
174
175   if (theState == 0)
176   {
177     // button up
178     myAISContext->ShiftSelect (aStartDragX, aStartDragY,
179                                theMouseX, theMouseY,
180                                theView, Standard_True);
181   }
182 }
183
184 //-----------------------------------------------------------------------------------------
185 //
186 //-----------------------------------------------------------------------------------------
187 void OCC_3dBaseDoc::ShiftInputEvent (const Standard_Integer /*theMouseX*/,
188                                      const Standard_Integer /*theMouseY*/,
189                                      const Handle(V3d_View)& /*theView*/)
190 {
191   myAISContext->ShiftSelect (Standard_True);
192 }
193
194 //-----------------------------------------------------------------------------------------
195 //
196 //-----------------------------------------------------------------------------------------
197 void  OCC_3dBaseDoc::Popup (const Standard_Integer theMouseX,
198                             const Standard_Integer theMouseY,
199                             const Handle(V3d_View)& theView)
200 {
201   // Base check which context menu to call
202   if (!myPopupMenuNumber)
203   {
204     myAISContext->InitSelected();
205     if (myAISContext->MoreSelected())
206       myPopupMenuNumber=1;
207   }
208
209   CMenu menu;
210   VERIFY(menu.LoadMenu(IDR_Popup3D));
211   CMenu* pPopup = menu.GetSubMenu(myPopupMenuNumber);
212
213   ASSERT(pPopup != NULL);
214    if (myPopupMenuNumber == 1) // more than 1 object.
215   {
216     bool OneOrMoreInShading = false;
217         for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
218     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
219         if(!OneOrMoreInShading)
220         pPopup->EnableMenuItem(5, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
221    }
222
223   POINT winCoord = { theMouseX , theMouseY };
224   ClientToScreen ((HWND )theView->Window()->NativeHandle(), &winCoord);
225   pPopup->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON,
226                           winCoord.x,
227                           winCoord.y,
228                           AfxGetMainWnd());
229 }
230
231 void OCC_3dBaseDoc::Fit()
232 {
233         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
234         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
235         OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
236         pView->FitAll();
237 }
238
239 int OCC_3dBaseDoc::OnFileImportBrep_WithInitDir (const wchar_t* )
240 {
241   if (CImportExport::ReadBREP (myAISContext) == 1)
242     return 1;
243   Fit();
244   return 0;
245 }
246
247 void OCC_3dBaseDoc::OnFileImportBrep() 
248 {   
249   OnFileImportBrep_WithInitDir (NULL);
250 }
251
252 void OCC_3dBaseDoc::OnFileExportBrep() 
253 {   CImportExport::SaveBREP(myAISContext);}
254
255 void OCC_3dBaseDoc::OnObjectColor() 
256 {
257         Handle(AIS_InteractiveObject) Current ;
258         COLORREF MSColor ;
259
260         myAISContext->InitSelected();
261     Current = myAISContext->SelectedInteractive();
262         if ( Current->HasColor () ) {
263       Quantity_Color CSFColor;
264       myAISContext->Color (Current, CSFColor);
265       MSColor = RGB (CSFColor.Red()*255.,CSFColor.Green()*255.,CSFColor.Blue()*255.);
266         }
267         else {
268           MSColor = RGB (255,255,255) ;
269         }
270         
271         CColorDialog dlgColor(MSColor);
272         if (dlgColor.DoModal() == IDOK)
273         {
274           MSColor = dlgColor.GetColor();
275           Quantity_Color CSFColor (GetRValue(MSColor)/255., GetGValue(MSColor)/255., GetBValue(MSColor)/255., Quantity_TOC_RGB);
276           for (;myAISContext->MoreSelected ();myAISContext->NextSelected ())
277       myAISContext->SetColor (myAISContext->SelectedInteractive(), CSFColor, Standard_False);
278     myAISContext->UpdateCurrentViewer();
279         }
280 }
281 void OCC_3dBaseDoc::OnUpdateObjectColor(CCmdUI* pCmdUI) 
282 {
283   bool OneOrMoreIsShadingOrWireframe = false;
284   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
285     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),0)
286                 ||myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1))
287                 OneOrMoreIsShadingOrWireframe=true;
288   pCmdUI->Enable (OneOrMoreIsShadingOrWireframe);
289 }
290
291 void OCC_3dBaseDoc::OnObjectErase() 
292 {
293   myAISContext->EraseSelected (Standard_True);
294 }
295 void OCC_3dBaseDoc::OnUpdateObjectErase(CCmdUI* pCmdUI) 
296 {
297   bool OneOrMoreIsDisplayed = false;
298   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
299   {
300     if (myAISContext->IsDisplayed (myAISContext->SelectedInteractive()))
301       OneOrMoreIsDisplayed = true;
302   }
303   pCmdUI->Enable (OneOrMoreIsDisplayed);
304 }
305
306 void OCC_3dBaseDoc::OnObjectWireframe() 
307 {
308   for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->NextSelected())
309         myAISContext->SetDisplayMode (myAISContext->SelectedInteractive(), 0, Standard_False);
310   myAISContext->UpdateCurrentViewer();
311 }
312 void OCC_3dBaseDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI) 
313 {
314   bool OneOrMoreInShading = false;
315   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
316     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
317         pCmdUI->Enable (OneOrMoreInShading);    
318 }
319
320 void OCC_3dBaseDoc::OnObjectShading() 
321 {
322   for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->NextSelected())
323       myAISContext->SetDisplayMode (myAISContext->SelectedInteractive(), 1, Standard_False);
324   myAISContext->UpdateCurrentViewer();
325 }
326
327 void OCC_3dBaseDoc::OnUpdateObjectShading(CCmdUI* pCmdUI) 
328 {
329   bool OneOrMoreInWireframe = false;
330   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
331     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),0)) OneOrMoreInWireframe=true;
332         pCmdUI->Enable (OneOrMoreInWireframe);  
333 }
334
335 void OCC_3dBaseDoc::OnObjectMaterial() 
336 {
337   CDialogMaterial DialBox(myAISContext);
338   DialBox.DoModal();
339   //CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
340   //CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
341   //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
342 }
343
344 void OCC_3dBaseDoc::OnUpdateObjectMaterial(CCmdUI* pCmdUI) 
345 {
346   bool OneOrMoreInShading = false;
347   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
348     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
349         pCmdUI->Enable (OneOrMoreInShading);    
350 }
351
352 BOOL OCC_3dBaseDoc::OnObjectMaterialRange(UINT nID) 
353 {
354   // the range ID_OBJECT_MATERIAL_BRASS to ID_OBJECT_MATERIAL_SILVER is
355   // continue with the same values as enumeration Type Of Material
356   Standard_Real aTransparency;
357
358   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ()){
359     aTransparency = myAISContext->SelectedInteractive()->Transparency();
360     myAISContext->SetMaterial (myAISContext->SelectedInteractive(),(Graphic3d_NameOfMaterial)(nID-ID_OBJECT_MATERIAL_BRASS), Standard_False);
361     myAISContext->SetTransparency (myAISContext->SelectedInteractive(),aTransparency, Standard_False);
362   }
363   myAISContext->UpdateCurrentViewer();
364   return true;
365
366 }
367
368 void OCC_3dBaseDoc::OnUpdateObjectMaterialRange(CCmdUI* pCmdUI) 
369 {
370   bool OneOrMoreInShading = false;
371   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
372     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
373         pCmdUI->Enable (OneOrMoreInShading);
374   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
375     if (myAISContext->SelectedInteractive()->Material() - (pCmdUI->m_nID - ID_OBJECT_MATERIAL_BRASS) == 0)
376                 pCmdUI->SetCheck(1);    
377 }
378
379
380 void OCC_3dBaseDoc::OnObjectTransparency()
381 {
382         CDialogTransparency DialBox(myAISContext);
383         DialBox.DoModal();
384         //CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
385         //CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
386         //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
387 //      pView->Redraw();
388 }
389
390 void OCC_3dBaseDoc::OnUpdateObjectTransparency(CCmdUI* pCmdUI) 
391 {
392   bool OneOrMoreInShading = false;
393   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
394     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
395         pCmdUI->Enable (OneOrMoreInShading);    
396 }
397
398
399 void OCC_3dBaseDoc::OnObjectDisplayall() 
400 {
401         myAISContext->DisplayAll (Standard_True);
402 }
403
404 void OCC_3dBaseDoc::OnUpdateObjectDisplayall(CCmdUI* pCmdUI) 
405 {
406   AIS_ListOfInteractive aList;
407   myAISContext->ObjectsInside (aList);
408   for (AIS_ListIteratorOfListOfInteractive aLI (aList);aLI.More();aLI.Next())
409   {
410     if (!myAISContext->IsDisplayed (aLI.Value()))
411     {
412       pCmdUI->Enable (true);
413       return;
414     }
415   }
416   pCmdUI->Enable (false);
417 }
418
419 void OCC_3dBaseDoc::OnObjectRemove() 
420 {
421         for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->InitSelected())
422         myAISContext->Remove(myAISContext->SelectedInteractive(),Standard_True);
423 }
424
425 void OCC_3dBaseDoc::OnUpdateObjectRemove(CCmdUI* pCmdUI) 
426 {
427   bool OneOrMoreIsDisplayed = false;
428   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
429     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive())) OneOrMoreIsDisplayed=true;
430   pCmdUI->Enable (OneOrMoreIsDisplayed);
431 }
432
433 void OCC_3dBaseDoc::SetMaterial(Graphic3d_NameOfMaterial Material) 
434 {
435   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
436     myAISContext->SetMaterial (myAISContext->SelectedInteractive(),
437     (Graphic3d_NameOfMaterial)(Material), Standard_False);
438   myAISContext->UpdateCurrentViewer();
439 }
440
441
442 // RayTracing
443 void OCC_3dBaseDoc::OnObjectRayTracing()
444 {
445   myRayTracingIsOn = !myRayTracingIsOn;
446   if(!myRayTracingIsOn)
447   {
448     myRaytracedShadowsIsOn = false;
449     myRaytracedReflectionsIsOn = false;
450     myRaytracedAntialiasingIsOn = false;
451   }
452   OnObjectRayTracingAction();
453 }
454 // Shadows
455 void OCC_3dBaseDoc::OnObjectShadows()
456 {
457   myRaytracedShadowsIsOn = !myRaytracedShadowsIsOn;
458   OnObjectRayTracingAction();
459 }
460 // Reflections
461 void OCC_3dBaseDoc::OnObjectReflections()
462 {
463   myRaytracedReflectionsIsOn = !myRaytracedReflectionsIsOn;
464   OnObjectRayTracingAction();
465 }
466 // Anti-aliasing
467 void OCC_3dBaseDoc::OnObjectAntiAliasing()
468 {
469   myRaytracedAntialiasingIsOn = !myRaytracedAntialiasingIsOn;
470   OnObjectRayTracingAction();
471 }
472 void OCC_3dBaseDoc::OnUpdateV3dButtons (CCmdUI* pCmdUI)
473 {
474   if (pCmdUI->m_nID == ID_OBJECT_RAY_TRACING)
475   {
476     pCmdUI->SetCheck(myRayTracingIsOn);
477   } else {
478     pCmdUI->Enable(myRayTracingIsOn);
479     if (pCmdUI->m_nID == ID_OBJECT_SHADOWS)
480       pCmdUI->SetCheck(myRaytracedShadowsIsOn);
481     if (pCmdUI->m_nID == ID_OBJECT_REFLECTIONS)
482       pCmdUI->SetCheck(myRaytracedReflectionsIsOn);
483     if (pCmdUI->m_nID == ID_OBJECT_ANTI_ALIASING)
484       pCmdUI->SetCheck(myRaytracedAntialiasingIsOn);
485   }
486 }
487 // Common function to change raytracing params and redraw view
488 void OCC_3dBaseDoc::OnObjectRayTracingAction()
489 {
490   Handle(V3d_View) aView = myAISContext->CurrentViewer()->ActiveViews().First();
491   Graphic3d_RenderingParams& aParams = aView->ChangeRenderingParams();
492   if (myRayTracingIsOn)
493     aParams.Method = Graphic3d_RM_RAYTRACING;
494   else
495     aParams.Method = Graphic3d_RM_RASTERIZATION;
496   aParams.IsShadowEnabled = myRaytracedShadowsIsOn;
497   aParams.IsReflectionEnabled = myRaytracedReflectionsIsOn;
498   aParams.IsAntialiasingEnabled = myRaytracedAntialiasingIsOn;
499   myAISContext->UpdateCurrentViewer();
500 }