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