7578112cb0ff55a3aaf59643ad3f4a0655a4fe82
[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   Handle(WNT_Window) aWNTWindow=
225   Handle(WNT_Window)::DownCast(theView->Window());
226   ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
227   pPopup->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON,
228                           winCoord.x,
229                           winCoord.y,
230                           AfxGetMainWnd());
231 }
232
233 void OCC_3dBaseDoc::Fit()
234 {
235         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
236         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
237         OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
238         pView->FitAll();
239 }
240
241 int OCC_3dBaseDoc::OnFileImportBrep_WithInitDir (const wchar_t* )
242 {
243   if (CImportExport::ReadBREP (myAISContext) == 1)
244     return 1;
245   Fit();
246   return 0;
247 }
248
249 void OCC_3dBaseDoc::OnFileImportBrep() 
250 {   
251   OnFileImportBrep_WithInitDir (NULL);
252 }
253
254 void OCC_3dBaseDoc::OnFileExportBrep() 
255 {   CImportExport::SaveBREP(myAISContext);}
256
257 void OCC_3dBaseDoc::OnObjectColor() 
258 {
259         Handle(AIS_InteractiveObject) Current ;
260         COLORREF MSColor ;
261
262         myAISContext->InitSelected();
263     Current = myAISContext->SelectedInteractive();
264         if ( Current->HasColor () ) {
265       Quantity_Color CSFColor;
266       myAISContext->Color (Current, CSFColor);
267       MSColor = RGB (CSFColor.Red()*255.,CSFColor.Green()*255.,CSFColor.Blue()*255.);
268         }
269         else {
270           MSColor = RGB (255,255,255) ;
271         }
272         
273         CColorDialog dlgColor(MSColor);
274         if (dlgColor.DoModal() == IDOK)
275         {
276           MSColor = dlgColor.GetColor();
277           Quantity_Color CSFColor (GetRValue(MSColor)/255., GetGValue(MSColor)/255., GetBValue(MSColor)/255., Quantity_TOC_RGB);
278           for (;myAISContext->MoreSelected ();myAISContext->NextSelected ())
279       myAISContext->SetColor (myAISContext->SelectedInteractive(), CSFColor, Standard_False);
280     myAISContext->UpdateCurrentViewer();
281         }
282 }
283 void OCC_3dBaseDoc::OnUpdateObjectColor(CCmdUI* pCmdUI) 
284 {
285   bool OneOrMoreIsShadingOrWireframe = false;
286   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
287     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),0)
288                 ||myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1))
289                 OneOrMoreIsShadingOrWireframe=true;
290   pCmdUI->Enable (OneOrMoreIsShadingOrWireframe);
291 }
292
293 void OCC_3dBaseDoc::OnObjectErase() 
294 {
295   myAISContext->EraseSelected (Standard_True);
296 }
297 void OCC_3dBaseDoc::OnUpdateObjectErase(CCmdUI* pCmdUI) 
298 {
299   bool OneOrMoreIsDisplayed = false;
300   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
301   {
302     if (myAISContext->IsDisplayed (myAISContext->SelectedInteractive()))
303       OneOrMoreIsDisplayed = true;
304   }
305   pCmdUI->Enable (OneOrMoreIsDisplayed);
306 }
307
308 void OCC_3dBaseDoc::OnObjectWireframe() 
309 {
310   for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->NextSelected())
311         myAISContext->SetDisplayMode (myAISContext->SelectedInteractive(), 0, Standard_False);
312   myAISContext->UpdateCurrentViewer();
313 }
314 void OCC_3dBaseDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI) 
315 {
316   bool OneOrMoreInShading = false;
317   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
318     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
319         pCmdUI->Enable (OneOrMoreInShading);    
320 }
321
322 void OCC_3dBaseDoc::OnObjectShading() 
323 {
324   for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->NextSelected())
325       myAISContext->SetDisplayMode (myAISContext->SelectedInteractive(), 1, Standard_False);
326   myAISContext->UpdateCurrentViewer();
327 }
328
329 void OCC_3dBaseDoc::OnUpdateObjectShading(CCmdUI* pCmdUI) 
330 {
331   bool OneOrMoreInWireframe = false;
332   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
333     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),0)) OneOrMoreInWireframe=true;
334         pCmdUI->Enable (OneOrMoreInWireframe);  
335 }
336
337 void OCC_3dBaseDoc::OnObjectMaterial() 
338 {
339   CDialogMaterial DialBox(myAISContext);
340   DialBox.DoModal();
341   //CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
342   //CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
343   //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
344 }
345
346 void OCC_3dBaseDoc::OnUpdateObjectMaterial(CCmdUI* pCmdUI) 
347 {
348   bool OneOrMoreInShading = false;
349   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
350     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
351         pCmdUI->Enable (OneOrMoreInShading);    
352 }
353
354 BOOL OCC_3dBaseDoc::OnObjectMaterialRange(UINT nID) 
355 {
356   // the range ID_OBJECT_MATERIAL_BRASS to ID_OBJECT_MATERIAL_SILVER is
357   // continue with the same values as enumeration Type Of Material
358   Standard_Real aTransparency;
359
360   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ()){
361     aTransparency = myAISContext->SelectedInteractive()->Transparency();
362     myAISContext->SetMaterial (myAISContext->SelectedInteractive(),(Graphic3d_NameOfMaterial)(nID-ID_OBJECT_MATERIAL_BRASS), Standard_False);
363     myAISContext->SetTransparency (myAISContext->SelectedInteractive(),aTransparency, Standard_False);
364   }
365   myAISContext->UpdateCurrentViewer();
366   return true;
367
368 }
369
370 void OCC_3dBaseDoc::OnUpdateObjectMaterialRange(CCmdUI* pCmdUI) 
371 {
372   bool OneOrMoreInShading = false;
373   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
374     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
375         pCmdUI->Enable (OneOrMoreInShading);
376   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
377     if (myAISContext->SelectedInteractive()->Material() - (pCmdUI->m_nID - ID_OBJECT_MATERIAL_BRASS) == 0)
378                 pCmdUI->SetCheck(1);    
379 }
380
381
382 void OCC_3dBaseDoc::OnObjectTransparency()
383 {
384         CDialogTransparency DialBox(myAISContext);
385         DialBox.DoModal();
386         //CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
387         //CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
388         //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
389 //      pView->Redraw();
390 }
391
392 void OCC_3dBaseDoc::OnUpdateObjectTransparency(CCmdUI* pCmdUI) 
393 {
394   bool OneOrMoreInShading = false;
395   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
396     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
397         pCmdUI->Enable (OneOrMoreInShading);    
398 }
399
400
401 void OCC_3dBaseDoc::OnObjectDisplayall() 
402 {
403         myAISContext->DisplayAll (Standard_True);
404 }
405
406 void OCC_3dBaseDoc::OnUpdateObjectDisplayall(CCmdUI* pCmdUI) 
407 {
408   AIS_ListOfInteractive aList;
409   myAISContext->ObjectsInside (aList);
410   for (AIS_ListIteratorOfListOfInteractive aLI (aList);aLI.More();aLI.Next())
411   {
412     if (!myAISContext->IsDisplayed (aLI.Value()))
413     {
414       pCmdUI->Enable (true);
415       return;
416     }
417   }
418   pCmdUI->Enable (false);
419 }
420
421 void OCC_3dBaseDoc::OnObjectRemove() 
422 {
423         for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->InitSelected())
424         myAISContext->Remove(myAISContext->SelectedInteractive(),Standard_True);
425 }
426
427 void OCC_3dBaseDoc::OnUpdateObjectRemove(CCmdUI* pCmdUI) 
428 {
429   bool OneOrMoreIsDisplayed = false;
430   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
431     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive())) OneOrMoreIsDisplayed=true;
432   pCmdUI->Enable (OneOrMoreIsDisplayed);
433 }
434
435 void OCC_3dBaseDoc::SetMaterial(Graphic3d_NameOfMaterial Material) 
436 {
437   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
438     myAISContext->SetMaterial (myAISContext->SelectedInteractive(),
439     (Graphic3d_NameOfMaterial)(Material), Standard_False);
440   myAISContext->UpdateCurrentViewer();
441 }
442
443
444 // RayTracing
445 void OCC_3dBaseDoc::OnObjectRayTracing()
446 {
447   myRayTracingIsOn = !myRayTracingIsOn;
448   if(!myRayTracingIsOn)
449   {
450     myRaytracedShadowsIsOn = false;
451     myRaytracedReflectionsIsOn = false;
452     myRaytracedAntialiasingIsOn = false;
453   }
454   OnObjectRayTracingAction();
455 }
456 // Shadows
457 void OCC_3dBaseDoc::OnObjectShadows()
458 {
459   myRaytracedShadowsIsOn = !myRaytracedShadowsIsOn;
460   OnObjectRayTracingAction();
461 }
462 // Reflections
463 void OCC_3dBaseDoc::OnObjectReflections()
464 {
465   myRaytracedReflectionsIsOn = !myRaytracedReflectionsIsOn;
466   OnObjectRayTracingAction();
467 }
468 // Anti-aliasing
469 void OCC_3dBaseDoc::OnObjectAntiAliasing()
470 {
471   myRaytracedAntialiasingIsOn = !myRaytracedAntialiasingIsOn;
472   OnObjectRayTracingAction();
473 }
474 void OCC_3dBaseDoc::OnUpdateV3dButtons (CCmdUI* pCmdUI)
475 {
476   if (pCmdUI->m_nID == ID_OBJECT_RAY_TRACING)
477   {
478     pCmdUI->SetCheck(myRayTracingIsOn);
479   } else {
480     pCmdUI->Enable(myRayTracingIsOn);
481     if (pCmdUI->m_nID == ID_OBJECT_SHADOWS)
482       pCmdUI->SetCheck(myRaytracedShadowsIsOn);
483     if (pCmdUI->m_nID == ID_OBJECT_REFLECTIONS)
484       pCmdUI->SetCheck(myRaytracedReflectionsIsOn);
485     if (pCmdUI->m_nID == ID_OBJECT_ANTI_ALIASING)
486       pCmdUI->SetCheck(myRaytracedAntialiasingIsOn);
487   }
488 }
489 // Common function to change raytracing params and redraw view
490 void OCC_3dBaseDoc::OnObjectRayTracingAction()
491 {
492   myAISContext->CurrentViewer()->InitActiveViews();
493   Handle(V3d_View) aView = myAISContext->CurrentViewer()->ActiveView();
494   Graphic3d_RenderingParams& aParams = aView->ChangeRenderingParams();
495   if (myRayTracingIsOn)
496     aParams.Method = Graphic3d_RM_RAYTRACING;
497   else
498     aParams.Method = Graphic3d_RM_RASTERIZATION;
499   aParams.IsShadowEnabled = myRaytracedShadowsIsOn;
500   aParams.IsReflectionEnabled = myRaytracedReflectionsIsOn;
501   aParams.IsAntialiasingEnabled = myRaytracedAntialiasingIsOn;
502   myAISContext->UpdateCurrentViewer();
503 }