0026628: Button Erase (Delete where erase algorythm is used) works incorrect
[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, Standard_ExtString("Visu3D") );
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);
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);
131   myAISContext->Select();
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);
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);
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);
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();
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->InitCurrent();
206     if (myAISContext->MoreCurrent())
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->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
219     if (myAISContext->IsDisplayed(myAISContext->Current(),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         Quantity_Color CSFColor ;
263
264         myAISContext->InitCurrent();
265     Current = myAISContext->Current();
266         if ( Current->HasColor () ) {
267       CSFColor = myAISContext->Color(myAISContext->Current());
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           CSFColor = Quantity_Color (GetRValue(MSColor)/255.,GetGValue(MSColor)/255.,
279                                                          GetBValue(MSColor)/255.,Quantity_TOC_RGB); 
280           for (;myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
281           myAISContext->SetColor (myAISContext->Current(),CSFColor.Name());
282         }
283 }
284 void OCC_3dBaseDoc::OnUpdateObjectColor(CCmdUI* pCmdUI) 
285 {
286   bool OneOrMoreIsShadingOrWireframe = false;
287   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
288     if (myAISContext->IsDisplayed(myAISContext->Current(),0)
289                 ||myAISContext->IsDisplayed(myAISContext->Current(),1)) 
290                 OneOrMoreIsShadingOrWireframe=true;
291   pCmdUI->Enable (OneOrMoreIsShadingOrWireframe);
292 }
293
294 void OCC_3dBaseDoc::OnObjectErase() 
295 {
296   myAISContext->EraseSelected();
297   myAISContext->ClearCurrents();
298 }
299 void OCC_3dBaseDoc::OnUpdateObjectErase(CCmdUI* pCmdUI) 
300 {
301   bool OneOrMoreIsDisplayed = false;
302   for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent())
303   {
304     if (myAISContext->IsDisplayed (myAISContext->Current()))
305       OneOrMoreIsDisplayed = true;
306   }
307   pCmdUI->Enable (OneOrMoreIsDisplayed);
308 }
309
310 void OCC_3dBaseDoc::OnObjectWireframe() 
311 {
312   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
313         myAISContext->SetDisplayMode(myAISContext->Current(),0);
314 }
315 void OCC_3dBaseDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI) 
316 {
317   bool OneOrMoreInShading = false;
318   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
319     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
320         pCmdUI->Enable (OneOrMoreInShading);    
321 }
322
323 void OCC_3dBaseDoc::OnObjectShading() 
324 {
325   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
326       myAISContext->SetDisplayMode(myAISContext->Current(),1);
327 }
328
329 void OCC_3dBaseDoc::OnUpdateObjectShading(CCmdUI* pCmdUI) 
330 {
331   bool OneOrMoreInWireframe = false;
332   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
333     if (myAISContext->IsDisplayed(myAISContext->Current(),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->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
350     if (myAISContext->IsDisplayed(myAISContext->Current(),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->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ()){
361         aTransparency = myAISContext->Current()->Transparency();
362         myAISContext->SetMaterial (myAISContext->Current(),(Graphic3d_NameOfMaterial)(nID-ID_OBJECT_MATERIAL_BRASS));
363         myAISContext->SetTransparency (myAISContext->Current(),aTransparency);
364   }
365   return true;
366
367 }
368
369 void OCC_3dBaseDoc::OnUpdateObjectMaterialRange(CCmdUI* pCmdUI) 
370 {
371   bool OneOrMoreInShading = false;
372   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
373     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
374         pCmdUI->Enable (OneOrMoreInShading);
375   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
376     if (myAISContext->Current()->Material() - (pCmdUI->m_nID - ID_OBJECT_MATERIAL_BRASS) == 0) 
377                 pCmdUI->SetCheck(1);    
378 }
379
380
381 void OCC_3dBaseDoc::OnObjectTransparency()
382 {
383         CDialogTransparency DialBox(myAISContext);
384         DialBox.DoModal();
385         //CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
386         //CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
387         //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
388 //      pView->Redraw();
389 }
390
391 void OCC_3dBaseDoc::OnUpdateObjectTransparency(CCmdUI* pCmdUI) 
392 {
393   bool OneOrMoreInShading = false;
394   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
395     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
396         pCmdUI->Enable (OneOrMoreInShading);    
397 }
398
399
400 void OCC_3dBaseDoc::OnObjectDisplayall() 
401 {
402         myAISContext->DisplayAll();
403 }
404
405 void OCC_3dBaseDoc::OnUpdateObjectDisplayall(CCmdUI* pCmdUI) 
406 {
407         
408         AIS_ListOfInteractive aList;
409         myAISContext->ObjectsInside(aList,AIS_KOI_Shape);
410         AIS_ListIteratorOfListOfInteractive aLI;
411         Standard_Boolean IS_ANY_OBJECT_ERASED=FALSE;
412         for (aLI.Initialize(aList);aLI.More();aLI.Next()){
413                 if(!myAISContext->IsDisplayed(aLI.Value()))
414                 IS_ANY_OBJECT_ERASED=TRUE;
415         }
416         pCmdUI->Enable (IS_ANY_OBJECT_ERASED);
417
418 }
419
420 void OCC_3dBaseDoc::OnObjectRemove() 
421 {
422         for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->InitCurrent())
423         myAISContext->Remove(myAISContext->Current(),Standard_True);
424 }
425
426 void OCC_3dBaseDoc::OnUpdateObjectRemove(CCmdUI* pCmdUI) 
427 {
428   bool OneOrMoreIsDisplayed = false;
429   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
430     if (myAISContext->IsDisplayed(myAISContext->Current())) OneOrMoreIsDisplayed=true;
431   pCmdUI->Enable (OneOrMoreIsDisplayed);
432 }
433
434 void OCC_3dBaseDoc::SetMaterial(Graphic3d_NameOfMaterial Material) 
435 {
436   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
437     myAISContext->SetMaterial (myAISContext->Current(),
438     (Graphic3d_NameOfMaterial)(Material));
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   myAISContext->CurrentViewer()->InitActiveViews();
491   Handle(V3d_View) aView = myAISContext->CurrentViewer()->ActiveView();
492   Graphic3d_RenderingParams& aParams = aView->ChangeRenderingParams();
493   if (myRayTracingIsOn)
494     aParams.Method = Graphic3d_RM_RAYTRACING;
495   else
496     aParams.Method = Graphic3d_RM_RASTERIZATION;
497   aParams.IsShadowEnabled = myRaytracedShadowsIsOn;
498   aParams.IsReflectionEnabled = myRaytracedReflectionsIsOn;
499   aParams.IsAntialiasingEnabled = myRaytracedAntialiasingIsOn;
500   myAISContext->UpdateCurrentViewer();
501 }