0026628: Button Erase (Delete where erase algorythm is used) works incorrect
[occt.git] / samples / mfc / standard / Common / OCC_3dBaseDoc.cpp
CommitLineData
7fd59977 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"
5c1f974e 10#include "OCC_App.h"
7fd59977 11#include <res\OCC_Resource.h>
12#include "ImportExport/ImportExport.h"
13#include "AISDialogs.h"
a6eb515f 14#include <AIS_LocalContext.hxx>
7fd59977 15#include <AIS_ListOfInteractive.hxx>
16#include <AIS_ListIteratorOfListOfInteractive.hxx>
a6eb515f 17#include <TColStd_ListIteratorOfListOfInteger.hxx>
18#include <TColStd_ListOfInteger.hxx>
19#include <TopoDS_Shape.hxx>
7fd59977 20
21BEGIN_MESSAGE_MAP(OCC_3dBaseDoc, OCC_BaseDoc)
5c1f974e 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)
a6eb515f 41
5c1f974e 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)
2c5b6d9c 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)
7fd59977 56END_MESSAGE_MAP()
57
58
59//////////////////////////////////////////////////////////////////////
60// Construction/Destruction
61//////////////////////////////////////////////////////////////////////
62
63OCC_3dBaseDoc::OCC_3dBaseDoc()
47162471 64:myPopupMenuNumber(0)
7fd59977 65{
5c1f974e 66 AfxInitRichEdit();
7fd59977 67
47162471 68 Handle(Graphic3d_GraphicDriver) aGraphicDriver = ((OCC_App*)AfxGetApp())->GetGraphicDriver();
7fd59977 69
5c1f974e 70 myViewer = new V3d_Viewer (aGraphicDriver, Standard_ExtString("Visu3D") );
71 myViewer->SetDefaultLights();
72 myViewer->SetLightOn();
73 myAISContext = new AIS_InteractiveContext (myViewer);
2c5b6d9c 74
75 myRayTracingIsOn = false;
76 myRaytracedShadowsIsOn = true;
77 myRaytracedReflectionsIsOn = false;
78 myRaytracedAntialiasingIsOn = false;
7fd59977 79}
80
234e52be 81OCC_3dBaseDoc::~OCC_3dBaseDoc()
82{
83}
84
5c1f974e 85//-----------------------------------------------------------------------------------------
86//
87//-----------------------------------------------------------------------------------------
88void OCC_3dBaseDoc::DragEvent (const Standard_Integer theMouseX,
89 const Standard_Integer theMouseY,
90 const Standard_Integer theState,
91 const Handle(V3d_View)& theView)
7fd59977 92{
5c1f974e 93 // TheState == -1 button down
94 // TheState == 0 move
95 // TheState == 1 button up
7fd59977 96
5c1f974e 97 static Standard_Integer aStartDragX = 0;
98 static Standard_Integer aStartDragY = 0;
7fd59977 99
47162471 100 switch (theState)
5c1f974e 101 {
47162471 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 };
7fd59977 121}
122
123//-----------------------------------------------------------------------------------------
124//
125//-----------------------------------------------------------------------------------------
5c1f974e 126void OCC_3dBaseDoc::InputEvent (const Standard_Integer theMouseX,
127 const Standard_Integer theMouseY,
128 const Handle(V3d_View)& theView)
7fd59977 129{
5c1f974e 130 myAISContext->MoveTo (theMouseX, theMouseY, theView);
131 myAISContext->Select();
7fd59977 132}
133
134//-----------------------------------------------------------------------------------------
135//
136//-----------------------------------------------------------------------------------------
5c1f974e 137void OCC_3dBaseDoc::MoveEvent (const Standard_Integer theMouseX,
138 const Standard_Integer theMouseY,
139 const Handle(V3d_View)& theView)
7fd59977 140{
5c1f974e 141 myAISContext->MoveTo (theMouseX, theMouseY, theView);
7fd59977 142}
143
144//-----------------------------------------------------------------------------------------
145//
146//-----------------------------------------------------------------------------------------
5c1f974e 147void OCC_3dBaseDoc::ShiftMoveEvent (const Standard_Integer theMouseX,
148 const Standard_Integer theMouseY,
149 const Handle(V3d_View)& theView)
7fd59977 150{
5c1f974e 151 myAISContext->MoveTo (theMouseX, theMouseY, theView);
7fd59977 152}
153
154//-----------------------------------------------------------------------------------------
155//
156//-----------------------------------------------------------------------------------------
5c1f974e 157void OCC_3dBaseDoc::ShiftDragEvent (const Standard_Integer theMouseX,
158 const Standard_Integer theMouseY,
159 const Standard_Integer theState,
160 const Handle(V3d_View)& theView)
7fd59977 161{
5c1f974e 162 // TheState == -1 button down
163 // TheState == 0 move
164 // TheState == 1 button up
7fd59977 165
5c1f974e 166 static Standard_Integer aStartDragX = 0;
167 static Standard_Integer aStartDragY = 0;
7fd59977 168
5c1f974e 169 if (theState == -1)
170 {
171 // button down
172 aStartDragX = theMouseX;
173 aStartDragY = theMouseY;
174 }
7fd59977 175
5c1f974e 176 if (theState == 0)
177 {
178 // button up
179 myAISContext->ShiftSelect (aStartDragX, aStartDragY,
180 theMouseX, theMouseY,
181 theView);
182 }
183}
7fd59977 184
185//-----------------------------------------------------------------------------------------
186//
187//-----------------------------------------------------------------------------------------
5c573e69 188void OCC_3dBaseDoc::ShiftInputEvent (const Standard_Integer /*theMouseX*/,
189 const Standard_Integer /*theMouseY*/,
190 const Handle(V3d_View)& /*theView*/)
7fd59977 191{
5c1f974e 192 myAISContext->ShiftSelect();
7fd59977 193}
194
195//-----------------------------------------------------------------------------------------
196//
197//-----------------------------------------------------------------------------------------
5c1f974e 198void OCC_3dBaseDoc::Popup (const Standard_Integer theMouseX,
199 const Standard_Integer theMouseY,
200 const Handle(V3d_View)& theView)
7fd59977 201{
a6eb515f 202 // Base check which context menu to call
203 if (!myPopupMenuNumber)
204 {
205 myAISContext->InitCurrent();
206 if (myAISContext->MoreCurrent())
207 myPopupMenuNumber=1;
208 }
7fd59977 209
210 CMenu menu;
211 VERIFY(menu.LoadMenu(IDR_Popup3D));
a6eb515f 212 CMenu* pPopup = menu.GetSubMenu(myPopupMenuNumber);
7fd59977 213
214 ASSERT(pPopup != NULL);
a6eb515f 215 if (myPopupMenuNumber == 1) // more than 1 object.
7fd59977 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
5c1f974e 224 POINT winCoord = { theMouseX , theMouseY };
7fd59977 225 Handle(WNT_Window) aWNTWindow=
5c1f974e 226 Handle(WNT_Window)::DownCast(theView->Window());
7fd59977 227 ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
a6eb515f 228 pPopup->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON,
229 winCoord.x,
230 winCoord.y,
231 AfxGetMainWnd());
7fd59977 232}
233
234void 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
576f8b11 242int OCC_3dBaseDoc::OnFileImportBrep_WithInitDir (const wchar_t* )
243{
244 if (CImportExport::ReadBREP (myAISContext) == 1)
245 return 1;
246 Fit();
247 return 0;
7fd59977 248}
249
250void OCC_3dBaseDoc::OnFileImportBrep()
251{
576f8b11 252 OnFileImportBrep_WithInitDir (NULL);
7fd59977 253}
254
255void OCC_3dBaseDoc::OnFileExportBrep()
256{ CImportExport::SaveBREP(myAISContext);}
257
258void 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}
284void OCC_3dBaseDoc::OnUpdateObjectColor(CCmdUI* pCmdUI)
285{
7fd59977 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;
b3837d74 291 pCmdUI->Enable (OneOrMoreIsShadingOrWireframe);
7fd59977 292}
293
294void OCC_3dBaseDoc::OnObjectErase()
295{
7140edaf 296 myAISContext->EraseSelected();
b3837d74 297 myAISContext->ClearCurrents();
7fd59977 298}
299void OCC_3dBaseDoc::OnUpdateObjectErase(CCmdUI* pCmdUI)
300{
301 bool OneOrMoreIsDisplayed = false;
b3837d74 302 for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent())
303 {
304 if (myAISContext->IsDisplayed (myAISContext->Current()))
305 OneOrMoreIsDisplayed = true;
306 }
307 pCmdUI->Enable (OneOrMoreIsDisplayed);
7fd59977 308}
309
310void OCC_3dBaseDoc::OnObjectWireframe()
311{
312 for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
313 myAISContext->SetDisplayMode(myAISContext->Current(),0);
314}
315void 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
323void OCC_3dBaseDoc::OnObjectShading()
324{
325 for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
326 myAISContext->SetDisplayMode(myAISContext->Current(),1);
327}
328
329void 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
337void OCC_3dBaseDoc::OnObjectMaterial()
338{
5c1f974e 339 CDialogMaterial DialBox(myAISContext);
340 DialBox.DoModal();
5c573e69 341 //CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
342 //CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
343 //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
7fd59977 344}
345
346void 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
354BOOL 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
369void 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
381void OCC_3dBaseDoc::OnObjectTransparency()
382{
383 CDialogTransparency DialBox(myAISContext);
384 DialBox.DoModal();
5c573e69 385 //CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
386 //CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
387 //OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
7fd59977 388// pView->Redraw();
389}
390
391void 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
400void OCC_3dBaseDoc::OnObjectDisplayall()
401{
eb4320f2 402 myAISContext->DisplayAll();
7fd59977 403}
404
405void 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
420void OCC_3dBaseDoc::OnObjectRemove()
421{
422 for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->InitCurrent())
423 myAISContext->Remove(myAISContext->Current(),Standard_True);
424}
425
426void 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;
a6eb515f 431 pCmdUI->Enable (OneOrMoreIsDisplayed);
7fd59977 432}
433
434void OCC_3dBaseDoc::SetMaterial(Graphic3d_NameOfMaterial Material)
435{
436 for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
5c1f974e 437 myAISContext->SetMaterial (myAISContext->Current(),
438 (Graphic3d_NameOfMaterial)(Material));
7fd59977 439}
2c5b6d9c 440
441
442// RayTracing
443void 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
455void OCC_3dBaseDoc::OnObjectShadows()
456{
457 myRaytracedShadowsIsOn = !myRaytracedShadowsIsOn;
458 OnObjectRayTracingAction();
459}
460// Reflections
461void OCC_3dBaseDoc::OnObjectReflections()
462{
463 myRaytracedReflectionsIsOn = !myRaytracedReflectionsIsOn;
464 OnObjectRayTracingAction();
465}
466// Anti-aliasing
467void OCC_3dBaseDoc::OnObjectAntiAliasing()
468{
469 myRaytracedAntialiasingIsOn = !myRaytracedAntialiasingIsOn;
470 OnObjectRayTracingAction();
471}
472void 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
488void 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}